From: Spencer Baugh Date: Wed, 22 May 2024 12:28:07 +0000 (-0400) Subject: Fix usage of cons cells in grep-find-ignored-files X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ba974705d2f9a3589d94a824d67a684839a0837f;p=emacs.git Fix usage of cons cells in grep-find-ignored-files 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) --- diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index db86e2205bb..b6f77992c76 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -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) diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index fd2d3b2f494..cd2f81dc24f 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -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." diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index e0164ae8023..6d467e5f610 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -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))))