emacs

enn 2025-12-12T16:02:08.366389Z

When I create a buffer for a new file in a Clojure project, something automatically populates it with an empty ns form based on the filename and path. 1. What is creating this ns form? I assume clojure-mode, but can't find anything in the docs about this feature. 2. clojure-mode-hooks seem to run before this ns form is inserted. Is there any hook I can use that will run after?

plexus 2025-12-14T13:44:28.546259Z

@enn this is the package https://github.com/corgi-emacs/clj-ns-name

plexus 2025-12-14T13:45:25.859299Z

Standard part of Corgi emacs

young-il 2025-12-14T03:07:30.327829Z

@enn You can easily customize how emacs creates buffer names for identical file names by setting 'uniquify-buffer-name-style'. For your use case (setq uniquify-buffer-name-style 'forward) will add the directory name in front of the filename to make the buffer name distinct: routes/foo.clj and handler/foo.clj

ericdallo 2025-12-12T16:06:06.362379Z

clojure-lsp and clj-refactor do that, probably it's your clojure-lsp

ericdallo 2025-12-12T16:07:12.934229Z

check :auto-add-ns-to-new-files? in clojure-lsp https://clojure-lsp.io/settings/

enn 2025-12-12T16:07:35.357589Z

ah, thank you

yuhan 2025-12-12T16:10:54.484759Z

check your find-file-hook in particular, clj-refactor adds itself to it when loaded.

enn 2025-12-12T16:27:15.300819Z

The context here is: I want to have my Clojure buffers named after the namespace rather than the filename. I have a function to do this that looks like so:

(defun rename-buffer-to-ns ()
  (interacive)
  (when (buffer-file-name)
    (let ((ns (clojure-find-ns)))
      (when (and ns (not (string= "" ns)))
        (rename-buffer ns)))))
I have this function added as a clojure-mode-hook and that works fine for opening already-existing Clojure files. It does not work for new files because clojure-find-ns returns nil because this hook runs before the ns form is inserted. If I run this function manually after the buffer is opened and the ns form is inserted, it renames the buffer correctly. Is there an obvious way to make this run after lsp-mode inserts the ns form? Or is there some other, better approach to having my buffers named after the namespace?

ericdallo 2025-12-12T16:29:03.113419Z

First why you want a different name? Probably because your source paths not match? If so, configuring Clojure-lsp source-paths may be a good idea

ag 2025-12-12T16:31:48.818479Z

@enn , iirc Arne - @plexus done something like that, I'm on my phone (can't look it up) but maybe check his emacs config (should be on gh).

ag 2025-12-12T16:33:11.806309Z

I do remember he at some point was experimenting with naming the buffers like FQN of namespaces

ag 2025-12-12T16:35:01.753829Z

If you're doing that purely for a better way of finding the right ns file quickly - maybe don't. Use consult-lsp instead

enn 2025-12-12T16:46:39.235219Z

My source paths match, I just find the namespace more useful than the filename. In my Clojure projects I often have a bunch of same-named files in different parts of the source tree. Maybe I have a routes/foo.clj that has the API route definitions for the foo domain entity, a handler/foo.clj that has the HTTP handlers, a domain/foo.clj that has the domain model, a validation/foo.clj that has some business logic, etc. I also usually have a bunch of core.cljs and util.cljs. By default Emacs gives these buffers the useless names foo.clj, foo.clj<2> , foo.clj<3>, etc. It's much more helpful to see names like my-project.routes.foo, my-project.handler.foo, etc. in the modeline and in my buffer list.

enn 2025-12-12T16:47:07.811079Z

I'll take a look at plexus's Emacs config, thanks for the tip

ericdallo 2025-12-12T16:53:25.295269Z

Ah so its not the ns you wanna customize, but the modeline to show the ns and not the file name? Got it

enn 2025-12-12T17:08:00.445489Z

right, I want the buffer name to be the ns

ericdallo 2025-12-12T17:08:57.326889Z

Yeah I'm not aware of any "official" way to do that, but sounds like somethhing interesting that lsp-mode could have optionally indeed

ag 2025-12-12T17:49:00.786939Z

@enn ah, I found Arne's experiment. Gosh it's been 7 years ago https://gist.github.com/plexus/5418819323afb892b481816745be15e0

Ed 2025-12-12T17:51:16.968029Z

I have this in my setup

(setq-default mode-line-buffer-identification
              '(:eval (format-mode-line
                       (propertized-buffer-identification
                        (or (when (project-current)
                              (when-let* ((buffer-file-truename buffer-file-truename)
                                          (prj (project-root (project-current)))
                                          (prj-parent (file-name-directory (directory-file-name (expand-file-name prj)))))
                                (concat (file-relative-name (file-name-directory buffer-file-truename) prj-parent) (file-name-nondirectory buffer-file-truename))))
                            ;; (file-name-nondirectory buffer-file-truename)
                            "%b")))))
which will put the project name and full path to the file in the place of the buffer name when the file it's pointing to is part of a project.el project, so it looks something like this

👍 1
Ed 2025-12-12T17:57:12.610999Z

I also tend to use project-find-file to find files and don't really look at the buffer list very much. So maybe that's why I'm fine with not renaming the buffers. I'm sure there are schemes for changing how unique buffer names are chosen when they clash, So you get a dir name instead of <2> etc.... let me google a sec

Ed 2025-12-12T18:01:45.593169Z

yeah maybe look at the variable uniquify-buffer-name-style? Maybe setting that to forward would be good enough?