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, 2007 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 54Some grep programs are able to surround matches with special 55markers in grep output. Such markers can be used to highlight 56matches in grep mode. 57 58This option sets the environment variable GREP_COLOR to specify 59markers for highlighting and GREP_OPTIONS to add the --color 60option in front of any explicit grep options before starting 61the grep. 62 63The default value of this variable is set up by `grep-compute-defaults'; 64call 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 74Setting it causes the grep commands to put point at the end of their 75output window so that the end of the output is always visible rather 76than 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]. 84If the grep program used supports an option to always include file names 85in its output (such as the `-H' option to GNU grep), it's a good idea to 86include it when specifying `grep-command'. 87 88The default value of this variable is set up by `grep-compute-defaults'; 89call 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]. 96The default value of this variable is set up by `grep-compute-defaults'; 97call that function before using this variable in your program. 98The 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. 110This is done to ensure that the output of grep includes the filename of 111any match in the case where only a single file is searched, and is not 112necessary if the grep program used supports the `-H' option. 113 114The default value of this variable is set up by `grep-compute-defaults'; 115call 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]. 124The default value of this variable is set up by `grep-compute-defaults'; 125call 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]. 132The default value of this variable is set up by `grep-compute-defaults'; 133call that function before using this variable in your program. 134The 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 ("tex" . "*.tex") 154 ("texi" . "*.texi") 155 ) 156 "*Alist of aliases for the FILES argument to `lgrep' and `rgrep'." 157 :type 'alist 158 :group 'grep) 159 160(defcustom grep-find-ignored-directories '("CVS" ".svn" "{arch}" ".hg" "_darcs" 161 ".git" ".bzr") 162 "*List of names of sub-directories which `rgrep' shall not recurse into." 163 :type '(repeat string) 164 :group 'grep) 165 166(defcustom grep-error-screen-columns nil 167 "*If non-nil, column numbers in grep hits are screen columns. 168See `compilation-error-screen-columns'" 169 :type '(choice (const :tag "Default" nil) 170 integer) 171 :version "22.1" 172 :group 'grep) 173 174;;;###autoload 175(defcustom grep-setup-hook nil 176 "List of hook functions run by `grep-process-setup' (see `run-hooks')." 177 :type 'hook 178 :group 'grep) 179 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) 185 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) 193 194 ;; Set up the menu-bar 195 (define-key map [menu-bar grep] 196 (cons "Grep" (make-sparse-keymap "Grep"))) 197 198 (define-key map [menu-bar grep compilation-kill-compilation] 199 '("Kill Grep" . kill-compilation)) 200 (define-key map [menu-bar grep compilation-separator2] 201 '("----" . nil)) 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] 211 '("----" . nil)) 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)) 218 map) 219 "Keymap for grep buffers. 220`compilation-minor-mode-map' is a cdr of this.") 221 222(defalias 'kill-grep 'kill-compilation) 223 224;;;; TODO --- refine this!! 225 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." 232;;; :type 'boolean 233;;; :group 'grep) 234 235;; override compilation-last-buffer 236(defvar grep-last-buffer nil 237 "The most recent grep buffer. 238A grep buffer becomes most recent when you select Grep mode in it. 239Notice that using \\[next-error] or \\[compile-goto-error] modifies 240`complation-last-buffer' rather than `grep-last-buffer'.") 241 242;;;###autoload 243(defvar grep-regexp-alist 244 '(("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2" 245 1 3) 246 ;; Rule to match column numbers is commented out since no known grep 247 ;; produces them 248 ;; ("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2\\(?:\\([0-9]+\\)\\(?:-\\([0-9]+\\)\\)?\\2\\)?" 249 ;; 1 3 (4 . 5)) 250 ("^\\(\\(.+?\\):\\([0-9]+\\):\\).*?\ 251\\(\033\\[01;31m\\(?:\033\\[K\\)?\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)" 252 2 3 253 ;; Calculate column positions (beg . end) of first grep match on a line 254 ((lambda () 255 (setq compilation-error-screen-columns nil) 256 (- (match-beginning 4) (match-end 1))) 257 . 258 (lambda () (- (match-end 5) (match-end 1) 259 (- (match-end 4) (match-beginning 4))))) 260 nil 1) 261 ("^Binary file \\(.+\\) matches$" 1 nil nil 0 1)) 262 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.") 263 264(defvar grep-error "grep hit" 265 "Message to print when no matches are found.") 266 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.") 271 272(defvar grep-error-face 'compilation-error 273 "Face name to use for grep error messages.") 274 275(defvar grep-match-face 'match 276 "Face name to use for grep matches.") 277 278(defvar grep-context-face 'shadow 279 "Face name to use for grep context lines.") 280 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\\)\\)$" 285 1 grep-error-face) 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) 295 (1 grep-error-face) 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)) 304 ((lambda (bound)) 305 (progn 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 312 ((lambda (bound)) 313 (replace-match "" t t nil 1)))) 314 "Additional things to highlight in grep output. 315This gets tacked on the end of the generated expressions.") 316 317;;;###autoload 318(defvar grep-program "grep" 319 "The default grep program for `grep-command' and `grep-find-command'. 320This variable's value takes effect when `grep-compute-defaults' is called.") 321 322;;;###autoload 323(defvar find-program "find" 324 "The default find program for `grep-find-command'. 325This variable's value takes effect when `grep-compute-defaults' is called.") 326 327;;;###autoload 328(defvar grep-find-use-xargs nil 329 "Non-nil means that `grep-find' uses the `xargs' utility by default. 330If `exec', use `find -exec'. 331If `gnu', use `find -print0' and `xargs -0'. 332Any other non-nil value means to use `find -print' and `xargs'. 333 334This variable's value takes effect when `grep-compute-defaults' is called.") 335 336;; History of grep commands. 337;;;###autoload 338(defvar grep-history nil) 339;;;###autoload 340(defvar grep-find-history nil) 341 342;; History of lgrep and rgrep regexp and files args. 343(defvar grep-regexp-history nil) 344(defvar grep-files-history '("ch" "el")) 345 346 347;;;###autoload 348(defun grep-process-setup () 349 "Setup compilation variables and buffer for `grep'. 350Set up `compilation-exit-message-function' and run `grep-setup-hook'." 351 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t)) 352 (grep-compute-defaults)) 353 (when (eq grep-highlight-matches t) 354 ;; Modify `process-environment' locally bound in `compilation-start' 355 (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always")) 356 ;; for GNU grep 2.5.1 357 (setenv "GREP_COLOR" "01;31") 358 ;; for GNU grep 2.5.1-cvs 359 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne")) 360 (set (make-local-variable 'compilation-exit-message-function) 361 (lambda (status code msg) 362 (if (eq status 'exit) 363 (cond ((zerop code) 364 '("finished (matches found)\n" . "matched")) 365 ((= code 1) 366 '("finished with no matches found\n" . "no match")) 367 (t 368 (cons msg code))) 369 (cons msg code)))) 370 (run-hooks 'grep-setup-hook)) 371 372(defun grep-probe (command args &optional func result) 373 (equal (condition-case nil 374 (apply (or func 'call-process) command args) 375 (error nil)) 376 (or result 0))) 377 378;;;###autoload 379(defun grep-compute-defaults () 380 (unless (or (not grep-use-null-device) (eq grep-use-null-device t)) 381 (setq grep-use-null-device 382 (with-temp-buffer 383 (let ((hello-file (expand-file-name "HELLO" data-directory))) 384 (not 385 (and (if grep-command 386 ;; `grep-command' is already set, so 387 ;; use that for testing. 388 (grep-probe grep-command 389 `(nil t nil "^English" ,hello-file) 390 #'call-process-shell-command) 391 ;; otherwise use `grep-program' 392 (grep-probe grep-program 393 `(nil t nil "-nH" "^English" ,hello-file))) 394 (progn 395 (goto-char (point-min)) 396 (looking-at 397 (concat (regexp-quote hello-file) 398 ":[0-9]+:English"))))))))) 399 (unless (and grep-command grep-find-command 400 grep-template grep-find-template) 401 (let ((grep-options 402 (concat (if grep-use-null-device "-n" "-nH") 403 (if (grep-probe grep-program 404 `(nil nil nil "-e" "foo" ,null-device) 405 nil 1) 406 " -e")))) 407 (unless grep-command 408 (setq grep-command 409 (format "%s %s " grep-program grep-options))) 410 (unless grep-template 411 (setq grep-template 412 (format "%s <C> %s <R> <F>" grep-program grep-options))) 413 (unless grep-find-use-xargs 414 (setq grep-find-use-xargs 415 (cond 416 ((and 417 (grep-probe find-program `(nil nil nil ,null-device "-print0")) 418 (grep-probe "xargs" `(nil nil nil "-0" "-e" "echo"))) 419 'gnu) 420 (t 421 'exec)))) 422 (unless grep-find-command 423 (setq grep-find-command 424 (cond ((eq grep-find-use-xargs 'gnu) 425 (format "%s . -type f -print0 | xargs -0 -e %s" 426 find-program grep-command)) 427 ((eq grep-find-use-xargs 'exec) 428 (let ((cmd0 (format "%s . -type f -exec %s" 429 find-program grep-command))) 430 (cons 431 (format "%s {} %s %s" 432 cmd0 null-device 433 (shell-quote-argument ";")) 434 (1+ (length cmd0))))) 435 (t 436 (format "%s . -type f -print | xargs %s" 437 find-program 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 ((eq grep-find-use-xargs 'exec) 446 (format "%s . <X> -type f <F> -exec %s {} %s %s" 447 find-program gcmd null-device 448 (shell-quote-argument ";"))) 449 (t 450 (format "%s . <X> -type f <F> -print | xargs %s" 451 find-program gcmd)))))))) 452 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t)) 453 (setq grep-highlight-matches 454 (with-temp-buffer 455 (and (grep-probe grep-program '(nil t nil "--help")) 456 (progn 457 (goto-char (point-min)) 458 (search-forward "--color" nil t)) 459 t))))) 460 461(defun grep-tag-default () 462 (or (and transient-mark-mode mark-active 463 (/= (point) (mark)) 464 (buffer-substring-no-properties (point) (mark))) 465 (funcall (or find-tag-default-function 466 (get major-mode 'find-tag-default-function) 467 'find-tag-default)) 468 "")) 469 470(defun grep-default-command () 471 "Compute the default grep command for C-u M-x grep to offer." 472 (let ((tag-default (shell-quote-argument (grep-tag-default))) 473 ;; This a regexp to match single shell arguments. 474 ;; Could someone please add comments explaining it? 475 (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)") 476 (grep-default (or (car grep-history) grep-command))) 477 ;; In the default command, find the arg that specifies the pattern. 478 (when (or (string-match 479 (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*" 480 sh-arg-re "\\(\\s +\\(\\S +\\)\\)?") 481 grep-default) 482 ;; If the string is not yet complete. 483 (string-match "\\(\\)\\'" grep-default)) 484 ;; Maybe we will replace the pattern with the default tag. 485 ;; But first, maybe replace the file name pattern. 486 (condition-case nil 487 (unless (or (not (stringp buffer-file-name)) 488 (when (match-beginning 2) 489 (save-match-data 490 (string-match 491 (wildcard-to-regexp 492 (file-name-nondirectory 493 (match-string 3 grep-default))) 494 (file-name-nondirectory buffer-file-name))))) 495 (setq grep-default (concat (substring grep-default 496 0 (match-beginning 2)) 497 " *." 498 (file-name-extension buffer-file-name)))) 499 ;; In case wildcard-to-regexp gets an error 500 ;; from invalid data. 501 (error nil)) 502 ;; Now replace the pattern with the default tag. 503 (replace-match tag-default t t grep-default 1)))) 504 505 506;;;###autoload 507(define-compilation-mode grep-mode "Grep" 508 "Sets `grep-last-buffer' and `compilation-window-height'." 509 (setq grep-last-buffer (current-buffer)) 510 (set (make-local-variable 'compilation-error-face) 511 grep-hit-face) 512 (set (make-local-variable 'compilation-error-regexp-alist) 513 grep-regexp-alist) 514 (set (make-local-variable 'compilation-process-setup-function) 515 'grep-process-setup) 516 (set (make-local-variable 'compilation-disable-input) t)) 517 518 519;;;###autoload 520(defun grep (command-args) 521 "Run grep, with user-specified args, and collect output in a buffer. 522While grep runs asynchronously, you can use \\[next-error] (M-x next-error), 523or \\<grep-mode-map>\\[compile-goto-error] in the grep \ 524output buffer, to go to the lines 525where grep found matches. 526 527For doing a recursive `grep', see the `rgrep' command. For running 528`grep' in a specific directory, see `lgrep'. 529 530This command uses a special history list for its COMMAND-ARGS, so you can 531easily repeat a grep command. 532 533A prefix argument says to default the argument based upon the current 534tag the cursor is over, substituting it into the last grep command 535in the grep command history (or into `grep-command' 536if that history list is empty)." 537 (interactive 538 (progn 539 (grep-compute-defaults) 540 (let ((default (grep-default-command))) 541 (list (read-from-minibuffer "Run grep (like this): " 542 (if current-prefix-arg 543 default grep-command) 544 nil nil 'grep-history 545 (if current-prefix-arg nil default)))))) 546 547 ;; Setting process-setup-function makes exit-message-function work 548 ;; even when async processes aren't supported. 549 (compilation-start (if (and grep-use-null-device null-device) 550 (concat command-args " " null-device) 551 command-args) 552 'grep-mode)) 553 554 555;;;###autoload 556(defun grep-find (command-args) 557 "Run grep via find, with user-specified args COMMAND-ARGS. 558Collect output in a buffer. 559While find runs asynchronously, you can use the \\[next-error] command 560to find the text that grep hits refer to. 561 562This command uses a special history list for its arguments, so you can 563easily repeat a find command." 564 (interactive 565 (progn 566 (grep-compute-defaults) 567 (if grep-find-command 568 (list (read-from-minibuffer "Run find (like this): " 569 grep-find-command nil nil 570 'grep-find-history)) 571 ;; No default was set 572 (read-string 573 "compile.el: No `grep-find-command' command available. Press RET.") 574 (list nil)))) 575 (when command-args 576 (let ((null-device nil)) ; see grep 577 (grep command-args)))) 578 579;;;###autoload 580(defalias 'find-grep 'grep-find) 581 582 583;; User-friendly interactive API. 584 585(defconst grep-expand-keywords 586 '(("<C>" . (and cf (isearch-no-upper-case-p regexp t) "-i")) 587 ("<D>" . dir) 588 ("<F>" . files) 589 ("<N>" . null-device) 590 ("<X>" . excl) 591 ("<R>" . (shell-quote-argument (or regexp "")))) 592 "List of substitutions performed by `grep-expand-template'. 593If car of an element matches, the cdr is evalled in to get the 594substitution string. Note dynamic scoping of variables.") 595 596(defun grep-expand-template (template &optional regexp files dir excl) 597 "Patch grep COMMAND string replacing <C>, <D>, <F>, <R>, and <X>." 598 (let ((command template) 599 (cf case-fold-search) 600 (case-fold-search nil)) 601 (dolist (kw grep-expand-keywords command) 602 (if (string-match (car kw) command) 603 (setq command 604 (replace-match 605 (or (if (symbolp (cdr kw)) 606 (symbol-value (cdr kw)) 607 (save-match-data (eval (cdr kw)))) 608 "") 609 t t command)))))) 610 611(defun grep-read-regexp () 612 "Read regexp arg for interactive grep." 613 (let ((default (grep-tag-default))) 614 (read-string 615 (concat "Search for" 616 (if (and default (> (length default) 0)) 617 (format " (default \"%s\"): " default) ": ")) 618 nil 'grep-regexp-history default))) 619 620(defun grep-read-files (regexp) 621 "Read files arg for interactive grep." 622 (let* ((bn (or (buffer-file-name) (buffer-name))) 623 (fn (and bn 624 (stringp bn) 625 (file-name-nondirectory bn))) 626 (default 627 (or (and fn 628 (let ((aliases grep-files-aliases) 629 alias) 630 (while aliases 631 (setq alias (car aliases) 632 aliases (cdr aliases)) 633 (if (string-match (wildcard-to-regexp (cdr alias)) fn) 634 (setq aliases nil) 635 (setq alias nil))) 636 (cdr alias))) 637 (and fn 638 (let ((ext (file-name-extension fn))) 639 (and ext (concat "*." ext)))) 640 (car grep-files-history) 641 (car (car grep-files-aliases)))) 642 (files (read-string 643 (concat "Search for \"" regexp 644 "\" in files" 645 (if default (concat " (default " default ")")) 646 ": ") 647 nil 'grep-files-history default))) 648 (and files 649 (or (cdr (assoc files grep-files-aliases)) 650 files)))) 651 652;;;###autoload 653(defun lgrep (regexp &optional files dir) 654 "Run grep, searching for REGEXP in FILES in directory DIR. 655The search is limited to file names matching shell pattern FILES. 656FILES may use abbreviations defined in `grep-files-aliases', e.g. 657entering `ch' is equivalent to `*.[ch]'. 658 659With \\[universal-argument] prefix, you can edit the constructed shell command line 660before it is executed. 661With two \\[universal-argument] prefixes, directly edit and run `grep-command'. 662 663Collect output in a buffer. While grep runs asynchronously, you 664can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] 665in the grep output buffer, to go to the lines where grep found matches. 666 667This command shares argument histories with \\[rgrep] and \\[grep]." 668 (interactive 669 (progn 670 (grep-compute-defaults) 671 (cond 672 ((and grep-command (equal current-prefix-arg '(16))) 673 (list (read-from-minibuffer "Run: " grep-command 674 nil nil 'grep-history) 675 nil)) 676 ((not grep-template) 677 (list nil 678 (read-string "grep.el: No `grep-template' available. Press RET."))) 679 (t (let* ((regexp (grep-read-regexp)) 680 (files (grep-read-files regexp)) 681 (dir (read-directory-name "In directory: " 682 nil default-directory t))) 683 (list regexp files dir)))))) 684 (when (and (stringp regexp) (> (length regexp) 0)) 685 (let ((command regexp)) 686 (if (null files) 687 (if (string= command grep-command) 688 (setq command nil)) 689 (setq dir (file-name-as-directory (expand-file-name dir))) 690 (setq command (grep-expand-template 691 grep-template 692 regexp 693 files)) 694 (when command 695 (if (equal current-prefix-arg '(4)) 696 (setq command 697 (read-from-minibuffer "Confirm: " 698 command nil nil 'grep-history)) 699 (add-to-history 'grep-history command)))) 700 (when command 701 (let ((default-directory dir)) 702 ;; Setting process-setup-function makes exit-message-function work 703 ;; even when async processes aren't supported. 704 (compilation-start (if (and grep-use-null-device null-device) 705 (concat command " " null-device) 706 command) 'grep-mode)) 707 (if (eq next-error-last-buffer (current-buffer)) 708 (setq default-directory dir)))))) 709 710 711 712;;;###autoload 713(defun rgrep (regexp &optional files dir) 714 "Recursively grep for REGEXP in FILES in directory tree rooted at DIR. 715The search is limited to file names matching shell pattern FILES. 716FILES may use abbreviations defined in `grep-files-aliases', e.g. 717entering `ch' is equivalent to `*.[ch]'. 718 719With \\[universal-argument] prefix, you can edit the constructed shell command line 720before it is executed. 721With two \\[universal-argument] prefixes, directly edit and run `grep-find-command'. 722 723Collect output in a buffer. While find runs asynchronously, you 724can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] 725in the grep output buffer, to go to the lines where grep found matches. 726 727This command shares argument histories with \\[lgrep] and \\[grep-find]." 728 (interactive 729 (progn 730 (grep-compute-defaults) 731 (cond 732 ((and grep-find-command (equal current-prefix-arg '(16))) 733 (list (read-from-minibuffer "Run: " grep-find-command 734 nil nil 'grep-find-history) 735 nil)) 736 ((not grep-find-template) 737 (list nil nil 738 (read-string "grep.el: No `grep-find-template' available. Press RET."))) 739 (t (let* ((regexp (grep-read-regexp)) 740 (files (grep-read-files regexp)) 741 (dir (read-directory-name "Base directory: " 742 nil default-directory t))) 743 (list regexp files dir)))))) 744 (when (and (stringp regexp) (> (length regexp) 0)) 745 (if (null files) 746 (if (not (string= regexp grep-find-command)) 747 (compilation-start regexp 'grep-mode)) 748 (setq dir (file-name-as-directory (expand-file-name dir))) 749 (let ((command (grep-expand-template 750 grep-find-template 751 regexp 752 (concat (shell-quote-argument "(") 753 " -name " 754 (mapconcat #'shell-quote-argument 755 (split-string files) 756 " -o -name ") 757 " " 758 (shell-quote-argument ")")) 759 dir 760 (and grep-find-ignored-directories 761 (concat (shell-quote-argument "(") 762 ;; we should use shell-quote-argument here 763 " -path " 764 (mapconcat #'(lambda (dir) 765 (shell-quote-argument 766 (concat "*/" dir))) 767 grep-find-ignored-directories 768 " -o -path ") 769 " " 770 (shell-quote-argument ")") 771 " -prune -o "))))) 772 (when command 773 (if current-prefix-arg 774 (setq command 775 (read-from-minibuffer "Confirm: " 776 command nil nil 'grep-find-history)) 777 (add-to-history 'grep-find-history command)) 778 (let ((default-directory dir)) 779 (compilation-start command 'grep-mode)) 780 ;; Set default-directory if we started rgrep in the *grep* buffer. 781 (if (eq next-error-last-buffer (current-buffer)) 782 (setq default-directory dir))))))) 783 784 785(provide 'grep) 786 787;; arch-tag: 5a5b9169-a79d-4f38-9c38-f69615f39c4d 788;;; grep.el ends here 789