diff options
| author | Stefan Kangas | 2025-03-08 13:02:58 +0100 |
|---|---|---|
| committer | Stefan Kangas | 2025-03-09 18:24:21 +0100 |
| commit | 87db670d045bea2d90139b1f741eea8db7c193ea (patch) | |
| tree | 4523b6f3a49164c6211f1c06192f1e6696b141b4 | |
| parent | 9c6e8589ee5eae0f793994c96d434b77f54749de (diff) | |
| download | emacs-87db670d045bea2d90139b1f741eea8db7c193ea.tar.gz emacs-87db670d045bea2d90139b1f741eea8db7c193ea.zip | |
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.
| -rw-r--r-- | lisp/files.el | 16 | ||||
| -rw-r--r-- | test/lisp/files-tests.el | 13 |
2 files changed, 26 insertions, 3 deletions
diff --git a/lisp/files.el b/lisp/files.el index bf05939ebeb..aa4da83bfe0 100644 --- a/lisp/files.el +++ b/lisp/files.el | |||
| @@ -1242,12 +1242,24 @@ inaccessible location." | |||
| 1242 | If NEW-NAME exists in `user-emacs-directory', return it. | 1242 | If NEW-NAME exists in `user-emacs-directory', return it. |
| 1243 | Else if OLD-NAME is non-nil and ~/OLD-NAME exists, return ~/OLD-NAME. | 1243 | Else if OLD-NAME is non-nil and ~/OLD-NAME exists, return ~/OLD-NAME. |
| 1244 | Else return NEW-NAME in `user-emacs-directory', creating the | 1244 | Else return NEW-NAME in `user-emacs-directory', creating the |
| 1245 | directory if it does not exist." | 1245 | directory if it does not exist. |
| 1246 | |||
| 1247 | NEW-NAME can also be a list, in which case consider all names in that | ||
| 1248 | list, from last to first, and use the first name that exists. If none | ||
| 1249 | of them exists, use the `car' of that list." | ||
| 1246 | (convert-standard-filename | 1250 | (convert-standard-filename |
| 1247 | (let* ((home (concat "~" (or init-file-user ""))) | 1251 | (let* ((home (concat "~" (or init-file-user ""))) |
| 1248 | (at-home (and old-name (expand-file-name old-name home))) | 1252 | (at-home (and old-name (expand-file-name old-name home))) |
| 1249 | (bestname (abbreviate-file-name | 1253 | (bestname (abbreviate-file-name |
| 1250 | (expand-file-name new-name user-emacs-directory)))) | 1254 | (if (listp new-name) |
| 1255 | (or (car (seq-filter | ||
| 1256 | #'file-exists-p | ||
| 1257 | (mapcar | ||
| 1258 | (lambda (f) | ||
| 1259 | (expand-file-name f user-emacs-directory)) | ||
| 1260 | (reverse new-name)))) | ||
| 1261 | (expand-file-name (car new-name) user-emacs-directory)) | ||
| 1262 | (expand-file-name new-name user-emacs-directory))))) | ||
| 1251 | (if (and at-home (not (file-readable-p bestname)) | 1263 | (if (and at-home (not (file-readable-p bestname)) |
| 1252 | (file-readable-p at-home)) | 1264 | (file-readable-p at-home)) |
| 1253 | at-home | 1265 | at-home |
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index ed63b6f6fb4..9f17747da1f 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el | |||
| @@ -68,7 +68,18 @@ | |||
| 68 | (should (equal (locate-user-emacs-file basename) | 68 | (should (equal (locate-user-emacs-file basename) |
| 69 | in-edir)) | 69 | in-edir)) |
| 70 | (should (equal (locate-user-emacs-file basename "anything") | 70 | (should (equal (locate-user-emacs-file basename "anything") |
| 71 | in-edir))))))) | 71 | in-edir))) |
| 72 | ;; NEW-FILE is a list. | ||
| 73 | (should (equal (locate-user-emacs-file '("first" "second")) | ||
| 74 | (expand-file-name "first" user-emacs-directory))) | ||
| 75 | (should (equal (locate-user-emacs-file '("first" "second") "never") | ||
| 76 | (expand-file-name "first" user-emacs-directory))) | ||
| 77 | (let ((exists (expand-file-name "exists" user-emacs-directory))) | ||
| 78 | (write-region "data" nil exists nil 'quietly) | ||
| 79 | (should (equal (locate-user-emacs-file '("missing" "exists")) | ||
| 80 | exists)) | ||
| 81 | (should (equal (locate-user-emacs-file '("missing1" "exists") "missing2") | ||
| 82 | exists))))))) | ||
| 72 | 83 | ||
| 73 | ;; Test combinations: | 84 | ;; Test combinations: |
| 74 | ;; `enable-local-variables' t, nil, :safe, :all, or something else. | 85 | ;; `enable-local-variables' t, nil, :safe, :all, or something else. |