]> git.eshelyaron.com Git - emacs.git/commitdiff
Make locate-user-emacs-file accept a list too
authorStefan Kangas <stefankangas@gmail.com>
Sat, 8 Mar 2025 12:02:58 +0000 (13:02 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 12 Mar 2025 18:47:25 +0000 (19:47 +0100)
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.

(cherry picked from commit 87db670d045bea2d90139b1f741eea8db7c193ea)

lisp/files.el
test/lisp/files-tests.el

index b6e2d8f865c2a2be9bae3645a4b96177b8c76894..bc8712fc920f3989e5fceff646f097c693e41958 100644 (file)
@@ -1246,12 +1246,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
index ed63b6f6fb48d02e14983e4e91e37c11145d5c08..9f17747da1f022a1e247b42f356b8c425fe9beaf 100644 (file)
           (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.