Function: projectile--grep

projectile--grep is a byte-compiled function defined in projectile.el.

Signature

(projectile--grep SEARCH-REGEXP &optional FILES)

Documentation

Run rgrep (or vc-git-grep) for SEARCH-REGEXP across the project.

FILES, when non-nil, is an rgrep files specification restricting the search. Honours Projectile's ignore configuration and runs projectile-grep-finished-hook when done.

Source Code

;; Defined in ~/.emacs.d/elpa/projectile-20260820.1509/projectile.el
;;;; Search engines

(defun projectile--grep (search-regexp &optional files)
  "Run rgrep (or `vc-git-grep') for SEARCH-REGEXP across the project.
FILES, when non-nil, is an rgrep files specification restricting the
search.  Honours Projectile's ignore configuration and runs
`projectile-grep-finished-hook' when done."
  (require 'grep) ;; for `rgrep'
  (let* ((project-root (projectile-acquire-root))
         (roots (projectile-get-project-directories project-root))
         ;; The ignore and ensure rules are the project's, so they're read
         ;; once and respelled per root below.
         (ignore-specs (projectile--grep-find-specs
                        (projectile--ignore-patterns project-root)))
         (ensure-specs (projectile--grep-find-specs
                        (projectile--ensure-patterns project-root))))
    (dolist (root-dir roots)
      (require 'vc-git) ;; for `vc-git-grep'
      ;; in git projects users have the option to use `vc-git-grep' instead of `rgrep'
      (if (and (eq (projectile-project-vcs) 'git)
               projectile-use-git-grep)
          (vc-git-grep search-regexp (or files "") root-dir)
        (let ((projectile-grep-find-ignored-paths
               (projectile--grep-rebase-paths (car ignore-specs)
                                              project-root root-dir))
              (projectile-grep-find-ignored-patterns (cdr ignore-specs))
              (projectile-grep-find-unignored-paths
               (projectile--grep-rebase-paths (car ensure-specs)
                                              project-root root-dir))
              (projectile-grep-find-unignored-patterns (cdr ensure-specs)))
          (grep-compute-defaults)
          (cl-letf (((symbol-function 'rgrep-default-command) #'projectile-rgrep-default-command))
            (rgrep search-regexp (or files "* .*") root-dir)
            (when (get-buffer "*grep*")
              ;; When grep is using a global *grep* buffer rename it to be
              ;; scoped to the current root to allow multiple concurrent grep
              ;; operations, one per root
              (with-current-buffer "*grep*"
                (rename-buffer (concat "*grep <" root-dir ">*") t)))))))
    (run-hooks 'projectile-grep-finished-hook)))