branch: externals/compat commit 512e6398af06c56dcf5853498f79b5480c17c8c1 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
compat-28: Fix and test with-existing-directory --- NEWS.org | 1 + compat-28.el | 25 ++++++++++++------------- compat-tests.el | 8 ++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/NEWS.org b/NEWS.org index c56076e834..50281fbb15 100644 --- a/NEWS.org +++ b/NEWS.org @@ -15,6 +15,7 @@ - Compat takes great care to remove unneeded definitions at compile time. On recent Emacs 29 the byte compiled files are empty and not loaded, such that Compat does not any cost to the Emacs process. +- compat-28: Fix and test ~with-existing-directory~. - compat-29: Drop broken functions ~string-pixel-width~ and ~buffer-text-pixel-size~. These functions had poor performance which lead to a downstream issue in the doom-modeline package. If a more efficient solution is diff --git a/compat-28.el b/compat-28.el index ce41338248..a39f148909 100644 --- a/compat-28.el +++ b/compat-28.el @@ -380,22 +380,21 @@ Also see `local-variable-p'." (void-variable nil (throw 'fail nil))) t)) -(compat-defmacro with-existing-directory (&rest body) ;; <UNTESTED> +(compat-defmacro with-existing-directory (&rest body) ;; <OK> "Execute BODY with `default-directory' bound to an existing directory. If `default-directory' is already an existing directory, it's not changed." (declare (indent 0) (debug t)) - (let ((quit (make-symbol "with-existing-directory-quit"))) - `(catch ',quit - (dolist (dir (list default-directory - (expand-file-name "~/") - (getenv "TMPDIR") - "/tmp/" - ;; XXX: check if "/" works on non-POSIX - ;; system. - "/")) - (when (and dir (file-exists-p dir)) - (throw ',quit (let ((default-directory dir)) - ,@body))))))) + `(let ((default-directory + (or (catch 'quit + (dolist (dir (list default-directory + (expand-file-name "~/") + temporary-file-directory + (getenv "TMPDIR") + "/tmp/")) + (when (and dir (file-exists-p dir)) + (throw 'quit dir)))) + "/"))) + ,@body)) (compat-defmacro dlet (binders &rest body) ;; <UNTESTED> "Like `let' but using dynamic scoping." diff --git a/compat-tests.el b/compat-tests.el index 487eca8b57..2de21eb18c 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -662,6 +662,14 @@ (should-equal t (always 1)) ;; single argument (should-equal t (always 1 2 3 4))) ;; multiple arguments +(ert-deftest with-existing-directory () + (let ((dir (make-temp-name "/tmp/not-exist-"))) + (let ((default-directory dir)) + (should-not (file-exists-p default-directory))) + (with-existing-directory + (should-not (equal dir default-directory)) + (should (file-exists-p default-directory))))) + (ert-deftest directory-name-p () (should (directory-name-p "/")) (should-not (directory-name-p "/file"))