2

In my ~/.emacs.d/init.el, I have auto-save-file-name-transforms set so that auto-save files are saved at paths such as /home/[user]/.emacs.d/auto-saves/#!home![user]!path!to!file#. This works fine.

In a directory ~/foo/bar version-controlled in Git, I would like to have auto-saves of files in that directory instead be saved into ~/foo/bar/.emacs.d/auto-saves, so I try to set auto-save-file-name-transforms in ~/foo/bar/.dir-locals.el, but this does not seem to work.

As a minimal reproduction, I make a new directory ~/tmp/test, run git init inside it, and put the following as ~/tmp/test/.dir-locals.el:

;;; Directory Local Variables            -*- no-byte-compile: t -*-
;;; For more information see (info "(emacs) Directory Variables")

((nil . ((eval . (let* ((root (project-root (project-current)))
                        (dir (file-name-concat root ".emacs.d/auto-saves/")))
                   (make-directory dir t)
                   (setq-local auto-save-file-name-transforms
                               `((".*" ,dir t))))))))

After I C-x C-c and reopen Emacs and reopen this .dir-locals.el file, I know Emacs is reading this file because it prompts me to confirm that I would like to run the code in the file. However, the setting of auto-save-file-name-transforms does not seem to work:

  • ~/tmp/test/.emacs.d/auto-saves/ is created as desired,

  • for the .dir-locals.el file,

    • auto-save-file-name-transforms is ((".*" "~/tmp/test/.emacs.d/auto-saves/" t)), which looks right, and

    • (make-auto-save-file-name) returns "/home/[user]/tmp/test/.emacs.d/auto-saves/#!home![user]!tmp!test!.dir-locals.el#", which is indeed the path at which I would like the file to be auto-saved,

    • but buffer-auto-save-file-name is "/home/[user]/.emacs.d/auto-saves/#!home![user]!tmp!test!.dir-locals.el#", which is the same as it would be if this .dir-locals.el did nothing,

      • and, indeed, if I edit the file, it is auto-saved at that unwanted path.

In the manual node "(emacs)Top > Files > Auto Save > Auto Save Files", I see "The file name to be used for auto-saving in a buffer is calculated when auto-saving is turned on in that buffer". I guess that buffer-auto-save-file-name is being set too early, before the .dir-locals.el file locally overwrites auto-save-file-name-transforms.

How, if at all, can I make the files in the Git-versioned directory be auto-saved into a .emacs.d/auto-saves/ in that same Git-versioned directory rather than to the .emacs.d/auto-saves/ in my home directory?

(emacs-version) gives "GNU Emacs 30.1 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.18.2, Xaw3d scroll bars)".

1 Answer 1

2

You say: "I guess that buffer-auto-save-file-name is being set too early, before the .dir-locals.el file locally overwrites auto-save-file-name-transforms."

That sounds right, but you can check by recalculating and locally setting buffer-auto-save-file-name:

;;; Directory Local Variables            -*- no-byte-compile: t -*-
;;; For more information see (info "(emacs) Directory Variables")

((nil . ((eval . (let* ((root (project-root (project-current)))
                        (dir (file-name-concat root ".emacs.d/auto-saves/")))
                   (make-directory dir t)
                   (setq-local auto-save-file-name-transforms
                               `((".*" ,dir t))
                               buffer-auto-save-file-name (make-auto-save-file-name)))))))

Since make-auto-save-file-name returns what you expect, that should fix it. Untested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.