-
Notifications
You must be signed in to change notification settings - Fork 0
/
clj-ns-name.el
95 lines (83 loc) · 3.65 KB
/
clj-ns-name.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;;; clj-ns-name.el --- Rename Clojure buffers to their namespace name -*- lexical-binding: t; -*-
;; Filename: clj-ns-name.el
;; Author: Arne Brasseur <[email protected]>
;; Maintainer: Arne Brasseur <[email protected]>
;; Created: Mi Jul 18 09:18:03 2018 (+0200)
;; Version: 0.2.1
;; Package-Requires: ((emacs "24.4") (walkclj "0.1.0") (projectile "2.0.0"))
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Give Clojure/ClojureScript buffers the name of the Clojure namespace, rather
;; than the filename.
;;
;; Run `(clj-ns-name-install)' to add function advice to activate this package.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Copyright (C) 2018 Arne Brasseur
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'walkclj)
(require 'projectile)
(defun clj-ns-name-uniquify (ns-name)
(when ns-name
(concat ns-name " <" (projectile-project-name) ">")))
(defun clj-ns-name-rename-clj-buffer-to-namespace* ()
(when (derived-mode-p 'clojure-mode)
(let* ((ns-name (clj-ns-name-uniquify (walkclj-current-ns))))
(when (and ns-name (not (equal (buffer-name) ns-name)))
(rename-buffer ns-name t)))))
(defun clj-ns-name-rename-clj-buffer-to-namespace (buffer)
(interactive "b")
(with-current-buffer buffer
(clj-ns-name-rename-clj-buffer-to-namespace*)))
(defun clj-ns-name-clojure-file-p (filename)
(and (or (and (file-exists-p filename)
(not (file-directory-p filename)))
(not (file-exists-p filename)))
(string-match "\\.clj[cxs]?$" filename)))
(defun clj-ns-name-find-file-noselect-advice (ffn-fun filename &rest args)
(let* ((buffer (apply ffn-fun filename args)))
(when (clj-ns-name-clojure-file-p filename)
(clj-ns-name-rename-clj-buffer-to-namespace buffer))
buffer))
(defun clj-ns-name-create-file-buffer-advice (cfb-fun filename &rest args)
(if (and (file-exists-p filename) (clj-ns-name-clojure-file-p filename))
(let* ((ns-name (with-temp-buffer
(insert-file-contents filename)
(walkclj-current-ns))))
(if (and ns-name (not (equal (buffer-name) ns-name)))
(progn
(generate-new-buffer ns-name)
ns-name)
(apply cfb-fun filename args)))
(apply cfb-fun filename args)))
(defun clj-ns-name-install ()
(advice-add 'find-file-noselect :around #'clj-ns-name-find-file-noselect-advice)
(advice-add 'create-file-buffer :around #'clj-ns-name-create-file-buffer-advice)
(add-hook 'after-save-hook #'clj-ns-name-rename-clj-buffer-to-namespace*))
(defun clj-ns-name-uninstall ()
(advice-remove 'find-file-noselect #'clj-ns-name-find-file-noselect-advice)
(advice-remove 'create-file-buffer #'clj-ns-name-create-file-buffer-advice)
(remove-hook 'after-save-hook #'clj-ns-name-rename-clj-buffer-to-namespace*))
(provide 'clj-ns-name)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; clj-ns-name.el ends here