Skip to content

Instantly share code, notes, and snippets.

@kototama
Created November 11, 2011 16:16
Show Gist options
  • Save kototama/1358405 to your computer and use it in GitHub Desktop.
Save kototama/1358405 to your computer and use it in GitHub Desktop.
Update the namespace declaration of a Clojure buffer according to its pathname.
(defun clojure-correct-ns
()
"Returns the namespace name that the file should have."
(let* ((nsname ())
(dirs (reverse (split-string (buffer-file-name) "/")))
(aftersrc nil))
(dolist (dir dirs)
(when (not aftersrc)
(if (or (string= dir "src") (string= dir "test"))
(setq aftersrc t)
(setq nsname (append nsname (list dir "."))))))
(when nsname
(replace-regexp-in-string "_" "-" (substring (apply 'concat (reverse nsname)) 1 -4)))))
(defun clojure-update-ns
()
"Updates the namespace of the current buffer. Useful if a file has been renamed."
(interactive)
(let ((nsname (clojure-correct-ns)))
(when nsname
(clojure-find-ns) ;; function defined in clojure-mode
(replace-match nsname nil nil nil 4))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment