Skip to content

Instantly share code, notes, and snippets.

@plexus
Created May 18, 2018 13:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/5418819323afb892b481816745be15e0 to your computer and use it in GitHub Desktop.
Save plexus/5418819323afb892b481816745be15e0 to your computer and use it in GitHub Desktop.
;; 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)))
@hlolli
Copy link

hlolli commented Jun 5, 2018

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.

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("\\.clj[cxs]?$" nil)
  (and (string-match "\\.clj[cxs]?$" filename) (string-match "(ns \\([^\n )]+\\)" buf-start))
  (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))
  (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)))
  plexus/clj-ns--rename-buffer-advice(#[128 "\300\301\302�#\207" [apply uniquify--rename-buffer-advice #<subr rename-buffer> nil] 5 369230 (list (read-string "Rename buffer (to new name): " nil (quote buffer-name-history) (buffer-name (current-buffer))) current-prefix-arg)] "app.module.ts<twlfront>" t)
  apply(plexus/clj-ns--rename-buffer-advice #[128 "\300\301\302�#\207" [apply uniquify--rename-buffer-advice #<subr rename-buffer> nil] 5 369230 (list (read-string "Rename buffer (to new name): " nil (quote buffer-name-history) (buffer-name (current-buffer))) current-prefix-arg)] ("app.module.ts<twlfront>" t))
  rename-buffer("app.module.ts<twlfront>" t)
  uniquify-rename-buffer([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront" #<buffer app.module.ts<2>> "app.module.ts<twlfront>"] "app.module.ts<twlfront>")
  uniquify-rationalize-conflicting-sublist(([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront" #<buffer app.module.ts<2>> "app.module.ts<twlfront>"]) "app.module.ts<twlfront>" 1)
  uniquify-rationalize-a-list(([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/angular-videojs" #<buffer app.module.ts> "app.module.ts<angular-videojs>"] [cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront" #<buffer app.module.ts<2>> "app.module.ts<twlfront>"]) 1)
  uniquify-rationalize-conflicting-sublist(([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/angular-videojs" #<buffer app.module.ts> "app.module.ts<angular-videojs>"] [cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront" #<buffer app.module.ts<2>> "app.module.ts<twlfront>"]) "app.module.ts" 0)
  uniquify-rationalize-a-list(([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront" #<buffer app.module.ts<2>> "app.module.ts<twlfront>"] [cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/angular-videojs" #<buffer app.module.ts> "app.module.ts<angular-videojs>"]))
  uniquify-rationalize(([cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/twlfront/src/app" #<buffer app.module.ts<2>> "app.module.ts"] [cl-struct-uniquify-item "app.module.ts" "/home/hlolli/Documents/angular-videojs/src/app" #<buffer app.module.ts> "app.module.ts"]))
  uniquify-rationalize-file-buffer-names("app.module.ts" "/home/hlolli/Documents/twlfront/src/app/" #<buffer app.module.ts<2>>)
  uniquify--create-file-buffer-advice(#[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] "~/Documents/twlfront/src/app/app.module.ts")
  apply(uniquify--create-file-buffer-advice #[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] "~/Documents/twlfront/src/app/app.module.ts")
  #[128 "\300\301\302�#\207" [apply uniquify--create-file-buffer-advice #[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] nil] 5 1914503]("~/Documents/twlfront/src/app/app.module.ts")
  apply(#[128 "\300\301\302�#\207" [apply uniquify--create-file-buffer-advice #[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] nil] 5 1914503] "~/Documents/twlfront/src/app/app.module.ts" nil)
  (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))
  (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)))
  (progn (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))))
  (unwind-protect (progn (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)))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))
  (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (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 ...)) (generate-new-buffer name) name) (apply cfb-fun filename args)))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))
  (let ((temp-buffer (generate-new-buffer " *temp*"))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert-file-contents filename) (let ((buf-start (buffer-substring-no-properties ... ...))) (if (and (string-match "\\.clj[cxs]?$" filename) (string-match "(ns \\([^\n )]+\\)" buf-start)) (let (...) (generate-new-buffer name) name) (apply cfb-fun filename args)))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
  (if (and (file-exists-p filename) (not (file-directory-p filename))) (let ((temp-buffer (generate-new-buffer " *temp*"))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert-file-contents filename) (let ((buf-start ...)) (if (and ... ...) (let ... ... name) (apply cfb-fun filename args)))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))) (apply cfb-fun filename args))
  plexus/clj-ns--create-file-buffer-advice(#[128 "\300\301\302�#\207" [apply uniquify--create-file-buffer-advice #[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] nil] 5 1914503] "~/Documents/twlfront/src/app/app.module.ts")
  apply(plexus/clj-ns--create-file-buffer-advice #[128 "\300\301\302�#\207" [apply uniquify--create-file-buffer-advice #[257 "\301�!\211\302\230\203\f��\262�\303\304�\305\306�\307���#)\266\203\203#�\310�P\202$��!\207" [inhibit-changing-match-data file-name-nondirectory "" generate-new-buffer "\\` " nil t string-match "|"] 10 0] nil] 5 1914503] "~/Documents/twlfront/src/app/app.module.ts")
  create-file-buffer("~/Documents/twlfront/src/app/app.module.ts")
  find-file-noselect("/home/hlolli/Documents/twlfront/src/app/app.module.ts" nil nil)
  ido-file-internal(raise-frame)
  ido-find-file()
  funcall-interactively(ido-find-file)
  call-interactively(ido-find-file nil nil)
  command-execute(ido-find-file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment