From: Stefan Monnier Date: Sun, 10 Dec 2023 00:46:07 +0000 (-0500) Subject: (file-expand-wildcards): Handle patterns ending in "/" X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=6cc1418fc3e8107cab2c9824c367ba7762235aef;p=emacs.git (file-expand-wildcards): Handle patterns ending in "/" The bug was encountered via the ls-lisp advice on Dired but it actually affects all uses of `file-expand-wildcards`, so better fix it there. * lisp/files.el (file-expand-wildcards): Fix bug#60819. * lisp/ls-lisp.el (ls-lisp--dired): Undo commit b365a7cc32e2. * test/lisp/files-tests.el (files-tests--expand-wildcards): New test. --- diff --git a/lisp/files.el b/lisp/files.el index 047854d3939..3c1d0c30e67 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -7547,27 +7547,34 @@ default directory. However, if FULL is non-nil, they are absolute." (dolist (dir (nreverse dirs)) (when (or (null dir) ; Possible if DIRPART is not wild. (file-accessible-directory-p dir)) - (let ((this-dir-contents - ;; Filter out "." and ".." - (delq nil - (mapcar (lambda (name) - (unless (string-match "\\`\\.\\.?\\'" - (file-name-nondirectory name)) - name)) - (directory-files - (or dir ".") full - (if regexp - ;; We're matching each file name - ;; element separately. - (concat "\\`" nondir "\\'") - (wildcard-to-regexp nondir))))))) - (setq contents - (nconc - (if (and dir (not full)) - (mapcar (lambda (name) (concat dir name)) - this-dir-contents) - this-dir-contents) - contents))))) + (if (equal "" nondir) + ;; `nondir' is "" when the pattern ends in "/". Basically "" + ;; refers to the directory itself, like ".", but it's not + ;; among the names returned by `directory-files', so we have + ;; to special-case it. + (push (or dir nondir) contents) + (let ((this-dir-contents + ;; Filter out "." and ".." + (delq nil + (mapcar (lambda (name) + (unless (string-match "\\`\\.\\.?\\'" + (file-name-nondirectory + name)) + name)) + (directory-files + (or dir ".") full + (if regexp + ;; We're matching each file name + ;; element separately. + (concat "\\`" nondir "\\'") + (wildcard-to-regexp nondir))))))) + (setq contents + (nconc + (if (and dir (not full)) + (mapcar (lambda (name) (concat dir name)) + this-dir-contents) + this-dir-contents) + contents)))))) contents))) (defcustom find-sibling-rules nil @@ -7757,7 +7764,7 @@ need to be passed verbatim to shell commands." (purecopy "ls")) "Absolute or relative name of the `ls'-like program. This is used by `insert-directory' and `dired-insert-directory' -(thus, also by `dired'). For Dired, this should ideally point to +\(thus, also by `dired'). For Dired, this should ideally point to GNU ls, or another version of ls that supports the \"--dired\" flag. See `dired-use-ls-dired'. diff --git a/lisp/ls-lisp.el b/lisp/ls-lisp.el index c576819c5d0..1066f38c050 100644 --- a/lisp/ls-lisp.el +++ b/lisp/ls-lisp.el @@ -483,22 +483,8 @@ not contain `d', so that a full listing is expected." (if (not dir-wildcard) (funcall orig-fun dir-or-list switches) (let* ((default-directory (car dir-wildcard)) - (wildcard (cdr dir-wildcard)) - (files (file-expand-wildcards wildcard)) + (files (file-expand-wildcards (cdr dir-wildcard))) (dir (car dir-wildcard))) - ;; When the wildcard ends in a slash, file-expand-wildcards - ;; returns nil; fix that by treating the wildcards as - ;; specifying only directories whose names match the - ;; widlcard. - (if (and (null files) - (directory-name-p wildcard)) - (setq files - (delq nil - (mapcar (lambda (fname) - (if (file-accessible-directory-p fname) - fname)) - (file-expand-wildcards - (directory-file-name wildcard)))))) (if files (let ((inhibit-read-only t) (buf diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index 3e499fff468..24b144c4247 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -2101,5 +2101,9 @@ Prompt users for any modified buffer with `buffer-offer-save' non-nil." (should (documentation 'bar)) (should (documentation 'zot))))) +(ert-deftest files-tests--expand-wildcards () + (should (file-expand-wildcards + (concat (directory-file-name default-directory) "*/")))) + (provide 'files-tests) ;;; files-tests.el ends here