comparison lisp/progmodes/grep.el @ 106243:20e6e5b85a34

Search recursively in gzipped files. (Bug#4982) (grep-highlight-matches): Add new options `always' and `auto'. Doc fix. (grep-process-setup): Check `grep-highlight-matches' for `auto-detect' to determine the need to compute grep defaults. Move Windows/DOS specific --colors settings handling to `grep-compute-defaults'. Check `grep-highlight-matches' to get the value of "--color=". (grep-compute-defaults): Compute `grep-highlight-matches' when it has the value `auto-detect'. Move Windows/DOS specific settings from `grep-process-setup'. (zrgrep): New command with alias `rzgrep'.
author Juri Linkov <juri@jurta.org>
date Wed, 25 Nov 2009 17:23:45 +0000
parents f58be5f9ad87
children 67d8bc783433
comparison
equal deleted inserted replaced
106242:fb2730a2950b 106243:20e6e5b85a34
67 integer) 67 integer)
68 :version "22.1" 68 :version "22.1"
69 :group 'grep) 69 :group 'grep)
70 70
71 (defcustom grep-highlight-matches 'auto-detect 71 (defcustom grep-highlight-matches 'auto-detect
72 "If t, use special markers to highlight grep matches. 72 "Use special markers to highlight grep matches.
73 73
74 Some grep programs are able to surround matches with special 74 Some grep programs are able to surround matches with special
75 markers in grep output. Such markers can be used to highlight 75 markers in grep output. Such markers can be used to highlight
76 matches in grep mode. 76 matches in grep mode.
77 77
78 This option sets the environment variable GREP_COLOR to specify 78 This option sets the environment variable GREP_COLORS to specify
79 markers for highlighting and GREP_OPTIONS to add the --color 79 markers for highlighting and GREP_OPTIONS to add the --color
80 option in front of any explicit grep options before starting 80 option in front of any explicit grep options before starting
81 the grep. 81 the grep.
82 82
83 When this option is `auto', grep uses `--color=auto' to highlight
84 matches only when it outputs to a terminal (when `grep' is the last
85 command in the pipe), thus avoiding the use of any potentially-harmful
86 escape sequences when standard output goes to a file or pipe.
87
88 To make grep highlight matches even into a pipe, you need the option
89 `always' that forces grep to use `--color=always' to unconditionally
90 output escape sequences.
91
83 In interactive usage, the actual value of this variable is set up 92 In interactive usage, the actual value of this variable is set up
84 by `grep-compute-defaults'; to change the default value, use 93 by `grep-compute-defaults' when the default value is `auto-detect'.
85 Customize or call the function `grep-apply-setting'." 94 To change the default value, use Customize or call the function
95 `grep-apply-setting'."
86 :type '(choice (const :tag "Do not highlight matches with grep markers" nil) 96 :type '(choice (const :tag "Do not highlight matches with grep markers" nil)
87 (const :tag "Highlight matches with grep markers" t) 97 (const :tag "Highlight matches with grep markers" t)
98 (const :tag "Use --color=always" always)
99 (const :tag "Use --color=auto" auto)
88 (other :tag "Not Set" auto-detect)) 100 (other :tag "Not Set" auto-detect))
89 :set 'grep-apply-setting 101 :set 'grep-apply-setting
90 :version "22.1" 102 :version "22.1"
91 :group 'grep) 103 :group 'grep)
92 104
440 452
441 ;;;###autoload 453 ;;;###autoload
442 (defun grep-process-setup () 454 (defun grep-process-setup ()
443 "Setup compilation variables and buffer for `grep'. 455 "Setup compilation variables and buffer for `grep'.
444 Set up `compilation-exit-message-function' and run `grep-setup-hook'." 456 Set up `compilation-exit-message-function' and run `grep-setup-hook'."
445 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t)) 457 (when (eq grep-highlight-matches 'auto-detect)
446 (grep-compute-defaults)) 458 (grep-compute-defaults))
447 (when (eq grep-highlight-matches t) 459 (unless (or (eq grep-highlight-matches 'auto-detect)
460 (null grep-highlight-matches))
448 ;; `setenv' modifies `process-environment' let-bound in `compilation-start' 461 ;; `setenv' modifies `process-environment' let-bound in `compilation-start'
449 ;; Any TERM except "dumb" allows GNU grep to use `--color=auto' 462 ;; Any TERM except "dumb" allows GNU grep to use `--color=auto'
450 (setenv "TERM" "emacs-grep") 463 (setenv "TERM" "emacs-grep")
451 ;; `--color=auto' emits escape sequences on a tty rather than on a pipe,
452 ;; thus allowing to use multiple grep filters on the command line
453 ;; and to output escape sequences only on the final grep output
454 (setenv "GREP_OPTIONS" 464 (setenv "GREP_OPTIONS"
455 (concat (getenv "GREP_OPTIONS") 465 (concat (getenv "GREP_OPTIONS")
456 ;; Windows and DOS pipes fail `isatty' detection in Grep. 466 " --color=" (if (eq grep-highlight-matches 'always)
457 " --color=" (if (memq system-type '(windows-nt ms-dos))
458 "always" "auto"))) 467 "always" "auto")))
459 ;; GREP_COLOR is used in GNU grep 2.5.1, but deprecated in later versions 468 ;; GREP_COLOR is used in GNU grep 2.5.1, but deprecated in later versions
460 (setenv "GREP_COLOR" "01;31") 469 (setenv "GREP_COLOR" "01;31")
461 ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions 470 ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions
462 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne")) 471 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne"))
577 find-program gcmd null-device 586 find-program gcmd null-device
578 (shell-quote-argument ";"))) 587 (shell-quote-argument ";")))
579 (t 588 (t
580 (format "%s . <X> -type f <F> -print | %s %s" 589 (format "%s . <X> -type f <F> -print | %s %s"
581 find-program xargs-program gcmd)))))))) 590 find-program xargs-program gcmd))))))))
582 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t)) 591 (when (eq grep-highlight-matches 'auto-detect)
583 (setq grep-highlight-matches 592 (setq grep-highlight-matches
584 (with-temp-buffer 593 (with-temp-buffer
585 (and (grep-probe grep-program '(nil t nil "--help")) 594 (and (grep-probe grep-program '(nil t nil "--help"))
586 (progn 595 (progn
587 (goto-char (point-min)) 596 (goto-char (point-min))
588 (search-forward "--color" nil t)) 597 (search-forward "--color" nil t))
589 t)))) 598 ;; Windows and DOS pipes fail `isatty' detection in Grep.
599 (if (memq system-type '(windows-nt ms-dos))
600 'always 'auto)))))
590 601
591 ;; Save defaults for this host. 602 ;; Save defaults for this host.
592 (setq grep-host-defaults-alist 603 (setq grep-host-defaults-alist
593 (delete (assq host-id grep-host-defaults-alist) 604 (delete (assq host-id grep-host-defaults-alist)
594 grep-host-defaults-alist)) 605 grep-host-defaults-alist))
976 (compilation-start command 'grep-mode)) 987 (compilation-start command 'grep-mode))
977 ;; Set default-directory if we started rgrep in the *grep* buffer. 988 ;; Set default-directory if we started rgrep in the *grep* buffer.
978 (if (eq next-error-last-buffer (current-buffer)) 989 (if (eq next-error-last-buffer (current-buffer))
979 (setq default-directory dir))))))) 990 (setq default-directory dir)))))))
980 991
992 ;;;###autoload
993 (defun zrgrep (regexp &optional files dir confirm grep-find-template)
994 "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR.
995 Like `rgrep' but uses `zgrep' for `grep-program', sets the default
996 file name to `*.gz', and sets `grep-highlight-matches' to `always'."
997 (interactive
998 (let ((grep-program "zgrep")
999 (grep-find-template nil) ; output of `grep-compute-defaults'
1000 (grep-find-command nil)
1001 (grep-host-defaults-alist nil)
1002 (grep-files-aliases '(("*.gz" . "*.gz") ; for `grep-read-files'
1003 ("all" . "* .*"))))
1004 ;; Recompute defaults using let-bound values above.
1005 (grep-compute-defaults)
1006 (cond
1007 ((and grep-find-command (equal current-prefix-arg '(16)))
1008 (list (read-from-minibuffer "Run: " grep-find-command
1009 nil nil 'grep-find-history)))
1010 ((not grep-find-template)
1011 (error "grep.el: No `grep-find-template' available"))
1012 (t (let* ((regexp (grep-read-regexp))
1013 (files (grep-read-files regexp))
1014 (dir (read-directory-name "Base directory: "
1015 nil default-directory t))
1016 (confirm (equal current-prefix-arg '(4))))
1017 (list regexp files dir confirm grep-find-template))))))
1018 ;; Set `grep-highlight-matches' to `always'
1019 ;; since `zgrep' puts filters in the grep output.
1020 (let ((grep-highlight-matches 'always))
1021 ;; `rgrep' uses the dynamically bound value `grep-find-template'
1022 ;; from the argument `grep-find-template' whose value is computed
1023 ;; in the `interactive' spec.
1024 (rgrep regexp files dir confirm)))
1025
1026 ;;;###autoload
1027 (defalias 'rzgrep 'zrgrep)
981 1028
982 (provide 'grep) 1029 (provide 'grep)
983 1030
984 ;; arch-tag: 5a5b9169-a79d-4f38-9c38-f69615f39c4d 1031 ;; arch-tag: 5a5b9169-a79d-4f38-9c38-f69615f39c4d
985 ;;; grep.el ends here 1032 ;;; grep.el ends here