]> code.delx.au - gnu-emacs/blob - lisp/progmodes/grep.el
(grep-read-files): Use buffer-name if no buffer-file-name.
[gnu-emacs] / lisp / progmodes / grep.el
1 ;;; grep.el --- run Grep as inferior of Emacs, parse match messages
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Roland McGrath <roland@gnu.org>
7 ;; Maintainer: FSF
8 ;; Keywords: tools, processes
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; This package provides the grep facilities documented in the Emacs
30 ;; user's manual.
31
32 ;;; Code:
33
34 (require 'compile)
35
36
37 (defgroup grep nil
38 "Run grep as inferior of Emacs, parse error messages."
39 :group 'tools
40 :group 'processes)
41
42
43 ;;;###autoload
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)
47 integer)
48 :version "22.1"
49 :group 'grep)
50
51 (defcustom grep-highlight-matches 'auto-detect
52 "If t, use special markers to highlight grep matches.
53
54 Some grep programs are able to surround matches with special
55 markers in grep output. Such markers can be used to highlight
56 matches in grep mode.
57
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
61 the grep.
62
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))
68 :version "22.1"
69 :group 'grep)
70
71 (defcustom grep-scroll-output nil
72 "*Non-nil to scroll the *grep* buffer window as output appears.
73
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
76 than the begining."
77 :type 'boolean
78 :version "22.1"
79 :group 'grep)
80
81 ;;;###autoload
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'.
87
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."
90 :type '(choice string
91 (const :tag "Not Set" nil))
92 :group 'grep)
93
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))
105 :version "22.1"
106 :group 'grep)
107
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.
113
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))
119 :group 'grep)
120
121 ;;;###autoload
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))
128 :group 'grep)
129
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))
142 :version "22.1"
143 :group 'grep)
144
145 (defcustom grep-files-aliases '(
146 ("el" . "*.el")
147 ("ch" . "*.[ch]")
148 ("c" . "*.c")
149 ("h" . "*.h")
150 ("asm" . "*.[sS]")
151 ("m" . "[Mm]akefile*")
152 ("l" . "[Cc]hange[Ll]og*")
153 )
154 "*Alist of aliases for the FILES argument to `lgrep' and `rgrep'."
155 :type 'alist
156 :group 'grep)
157
158 (defcustom grep-find-ignored-directories '("CVS" ".hg" "{arch}")
159 "*List of names of sub-directories which `rgrep' shall not recurse into."
160 :type '(repeat string)
161 :group 'grep)
162
163 (defcustom grep-error-screen-columns nil
164 "*If non-nil, column numbers in grep hits are screen columns.
165 See `compilation-error-screen-columns'"
166 :type '(choice (const :tag "Default" nil)
167 integer)
168 :version "22.1"
169 :group 'grep)
170
171 ;;;###autoload
172 (defcustom grep-setup-hook nil
173 "List of hook functions run by `grep-process-setup' (see `run-hooks')."
174 :type 'hook
175 :group 'grep)
176
177 (defvar grep-mode-map
178 (let ((map (cons 'keymap compilation-minor-mode-map)))
179 (define-key map " " 'scroll-up)
180 (define-key map "\^?" 'scroll-down)
181 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
182
183 (define-key map "\r" 'compile-goto-error) ;; ?
184 (define-key map "n" 'next-error-no-select)
185 (define-key map "p" 'previous-error-no-select)
186 (define-key map "{" 'compilation-previous-file)
187 (define-key map "}" 'compilation-next-file)
188 (define-key map "\t" 'compilation-next-error)
189 (define-key map [backtab] 'compilation-previous-error)
190
191 ;; Set up the menu-bar
192 (define-key map [menu-bar grep]
193 (cons "Grep" (make-sparse-keymap "Grep")))
194
195 (define-key map [menu-bar grep compilation-kill-compilation]
196 '("Kill Grep" . kill-compilation))
197 (define-key map [menu-bar grep compilation-separator2]
198 '("----" . nil))
199 (define-key map [menu-bar grep compilation-compile]
200 '("Compile..." . compile))
201 (define-key map [menu-bar grep compilation-grep]
202 '("Another grep..." . grep))
203 (define-key map [menu-bar grep compilation-grep-find]
204 '("Recursive grep..." . grep-find))
205 (define-key map [menu-bar grep compilation-recompile]
206 '("Repeat grep" . recompile))
207 (define-key map [menu-bar grep compilation-separator2]
208 '("----" . nil))
209 (define-key map [menu-bar grep compilation-first-error]
210 '("First Match" . first-error))
211 (define-key map [menu-bar grep compilation-previous-error]
212 '("Previous Match" . previous-error))
213 (define-key map [menu-bar grep compilation-next-error]
214 '("Next Match" . next-error))
215 map)
216 "Keymap for grep buffers.
217 `compilation-minor-mode-map' is a cdr of this.")
218
219 (defalias 'kill-grep 'kill-compilation)
220
221 ;;;; TODO --- refine this!!
222
223 ;;; (defcustom grep-use-compilation-buffer t
224 ;;; "When non-nil, grep specific commands update `compilation-last-buffer'.
225 ;;; This means that standard compile commands like \\[next-error] and \\[compile-goto-error]
226 ;;; can be used to navigate between grep matches (the default).
227 ;;; Otherwise, the grep specific commands like \\[grep-next-match] must
228 ;;; be used to navigate between grep matches."
229 ;;; :type 'boolean
230 ;;; :group 'grep)
231
232 ;; override compilation-last-buffer
233 (defvar grep-last-buffer nil
234 "The most recent grep buffer.
235 A grep buffer becomes most recent when its process is started
236 or when it is used with \\[grep-next-match].
237 Notice that using \\[next-error] or \\[compile-goto-error] modifies
238 `complation-last-buffer' rather than `grep-last-buffer'.")
239
240 ;;;###autoload
241 (defvar grep-regexp-alist
242 '(("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2"
243 1 3)
244 ;; Rule to match column numbers is commented out since no known grep
245 ;; produces them
246 ;; ("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2\\(?:\\([0-9]+\\)\\(?:-\\([0-9]+\\)\\)?\\2\\)?"
247 ;; 1 3 (4 . 5))
248 ("^\\(\\(.+?\\):\\([0-9]+\\):\\).*?\
249 \\(\033\\[01;31m\\(?:\033\\[K\\)?\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
250 2 3
251 ;; Calculate column positions (beg . end) of first grep match on a line
252 ((lambda ()
253 (setq compilation-error-screen-columns nil)
254 (- (match-beginning 4) (match-end 1)))
255 .
256 (lambda () (- (match-end 5) (match-end 1)
257 (- (match-end 4) (match-beginning 4)))))
258 nil 1)
259 ("^Binary file \\(.+\\) matches$" 1 nil nil 0 1))
260 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
261
262 (defvar grep-error "grep hit"
263 "Message to print when no matches are found.")
264
265 ;; Reverse the colors because grep hits are not errors (though we jump there
266 ;; with `next-error'), and unreadable files can't be gone to.
267 (defvar grep-hit-face compilation-info-face
268 "Face name to use for grep hits.")
269
270 (defvar grep-error-face 'compilation-error
271 "Face name to use for grep error messages.")
272
273 (defvar grep-match-face 'match
274 "Face name to use for grep matches.")
275
276 (defvar grep-context-face 'shadow
277 "Face name to use for grep context lines.")
278
279 (defvar grep-mode-font-lock-keywords
280 '(;; Command output lines.
281 ("^\\([A-Za-z_0-9/\.+-]+\\)[ \t]*:" 1 font-lock-function-name-face)
282 (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$"
283 1 grep-error-face)
284 ;; remove match from grep-regexp-alist before fontifying
285 ("^Grep started.*"
286 (0 '(face nil message nil help-echo nil mouse-face nil) t))
287 ("^Grep finished \\(?:(\\(matches found\\))\\|with \\(no matches found\\)\\).*"
288 (0 '(face nil message nil help-echo nil mouse-face nil) t)
289 (1 compilation-info-face nil t)
290 (2 compilation-warning-face nil t))
291 ("^Grep \\(exited abnormally\\|interrupt\\|killed\\|terminated\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
292 (0 '(face nil message nil help-echo nil mouse-face nil) t)
293 (1 grep-error-face)
294 (2 grep-error-face nil t))
295 ("^.+?-[0-9]+-.*\n" (0 grep-context-face))
296 ;; Highlight grep matches and delete markers
297 ("\\(\033\\[01;31m\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
298 ;; Refontification does not work after the markers have been
299 ;; deleted. So we use the font-lock-face property here as Font
300 ;; Lock does not clear that.
301 (2 (list 'face nil 'font-lock-face grep-match-face))
302 ((lambda (bound))
303 (progn
304 ;; Delete markers with `replace-match' because it updates
305 ;; the match-data, whereas `delete-region' would render it obsolete.
306 (replace-match "" t t nil 3)
307 (replace-match "" t t nil 1))))
308 ("\\(\033\\[[0-9;]*[mK]\\)"
309 ;; Delete all remaining escape sequences
310 ((lambda (bound))
311 (replace-match "" t t nil 1))))
312 "Additional things to highlight in grep output.
313 This gets tacked on the end of the generated expressions.")
314
315 ;;;###autoload
316 (defvar grep-program
317 ;; Currently zgrep has trouble. It runs egrep instead of grep,
318 ;; and it doesn't pass along long options right.
319 "grep"
320 ;; (if (equal (condition-case nil ; in case "zgrep" isn't in exec-path
321 ;; (call-process "zgrep" nil nil nil
322 ;; "foo" null-device)
323 ;; (error nil))
324 ;; 1)
325 ;; "zgrep"
326 ;; "grep")
327 "The default grep program for `grep-command' and `grep-find-command'.
328 This variable's value takes effect when `grep-compute-defaults' is called.")
329
330 ;;;###autoload
331 (defvar find-program "find"
332 "The default find program for `grep-find-command'.
333 This variable's value takes effect when `grep-compute-defaults' is called.")
334
335 ;;;###autoload
336 (defvar grep-find-use-xargs nil
337 "Whether \\[grep-find] uses the `xargs' utility by default.
338
339 If nil, it uses `find -exec'; if `gnu', it uses `find -print0' and `xargs -0';
340 if not nil and not `gnu', it uses `find -print' and `xargs'.
341
342 This variable's value takes effect when `grep-compute-defaults' is called.")
343
344 ;; History of grep commands.
345 ;;;###autoload
346 (defvar grep-history nil)
347 ;;;###autoload
348 (defvar grep-find-history nil)
349
350 ;; History of lgrep and rgrep regexp and files args.
351 (defvar grep-regexp-history nil)
352 (defvar grep-files-history '("ch" "el"))
353
354
355 ;;;###autoload
356 (defun grep-process-setup ()
357 "Setup compilation variables and buffer for `grep'.
358 Set up `compilation-exit-message-function' and run `grep-setup-hook'."
359 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
360 (grep-compute-defaults))
361 (when (eq grep-highlight-matches t)
362 ;; Modify `process-environment' locally bound in `compilation-start'
363 (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always"))
364 ;; for GNU grep 2.5.1
365 (setenv "GREP_COLOR" "01;31")
366 ;; for GNU grep 2.5.1-cvs
367 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne"))
368 (set (make-local-variable 'compilation-exit-message-function)
369 (lambda (status code msg)
370 (if (eq status 'exit)
371 (cond ((zerop code)
372 '("finished (matches found)\n" . "matched"))
373 ((= code 1)
374 '("finished with no matches found\n" . "no match"))
375 (t
376 (cons msg code)))
377 (cons msg code))))
378 (run-hooks 'grep-setup-hook))
379
380 (defun grep-probe (command args &optional func result)
381 (equal (condition-case nil
382 (apply (or func 'call-process) command args)
383 (error nil))
384 (or result 0)))
385
386 ;;;###autoload
387 (defun grep-compute-defaults ()
388 (unless (or (not grep-use-null-device) (eq grep-use-null-device t))
389 (setq grep-use-null-device
390 (with-temp-buffer
391 (let ((hello-file (expand-file-name "HELLO" data-directory)))
392 (not
393 (and (if grep-command
394 ;; `grep-command' is already set, so
395 ;; use that for testing.
396 (grep-probe grep-command
397 `(nil t nil "^English" ,hello-file)
398 #'call-process-shell-command)
399 ;; otherwise use `grep-program'
400 (grep-probe grep-program
401 `(nil t nil "-nH" "^English" ,hello-file)))
402 (progn
403 (goto-char (point-min))
404 (looking-at
405 (concat (regexp-quote hello-file)
406 ":[0-9]+:English")))))))))
407 (unless (and grep-command grep-find-command
408 grep-template grep-find-template)
409 (let ((grep-options
410 (concat (if grep-use-null-device "-n" "-nH")
411 (if (grep-probe grep-program
412 `(nil nil nil "-e" "foo" ,null-device)
413 nil 1)
414 " -e"))))
415 (unless grep-command
416 (setq grep-command
417 (format "%s %s " grep-program grep-options)))
418 (unless grep-template
419 (setq grep-template
420 (format "%s <C> %s <R> <F>" grep-program grep-options)))
421 (unless grep-find-use-xargs
422 (setq grep-find-use-xargs
423 (if (and
424 (grep-probe find-program `(nil nil nil ,null-device "-print0"))
425 (grep-probe "xargs" `(nil nil nil "-0" "-e" "echo")))
426 'gnu)))
427 (unless grep-find-command
428 (setq grep-find-command
429 (cond ((eq grep-find-use-xargs 'gnu)
430 (format "%s . -type f -print0 | xargs -0 -e %s"
431 find-program grep-command))
432 (grep-find-use-xargs
433 (format "%s . -type f -print | xargs %s"
434 find-program grep-command))
435 (t (cons (format "%s . -type f -exec %s {} %s \\;"
436 find-program grep-command null-device)
437 (+ 22 (length grep-command)))))))
438 (unless grep-find-template
439 (setq grep-find-template
440 (let ((gcmd (format "%s <C> %s <R>"
441 grep-program grep-options)))
442 (cond ((eq grep-find-use-xargs 'gnu)
443 (format "%s . <X> -type f <F> -print0 | xargs -0 -e %s"
444 find-program gcmd))
445 (grep-find-use-xargs
446 (format "%s . <X> -type f <F> -print | xargs %s"
447 find-program gcmd))
448 (t (format "%s . <X> -type f <F> -exec %s {} %s \\;"
449 find-program gcmd null-device))))))))
450 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
451 (setq grep-highlight-matches
452 (with-temp-buffer
453 (and (grep-probe grep-program '(nil t nil "--help"))
454 (progn
455 (goto-char (point-min))
456 (search-forward "--color" nil t))
457 t)))))
458
459 (defun grep-default-command ()
460 (let ((tag-default
461 (shell-quote-argument
462 (or (funcall (or find-tag-default-function
463 (get major-mode 'find-tag-default-function)
464 'find-tag-default))
465 "")))
466 (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)")
467 (grep-default (or (car grep-history) grep-command)))
468 ;; Replace the thing matching for with that around cursor.
469 (when (or (string-match
470 (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*"
471 sh-arg-re "\\(\\s +\\(\\S +\\)\\)?")
472 grep-default)
473 ;; If the string is not yet complete.
474 (string-match "\\(\\)\\'" grep-default))
475 (unless (or (not (stringp buffer-file-name))
476 (when (match-beginning 2)
477 (save-match-data
478 (string-match
479 (wildcard-to-regexp
480 (file-name-nondirectory
481 (match-string 3 grep-default)))
482 (file-name-nondirectory buffer-file-name)))))
483 (setq grep-default (concat (substring grep-default
484 0 (match-beginning 2))
485 " *."
486 (file-name-extension buffer-file-name))))
487 (replace-match tag-default t t grep-default 1))))
488
489
490 ;;;###autoload
491 (define-compilation-mode grep-mode "Grep"
492 "Sets `grep-last-buffer' and `compilation-window-height'."
493 (setq grep-last-buffer (current-buffer))
494 (set (make-local-variable 'compilation-error-face)
495 grep-hit-face)
496 (set (make-local-variable 'compilation-error-regexp-alist)
497 grep-regexp-alist)
498 (set (make-local-variable 'compilation-process-setup-function)
499 'grep-process-setup)
500 (set (make-local-variable 'compilation-disable-input) t))
501
502
503 ;;;###autoload
504 (defun grep (command-args)
505 "Run grep, with user-specified args, and collect output in a buffer.
506 While grep runs asynchronously, you can use \\[next-error] (M-x next-error),
507 or \\<grep-mode-map>\\[compile-goto-error] in the grep \
508 output buffer, to go to the lines
509 where grep found matches.
510
511 This command uses a special history list for its COMMAND-ARGS, so you can
512 easily repeat a grep command.
513
514 A prefix argument says to default the argument based upon the current
515 tag the cursor is over, substituting it into the last grep command
516 in the grep command history (or into `grep-command'
517 if that history list is empty)."
518 (interactive
519 (progn
520 (grep-compute-defaults)
521 (let ((default (grep-default-command)))
522 (list (read-from-minibuffer "Run grep (like this): "
523 (if current-prefix-arg
524 default grep-command)
525 nil nil 'grep-history
526 (if current-prefix-arg nil default))))))
527
528 ;; Setting process-setup-function makes exit-message-function work
529 ;; even when async processes aren't supported.
530 (compilation-start (if (and grep-use-null-device null-device)
531 (concat command-args " " null-device)
532 command-args)
533 'grep-mode))
534
535
536 ;;;###autoload
537 (defun grep-find (command-args)
538 "Run grep via find, with user-specified args COMMAND-ARGS.
539 Collect output in a buffer.
540 While find runs asynchronously, you can use the \\[next-error] command
541 to find the text that grep hits refer to.
542
543 This command uses a special history list for its arguments, so you can
544 easily repeat a find command."
545 (interactive
546 (progn
547 (grep-compute-defaults)
548 (if grep-find-command
549 (list (read-from-minibuffer "Run find (like this): "
550 grep-find-command nil nil
551 'grep-find-history))
552 ;; No default was set
553 (read-string
554 "compile.el: No `grep-find-command' command available. Press RET.")
555 (list nil))))
556 (when (and grep-find-command command-args)
557 (let ((null-device nil)) ; see grep
558 (grep command-args))))
559
560 ;;;###autoload
561 (defalias 'find-grep 'grep-find)
562
563
564 ;; User-friendly interactive API.
565
566 (defconst grep-expand-keywords
567 '(("<C>" . (and cf (isearch-no-upper-case-p regexp t) "-i"))
568 ("<D>" . dir)
569 ("<F>" . files)
570 ("<N>" . null-device)
571 ("<X>" . excl)
572 ("<R>" . (shell-quote-argument (or regexp ""))))
573 "List of substitutions performed by `grep-expand-template'.
574 If car of an element matches, the cdr is evalled in to get the
575 substitution string. Note dynamic scoping of variables.")
576
577 (defun grep-expand-template (template &optional regexp files dir excl)
578 "Patch grep COMMAND string replacing <C>, <D>, <F>, <R>, and <X>."
579 (let ((command template)
580 (cf case-fold-search)
581 (case-fold-search nil))
582 (dolist (kw grep-expand-keywords command)
583 (if (string-match (car kw) command)
584 (setq command (replace-match (or (eval (cdr kw)) "")
585 t t command))))))
586
587 (defun grep-read-regexp ()
588 "Read regexp arg for interactive grep."
589 (let ((default
590 (or (funcall (or find-tag-default-function
591 (get major-mode 'find-tag-default-function)
592 'find-tag-default))
593 "")))
594 (read-string
595 (concat "Search for"
596 (if (and default (> (length default) 0))
597 (format " (default %s): " default) ": "))
598 nil 'grep-regexp-history default)))
599
600 (defun grep-read-files (regexp)
601 "Read files arg for interactive grep."
602 (let* ((bn (or (buffer-file-name) (buffer-name)))
603 (fn (and bn
604 (stringp bn)
605 (file-name-nondirectory bn)))
606 (default
607 (or (and fn
608 (let ((aliases grep-files-aliases)
609 alias)
610 (while aliases
611 (setq alias (car aliases)
612 aliases (cdr aliases))
613 (if (string-match (wildcard-to-regexp (cdr alias)) fn)
614 (setq aliases nil)
615 (setq alias nil)))
616 (cdr alias)))
617 (and fn
618 (let ((ext (file-name-extension fn)))
619 (and ext (concat "*." ext))))))
620 (files (read-string
621 (concat "Search for \"" regexp
622 "\" in files"
623 (if default (concat " (default " default ")"))
624 ": ")
625 nil 'grep-files-history default)))
626 (and files
627 (or (cdr (assoc files grep-files-aliases))
628 files))))
629
630 ;;;###autoload
631 (defun lgrep (regexp &optional files)
632 "Run grep, searching for REGEXP in FILES in current directory.
633 The search is limited to file names matching shell pattern FILES.
634 FILES may use abbreviations defined in `grep-files-aliases', e.g.
635 entering `ch' is equivalent to `*.[ch]'.
636
637 With \\[universal-argument] prefix, allow user to edit the constructed
638 shell command line before it is executed.
639 With two \\[universal-argument] prefixes, edit and run grep shell command.
640
641 Collect output in a buffer. While grep runs asynchronously, you
642 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
643 in the grep output buffer, to go to the lines where grep found matches.
644
645 This command shares argument histories with \\[rgrep] and \\[grep]."
646 (interactive
647 (progn
648 (grep-compute-defaults)
649 (cond
650 ((and grep-command (equal current-prefix-arg '(16)))
651 (list (read-from-minibuffer "Run: " grep-command
652 nil nil 'grep-history)
653 nil))
654 ((not grep-template)
655 (list nil
656 (read-string "grep.el: No `grep-template' available. Press RET.")))
657 (t (let* ((regexp (grep-read-regexp))
658 (files (grep-read-files regexp)))
659 (list regexp files))))))
660 (when (and (stringp regexp) (> (length regexp) 0))
661 (let ((command regexp))
662 (if (null files)
663 (if (string= command grep-command)
664 (setq command nil))
665 (setq command (grep-expand-template
666 grep-template
667 regexp
668 files))
669 (when command
670 (if (equal current-prefix-arg '(4))
671 (setq command
672 (read-from-minibuffer "Confirm: "
673 command nil nil 'grep-history))
674 (push command grep-history))))
675 (when command
676 ;; Setting process-setup-function makes exit-message-function work
677 ;; even when async processes aren't supported.
678 (compilation-start (if (and grep-use-null-device null-device)
679 (concat command " " null-device)
680 command) 'grep-mode)))))
681
682
683 ;;;###autoload
684 (defun rgrep (regexp &optional files dir)
685 "Recusively grep for REGEXP in FILES in directory tree rooted at DIR.
686 The search is limited to file names matching shell pattern FILES.
687 FILES may use abbreviations defined in `grep-files-aliases', e.g.
688 entering `ch' is equivalent to `*.[ch]'.
689
690 With \\[universal-argument] prefix, allow user to edit the constructed
691 shell command line before it is executed.
692 With two \\[universal-argument] prefixes, edit and run grep-find shell command.
693
694 Collect output in a buffer. While find runs asynchronously, you
695 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
696 in the grep output buffer, to go to the lines where grep found matches.
697
698 This command shares argument histories with \\[lgrep] and \\[grep-find]."
699 (interactive
700 (progn
701 (grep-compute-defaults)
702 (cond
703 ((and grep-find-command (equal current-prefix-arg '(16)))
704 (list (read-from-minibuffer "Run: " grep-find-command
705 nil nil 'grep-find-history)
706 nil))
707 ((not grep-find-template)
708 (list nil nil
709 (read-string "grep.el: No `grep-find-template' available. Press RET.")))
710 (t (let* ((regexp (grep-read-regexp))
711 (files (grep-read-files regexp))
712 (dir (read-directory-name "Base directory: "
713 nil default-directory t)))
714 (list regexp files dir))))))
715 (when (and (stringp regexp) (> (length regexp) 0))
716 (if (null files)
717 (if (not (string= regexp grep-find-command))
718 (compilation-start regexp 'grep-mode))
719 (let* ((default-directory (file-name-as-directory (expand-file-name dir)))
720 (command (grep-expand-template
721 grep-find-template
722 regexp
723 (concat "\\( -name "
724 (mapconcat #'shell-quote-argument
725 (split-string files)
726 " -o -name ")
727 " \\)")
728 default-directory
729 (and grep-find-ignored-directories
730 (concat "\\( -path '*/"
731 (mapconcat #'identity
732 grep-find-ignored-directories
733 "' -o -path '*/")
734 "' \\) -prune -o ")))))
735 (when command
736 (if current-prefix-arg
737 (setq command
738 (read-from-minibuffer "Confirm: "
739 command nil nil 'grep-find-history))
740 (push command grep-find-history))
741 (compilation-start command 'grep-mode))))))
742
743
744 (provide 'grep)
745
746 ;; arch-tag: 5a5b9169-a79d-4f38-9c38-f69615f39c4d
747 ;;; grep.el ends here