[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
features/user-directory a8f39ac 1/2: Make user-file look in user-emacs-d
From: |
Stefan Kangas |
Subject: |
features/user-directory a8f39ac 1/2: Make user-file look in user-emacs-directory as well |
Date: |
Sun, 7 Nov 2021 22:25:43 -0500 (EST) |
branch: features/user-directory
commit a8f39ac219b67220e45c159c15917c3ad6ad3ecf
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>
Make user-file look in user-emacs-directory as well
* lisp/user-directory.el
(user-file): Look for old file name in `user-emacs-directory' as well.
(user-directory--find-old-name): New defun.
(user-directory--find-or-create-dir): Fix typo.
* test/lisp/user-directory-tests.el
(user-directory-tests-user-file/prefer-existing-in-emacs-dir): New
test.
---
lisp/user-directory.el | 29 +++++++++++++++++++++--------
test/lisp/user-directory-tests.el | 8 ++++++++
2 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/lisp/user-directory.el b/lisp/user-directory.el
index c5d9b82..28da9b2 100644
--- a/lisp/user-directory.el
+++ b/lisp/user-directory.el
@@ -227,6 +227,21 @@ of recently opened files probably belong here."
;;;; user-file
+(defun user-directory--find-old-name (old-name)
+ "Create a list of readable file names based on OLD-NAME.
+OLD-NAME is a string or a list, as in `user-file'.
+
+This is an internal helper function to `user-file'."
+ (catch 'found
+ (dolist (name (or (and (listp old-name) old-name)
+ (list old-name)))
+ (mapcar (lambda (name)
+ (when (file-readable-p name)
+ (throw 'found name)))
+ (list old-name
+ (expand-file-name old-name
+ user-emacs-directory))))))
+
;;;###autoload
(defun user-file (type name &optional old-name)
"Return an absolute per-user Emacs-specific file name.
@@ -249,14 +264,12 @@ you will need to add an explicit \"~/\" at the beginning
of the
string, when converting calls from that function to this one."
(convert-standard-filename
(let* ((dir (user-directory type))
- (new-name (abbreviate-file-name (expand-file-name name dir))))
- (or (and old-name
- (not (file-readable-p new-name))
- (or (and (listp old-name)
- (car (seq-filter #'file-readable-p old-name)))
- (and (file-readable-p old-name)
- old-name)))
- new-name))))
+ (new-name (expand-file-name name dir)))
+ (abbreviate-file-name
+ (or (and old-name
+ (not (file-readable-p new-name))
+ (user-directory--find-old-name old-name))
+ new-name)))))
(provide 'user-directory)
diff --git a/test/lisp/user-directory-tests.el
b/test/lisp/user-directory-tests.el
index f7a3f16..638465d 100644
--- a/test/lisp/user-directory-tests.el
+++ b/test/lisp/user-directory-tests.el
@@ -95,6 +95,14 @@
(file-name-base
(user-file 'downloads "foo"))))))
+(ert-deftest user-directory-tests-user-file/prefer-existing-in-emacs-dir ()
+ (with-user-directory-test
+ (ert-with-temp-file conf
+ (should-not (string-match "\\`foo-bar-baz\\'"
+ (file-name-base
+ (user-file 'downloads "foo-bar-baz"
+ conf)))))))
+
(ert-deftest user-directory-tests-user-file/name-missing ()
(with-user-directory-test
(should (string-match "\\`foo\\'"