]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix usage of cons cells in grep-find-ignored-files
authorSpencer Baugh <sbaugh@janestreet.com>
Wed, 22 May 2024 12:28:07 +0000 (08:28 -0400)
committerEshel Yaron <me@eshelyaron.com>
Sun, 26 May 2024 05:55:27 +0000 (07:55 +0200)
grep-find-ignored-files is documented to also include cons
cells, not just globs, but there were two places outside grep.el
where we were using it as if it was only a string list.

To fix this, add a helper function named grep-find-ignored-files
which handles grep-find-ignored-files properly and returns the
list of globs, and use it everywhere.

* lisp/progmodes/grep.el (grep--filter-list-by-dir)
(grep-find-ignored-files): New functions.
(rgrep-find-ignored-directories): Use grep--filter-list-by-dir.
(lgrep, rgrep-default-command):
Use grep-find-ignored-files function.
* lisp/dired-aux.el (dired-do-find-regexp):
Use grep-find-ignored-files function.
* lisp/progmodes/project.el (project-ignores):
Use grep-find-ignored-files function, if bound. (bug#71115)

(cherry picked from commit c812c935486010bfe2f80c3887c708fbaa4907a6)

lisp/dired-aux.el
lisp/progmodes/grep.el
lisp/progmodes/project.el

index db86e2205bb2fd4c2396405bf98cee68df609b0d..b6f77992c76d1790628aee5b57fec0252c588c64 100644 (file)
@@ -3837,13 +3837,13 @@ REGEXP should use constructs supported by your local `grep' command."
   (interactive "sSearch marked files (regexp): " dired-mode)
   (require 'grep)
   (require 'xref)
-  (defvar grep-find-ignored-files)
   (declare-function rgrep-find-ignored-directories "grep" (dir))
+  (declare-function grep-find-ignored-files "grep" (dir))
   (let* ((marks (dired-get-marked-files nil nil nil nil t))
          (ignores (nconc (mapcar
                           #'file-name-as-directory
                           (rgrep-find-ignored-directories default-directory))
-                         grep-find-ignored-files))
+                         (grep-find-ignored-files default-directory)))
          (files nil))
     (dolist (mark (reverse marks))
       (if (file-directory-p mark)
index fd2d3b2f494527b875fa27d64ad6d70974e4f770..cd2f81dc24f46923552dbd997ed1d01d0783672d 100644 (file)
@@ -1177,6 +1177,19 @@ REGEXP is used as a string in the prompt."
 
 (defvar grep-use-directories-skip 'auto-detect)
 
+(defun grep--filter-list-by-dir (list dir)
+  "Include elements of LIST which are applicable to DIR."
+  (delq nil (mapcar
+             (lambda (ignore)
+               (cond ((stringp ignore) ignore)
+                     ((consp ignore)
+                      (and (funcall (car ignore) dir) (cdr ignore)))))
+             list)))
+
+(defun grep-find-ignored-files (dir)
+  "Return the list of ignored files applicable to DIR."
+  (grep--filter-list-by-dir grep-find-ignored-files dir))
+
 ;;;###autoload
 (defun lgrep (regexp &optional files dir confirm)
   "Run grep, searching for REGEXP in FILES in directory DIR.
@@ -1237,20 +1250,13 @@ command before it's run."
                       regexp
                       files
                       nil
-                      (and grep-find-ignored-files
-                           (concat " --exclude="
-                                   (mapconcat
-                                     (lambda (ignore)
-                                       (cond ((stringp ignore)
-                                              (shell-quote-argument
-                                               ignore grep-quoting-style))
-                                             ((consp ignore)
-                                              (and (funcall (car ignore) dir)
-                                                   (shell-quote-argument
-                                                    (cdr ignore)
-                                                    grep-quoting-style)))))
-                                    grep-find-ignored-files
-                                    " --exclude=")))
+                       (when-let ((ignores (grep-find-ignored-files dir)))
+                        (concat " --exclude="
+                                (mapconcat
+                                  (lambda (ignore)
+                                    (shell-quote-argument ignore grep-quoting-style))
+                                  ignores
+                                  " --exclude=")))
                       (and (eq grep-use-directories-skip t)
                            '("--directories=skip"))))
        (when command
@@ -1354,13 +1360,8 @@ to indicate whether the grep should be case sensitive or not."
              (setq default-directory dir)))))))
 
 (defun rgrep-find-ignored-directories (dir)
-  "Return the list of ignored directories applicable to `dir'."
-  (delq nil (mapcar
-             (lambda (ignore)
-               (cond ((stringp ignore) ignore)
-                     ((consp ignore)
-                      (and (funcall (car ignore) dir) (cdr ignore)))))
-             grep-find-ignored-directories)))
+  "Return the list of ignored directories applicable to DIR."
+  (grep--filter-list-by-dir grep-find-ignored-directories dir))
 
 (defun rgrep-default-command (regexp files dir)
   "Compute the command for \\[rgrep] to use by default."
@@ -1378,37 +1379,31 @@ to indicate whether the grep should be case sensitive or not."
            (shell-quote-argument ")" grep-quoting-style))
    dir
    (concat
-    (and grep-find-ignored-directories
-         (concat "-type d "
-                 (shell-quote-argument "(" grep-quoting-style)
-                 ;; we should use shell-quote-argument here
-                 " -path "
-                 (mapconcat
-                  (lambda (d)
-                    (shell-quote-argument (concat "*/" d) grep-quoting-style))
-                  (rgrep-find-ignored-directories dir)
-                  " -o -path ")
-                 " "
-                 (shell-quote-argument ")" grep-quoting-style)
-                 " -prune -o "))
-    (and grep-find-ignored-files
-         (concat (shell-quote-argument "!" grep-quoting-style) " -type d "
-                 (shell-quote-argument "(" grep-quoting-style)
-                 ;; we should use shell-quote-argument here
-                 " -name "
-                 (mapconcat
-                  (lambda (ignore)
-                    (cond ((stringp ignore)
-                           (shell-quote-argument ignore grep-quoting-style))
-                          ((consp ignore)
-                           (and (funcall (car ignore) dir)
-                                (shell-quote-argument
-                                 (cdr ignore) grep-quoting-style)))))
-                  grep-find-ignored-files
-                  " -o -name ")
-                 " "
-                 (shell-quote-argument ")" grep-quoting-style)
-                 " -prune -o ")))))
+    (when-let ((ignored-dirs (rgrep-find-ignored-directories dir)))
+      (concat "-type d "
+              (shell-quote-argument "(" grep-quoting-style)
+              ;; we should use shell-quote-argument here
+              " -path "
+              (mapconcat
+               (lambda (d)
+                 (shell-quote-argument (concat "*/" d) grep-quoting-style))
+               ignored-dirs
+               " -o -path ")
+              " "
+              (shell-quote-argument ")" grep-quoting-style)
+              " -prune -o "))
+    (when-let ((ignored-files (grep-find-ignored-files dir)))
+      (concat (shell-quote-argument "!" grep-quoting-style) " -type d "
+              (shell-quote-argument "(" grep-quoting-style)
+              ;; we should use shell-quote-argument here
+              " -name "
+              (mapconcat
+               (lambda (ignore) (shell-quote-argument ignore grep-quoting-style))
+               ignored-files
+               " -o -name ")
+              " "
+              (shell-quote-argument ")" grep-quoting-style)
+              " -prune -o ")))))
 
 (defun grep-find-toggle-abbreviation ()
   "Toggle showing the hidden part of rgrep/lgrep/zrgrep command line."
index e0164ae80230d59606f7f6c12228a24d55b1ed91..6d467e5f610406c9cfb499957de8560b6c5a34db 100644 (file)
@@ -295,7 +295,7 @@ headers search path, load path, class path, and so on."
 Nominally unique, but not enforced."
   (file-name-nondirectory (directory-file-name (project-root project))))
 
-(cl-defgeneric project-ignores (_project _dir)
+(cl-defgeneric project-ignores (_project dir)
   "Return the list of glob patterns to ignore inside DIR.
 Patterns can match both regular files and directories.
 To root an entry, start it with `./'.  To match directories only,
@@ -305,12 +305,15 @@ end it with `/'.  DIR must be either `project-root' or one of
   ;; TODO: Support whitelist entries.
   (require 'grep)
   (defvar grep-find-ignored-files)
+  (declare-function grep-find-ignored-files "grep" (dir))
   (nconc
    (mapcar
     (lambda (dir)
       (concat dir "/"))
     vc-directory-exclusion-list)
-   grep-find-ignored-files))
+   (if (fboundp 'grep-find-ignored-files)
+       (grep-find-ignored-files dir)
+     grep-find-ignored-files)))
 
 (defun project--file-completion-table (all-files)
   (completion-table-with-metadata all-files '((category . project-file))))