Created
May 18, 2018 13:25
-
-
Save plexus/5418819323afb892b481816745be15e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Rename clj/cljs/cljc buffers to their namespace name, so you see | |
;; `foo.bar.core' in the modeline, rather than `core.clj' | |
(advice-add 'rename-buffer :around #'plexus/clj-ns--rename-buffer-advice) | |
(defun plexus/clj-ns--rename-buffer-advice (rb-fun newname &optional unique &rest args) | |
(let ((filename (buffer-file-name (current-buffer))) | |
(buf-start (buffer-substring-no-properties (point-min) (point-min)))) | |
(if (and (string-match "\\.clj[cxs]?$" filename) | |
(string-match "(ns \\([^\n )]+\\)" buf-start)) | |
(match-string-no-properties 1 buf-start) | |
(apply rb-fun newname unique args)))) | |
(advice-add 'create-file-buffer :around #'plexus/clj-ns--create-file-buffer-advice) | |
(defun plexus/clj-ns--create-file-buffer-advice (cfb-fun filename &rest args) | |
(if (and (file-exists-p filename) (not (file-directory-p filename))) | |
(with-temp-buffer | |
(insert-file-contents filename) | |
(let ((buf-start (buffer-substring-no-properties (point-min) (point-max)))) | |
(if (and (string-match "\\.clj[cxs]?$" filename) | |
(string-match "(ns \\([^\n )]+\\)" buf-start)) | |
(let ((name (match-string-no-properties 1 buf-start))) | |
(generate-new-buffer name) | |
name) | |
(apply cfb-fun filename args)))) | |
(apply cfb-fun filename args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love this, thanks for making this public.
Sometimes when opening files, I encounter error, that I believe can be traced to this function. Useing ido. But it always works when I try to open the same file the second time. Here I'm opening a typescript file. The stacktrace is below, maybe enough info here to find the cause.