1 ;;; grep.el --- run Grep as inferior of Emacs, parse match messages
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.org>
8 ;; Keywords: tools, processes
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This package provides the grep facilities documented in the Emacs
38 "Run grep as inferior of Emacs, parse error messages."
44 (defcustom grep-window-height nil
45 "*Number of lines in a grep window. If nil, use `compilation-window-height'."
46 :type
'(choice (const :tag
"Default" nil
)
51 (defcustom grep-highlight-matches
'auto-detect
52 "If t, use special markers to highlight grep matches.
54 Some grep programs are able to surround matches with special
55 markers in grep output. Such markers can be used to highlight
58 This option sets the environment variable GREP_COLOR to specify
59 markers for highlighting and GREP_OPTIONS to add the --color
60 option in front of any explicit grep options before starting
63 The default value of this variable is set up by `grep-compute-defaults';
64 call that function before using this variable in your program."
65 :type
'(choice (const :tag
"Do not highlight matches with grep markers" nil
)
66 (const :tag
"Highlight matches with grep markers" t
)
67 (other :tag
"Not Set" auto-detect
))
71 (defcustom grep-scroll-output nil
72 "*Non-nil to scroll the *grep* buffer window as output appears.
74 Setting it causes the grep commands to put point at the end of their
75 output window so that the end of the output is always visible rather
82 (defcustom grep-command nil
83 "The default grep command for \\[grep].
84 If the grep program used supports an option to always include file names
85 in its output (such as the `-H' option to GNU grep), it's a good idea to
86 include it when specifying `grep-command'.
88 The default value of this variable is set up by `grep-compute-defaults';
89 call that function before using this variable in your program."
91 (const :tag
"Not Set" nil
))
94 (defcustom grep-template nil
95 "The default command to run for \\[lgrep].
96 The default value of this variable is set up by `grep-compute-defaults';
97 call that function before using this variable in your program.
98 The following place holders should be present in the string:
99 <C> - place to put -i if case insensitive grep.
100 <F> - file names and wildcards to search.
101 <R> - the regular expression searched for.
102 <N> - place to insert null-device."
103 :type
'(choice string
104 (const :tag
"Not Set" nil
))
108 (defcustom grep-use-null-device
'auto-detect
109 "If t, append the value of `null-device' to `grep' commands.
110 This is done to ensure that the output of grep includes the filename of
111 any match in the case where only a single file is searched, and is not
112 necessary if the grep program used supports the `-H' option.
114 The default value of this variable is set up by `grep-compute-defaults';
115 call that function before using this variable in your program."
116 :type
'(choice (const :tag
"Do Not Append Null Device" nil
)
117 (const :tag
"Append Null Device" t
)
118 (other :tag
"Not Set" auto-detect
))
122 (defcustom grep-find-command nil
123 "The default find command for \\[grep-find].
124 The default value of this variable is set up by `grep-compute-defaults';
125 call that function before using this variable in your program."
126 :type
'(choice string
127 (const :tag
"Not Set" nil
))
130 (defcustom grep-find-template nil
131 "The default command to run for \\[rgrep].
132 The default value of this variable is set up by `grep-compute-defaults';
133 call that function before using this variable in your program.
134 The following place holders should be present in the string:
135 <D> - base directory for find
136 <X> - find options to restrict or expand the directory list
137 <F> - find options to limit the files matched
138 <C> - place to put -i if case insensitive grep
139 <R> - the regular expression searched for."
140 :type
'(choice string
141 (const :tag
"Not Set" nil
))
145 (defcustom grep-files-aliases
'(
151 ("m" .
"[Mm]akefile*")
152 ("l" .
"[Cc]hange[Ll]og*")
156 "*Alist of aliases for the FILES argument to `lgrep' and `rgrep'."
160 (defcustom grep-find-ignored-directories
'("CVS" ".svn" "{arch}" ".hg" "_darcs"
162 "*List of names of sub-directories which `rgrep' shall not recurse into."
163 :type
'(repeat string
)
166 (defcustom grep-error-screen-columns nil
167 "*If non-nil, column numbers in grep hits are screen columns.
168 See `compilation-error-screen-columns'"
169 :type
'(choice (const :tag
"Default" nil
)
175 (defcustom grep-setup-hook nil
176 "List of hook functions run by `grep-process-setup' (see `run-hooks')."
180 (defvar grep-mode-map
181 (let ((map (cons 'keymap compilation-minor-mode-map
)))
182 (define-key map
" " 'scroll-up
)
183 (define-key map
"\^?" 'scroll-down
)
184 (define-key map
"\C-c\C-f" 'next-error-follow-minor-mode
)
186 (define-key map
"\r" 'compile-goto-error
) ;; ?
187 (define-key map
"n" 'next-error-no-select
)
188 (define-key map
"p" 'previous-error-no-select
)
189 (define-key map
"{" 'compilation-previous-file
)
190 (define-key map
"}" 'compilation-next-file
)
191 (define-key map
"\t" 'compilation-next-error
)
192 (define-key map
[backtab] 'compilation-previous-error)
194 ;; Set up the menu-bar
195 (define-key map [menu-bar grep]
196 (cons "Grep" (make-sparse-keymap "Grep")))
198 (define-key map [menu-bar grep compilation-kill-compilation]
199 '("Kill Grep" . kill-compilation))
200 (define-key map [menu-bar grep compilation-separator2]
202 (define-key map [menu-bar grep compilation-compile]
203 '("Compile..." . compile))
204 (define-key map [menu-bar grep compilation-grep]
205 '("Another grep..." . grep))
206 (define-key map [menu-bar grep compilation-grep-find]
207 '("Recursive grep..." . grep-find))
208 (define-key map [menu-bar grep compilation-recompile]
209 '("Repeat grep" . recompile))
210 (define-key map [menu-bar grep compilation-separator2]
212 (define-key map [menu-bar grep compilation-first-error]
213 '("First Match" . first-error))
214 (define-key map [menu-bar grep compilation-previous-error]
215 '("Previous Match" . previous-error))
216 (define-key map [menu-bar grep compilation-next-error]
217 '("Next Match" . next-error))
219 "Keymap for grep buffers.
220 `compilation-minor-mode-map' is a cdr of this.")
222 (defalias 'kill-grep 'kill-compilation)
224 ;;;; TODO --- refine this!!
226 ;;; (defcustom grep-use-compilation-buffer t
227 ;;; "When non-nil, grep specific commands update `compilation-last-buffer'.
228 ;;; This means that standard compile commands like \\[next-error] and \\[compile-goto-error]
229 ;;; can be used to navigate between grep matches (the default).
230 ;;; Otherwise, the grep specific commands like \\[grep-next-match] must
231 ;;; be used to navigate between grep matches."
235 ;; override compilation-last-buffer
236 (defvar grep-last-buffer nil
237 "The most recent grep buffer.
238 A grep buffer becomes most recent when you select Grep mode in it.
239 Notice that using \\[next-error] or \\[compile-goto-error] modifies
240 `complation-last-buffer' rather than `grep-last-buffer'.")
243 (defvar grep-regexp-alist
244 '(("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2"
246 ;; Rule to match column numbers is commented out since no known grep
248 ;; ("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2\\(?:\\([0-9]+\\)\\(?:-\\([0-9]+\\)\\)?\\2\\)?"
250 ("^\\(\\(.+?\\):\\([0-9]+\\):\\).*?\
251 \\(\033\\[01;31m\\(?:\033\\[K\\)?\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
253 ;; Calculate column positions (beg . end) of first grep match on a line
255 (setq compilation-error-screen-columns nil)
256 (- (match-beginning 4) (match-end 1)))
258 (lambda () (- (match-end 5) (match-end 1)
259 (- (match-end 4) (match-beginning 4)))))
261 ("^Binary file \\(.+\\) matches$" 1 nil nil 0 1))
262 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
264 (defvar grep-error "grep hit"
265 "Message to print when no matches are found.")
267 ;; Reverse the colors because grep hits are not errors (though we jump there
268 ;; with `next-error'), and unreadable files can't be gone to.
269 (defvar grep-hit-face compilation-info-face
270 "Face name to use for grep hits.")
272 (defvar grep-error-face 'compilation-error
273 "Face name to use for grep error messages.")
275 (defvar grep-match-face 'match
276 "Face name to use for grep matches.")
278 (defvar grep-context-face 'shadow
279 "Face name to use for grep context lines.")
281 (defvar grep-mode-font-lock-keywords
282 '(;; Command output lines.
283 ("^\\([A-Za-z_0-9/\.+-]+\\)[ \t]*:" 1 font-lock-function-name-face)
284 (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$"
286 ;; remove match from grep-regexp-alist before fontifying
287 ("^Grep[/a-zA-z]* started.*"
288 (0 '(face nil message nil help-echo nil mouse-face nil) t))
289 ("^Grep[/a-zA-z]* finished \\(?:(\\(matches found\\))\\|with \\(no matches found\\)\\).*"
290 (0 '(face nil message nil help-echo nil mouse-face nil) t)
291 (1 compilation-info-face nil t)
292 (2 compilation-warning-face nil t))
293 ("^Grep[/a-zA-z]* \\(exited abnormally\\|interrupt\\|killed\\|terminated\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
294 (0 '(face nil message nil help-echo nil mouse-face nil) t)
296 (2 grep-error-face nil t))
297 ("^.+?-[0-9]+-.*\n" (0 grep-context-face))
298 ;; Highlight grep matches and delete markers
299 ("\\(\033\\[01;31m\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
300 ;; Refontification does not work after the markers have been
301 ;; deleted. So we use the font-lock-face property here as Font
302 ;; Lock does not clear that.
303 (2 (list 'face nil 'font-lock-face grep-match-face))
306 ;; Delete markers with `replace-match' because it updates
307 ;; the match-data, whereas `delete-region' would render it obsolete.
308 (replace-match "" t t nil 3)
309 (replace-match "" t t nil 1))))
310 ("\\(\033\\[[0-9;]*[mK]\\)"
311 ;; Delete all remaining escape sequences
313 (replace-match "" t t nil 1))))
314 "Additional things to highlight in grep output.
315 This gets tacked on the end of the generated expressions.")
318 (defvar grep-program "grep"
319 "The default grep program for `grep-command' and `grep-find-command'.
320 This variable's value takes effect when `grep-compute-defaults' is called.")
323 (defvar find-program "find"
324 "The default find program for `grep-find-command'.
325 This variable's value takes effect when `grep-compute-defaults' is called.")
328 (defvar grep-find-use-xargs nil
329 "Non-nil means that `grep-find' uses the `xargs' utility by default.
330 If `exec', use `find -exec'.
331 If `gnu', use `find -print0' and `xargs -0'.
332 Any other non-nil value means to use `find -print' and `xargs'.
334 This variable's value takes effect when `grep-compute-defaults' is called.")
336 ;; History of grep commands.
338 (defvar grep-history nil)
340 (defvar grep-find-history nil)
342 ;; History of lgrep and rgrep regexp and files args.
343 (defvar grep-regexp-history nil)
344 (defvar grep-files-history '("ch" "el"))
346 (defvar grep-host-defaults-alist nil
347 "Default values depending on target host.
348 `grep-compute-defaults' returns default values for every local or
349 remote host `grep' runs. These values can differ from host to
350 host. Once computed, the default values are kept here in order
351 to avoid computing them again.")
354 (defun grep-process-setup ()
355 "Setup compilation variables and buffer for `grep'.
356 Set up `compilation-exit-message-function' and run `grep-setup-hook'."
357 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
358 (grep-compute-defaults))
359 (when (eq grep-highlight-matches t)
360 ;; Modify `process-environment' locally bound in `compilation-start'
361 (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always"))
362 ;; for GNU grep 2.5.1
363 (setenv "GREP_COLOR" "01;31")
364 ;; for GNU grep 2.5.1-cvs
365 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne"))
366 (set (make-local-variable 'compilation-exit-message-function)
367 (lambda (status code msg)
368 (if (eq status 'exit)
370 '("finished (matches found)\n" . "matched"))
372 '("finished with no matches found\n" . "no match"))
376 (run-hooks 'grep-setup-hook))
378 (defun grep-probe (command args &optional func result)
379 (equal (condition-case nil
380 (apply (or func 'process-file) command args)
385 (defun grep-compute-defaults ()
387 (intern (or (file-remote-p default-directory 'host) "localhost"))))
388 ;; There are different defaults on different hosts. They must be
389 ;; computed for every host once, then they are kept in the
390 ;; variables' property host-id for reuse.
392 (or (cadr (assq 'grep-command
393 (assq host-id grep-host-defaults-alist)))
394 (eval (car (get 'grep-command 'standard-value))))
397 (or (cadr (assq 'grep-template
398 (assq host-id grep-host-defaults-alist)))
399 (eval (car (get 'grep-template 'standard-value))))
402 (or (cadr (assq 'grep-use-null-device
403 (assq host-id grep-host-defaults-alist)))
404 (eval (car (get 'grep-use-null-device 'standard-value))))
407 (or (cadr (assq 'grep-find-command
408 (assq host-id grep-host-defaults-alist)))
409 (eval (car (get 'grep-find-command 'standard-value))))
412 (or (cadr (assq 'grep-find-template
413 (assq host-id grep-host-defaults-alist)))
414 (eval (car (get 'grep-find-template 'standard-value))))
417 (or (cadr (assq 'grep-find-use-xargs
418 (assq host-id grep-host-defaults-alist)))
419 (eval (car (get 'grep-find-use-xargs 'standard-value))))
421 grep-highlight-matches
422 (or (cadr (assq 'grep-highlight-matches
423 (assq host-id grep-host-defaults-alist)))
424 (eval (car (get 'grep-highlight-matches 'standard-value)))))
426 (unless (or (not grep-use-null-device) (eq grep-use-null-device t))
427 (setq grep-use-null-device
429 (let ((hello-file (expand-file-name "HELLO" data-directory)))
431 (and (if grep-command
432 ;; `grep-command' is already set, so
433 ;; use that for testing.
434 (grep-probe grep-command
435 `(nil t nil "^English" ,hello-file)
436 #'call-process-shell-command)
437 ;; otherwise use `grep-program'
438 (grep-probe grep-program
439 `(nil t nil "-nH" "^English" ,hello-file)))
441 (goto-char (point-min))
443 (concat (regexp-quote hello-file)
444 ":[0-9]+:English")))))))))
445 (unless (and grep-command grep-find-command
446 grep-template grep-find-template)
448 (concat (if grep-use-null-device "-n" "-nH")
449 (if (grep-probe grep-program
450 `(nil nil nil "-e" "foo" ,null-device)
455 (format "%s %s " grep-program grep-options)))
456 (unless grep-template
458 (format "%s <C> %s <R> <F>" grep-program grep-options)))
459 (unless grep-find-use-xargs
460 (setq grep-find-use-xargs
463 (grep-probe find-program `(nil nil nil ,null-device "-print0"))
464 (grep-probe "xargs" `(nil nil nil "-0" "-e" "echo")))
468 (unless grep-find-command
469 (setq grep-find-command
470 (cond ((eq grep-find-use-xargs 'gnu)
471 (format "%s . -type f -print0 | xargs -0 -e %s"
472 find-program grep-command))
473 ((eq grep-find-use-xargs 'exec)
474 (let ((cmd0 (format "%s . -type f -exec %s"
475 find-program grep-command)))
477 (format "%s {} %s %s"
479 (shell-quote-argument ";"))
480 (1+ (length cmd0)))))
482 (format "%s . -type f -print | xargs %s"
483 find-program grep-command)))))
484 (unless grep-find-template
485 (setq grep-find-template
486 (let ((gcmd (format "%s <C> %s <R>"
487 grep-program grep-options)))
488 (cond ((eq grep-find-use-xargs 'gnu)
489 (format "%s . <X> -type f <F> -print0 | xargs -0 -e %s"
491 ((eq grep-find-use-xargs 'exec)
492 (format "%s . <X> -type f <F> -exec %s {} %s %s"
493 find-program gcmd null-device
494 (shell-quote-argument ";")))
496 (format "%s . <X> -type f <F> -print | xargs %s"
497 find-program gcmd))))))))
498 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
499 (setq grep-highlight-matches
501 (and (grep-probe grep-program '(nil t nil "--help"))
503 (goto-char (point-min))
504 (search-forward "--color" nil t))
507 ;; Save defaults for this host.
508 (setq grep-host-defaults-alist
509 (delete (assq host-id grep-host-defaults-alist)
510 grep-host-defaults-alist))
512 'grep-host-defaults-alist
514 `((grep-command ,grep-command)
515 (grep-template ,grep-template)
516 (grep-use-null-device ,grep-use-null-device)
517 (grep-find-command ,grep-find-command)
518 (grep-find-template ,grep-find-template)
519 (grep-find-use-xargs ,grep-find-use-xargs)
520 (grep-highlight-matches ,grep-highlight-matches))))))
522 (defun grep-tag-default ()
523 (or (and transient-mark-mode mark-active
525 (buffer-substring-no-properties (point) (mark)))
526 (funcall (or find-tag-default-function
527 (get major-mode 'find-tag-default-function)
531 (defun grep-default-command ()
532 "Compute the default grep command for C-u M-x grep to offer."
533 (let ((tag-default (shell-quote-argument (grep-tag-default)))
534 ;; This a regexp to match single shell arguments.
535 ;; Could someone please add comments explaining it?
536 (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)")
537 (grep-default (or (car grep-history) grep-command)))
538 ;; In the default command, find the arg that specifies the pattern.
539 (when (or (string-match
540 (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*"
541 sh-arg-re "\\(\\s +\\(\\S +\\)\\)?")
543 ;; If the string is not yet complete.
544 (string-match "\\(\\)\\'" grep-default))
545 ;; Maybe we will replace the pattern with the default tag.
546 ;; But first, maybe replace the file name pattern.
548 (unless (or (not (stringp buffer-file-name))
549 (when (match-beginning 2)
553 (file-name-nondirectory
554 (match-string 3 grep-default)))
555 (file-name-nondirectory buffer-file-name)))))
556 (setq grep-default (concat (substring grep-default
557 0 (match-beginning 2))
559 (file-name-extension buffer-file-name))))
560 ;; In case wildcard-to-regexp gets an error
561 ;; from invalid data.
563 ;; Now replace the pattern with the default tag.
564 (replace-match tag-default t t grep-default 1))))
568 (define-compilation-mode grep-mode "Grep"
569 "Sets `grep-last-buffer' and `compilation-window-height'."
570 (setq grep-last-buffer (current-buffer))
571 (set (make-local-variable 'compilation-error-face)
573 (set (make-local-variable 'compilation-error-regexp-alist)
575 (set (make-local-variable 'compilation-process-setup-function)
577 (set (make-local-variable 'compilation-disable-input) t))
581 (defun grep (command-args)
582 "Run grep, with user-specified args, and collect output in a buffer.
583 While grep runs asynchronously, you can use \\[next-error] (M-x next-error),
584 or \\<grep-mode-map>\\[compile-goto-error] in the grep \
585 output buffer, to go to the lines
586 where grep found matches.
588 For doing a recursive `grep', see the `rgrep' command. For running
589 `grep' in a specific directory, see `lgrep'.
591 This command uses a special history list for its COMMAND-ARGS, so you can
592 easily repeat a grep command.
594 A prefix argument says to default the argument based upon the current
595 tag the cursor is over, substituting it into the last grep command
596 in the grep command history (or into `grep-command'
597 if that history list is empty)."
600 (grep-compute-defaults)
601 (let ((default (grep-default-command)))
602 (list (read-from-minibuffer "Run grep (like this): "
603 (if current-prefix-arg
604 default grep-command)
605 nil nil 'grep-history
606 (if current-prefix-arg nil default))))))
608 ;; Setting process-setup-function makes exit-message-function work
609 ;; even when async processes aren't supported.
610 (compilation-start (if (and grep-use-null-device null-device)
611 (concat command-args " " null-device)
617 (defun grep-find (command-args)
618 "Run grep via find, with user-specified args COMMAND-ARGS.
619 Collect output in a buffer.
620 While find runs asynchronously, you can use the \\[next-error] command
621 to find the text that grep hits refer to.
623 This command uses a special history list for its arguments, so you can
624 easily repeat a find command."
627 (grep-compute-defaults)
628 (if grep-find-command
629 (list (read-from-minibuffer "Run find (like this): "
630 grep-find-command nil nil
632 ;; No default was set
634 "compile.el: No `grep-find-command' command available. Press RET.")
637 (let ((null-device nil)) ; see grep
638 (grep command-args))))
641 (defalias 'find-grep 'grep-find)
644 ;; User-friendly interactive API.
646 (defconst grep-expand-keywords
647 '(("<C>" . (and cf (isearch-no-upper-case-p regexp t) "-i"))
650 ("<N>" . null-device)
652 ("<R>" . (shell-quote-argument (or regexp ""))))
653 "List of substitutions performed by `grep-expand-template'.
654 If car of an element matches, the cdr is evalled in to get the
655 substitution string. Note dynamic scoping of variables.")
657 (defun grep-expand-template (template &optional regexp files dir excl)
658 "Patch grep COMMAND string replacing <C>, <D>, <F>, <R>, and <X>."
659 (let ((command template)
660 (cf case-fold-search)
661 (case-fold-search nil))
662 (dolist (kw grep-expand-keywords command)
663 (if (string-match (car kw) command)
666 (or (if (symbolp (cdr kw))
667 (symbol-value (cdr kw))
668 (save-match-data (eval (cdr kw))))
672 (defun grep-read-regexp ()
673 "Read regexp arg for interactive grep."
674 (let ((default (grep-tag-default)))
677 (if (and default (> (length default) 0))
678 (format " (default \"%s\"): " default) ": "))
679 nil 'grep-regexp-history default)))
681 (defun grep-read-files (regexp)
682 "Read files arg for interactive grep."
683 (let* ((bn (or (buffer-file-name) (buffer-name)))
686 (file-name-nondirectory bn)))
689 (let ((aliases grep-files-aliases)
692 (setq alias (car aliases)
693 aliases (cdr aliases))
694 (if (string-match (wildcard-to-regexp (cdr alias)) fn)
699 (let ((ext (file-name-extension fn)))
700 (and ext (concat "*." ext))))
701 (car grep-files-history)
702 (car (car grep-files-aliases))))
704 (concat "Search for \"" regexp
706 (if default (concat " (default " default ")"))
708 nil 'grep-files-history default)))
710 (or (cdr (assoc files grep-files-aliases))
714 (defun lgrep (regexp &optional files dir)
715 "Run grep, searching for REGEXP in FILES in directory DIR.
716 The search is limited to file names matching shell pattern FILES.
717 FILES may use abbreviations defined in `grep-files-aliases', e.g.
718 entering `ch' is equivalent to `*.[ch]'.
720 With \\[universal-argument] prefix, you can edit the constructed shell command line
721 before it is executed.
722 With two \\[universal-argument] prefixes, directly edit and run `grep-command'.
724 Collect output in a buffer. While grep runs asynchronously, you
725 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
726 in the grep output buffer, to go to the lines where grep found matches.
728 This command shares argument histories with \\[rgrep] and \\[grep]."
731 (grep-compute-defaults)
733 ((and grep-command (equal current-prefix-arg '(16)))
734 (list (read-from-minibuffer "Run: " grep-command
735 nil nil 'grep-history)
739 (read-string "grep.el: No `grep-template' available. Press RET.")))
740 (t (let* ((regexp (grep-read-regexp))
741 (files (grep-read-files regexp))
742 (dir (read-directory-name "In directory: "
743 nil default-directory t)))
744 (list regexp files dir))))))
745 (when (and (stringp regexp) (> (length regexp) 0))
746 (let ((command regexp))
748 (if (string= command grep-command)
750 (setq dir (file-name-as-directory (expand-file-name dir)))
751 (setq command (grep-expand-template
756 (if (equal current-prefix-arg '(4))
758 (read-from-minibuffer "Confirm: "
759 command nil nil 'grep-history))
760 (add-to-history 'grep-history command))))
762 (let ((default-directory dir))
763 ;; Setting process-setup-function makes exit-message-function work
764 ;; even when async processes aren't supported.
765 (compilation-start (if (and grep-use-null-device null-device)
766 (concat command " " null-device)
767 command) 'grep-mode))
768 (if (eq next-error-last-buffer (current-buffer))
769 (setq default-directory dir))))))
774 (defun rgrep (regexp &optional files dir)
775 "Recursively grep for REGEXP in FILES in directory tree rooted at DIR.
776 The search is limited to file names matching shell pattern FILES.
777 FILES may use abbreviations defined in `grep-files-aliases', e.g.
778 entering `ch' is equivalent to `*.[ch]'.
780 With \\[universal-argument] prefix, you can edit the constructed shell command line
781 before it is executed.
782 With two \\[universal-argument] prefixes, directly edit and run `grep-find-command'.
784 Collect output in a buffer. While find runs asynchronously, you
785 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
786 in the grep output buffer, to go to the lines where grep found matches.
788 This command shares argument histories with \\[lgrep] and \\[grep-find]."
791 (grep-compute-defaults)
793 ((and grep-find-command (equal current-prefix-arg '(16)))
794 (list (read-from-minibuffer "Run: " grep-find-command
795 nil nil 'grep-find-history)
797 ((not grep-find-template)
799 (read-string "grep.el: No `grep-find-template' available. Press RET.")))
800 (t (let* ((regexp (grep-read-regexp))
801 (files (grep-read-files regexp))
802 (dir (read-directory-name "Base directory: "
803 nil default-directory t)))
804 (list regexp files dir))))))
805 (when (and (stringp regexp) (> (length regexp) 0))
807 (if (not (string= regexp grep-find-command))
808 (compilation-start regexp 'grep-mode))
809 (setq dir (file-name-as-directory (expand-file-name dir)))
810 (let ((command (grep-expand-template
813 (concat (shell-quote-argument "(")
815 (mapconcat #'shell-quote-argument
819 (shell-quote-argument ")"))
821 (and grep-find-ignored-directories
822 (concat (shell-quote-argument "(")
823 ;; we should use shell-quote-argument here
825 (mapconcat #'(lambda (dir)
826 (shell-quote-argument
828 grep-find-ignored-directories
831 (shell-quote-argument ")")
834 (if current-prefix-arg
836 (read-from-minibuffer "Confirm: "
837 command nil nil 'grep-find-history))
838 (add-to-history 'grep-find-history command))
839 (let ((default-directory dir))
840 (compilation-start command 'grep-mode))
841 ;; Set default-directory if we started rgrep in the *grep* buffer.
842 (if (eq next-error-last-buffer (current-buffer))
843 (setq default-directory dir)))))))
848 ;; arch-tag: 5a5b9169-a79d-4f38-9c38-f69615f39c4d
849 ;;; grep.el ends here