Make locate-user-emacs-file accept a list too

This can be used to migrate to a new name, while also keeping
backwards-compatible support for an old name.

* lisp/files.el (locate-user-emacs-file): Accept a list as the
NEW-NAME argument.
* test/lisp/files-tests.el (files-test-locate-user-emacs-file): New
test for the above.
This commit is contained in:
Stefan Kangas 2025-03-08 13:02:58 +01:00
parent 9c6e8589ee
commit 87db670d04
2 changed files with 26 additions and 3 deletions

View file

@ -1242,12 +1242,24 @@ inaccessible location."
If NEW-NAME exists in `user-emacs-directory', return it.
Else if OLD-NAME is non-nil and ~/OLD-NAME exists, return ~/OLD-NAME.
Else return NEW-NAME in `user-emacs-directory', creating the
directory if it does not exist."
directory if it does not exist.
NEW-NAME can also be a list, in which case consider all names in that
list, from last to first, and use the first name that exists. If none
of them exists, use the `car' of that list."
(convert-standard-filename
(let* ((home (concat "~" (or init-file-user "")))
(at-home (and old-name (expand-file-name old-name home)))
(bestname (abbreviate-file-name
(expand-file-name new-name user-emacs-directory))))
(if (listp new-name)
(or (car (seq-filter
#'file-exists-p
(mapcar
(lambda (f)
(expand-file-name f user-emacs-directory))
(reverse new-name))))
(expand-file-name (car new-name) user-emacs-directory))
(expand-file-name new-name user-emacs-directory)))))
(if (and at-home (not (file-readable-p bestname))
(file-readable-p at-home))
at-home

View file

@ -68,7 +68,18 @@
(should (equal (locate-user-emacs-file basename)
in-edir))
(should (equal (locate-user-emacs-file basename "anything")
in-edir)))))))
in-edir)))
;; NEW-FILE is a list.
(should (equal (locate-user-emacs-file '("first" "second"))
(expand-file-name "first" user-emacs-directory)))
(should (equal (locate-user-emacs-file '("first" "second") "never")
(expand-file-name "first" user-emacs-directory)))
(let ((exists (expand-file-name "exists" user-emacs-directory)))
(write-region "data" nil exists nil 'quietly)
(should (equal (locate-user-emacs-file '("missing" "exists"))
exists))
(should (equal (locate-user-emacs-file '("missing1" "exists") "missing2")
exists)))))))
;; Test combinations:
;; `enable-local-variables' t, nil, :safe, :all, or something else.