Commit | Line | Data |
---|---|---|
f7993853 | 1 | ;;; grep.el --- run `grep' and display the results -*- lexical-binding:t -*- |
318e2976 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1985-1987, 1993-1999, 2001-2014 Free Software |
ab422c4d | 4 | ;; Foundation, Inc. |
318e2976 KS |
5 | |
6 | ;; Author: Roland McGrath <roland@gnu.org> | |
34dc21db | 7 | ;; Maintainer: emacs-devel@gnu.org |
318e2976 KS |
8 | ;; Keywords: tools, processes |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
b1fc2b50 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
318e2976 | 13 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
318e2976 KS |
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 | |
b1fc2b50 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
318e2976 KS |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; This package provides the grep facilities documented in the Emacs | |
28 | ;; user's manual. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (require 'compile) | |
33 | ||
d70b5ac5 | 34 | |
318e2976 | 35 | (defgroup grep nil |
d9c54a06 | 36 | "Run `grep' and display the results." |
318e2976 KS |
37 | :group 'tools |
38 | :group 'processes) | |
39 | ||
d4bbd646 CY |
40 | (defvar grep-host-defaults-alist nil |
41 | "Default values depending on target host. | |
42 | `grep-compute-defaults' returns default values for every local or | |
43 | remote host `grep' runs. These values can differ from host to | |
44 | host. Once computed, the default values are kept here in order | |
45 | to avoid computing them again.") | |
46 | ||
47 | (defun grep-apply-setting (symbol value) | |
48 | "Set SYMBOL to VALUE, and update `grep-host-defaults-alist'. | |
49 | SYMBOL should be one of `grep-command', `grep-template', | |
50 | `grep-use-null-device', `grep-find-command', | |
51 | `grep-find-template', `grep-find-use-xargs', or | |
52 | `grep-highlight-matches'." | |
53 | (when grep-host-defaults-alist | |
54 | (let* ((host-id | |
55 | (intern (or (file-remote-p default-directory) "localhost"))) | |
56 | (host-defaults (assq host-id grep-host-defaults-alist)) | |
57 | (defaults (assq nil grep-host-defaults-alist))) | |
58 | (setcar (cdr (assq symbol host-defaults)) value) | |
59 | (setcar (cdr (assq symbol defaults)) value))) | |
60 | (set-default symbol value)) | |
318e2976 KS |
61 | |
62 | ;;;###autoload | |
63 | (defcustom grep-window-height nil | |
fb7ada5f | 64 | "Number of lines in a grep window. If nil, use `compilation-window-height'." |
318e2976 KS |
65 | :type '(choice (const :tag "Default" nil) |
66 | integer) | |
bf247b6e | 67 | :version "22.1" |
318e2976 KS |
68 | :group 'grep) |
69 | ||
00889cf9 | 70 | (defcustom grep-highlight-matches 'auto-detect |
a601d313 | 71 | "Use special markers to highlight grep matches. |
277df088 JL |
72 | |
73 | Some grep programs are able to surround matches with special | |
74 | markers in grep output. Such markers can be used to highlight | |
a915d7a1 GM |
75 | matches in grep mode. This requires `font-lock-mode' to be active |
76 | in grep buffers, so if you have globally disabled font-lock-mode, | |
77 | you will not get highlighting. | |
277df088 | 78 | |
a601d313 | 79 | This option sets the environment variable GREP_COLORS to specify |
277df088 JL |
80 | markers for highlighting and GREP_OPTIONS to add the --color |
81 | option in front of any explicit grep options before starting | |
a58c94a2 JL |
82 | the grep. |
83 | ||
a601d313 JL |
84 | When this option is `auto', grep uses `--color=auto' to highlight |
85 | matches only when it outputs to a terminal (when `grep' is the last | |
86 | command in the pipe), thus avoiding the use of any potentially-harmful | |
87 | escape sequences when standard output goes to a file or pipe. | |
88 | ||
89 | To make grep highlight matches even into a pipe, you need the option | |
90 | `always' that forces grep to use `--color=always' to unconditionally | |
91 | output escape sequences. | |
92 | ||
d4bbd646 | 93 | In interactive usage, the actual value of this variable is set up |
a601d313 JL |
94 | by `grep-compute-defaults' when the default value is `auto-detect'. |
95 | To change the default value, use Customize or call the function | |
96 | `grep-apply-setting'." | |
00889cf9 JL |
97 | :type '(choice (const :tag "Do not highlight matches with grep markers" nil) |
98 | (const :tag "Highlight matches with grep markers" t) | |
a601d313 JL |
99 | (const :tag "Use --color=always" always) |
100 | (const :tag "Use --color=auto" auto) | |
00889cf9 | 101 | (other :tag "Not Set" auto-detect)) |
d4bbd646 | 102 | :set 'grep-apply-setting |
bf247b6e | 103 | :version "22.1" |
277df088 JL |
104 | :group 'grep) |
105 | ||
318e2976 | 106 | (defcustom grep-scroll-output nil |
fb7ada5f | 107 | "Non-nil to scroll the *grep* buffer window as output appears. |
318e2976 KS |
108 | |
109 | Setting it causes the grep commands to put point at the end of their | |
110 | output window so that the end of the output is always visible rather | |
333f9019 | 111 | than the beginning." |
318e2976 | 112 | :type 'boolean |
bf247b6e | 113 | :version "22.1" |
318e2976 KS |
114 | :group 'grep) |
115 | ||
9b19858e | 116 | ;;;###autoload |
318e2976 KS |
117 | (defcustom grep-command nil |
118 | "The default grep command for \\[grep]. | |
119 | If the grep program used supports an option to always include file names | |
120 | in its output (such as the `-H' option to GNU grep), it's a good idea to | |
121 | include it when specifying `grep-command'. | |
122 | ||
d4bbd646 CY |
123 | In interactive usage, the actual value of this variable is set up |
124 | by `grep-compute-defaults'; to change the default value, use | |
125 | Customize or call the function `grep-apply-setting'." | |
318e2976 KS |
126 | :type '(choice string |
127 | (const :tag "Not Set" nil)) | |
d4bbd646 | 128 | :set 'grep-apply-setting |
318e2976 KS |
129 | :group 'grep) |
130 | ||
0acfb7ce KS |
131 | (defcustom grep-template nil |
132 | "The default command to run for \\[lgrep]. | |
0acfb7ce KS |
133 | The following place holders should be present in the string: |
134 | <C> - place to put -i if case insensitive grep. | |
135 | <F> - file names and wildcards to search. | |
7ae62430 | 136 | <X> - file names and wildcards to exclude. |
0acfb7ce | 137 | <R> - the regular expression searched for. |
d4bbd646 CY |
138 | <N> - place to insert null-device. |
139 | ||
140 | In interactive usage, the actual value of this variable is set up | |
141 | by `grep-compute-defaults'; to change the default value, use | |
142 | Customize or call the function `grep-apply-setting'." | |
0acfb7ce KS |
143 | :type '(choice string |
144 | (const :tag "Not Set" nil)) | |
d4bbd646 | 145 | :set 'grep-apply-setting |
0acfb7ce KS |
146 | :version "22.1" |
147 | :group 'grep) | |
148 | ||
318e2976 KS |
149 | (defcustom grep-use-null-device 'auto-detect |
150 | "If t, append the value of `null-device' to `grep' commands. | |
151 | This is done to ensure that the output of grep includes the filename of | |
152 | any match in the case where only a single file is searched, and is not | |
153 | necessary if the grep program used supports the `-H' option. | |
154 | ||
d4bbd646 CY |
155 | In interactive usage, the actual value of this variable is set up |
156 | by `grep-compute-defaults'; to change the default value, use | |
157 | Customize or call the function `grep-apply-setting'." | |
318e2976 KS |
158 | :type '(choice (const :tag "Do Not Append Null Device" nil) |
159 | (const :tag "Append Null Device" t) | |
160 | (other :tag "Not Set" auto-detect)) | |
d4bbd646 | 161 | :set 'grep-apply-setting |
318e2976 KS |
162 | :group 'grep) |
163 | ||
9b19858e | 164 | ;;;###autoload |
318e2976 KS |
165 | (defcustom grep-find-command nil |
166 | "The default find command for \\[grep-find]. | |
d4bbd646 CY |
167 | In interactive usage, the actual value of this variable is set up |
168 | by `grep-compute-defaults'; to change the default value, use | |
169 | Customize or call the function `grep-apply-setting'." | |
318e2976 KS |
170 | :type '(choice string |
171 | (const :tag "Not Set" nil)) | |
d4bbd646 | 172 | :set 'grep-apply-setting |
318e2976 KS |
173 | :group 'grep) |
174 | ||
0acfb7ce KS |
175 | (defcustom grep-find-template nil |
176 | "The default command to run for \\[rgrep]. | |
318e2976 KS |
177 | The following place holders should be present in the string: |
178 | <D> - base directory for find | |
179 | <X> - find options to restrict or expand the directory list | |
180 | <F> - find options to limit the files matched | |
181 | <C> - place to put -i if case insensitive grep | |
d4bbd646 CY |
182 | <R> - the regular expression searched for. |
183 | In interactive usage, the actual value of this variable is set up | |
184 | by `grep-compute-defaults'; to change the default value, use | |
185 | Customize or call the function `grep-apply-setting'." | |
318e2976 KS |
186 | :type '(choice string |
187 | (const :tag "Not Set" nil)) | |
d4bbd646 | 188 | :set 'grep-apply-setting |
bf247b6e | 189 | :version "22.1" |
318e2976 KS |
190 | :group 'grep) |
191 | ||
2796180f | 192 | (defcustom grep-files-aliases |
7ae62430 JL |
193 | '(("all" . "* .*") |
194 | ("el" . "*.el") | |
195 | ("ch" . "*.[ch]") | |
2796180f | 196 | ("c" . "*.c") |
01623c28 | 197 | ("cc" . "*.cc *.cxx *.cpp *.C *.CC *.c++") |
7ae62430 | 198 | ("cchh" . "*.cc *.[ch]xx *.[ch]pp *.[CHh] *.CC *.HH *.[ch]++") |
01623c28 | 199 | ("hh" . "*.hxx *.hpp *.[Hh] *.HH *.h++") |
2796180f | 200 | ("h" . "*.h") |
7ae62430 | 201 | ("l" . "[Cc]hange[Ll]og*") |
2796180f | 202 | ("m" . "[Mm]akefile*") |
7ae62430 JL |
203 | ("tex" . "*.tex") |
204 | ("texi" . "*.texi") | |
205 | ("asm" . "*.[sS]")) | |
fb7ada5f | 206 | "Alist of aliases for the FILES argument to `lgrep' and `rgrep'." |
318e2976 KS |
207 | :type 'alist |
208 | :group 'grep) | |
209 | ||
a2e548a9 | 210 | (defcustom grep-find-ignored-directories |
1f0bee0a | 211 | vc-directory-exclusion-list |
fb7ada5f | 212 | "List of names of sub-directories which `rgrep' shall not recurse into. |
49405d0e SS |
213 | If an element is a cons cell, the car is called on the search directory |
214 | to determine whether cdr should not be recursed into." | |
7ae62430 JL |
215 | :type '(choice (repeat :tag "Ignored directories" string) |
216 | (const :tag "No ignored directories" nil)) | |
217 | :group 'grep) | |
218 | ||
219 | (defcustom grep-find-ignored-files | |
220 | (cons ".#*" (delq nil (mapcar (lambda (s) | |
221 | (unless (string-match-p "/\\'" s) | |
222 | (concat "*" s))) | |
223 | completion-ignored-extensions))) | |
fb7ada5f | 224 | "List of file names which `rgrep' and `lgrep' shall exclude. |
7ae62430 JL |
225 | If an element is a cons cell, the car is called on the search directory |
226 | to determine whether cdr should not be excluded." | |
227 | :type '(choice (repeat :tag "Ignored file" string) | |
228 | (const :tag "No ignored files" nil)) | |
318e2976 KS |
229 | :group 'grep) |
230 | ||
bb72b9d0 | 231 | (defcustom grep-error-screen-columns nil |
fb7ada5f | 232 | "If non-nil, column numbers in grep hits are screen columns. |
bb72b9d0 DP |
233 | See `compilation-error-screen-columns'" |
234 | :type '(choice (const :tag "Default" nil) | |
235 | integer) | |
bf247b6e | 236 | :version "22.1" |
bb72b9d0 DP |
237 | :group 'grep) |
238 | ||
318e2976 KS |
239 | ;;;###autoload |
240 | (defcustom grep-setup-hook nil | |
241 | "List of hook functions run by `grep-process-setup' (see `run-hooks')." | |
242 | :type 'hook | |
243 | :group 'grep) | |
244 | ||
245 | (defvar grep-mode-map | |
86c7460f SS |
246 | (let ((map (make-sparse-keymap))) |
247 | (set-keymap-parent map compilation-minor-mode-map) | |
ce3cefcc | 248 | (define-key map " " 'scroll-up-command) |
958614cf | 249 | (define-key map [?\S-\ ] 'scroll-down-command) |
ce3cefcc | 250 | (define-key map "\^?" 'scroll-down-command) |
0443d889 | 251 | (define-key map "\C-c\C-f" 'next-error-follow-minor-mode) |
318e2976 | 252 | |
318e2976 KS |
253 | (define-key map "\r" 'compile-goto-error) ;; ? |
254 | (define-key map "n" 'next-error-no-select) | |
255 | (define-key map "p" 'previous-error-no-select) | |
256 | (define-key map "{" 'compilation-previous-file) | |
257 | (define-key map "}" 'compilation-next-file) | |
94c713b2 JL |
258 | (define-key map "\t" 'compilation-next-error) |
259 | (define-key map [backtab] 'compilation-previous-error) | |
318e2976 KS |
260 | |
261 | ;; Set up the menu-bar | |
262 | (define-key map [menu-bar grep] | |
263 | (cons "Grep" (make-sparse-keymap "Grep"))) | |
264 | ||
265 | (define-key map [menu-bar grep compilation-kill-compilation] | |
38805987 DN |
266 | '(menu-item "Kill Grep" kill-compilation |
267 | :help "Kill the currently running grep process")) | |
268 | (define-key map [menu-bar grep compilation-separator2] '("----")) | |
318e2976 | 269 | (define-key map [menu-bar grep compilation-compile] |
38805987 DN |
270 | '(menu-item "Compile..." compile |
271 | :help "Compile the program including the current buffer. Default: run `make'")) | |
5d91db30 JL |
272 | (define-key map [menu-bar grep compilation-rgrep] |
273 | '(menu-item "Recursive grep..." rgrep | |
274 | :help "User-friendly recursive grep in directory tree")) | |
275 | (define-key map [menu-bar grep compilation-lgrep] | |
276 | '(menu-item "Local grep..." lgrep | |
277 | :help "User-friendly grep in a directory")) | |
278 | (define-key map [menu-bar grep compilation-grep-find] | |
279 | '(menu-item "Grep via Find..." grep-find | |
280 | :help "Run grep via find, with user-specified args")) | |
318e2976 | 281 | (define-key map [menu-bar grep compilation-grep] |
38805987 DN |
282 | '(menu-item "Another grep..." grep |
283 | :help "Run grep, with user-specified args, and collect output in a buffer.")) | |
318e2976 | 284 | (define-key map [menu-bar grep compilation-recompile] |
38805987 DN |
285 | '(menu-item "Repeat grep" recompile |
286 | :help "Run grep again")) | |
287 | (define-key map [menu-bar grep compilation-separator2] '("----")) | |
318e2976 | 288 | (define-key map [menu-bar grep compilation-first-error] |
38805987 DN |
289 | '(menu-item "First Match" first-error |
290 | :help "Restart at the first match, visit corresponding location")) | |
318e2976 | 291 | (define-key map [menu-bar grep compilation-previous-error] |
38805987 DN |
292 | '(menu-item "Previous Match" previous-error |
293 | :help "Visit the previous match and corresponding location")) | |
318e2976 | 294 | (define-key map [menu-bar grep compilation-next-error] |
38805987 DN |
295 | '(menu-item "Next Match" next-error |
296 | :help "Visit the next match and corresponding location")) | |
318e2976 KS |
297 | map) |
298 | "Keymap for grep buffers. | |
299 | `compilation-minor-mode-map' is a cdr of this.") | |
300 | ||
38cd9f17 | 301 | (defvar grep-mode-tool-bar-map |
30b72491 CY |
302 | ;; When bootstrapping, tool-bar-map is not properly initialized yet, |
303 | ;; so don't do anything. | |
304 | (when (keymapp (butlast tool-bar-map)) | |
305 | (let ((map (butlast (copy-keymap tool-bar-map))) | |
306 | (help (last tool-bar-map))) ;; Keep Help last in tool bar | |
307 | (tool-bar-local-item | |
308 | "left-arrow" 'previous-error-no-select 'previous-error-no-select map | |
309 | :rtl "right-arrow" | |
310 | :help "Goto previous match") | |
311 | (tool-bar-local-item | |
312 | "right-arrow" 'next-error-no-select 'next-error-no-select map | |
313 | :rtl "left-arrow" | |
314 | :help "Goto next match") | |
315 | (tool-bar-local-item | |
316 | "cancel" 'kill-compilation 'kill-compilation map | |
317 | :enable '(let ((buffer (compilation-find-buffer))) | |
318 | (get-buffer-process buffer)) | |
319 | :help "Stop grep") | |
320 | (tool-bar-local-item | |
321 | "refresh" 'recompile 'recompile map | |
322 | :help "Restart grep") | |
323 | (append map help)))) | |
38cd9f17 | 324 | |
ab55f76f SM |
325 | (defalias 'kill-grep 'kill-compilation) |
326 | ||
318e2976 KS |
327 | ;;;; TODO --- refine this!! |
328 | ||
6b3a84f2 SM |
329 | ;; (defcustom grep-use-compilation-buffer t |
330 | ;; "When non-nil, grep specific commands update `compilation-last-buffer'. | |
331 | ;; This means that standard compile commands like \\[next-error] and \\[compile-goto-error] | |
332 | ;; can be used to navigate between grep matches (the default). | |
333 | ;; Otherwise, the grep specific commands like \\[grep-next-match] must | |
334 | ;; be used to navigate between grep matches." | |
335 | ;; :type 'boolean | |
336 | ;; :group 'grep) | |
318e2976 KS |
337 | |
338 | ;; override compilation-last-buffer | |
339 | (defvar grep-last-buffer nil | |
340 | "The most recent grep buffer. | |
f911a445 | 341 | A grep buffer becomes most recent when you select Grep mode in it. |
318e2976 | 342 | Notice that using \\[next-error] or \\[compile-goto-error] modifies |
da6062e6 | 343 | `compilation-last-buffer' rather than `grep-last-buffer'.") |
318e2976 | 344 | |
9b19858e | 345 | ;;;###autoload |
2f7f4bee | 346 | (defconst grep-regexp-alist |
bfe5d7f0 JL |
347 | '( |
348 | ;; Rule to match column numbers is commented out since no known grep | |
349 | ;; produces them | |
350 | ;; ("^\\(.+?\\)\\(:[ \t]*\\)\\([1-9][0-9]*\\)\\2\\(?:\\([1-9][0-9]*\\)\\(?:-\\([1-9][0-9]*\\)\\)?\\2\\)?" | |
351 | ;; 1 3 (4 . 5)) | |
352 | ;; Note that we want to use as tight a regexp as we can to try and | |
353 | ;; handle weird file names (with colons in them) as well as possible. | |
354 | ;; E.g. we use [1-9][0-9]* rather than [0-9]+ so as to accept ":034:" | |
355 | ;; in file names. | |
356 | ("^\\(.+?\\)\\(:[ \t]*\\)\\([1-9][0-9]*\\)\\2" | |
0527e251 JL |
357 | 1 3 |
358 | ;; Calculate column positions (col . end-col) of first grep match on a line | |
359 | ((lambda () | |
360 | (when grep-highlight-matches | |
361 | (let* ((beg (match-end 0)) | |
362 | (end (save-excursion (goto-char beg) (line-end-position))) | |
98da8c0f | 363 | (mbeg (text-property-any beg end 'font-lock-face grep-match-face))) |
0527e251 JL |
364 | (when mbeg |
365 | (- mbeg beg))))) | |
366 | . | |
367 | (lambda () | |
368 | (when grep-highlight-matches | |
369 | (let* ((beg (match-end 0)) | |
370 | (end (save-excursion (goto-char beg) (line-end-position))) | |
98da8c0f | 371 | (mbeg (text-property-any beg end 'font-lock-face grep-match-face)) |
0527e251 JL |
372 | (mend (and mbeg (next-single-property-change mbeg 'font-lock-face nil end)))) |
373 | (when mend | |
374 | (- mend beg))))))) | |
ba65be24 | 375 | ("^Binary file \\(.+\\) matches$" 1 nil nil 0 1)) |
318e2976 KS |
376 | "Regexp used to match grep hits. See `compilation-error-regexp-alist'.") |
377 | ||
5ddce96c GM |
378 | (defvar grep-first-column 0 ; bug#10594 |
379 | "Value to use for `compilation-first-column' in grep buffers.") | |
380 | ||
ab55f76f SM |
381 | (defvar grep-error "grep hit" |
382 | "Message to print when no matches are found.") | |
383 | ||
384 | ;; Reverse the colors because grep hits are not errors (though we jump there | |
385 | ;; with `next-error'), and unreadable files can't be gone to. | |
386 | (defvar grep-hit-face compilation-info-face | |
387 | "Face name to use for grep hits.") | |
388 | ||
7462142d | 389 | (defvar grep-error-face 'compilation-error |
ab55f76f SM |
390 | "Face name to use for grep error messages.") |
391 | ||
f30c45e1 JL |
392 | (defvar grep-match-face 'match |
393 | "Face name to use for grep matches.") | |
394 | ||
35952129 JL |
395 | (defvar grep-context-face 'shadow |
396 | "Face name to use for grep context lines.") | |
397 | ||
ab55f76f SM |
398 | (defvar grep-mode-font-lock-keywords |
399 | '(;; Command output lines. | |
ab55f76f SM |
400 | (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$" |
401 | 1 grep-error-face) | |
402 | ;; remove match from grep-regexp-alist before fontifying | |
c2c04c28 | 403 | ("^Grep[/a-zA-z]* started.*" |
1a0a6cef | 404 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)) |
c2c04c28 | 405 | ("^Grep[/a-zA-z]* finished \\(?:(\\(matches found\\))\\|with \\(no matches found\\)\\).*" |
1a0a6cef | 406 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) |
7462142d JL |
407 | (1 compilation-info-face nil t) |
408 | (2 compilation-warning-face nil t)) | |
c2c04c28 | 409 | ("^Grep[/a-zA-z]* \\(exited abnormally\\|interrupt\\|killed\\|terminated\\)\\(?:.*with code \\([0-9]+\\)\\)?.*" |
1a0a6cef | 410 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) |
7462142d | 411 | (1 grep-error-face) |
cf115520 | 412 | (2 grep-error-face nil t)) |
7a1d7ba7 JL |
413 | ;; "filename-linenumber-" format is used for context lines in GNU grep, |
414 | ;; "filename=linenumber=" for lines with function names in "git grep -p". | |
415 | ("^.+?[-=][0-9]+[-=].*\n" (0 grep-context-face))) | |
ab55f76f SM |
416 | "Additional things to highlight in grep output. |
417 | This gets tacked on the end of the generated expressions.") | |
418 | ||
9b19858e | 419 | ;;;###autoload |
6bdad9ae | 420 | (defvar grep-program (purecopy "grep") |
318e2976 KS |
421 | "The default grep program for `grep-command' and `grep-find-command'. |
422 | This variable's value takes effect when `grep-compute-defaults' is called.") | |
423 | ||
9b19858e | 424 | ;;;###autoload |
6bdad9ae | 425 | (defvar find-program (purecopy "find") |
3ab49e2c LI |
426 | "The default find program. |
427 | This is used by commands like `grep-find-command', `find-dired' | |
428 | and others.") | |
318e2976 | 429 | |
1571d112 | 430 | ;;;###autoload |
6bdad9ae | 431 | (defvar xargs-program (purecopy "xargs") |
1571d112 JB |
432 | "The default xargs program for `grep-find-command'. |
433 | See `grep-find-use-xargs'. | |
434 | This variable's value takes effect when `grep-compute-defaults' is called.") | |
435 | ||
9b19858e | 436 | ;;;###autoload |
318e2976 | 437 | (defvar grep-find-use-xargs nil |
f3ca7378 GM |
438 | "How to invoke find and grep. |
439 | If `exec', use `find -exec {} ;'. | |
440 | If `exec-plus' use `find -exec {} +'. | |
7f3afa3d | 441 | If `gnu', use `find -print0' and `xargs -0'. |
f3ca7378 | 442 | Any other value means to use `find -print' and `xargs'. |
318e2976 KS |
443 | |
444 | This variable's value takes effect when `grep-compute-defaults' is called.") | |
445 | ||
446 | ;; History of grep commands. | |
9b19858e | 447 | ;;;###autoload |
3adbe224 | 448 | (defvar grep-history nil "History list for grep.") |
9b19858e | 449 | ;;;###autoload |
3adbe224 | 450 | (defvar grep-find-history nil "History list for grep-find.") |
318e2976 | 451 | |
0acfb7ce KS |
452 | ;; History of lgrep and rgrep regexp and files args. |
453 | (defvar grep-regexp-history nil) | |
7ae62430 | 454 | (defvar grep-files-history nil) |
0acfb7ce | 455 | |
9b19858e | 456 | ;;;###autoload |
318e2976 KS |
457 | (defun grep-process-setup () |
458 | "Setup compilation variables and buffer for `grep'. | |
ab55f76f | 459 | Set up `compilation-exit-message-function' and run `grep-setup-hook'." |
a601d313 | 460 | (when (eq grep-highlight-matches 'auto-detect) |
a58c94a2 | 461 | (grep-compute-defaults)) |
a601d313 | 462 | (unless (or (eq grep-highlight-matches 'auto-detect) |
90439906 JL |
463 | (null grep-highlight-matches) |
464 | ;; Don't output color escapes if they can't be | |
465 | ;; highlighted with `font-lock-face' by `grep-filter'. | |
466 | (null font-lock-mode)) | |
6b8d0852 JL |
467 | ;; `setenv' modifies `process-environment' let-bound in `compilation-start' |
468 | ;; Any TERM except "dumb" allows GNU grep to use `--color=auto' | |
469 | (setenv "TERM" "emacs-grep") | |
8f0e19af EZ |
470 | (setenv "GREP_OPTIONS" |
471 | (concat (getenv "GREP_OPTIONS") | |
a601d313 | 472 | " --color=" (if (eq grep-highlight-matches 'always) |
8f0e19af | 473 | "always" "auto"))) |
6b8d0852 | 474 | ;; GREP_COLOR is used in GNU grep 2.5.1, but deprecated in later versions |
983203ee | 475 | (setenv "GREP_COLOR" "01;31") |
6b8d0852 | 476 | ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions |
f62bd846 | 477 | (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")) |
318e2976 KS |
478 | (set (make-local-variable 'compilation-exit-message-function) |
479 | (lambda (status code msg) | |
480 | (if (eq status 'exit) | |
262a1439 JL |
481 | ;; This relies on the fact that `compilation-start' |
482 | ;; sets buffer-modified to nil before running the command, | |
483 | ;; so the buffer is still unmodified if there is no output. | |
484 | (cond ((and (zerop code) (buffer-modified-p)) | |
318e2976 | 485 | '("finished (matches found)\n" . "matched")) |
46c5cf66 | 486 | ((not (buffer-modified-p)) |
318e2976 KS |
487 | '("finished with no matches found\n" . "no match")) |
488 | (t | |
489 | (cons msg code))) | |
490 | (cons msg code)))) | |
318e2976 KS |
491 | (run-hooks 'grep-setup-hook)) |
492 | ||
d9c54a06 CY |
493 | (defun grep-filter () |
494 | "Handle match highlighting escape sequences inserted by the grep process. | |
495 | This function is called from `compilation-filter-hook'." | |
496 | (save-excursion | |
c89be45f | 497 | (forward-line 0) |
f62bd846 | 498 | (let ((end (point)) beg) |
d9c54a06 | 499 | (goto-char compilation-filter-start) |
c89be45f | 500 | (forward-line 0) |
f62bd846 | 501 | (setq beg (point)) |
c89be45f SM |
502 | ;; Only operate on whole lines so we don't get caught with part of an |
503 | ;; escape sequence in one chunk and the rest in another. | |
504 | (when (< (point) end) | |
505 | (setq end (copy-marker end)) | |
506 | ;; Highlight grep matches and delete marking sequences. | |
f62bd846 | 507 | (while (re-search-forward "\033\\[0?1;31m\\(.*?\\)\033\\[[0-9]*m" end 1) |
c89be45f SM |
508 | (replace-match (propertize (match-string 1) |
509 | 'face nil 'font-lock-face grep-match-face) | |
510 | t t)) | |
511 | ;; Delete all remaining escape sequences | |
f62bd846 | 512 | (goto-char beg) |
c89be45f SM |
513 | (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1) |
514 | (replace-match "" t t)))))) | |
d9c54a06 | 515 | |
0acfb7ce | 516 | (defun grep-probe (command args &optional func result) |
458c8d31 MA |
517 | (let (process-file-side-effects) |
518 | (equal (condition-case nil | |
519 | (apply (or func 'process-file) command args) | |
520 | (error nil)) | |
521 | (or result 0)))) | |
0acfb7ce | 522 | |
9b19858e | 523 | ;;;###autoload |
318e2976 | 524 | (defun grep-compute-defaults () |
2e2eead3 MA |
525 | ;; Keep default values. |
526 | (unless grep-host-defaults-alist | |
527 | (add-to-list | |
528 | 'grep-host-defaults-alist | |
529 | (cons nil | |
530 | `((grep-command ,grep-command) | |
531 | (grep-template ,grep-template) | |
532 | (grep-use-null-device ,grep-use-null-device) | |
533 | (grep-find-command ,grep-find-command) | |
534 | (grep-find-template ,grep-find-template) | |
535 | (grep-find-use-xargs ,grep-find-use-xargs) | |
536 | (grep-highlight-matches ,grep-highlight-matches))))) | |
537 | (let* ((host-id | |
85e7298f | 538 | (intern (or (file-remote-p default-directory) "localhost"))) |
2e2eead3 MA |
539 | (host-defaults (assq host-id grep-host-defaults-alist)) |
540 | (defaults (assq nil grep-host-defaults-alist))) | |
05c7c7b1 | 541 | ;; There are different defaults on different hosts. They must be |
2e2eead3 | 542 | ;; computed for every host once. |
d4bbd646 CY |
543 | (dolist (setting '(grep-command grep-template |
544 | grep-use-null-device grep-find-command | |
545 | grep-find-template grep-find-use-xargs | |
546 | grep-highlight-matches)) | |
547 | (set setting | |
7860d2e3 CY |
548 | (cadr (or (assq setting host-defaults) |
549 | (assq setting defaults))))) | |
05c7c7b1 MA |
550 | |
551 | (unless (or (not grep-use-null-device) (eq grep-use-null-device t)) | |
552 | (setq grep-use-null-device | |
553 | (with-temp-buffer | |
554 | (let ((hello-file (expand-file-name "HELLO" data-directory))) | |
555 | (not | |
556 | (and (if grep-command | |
557 | ;; `grep-command' is already set, so | |
558 | ;; use that for testing. | |
559 | (grep-probe grep-command | |
560 | `(nil t nil "^English" ,hello-file) | |
561 | #'call-process-shell-command) | |
562 | ;; otherwise use `grep-program' | |
563 | (grep-probe grep-program | |
564 | `(nil t nil "-nH" "^English" ,hello-file))) | |
565 | (progn | |
566 | (goto-char (point-min)) | |
567 | (looking-at | |
568 | (concat (regexp-quote hello-file) | |
569 | ":[0-9]+:English"))))))))) | |
570 | (unless (and grep-command grep-find-command | |
571 | grep-template grep-find-template) | |
572 | (let ((grep-options | |
573 | (concat (if grep-use-null-device "-n" "-nH") | |
574 | (if (grep-probe grep-program | |
575 | `(nil nil nil "-e" "foo" ,null-device) | |
576 | nil 1) | |
577 | " -e")))) | |
578 | (unless grep-command | |
579 | (setq grep-command | |
580 | (format "%s %s " grep-program grep-options))) | |
581 | (unless grep-template | |
582 | (setq grep-template | |
7ae62430 | 583 | (format "%s <X> <C> %s <R> <F>" grep-program grep-options))) |
05c7c7b1 MA |
584 | (unless grep-find-use-xargs |
585 | (setq grep-find-use-xargs | |
586 | (cond | |
f3ca7378 GM |
587 | ((grep-probe find-program |
588 | `(nil nil nil ,null-device "-exec" "echo" | |
589 | "{}" "+")) | |
590 | 'exec-plus) | |
05c7c7b1 MA |
591 | ((and |
592 | (grep-probe find-program `(nil nil nil ,null-device "-print0")) | |
6fccd6e8 | 593 | (grep-probe xargs-program `(nil nil nil "-0" "echo"))) |
05c7c7b1 MA |
594 | 'gnu) |
595 | (t | |
596 | 'exec)))) | |
597 | (unless grep-find-command | |
598 | (setq grep-find-command | |
0acfb7ce | 599 | (cond ((eq grep-find-use-xargs 'gnu) |
362b9d48 GM |
600 | ;; Windows shells need the program file name |
601 | ;; after the pipe symbol be quoted if they use | |
602 | ;; forward slashes as directory separators. | |
6fccd6e8 | 603 | (format "%s . -type f -print0 | \"%s\" -0 %s" |
1571d112 | 604 | find-program xargs-program grep-command)) |
f3ca7378 | 605 | ((memq grep-find-use-xargs '(exec exec-plus)) |
05c7c7b1 | 606 | (let ((cmd0 (format "%s . -type f -exec %s" |
f3ca7378 GM |
607 | find-program grep-command)) |
608 | (null (if grep-use-null-device | |
609 | (format "%s " null-device) | |
610 | ""))) | |
05c7c7b1 | 611 | (cons |
f3ca7378 GM |
612 | (if (eq grep-find-use-xargs 'exec-plus) |
613 | (format "%s %s{} +" cmd0 null) | |
614 | (format "%s {} %s%s" cmd0 null | |
615 | (shell-quote-argument ";"))) | |
05c7c7b1 | 616 | (1+ (length cmd0))))) |
84d797c9 | 617 | (t |
362b9d48 | 618 | (format "%s . -type f -print | \"%s\" %s" |
1571d112 | 619 | find-program xargs-program grep-command))))) |
05c7c7b1 MA |
620 | (unless grep-find-template |
621 | (setq grep-find-template | |
622 | (let ((gcmd (format "%s <C> %s <R>" | |
f3ca7378 GM |
623 | grep-program grep-options)) |
624 | (null (if grep-use-null-device | |
625 | (format "%s " null-device) | |
626 | ""))) | |
05c7c7b1 | 627 | (cond ((eq grep-find-use-xargs 'gnu) |
6fccd6e8 | 628 | (format "%s . <X> -type f <F> -print0 | \"%s\" -0 %s" |
1571d112 | 629 | find-program xargs-program gcmd)) |
05c7c7b1 | 630 | ((eq grep-find-use-xargs 'exec) |
f3ca7378 GM |
631 | (format "%s . <X> -type f <F> -exec %s {} %s%s" |
632 | find-program gcmd null | |
05c7c7b1 | 633 | (shell-quote-argument ";"))) |
f3ca7378 GM |
634 | ((eq grep-find-use-xargs 'exec-plus) |
635 | (format "%s . <X> -type f <F> -exec %s %s{} +" | |
636 | find-program gcmd null)) | |
05c7c7b1 | 637 | (t |
362b9d48 | 638 | (format "%s . <X> -type f <F> -print | \"%s\" %s" |
1571d112 | 639 | find-program xargs-program gcmd)))))))) |
a601d313 | 640 | (when (eq grep-highlight-matches 'auto-detect) |
05c7c7b1 MA |
641 | (setq grep-highlight-matches |
642 | (with-temp-buffer | |
643 | (and (grep-probe grep-program '(nil t nil "--help")) | |
644 | (progn | |
645 | (goto-char (point-min)) | |
646 | (search-forward "--color" nil t)) | |
a601d313 JL |
647 | ;; Windows and DOS pipes fail `isatty' detection in Grep. |
648 | (if (memq system-type '(windows-nt ms-dos)) | |
649 | 'always 'auto))))) | |
05c7c7b1 MA |
650 | |
651 | ;; Save defaults for this host. | |
6e3aa3f5 MA |
652 | (setq grep-host-defaults-alist |
653 | (delete (assq host-id grep-host-defaults-alist) | |
654 | grep-host-defaults-alist)) | |
655 | (add-to-list | |
656 | 'grep-host-defaults-alist | |
657 | (cons host-id | |
658 | `((grep-command ,grep-command) | |
659 | (grep-template ,grep-template) | |
660 | (grep-use-null-device ,grep-use-null-device) | |
661 | (grep-find-command ,grep-find-command) | |
662 | (grep-find-template ,grep-find-template) | |
663 | (grep-find-use-xargs ,grep-find-use-xargs) | |
664 | (grep-highlight-matches ,grep-highlight-matches)))))) | |
318e2976 | 665 | |
7cb0d0ef KS |
666 | (defun grep-tag-default () |
667 | (or (and transient-mark-mode mark-active | |
668 | (/= (point) (mark)) | |
669 | (buffer-substring-no-properties (point) (mark))) | |
670 | (funcall (or find-tag-default-function | |
671 | (get major-mode 'find-tag-default-function) | |
672 | 'find-tag-default)) | |
673 | "")) | |
674 | ||
318e2976 | 675 | (defun grep-default-command () |
1571d112 | 676 | "Compute the default grep command for \\[universal-argument] \\[grep] to offer." |
7cb0d0ef | 677 | (let ((tag-default (shell-quote-argument (grep-tag-default))) |
13eb1bde RS |
678 | ;; This a regexp to match single shell arguments. |
679 | ;; Could someone please add comments explaining it? | |
318e2976 KS |
680 | (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)") |
681 | (grep-default (or (car grep-history) grep-command))) | |
13eb1bde | 682 | ;; In the default command, find the arg that specifies the pattern. |
318e2976 KS |
683 | (when (or (string-match |
684 | (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*" | |
685 | sh-arg-re "\\(\\s +\\(\\S +\\)\\)?") | |
686 | grep-default) | |
687 | ;; If the string is not yet complete. | |
688 | (string-match "\\(\\)\\'" grep-default)) | |
13eb1bde RS |
689 | ;; Maybe we will replace the pattern with the default tag. |
690 | ;; But first, maybe replace the file name pattern. | |
691 | (condition-case nil | |
692 | (unless (or (not (stringp buffer-file-name)) | |
693 | (when (match-beginning 2) | |
694 | (save-match-data | |
695 | (string-match | |
696 | (wildcard-to-regexp | |
697 | (file-name-nondirectory | |
698 | (match-string 3 grep-default))) | |
699 | (file-name-nondirectory buffer-file-name))))) | |
700 | (setq grep-default (concat (substring grep-default | |
701 | 0 (match-beginning 2)) | |
702 | " *." | |
703 | (file-name-extension buffer-file-name)))) | |
704 | ;; In case wildcard-to-regexp gets an error | |
705 | ;; from invalid data. | |
706 | (error nil)) | |
707 | ;; Now replace the pattern with the default tag. | |
02b73b97 | 708 | (replace-match tag-default t t grep-default 1)))) |
318e2976 | 709 | |
0acfb7ce | 710 | |
318e2976 | 711 | ;;;###autoload |
0acfb7ce KS |
712 | (define-compilation-mode grep-mode "Grep" |
713 | "Sets `grep-last-buffer' and `compilation-window-height'." | |
714 | (setq grep-last-buffer (current-buffer)) | |
38cd9f17 | 715 | (set (make-local-variable 'tool-bar-map) grep-mode-tool-bar-map) |
0acfb7ce KS |
716 | (set (make-local-variable 'compilation-error-face) |
717 | grep-hit-face) | |
718 | (set (make-local-variable 'compilation-error-regexp-alist) | |
719 | grep-regexp-alist) | |
b7cf2c79 SM |
720 | ;; compilation-directory-matcher can't be nil, so we set it to a regexp that |
721 | ;; can never match. | |
79106a44 | 722 | (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`")) |
0acfb7ce KS |
723 | (set (make-local-variable 'compilation-process-setup-function) |
724 | 'grep-process-setup) | |
d9c54a06 | 725 | (set (make-local-variable 'compilation-disable-input) t) |
0527e251 JL |
726 | (set (make-local-variable 'compilation-error-screen-columns) |
727 | grep-error-screen-columns) | |
d9c54a06 | 728 | (add-hook 'compilation-filter-hook 'grep-filter nil t)) |
0acfb7ce KS |
729 | |
730 | ||
731 | ;;;###autoload | |
732 | (defun grep (command-args) | |
318e2976 KS |
733 | "Run grep, with user-specified args, and collect output in a buffer. |
734 | While grep runs asynchronously, you can use \\[next-error] (M-x next-error), | |
d02766ab CY |
735 | or \\<grep-mode-map>\\[compile-goto-error] in the *grep* \ |
736 | buffer, to go to the lines where grep found | |
737 | matches. To kill the grep job before it finishes, type \\[kill-compilation]. | |
318e2976 | 738 | |
215c3847 | 739 | For doing a recursive `grep', see the `rgrep' command. For running |
9d4a3d39 | 740 | `grep' in a specific directory, see `lgrep'. |
215c3847 | 741 | |
1571d112 JB |
742 | This command uses a special history list for its COMMAND-ARGS, so you |
743 | can easily repeat a grep command. | |
318e2976 KS |
744 | |
745 | A prefix argument says to default the argument based upon the current | |
746 | tag the cursor is over, substituting it into the last grep command | |
1571d112 JB |
747 | in the grep command history (or into `grep-command' if that history |
748 | list is empty)." | |
318e2976 KS |
749 | (interactive |
750 | (progn | |
0acfb7ce | 751 | (grep-compute-defaults) |
318e2976 | 752 | (let ((default (grep-default-command))) |
286d4b3b SM |
753 | (list (read-shell-command "Run grep (like this): " |
754 | (if current-prefix-arg default grep-command) | |
755 | 'grep-history | |
756 | (if current-prefix-arg nil default)))))) | |
318e2976 KS |
757 | |
758 | ;; Setting process-setup-function makes exit-message-function work | |
759 | ;; even when async processes aren't supported. | |
19b3905f JL |
760 | (compilation-start (if (and grep-use-null-device null-device) |
761 | (concat command-args " " null-device) | |
762 | command-args) | |
0acfb7ce | 763 | 'grep-mode)) |
ab55f76f | 764 | |
318e2976 | 765 | |
318e2976 KS |
766 | ;;;###autoload |
767 | (defun grep-find (command-args) | |
768 | "Run grep via find, with user-specified args COMMAND-ARGS. | |
769 | Collect output in a buffer. | |
770 | While find runs asynchronously, you can use the \\[next-error] command | |
771 | to find the text that grep hits refer to. | |
772 | ||
773 | This command uses a special history list for its arguments, so you can | |
774 | easily repeat a find command." | |
775 | (interactive | |
776 | (progn | |
0acfb7ce | 777 | (grep-compute-defaults) |
87f54c05 | 778 | (if grep-find-command |
286d4b3b SM |
779 | (list (read-shell-command "Run find (like this): " |
780 | grep-find-command 'grep-find-history)) | |
87f54c05 RS |
781 | ;; No default was set |
782 | (read-string | |
783 | "compile.el: No `grep-find-command' command available. Press RET.") | |
784 | (list nil)))) | |
a34f7eb1 | 785 | (when command-args |
87f54c05 RS |
786 | (let ((null-device nil)) ; see grep |
787 | (grep command-args)))) | |
318e2976 | 788 | |
3d4d788a DK |
789 | ;;;###autoload |
790 | (defalias 'find-grep 'grep-find) | |
791 | ||
0acfb7ce KS |
792 | |
793 | ;; User-friendly interactive API. | |
794 | ||
795 | (defconst grep-expand-keywords | |
796 | '(("<C>" . (and cf (isearch-no-upper-case-p regexp t) "-i")) | |
797 | ("<D>" . dir) | |
798 | ("<F>" . files) | |
799 | ("<N>" . null-device) | |
800 | ("<X>" . excl) | |
801 | ("<R>" . (shell-quote-argument (or regexp "")))) | |
802 | "List of substitutions performed by `grep-expand-template'. | |
803 | If car of an element matches, the cdr is evalled in to get the | |
804 | substitution string. Note dynamic scoping of variables.") | |
805 | ||
806 | (defun grep-expand-template (template &optional regexp files dir excl) | |
807 | "Patch grep COMMAND string replacing <C>, <D>, <F>, <R>, and <X>." | |
f7993853 SM |
808 | (let* ((command template) |
809 | (env `((cf . ,case-fold-search) | |
810 | (excl . ,excl) | |
811 | (dir . ,dir) | |
812 | (files . ,files) | |
813 | (regexp . ,regexp))) | |
814 | (case-fold-search nil)) | |
0acfb7ce KS |
815 | (dolist (kw grep-expand-keywords command) |
816 | (if (string-match (car kw) command) | |
d0afff34 KS |
817 | (setq command |
818 | (replace-match | |
819 | (or (if (symbolp (cdr kw)) | |
f7993853 SM |
820 | (eval (cdr kw) env) |
821 | (save-match-data (eval (cdr kw) env))) | |
d0afff34 KS |
822 | "") |
823 | t t command)))))) | |
0acfb7ce KS |
824 | |
825 | (defun grep-read-regexp () | |
b2bf2a25 | 826 | "Read regexp arg for interactive grep using `read-regexp'." |
24da7273 | 827 | (read-regexp "Search for" 'grep-tag-default 'grep-regexp-history)) |
0acfb7ce KS |
828 | |
829 | (defun grep-read-files (regexp) | |
830 | "Read files arg for interactive grep." | |
6830f449 JL |
831 | (let* ((bn (or (buffer-file-name) |
832 | (replace-regexp-in-string "<[0-9]+>\\'" "" (buffer-name)))) | |
208cc91f KS |
833 | (fn (and bn |
834 | (stringp bn) | |
835 | (file-name-nondirectory bn))) | |
7ae62430 JL |
836 | (default-alias |
837 | (and fn | |
df9db151 JL |
838 | (let ((aliases (remove (assoc "all" grep-files-aliases) |
839 | grep-files-aliases)) | |
7ae62430 JL |
840 | alias) |
841 | (while aliases | |
842 | (setq alias (car aliases) | |
843 | aliases (cdr aliases)) | |
df9db151 JL |
844 | (if (string-match (mapconcat |
845 | 'wildcard-to-regexp | |
846 | (split-string (cdr alias) nil t) | |
847 | "\\|") | |
848 | fn) | |
7ae62430 JL |
849 | (setq aliases nil) |
850 | (setq alias nil))) | |
851 | (cdr alias)))) | |
852 | (default-extension | |
853 | (and fn | |
854 | (let ((ext (file-name-extension fn))) | |
855 | (and ext (concat "*." ext))))) | |
208cc91f | 856 | (default |
7ae62430 JL |
857 | (or default-alias |
858 | default-extension | |
bcdf86fb KS |
859 | (car grep-files-history) |
860 | (car (car grep-files-aliases)))) | |
9136e895 | 861 | (files (completing-read |
0acfb7ce | 862 | (concat "Search for \"" regexp |
208cc91f KS |
863 | "\" in files" |
864 | (if default (concat " (default " default ")")) | |
865 | ": ") | |
9136e895 JL |
866 | 'read-file-name-internal |
867 | nil nil nil 'grep-files-history | |
7ae62430 JL |
868 | (delete-dups |
869 | (delq nil (append (list default default-alias default-extension) | |
870 | (mapcar 'car grep-files-aliases))))))) | |
0acfb7ce KS |
871 | (and files |
872 | (or (cdr (assoc files grep-files-aliases)) | |
873 | files)))) | |
318e2976 KS |
874 | |
875 | ;;;###autoload | |
32a2cf25 | 876 | (defun lgrep (regexp &optional files dir confirm) |
9d4a3d39 | 877 | "Run grep, searching for REGEXP in FILES in directory DIR. |
318e2976 | 878 | The search is limited to file names matching shell pattern FILES. |
0acfb7ce | 879 | FILES may use abbreviations defined in `grep-files-aliases', e.g. |
318e2976 KS |
880 | entering `ch' is equivalent to `*.[ch]'. |
881 | ||
6e7c574f KS |
882 | With \\[universal-argument] prefix, you can edit the constructed shell command line |
883 | before it is executed. | |
884 | With two \\[universal-argument] prefixes, directly edit and run `grep-command'. | |
318e2976 | 885 | |
0acfb7ce | 886 | Collect output in a buffer. While grep runs asynchronously, you |
1571d112 JB |
887 | can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \ |
888 | in the grep output buffer, | |
889 | to go to the lines where grep found matches. | |
318e2976 | 890 | |
0acfb7ce | 891 | This command shares argument histories with \\[rgrep] and \\[grep]." |
318e2976 | 892 | (interactive |
0acfb7ce KS |
893 | (progn |
894 | (grep-compute-defaults) | |
895 | (cond | |
896 | ((and grep-command (equal current-prefix-arg '(16))) | |
897 | (list (read-from-minibuffer "Run: " grep-command | |
32a2cf25 | 898 | nil nil 'grep-history))) |
0acfb7ce | 899 | ((not grep-template) |
5a0c3f56 | 900 | (error "grep.el: No `grep-template' available")) |
0acfb7ce | 901 | (t (let* ((regexp (grep-read-regexp)) |
9d4a3d39 KS |
902 | (files (grep-read-files regexp)) |
903 | (dir (read-directory-name "In directory: " | |
32a2cf25 JL |
904 | nil default-directory t)) |
905 | (confirm (equal current-prefix-arg '(4)))) | |
906 | (list regexp files dir confirm)))))) | |
0acfb7ce | 907 | (when (and (stringp regexp) (> (length regexp) 0)) |
32a2cf25 JL |
908 | (unless (and dir (file-directory-p dir) (file-readable-p dir)) |
909 | (setq dir default-directory)) | |
0acfb7ce KS |
910 | (let ((command regexp)) |
911 | (if (null files) | |
912 | (if (string= command grep-command) | |
913 | (setq command nil)) | |
9d4a3d39 | 914 | (setq dir (file-name-as-directory (expand-file-name dir))) |
0acfb7ce KS |
915 | (setq command (grep-expand-template |
916 | grep-template | |
917 | regexp | |
7ae62430 JL |
918 | files |
919 | nil | |
920 | (and grep-find-ignored-files | |
921 | (concat " --exclude=" | |
922 | (mapconcat | |
923 | #'(lambda (ignore) | |
924 | (cond ((stringp ignore) | |
925 | (shell-quote-argument ignore)) | |
926 | ((consp ignore) | |
927 | (and (funcall (car ignore) dir) | |
928 | (shell-quote-argument | |
929 | (cdr ignore)))))) | |
930 | grep-find-ignored-files | |
931 | " --exclude="))))) | |
0acfb7ce | 932 | (when command |
32a2cf25 | 933 | (if confirm |
0acfb7ce KS |
934 | (setq command |
935 | (read-from-minibuffer "Confirm: " | |
936 | command nil nil 'grep-history)) | |
c3e9438b | 937 | (add-to-history 'grep-history command)))) |
0acfb7ce | 938 | (when command |
32a2cf25 | 939 | (let ((default-directory dir)) |
9d4a3d39 KS |
940 | ;; Setting process-setup-function makes exit-message-function work |
941 | ;; even when async processes aren't supported. | |
942 | (compilation-start (if (and grep-use-null-device null-device) | |
943 | (concat command " " null-device) | |
c60c6d91 RS |
944 | command) |
945 | 'grep-mode)) | |
9d4a3d39 KS |
946 | (if (eq next-error-last-buffer (current-buffer)) |
947 | (setq default-directory dir)))))) | |
948 | ||
0acfb7ce | 949 | |
da205913 | 950 | (defvar find-name-arg) ; not autoloaded but defined in find-dired |
0acfb7ce KS |
951 | |
952 | ;;;###autoload | |
32a2cf25 | 953 | (defun rgrep (regexp &optional files dir confirm) |
6e7c574f | 954 | "Recursively grep for REGEXP in FILES in directory tree rooted at DIR. |
0acfb7ce KS |
955 | The search is limited to file names matching shell pattern FILES. |
956 | FILES may use abbreviations defined in `grep-files-aliases', e.g. | |
957 | entering `ch' is equivalent to `*.[ch]'. | |
958 | ||
6e7c574f KS |
959 | With \\[universal-argument] prefix, you can edit the constructed shell command line |
960 | before it is executed. | |
961 | With two \\[universal-argument] prefixes, directly edit and run `grep-find-command'. | |
0acfb7ce | 962 | |
d02766ab CY |
963 | Collect output in a buffer. While the recursive grep is running, |
964 | you can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \ | |
1571d112 | 965 | in the grep output buffer, |
d02766ab CY |
966 | to visit the lines where matches were found. To kill the job |
967 | before it finishes, type \\[kill-compilation]. | |
0acfb7ce | 968 | |
1c4757d6 JL |
969 | This command shares argument histories with \\[lgrep] and \\[grep-find]. |
970 | ||
971 | When called programmatically and FILES is nil, REGEXP is expected | |
972 | to specify a command to run." | |
0acfb7ce KS |
973 | (interactive |
974 | (progn | |
975 | (grep-compute-defaults) | |
976 | (cond | |
977 | ((and grep-find-command (equal current-prefix-arg '(16))) | |
978 | (list (read-from-minibuffer "Run: " grep-find-command | |
32a2cf25 | 979 | nil nil 'grep-find-history))) |
0acfb7ce | 980 | ((not grep-find-template) |
5a0c3f56 | 981 | (error "grep.el: No `grep-find-template' available")) |
0acfb7ce KS |
982 | (t (let* ((regexp (grep-read-regexp)) |
983 | (files (grep-read-files regexp)) | |
984 | (dir (read-directory-name "Base directory: " | |
32a2cf25 JL |
985 | nil default-directory t)) |
986 | (confirm (equal current-prefix-arg '(4)))) | |
987 | (list regexp files dir confirm)))))) | |
0acfb7ce | 988 | (when (and (stringp regexp) (> (length regexp) 0)) |
32a2cf25 JL |
989 | (unless (and dir (file-directory-p dir) (file-readable-p dir)) |
990 | (setq dir default-directory)) | |
0acfb7ce | 991 | (if (null files) |
02b404de JL |
992 | (if (not (string= regexp (if (consp grep-find-command) |
993 | (car grep-find-command) | |
994 | grep-find-command))) | |
12b16734 | 995 | (compilation-start regexp 'grep-mode)) |
c0b87c3f | 996 | (setq dir (file-name-as-directory (expand-file-name dir))) |
da205913 | 997 | (require 'find-dired) ; for `find-name-arg' |
c0b87c3f KS |
998 | (let ((command (grep-expand-template |
999 | grep-find-template | |
1000 | regexp | |
84d797c9 | 1001 | (concat (shell-quote-argument "(") |
34fac407 | 1002 | " " find-name-arg " " |
91b982a0 MA |
1003 | (mapconcat |
1004 | #'shell-quote-argument | |
1005 | (split-string files) | |
4803595d | 1006 | (concat " -o " find-name-arg " ")) |
84d797c9 KS |
1007 | " " |
1008 | (shell-quote-argument ")")) | |
7ae62430 JL |
1009 | dir |
1010 | (concat | |
0acfb7ce | 1011 | (and grep-find-ignored-directories |
453de99f OG |
1012 | (concat "-type d " |
1013 | (shell-quote-argument "(") | |
84d797c9 KS |
1014 | ;; we should use shell-quote-argument here |
1015 | " -path " | |
49405d0e | 1016 | (mapconcat |
7ae62430 JL |
1017 | #'(lambda (ignore) |
1018 | (cond ((stringp ignore) | |
1019 | (shell-quote-argument | |
1020 | (concat "*/" ignore))) | |
1021 | ((consp ignore) | |
1022 | (and (funcall (car ignore) dir) | |
1023 | (shell-quote-argument | |
1024 | (concat "*/" | |
1025 | (cdr ignore))))))) | |
1026 | grep-find-ignored-directories | |
4803595d | 1027 | " -o -path ") |
7ae62430 JL |
1028 | " " |
1029 | (shell-quote-argument ")") | |
1030 | " -prune -o ")) | |
1031 | (and grep-find-ignored-files | |
49a2697c WJ |
1032 | (concat (shell-quote-argument "!") " -type d " |
1033 | (shell-quote-argument "(") | |
7ae62430 JL |
1034 | ;; we should use shell-quote-argument here |
1035 | " -name " | |
1036 | (mapconcat | |
1037 | #'(lambda (ignore) | |
1038 | (cond ((stringp ignore) | |
1039 | (shell-quote-argument ignore)) | |
1040 | ((consp ignore) | |
1041 | (and (funcall (car ignore) dir) | |
1042 | (shell-quote-argument | |
1043 | (cdr ignore)))))) | |
1044 | grep-find-ignored-files | |
4803595d | 1045 | " -o -name ") |
84d797c9 KS |
1046 | " " |
1047 | (shell-quote-argument ")") | |
7ae62430 | 1048 | " -prune -o ")))))) |
0acfb7ce | 1049 | (when command |
32a2cf25 | 1050 | (if confirm |
0acfb7ce KS |
1051 | (setq command |
1052 | (read-from-minibuffer "Confirm: " | |
1053 | command nil nil 'grep-find-history)) | |
c3e9438b | 1054 | (add-to-history 'grep-find-history command)) |
ac8cf6e6 | 1055 | (let ((default-directory dir)) |
c0b87c3f KS |
1056 | (compilation-start command 'grep-mode)) |
1057 | ;; Set default-directory if we started rgrep in the *grep* buffer. | |
1058 | (if (eq next-error-last-buffer (current-buffer)) | |
1059 | (setq default-directory dir))))))) | |
318e2976 | 1060 | |
a601d313 | 1061 | ;;;###autoload |
f7993853 | 1062 | (defun zrgrep (regexp &optional files dir confirm template) |
a601d313 JL |
1063 | "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR. |
1064 | Like `rgrep' but uses `zgrep' for `grep-program', sets the default | |
1065 | file name to `*.gz', and sets `grep-highlight-matches' to `always'." | |
1066 | (interactive | |
ad6fc8f4 JL |
1067 | (progn |
1068 | ;; Compute standard default values. | |
a601d313 | 1069 | (grep-compute-defaults) |
ad6fc8f4 JL |
1070 | ;; Compute the default zrgrep command by running `grep-compute-defaults' |
1071 | ;; for grep program "zgrep", but not changing global values. | |
1072 | (let ((grep-program "zgrep") | |
1073 | ;; Don't change global values for variables computed | |
1074 | ;; by `grep-compute-defaults'. | |
1075 | (grep-find-template nil) | |
1076 | (grep-find-command nil) | |
1077 | (grep-host-defaults-alist nil) | |
1078 | ;; Use for `grep-read-files' | |
1079 | (grep-files-aliases '(("all" . "* .*") | |
1080 | ("gz" . "*.gz")))) | |
1081 | ;; Recompute defaults using let-bound values above. | |
1082 | (grep-compute-defaults) | |
1083 | (cond | |
1084 | ((and grep-find-command (equal current-prefix-arg '(16))) | |
1085 | (list (read-from-minibuffer "Run: " grep-find-command | |
1086 | nil nil 'grep-find-history))) | |
1087 | ((not grep-find-template) | |
1088 | (error "grep.el: No `grep-find-template' available")) | |
1089 | (t (let* ((regexp (grep-read-regexp)) | |
1090 | (files (grep-read-files regexp)) | |
1091 | (dir (read-directory-name "Base directory: " | |
1092 | nil default-directory t)) | |
1093 | (confirm (equal current-prefix-arg '(4)))) | |
1094 | (list regexp files dir confirm grep-find-template))))))) | |
a601d313 JL |
1095 | ;; Set `grep-highlight-matches' to `always' |
1096 | ;; since `zgrep' puts filters in the grep output. | |
f7993853 SM |
1097 | (let ((grep-find-template template) |
1098 | (grep-highlight-matches 'always)) | |
a601d313 JL |
1099 | (rgrep regexp files dir confirm))) |
1100 | ||
1101 | ;;;###autoload | |
1102 | (defalias 'rzgrep 'zrgrep) | |
318e2976 KS |
1103 | |
1104 | (provide 'grep) | |
1105 | ||
06626cf2 | 1106 | ;;; grep.el ends here |