all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
To: jidanni@jidanni.org
Cc: 4982@emacsbugs.donarmstrong.com
Subject: bug#4982: rgrep can't deal with .gz files
Date: Tue, 24 Nov 2009 19:04:43 +0200	[thread overview]
Message-ID: <87fx84ujec.fsf@mail.jurta.org> (raw)
In-Reply-To: <874oolxaeq.fsf@jidanni.org> (jidanni@jidanni.org's message of "Tue, 24 Nov 2009 04:18:21 +0800")

> JL> So you don't see yellow matches?
> OK, I do if I mouseover them. By the way, some ugliness now that I set
> grep-command to zgrep:
>
> gzip: stdout: Broken pipe
> ./libchewing3-data_0.3.0-2_i386.deb:Binary file (standard input) matches
>
> gzip: stdout: Broken pipe
> ./Nokia_3315_APAC_UG_En.pdf:Binary file (standard input) matches

Yes, zgrep incorrectly processes non-gzipped files.  To search
recursively in gzipped files, I created a new command `zrgrep'.
It works like `rgrep' but uses `zgrep' for `grep-program', sets the default
file name to `*.gz', and sets `grep-highlight-matches' to `always'."

Other changes in the patch below include:

1. grep-highlight-matches has two new options `always' and `auto'.
This allows `zrgrep' to override the default value with `always'.

2. Move Windows/DOS specific --colors settings handling from
`grep-process-setup' to `grep-compute-defaults'.

Index: lisp/progmodes/grep.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/progmodes/grep.el,v
retrieving revision 1.111
diff -c -r1.111 grep.el
*** lisp/progmodes/grep.el	19 Nov 2009 17:37:25 -0000	1.111
--- lisp/progmodes/grep.el	24 Nov 2009 17:04:11 -0000
***************
*** 69,90 ****
    :group 'grep)
  
  (defcustom grep-highlight-matches 'auto-detect
!   "If t, use special markers to highlight grep matches.
  
  Some grep programs are able to surround matches with special
  markers in grep output.  Such markers can be used to highlight
  matches in grep mode.
  
! This option sets the environment variable GREP_COLOR to specify
  markers for highlighting and GREP_OPTIONS to add the --color
  option in front of any explicit grep options before starting
  the grep.
  
  In interactive usage, the actual value of this variable is set up
! by `grep-compute-defaults'; to change the default value, use
! Customize or call the function `grep-apply-setting'."
    :type '(choice (const :tag "Do not highlight matches with grep markers" nil)
  		 (const :tag "Highlight matches with grep markers" t)
  		 (other :tag "Not Set" auto-detect))
    :set 'grep-apply-setting
    :version "22.1"
--- 69,102 ----
    :group 'grep)
  
  (defcustom grep-highlight-matches 'auto-detect
!   "Use special markers to highlight grep matches.
  
  Some grep programs are able to surround matches with special
  markers in grep output.  Such markers can be used to highlight
  matches in grep mode.
  
! This option sets the environment variable GREP_COLORS to specify
  markers for highlighting and GREP_OPTIONS to add the --color
  option in front of any explicit grep options before starting
  the grep.
  
+ When this option is `auto', grep uses `--color=auto' to highlight
+ matches only when it outputs to a terminal (when `grep' is the last
+ command in the pipe), thus avoiding the use of any potentially-harmful
+ escape sequences when standard output goes to a file or pipe.
+ 
+ To make grep highlight matches even into a pipe, you need the option
+ `always' that forces grep to use `--color=always' to unconditionally
+ output escape sequences.
+ 
  In interactive usage, the actual value of this variable is set up
! by `grep-compute-defaults' when the default value is `auto-detect'.
! To change the default value, use Customize or call the function
! `grep-apply-setting'."
    :type '(choice (const :tag "Do not highlight matches with grep markers" nil)
  		 (const :tag "Highlight matches with grep markers" t)
+ 		 (const :tag "Use --color=always" always)
+ 		 (const :tag "Use --color=auto" auto)
  		 (other :tag "Not Set" auto-detect))
    :set 'grep-apply-setting
    :version "22.1"
***************
*** 442,460 ****
  (defun grep-process-setup ()
    "Setup compilation variables and buffer for `grep'.
  Set up `compilation-exit-message-function' and run `grep-setup-hook'."
!   (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
      (grep-compute-defaults))
!   (when (eq grep-highlight-matches t)
      ;; `setenv' modifies `process-environment' let-bound in `compilation-start'
      ;; Any TERM except "dumb" allows GNU grep to use `--color=auto'
      (setenv "TERM" "emacs-grep")
-     ;; `--color=auto' emits escape sequences on a tty rather than on a pipe,
-     ;; thus allowing to use multiple grep filters on the command line
-     ;; and to output escape sequences only on the final grep output
      (setenv "GREP_OPTIONS"
  	    (concat (getenv "GREP_OPTIONS")
! 		    ;; Windows and DOS pipes fail `isatty' detection in Grep.
! 		    " --color=" (if (memq system-type '(windows-nt ms-dos))
  				    "always" "auto")))
      ;; GREP_COLOR is used in GNU grep 2.5.1, but deprecated in later versions
      (setenv "GREP_COLOR" "01;31")
--- 454,469 ----
  (defun grep-process-setup ()
    "Setup compilation variables and buffer for `grep'.
  Set up `compilation-exit-message-function' and run `grep-setup-hook'."
!   (when (eq grep-highlight-matches 'auto-detect)
      (grep-compute-defaults))
!   (unless (or (eq grep-highlight-matches 'auto-detect)
! 	      (null grep-highlight-matches))
      ;; `setenv' modifies `process-environment' let-bound in `compilation-start'
      ;; Any TERM except "dumb" allows GNU grep to use `--color=auto'
      (setenv "TERM" "emacs-grep")
      (setenv "GREP_OPTIONS"
  	    (concat (getenv "GREP_OPTIONS")
! 		    " --color=" (if (eq grep-highlight-matches 'always)
  				    "always" "auto")))
      ;; GREP_COLOR is used in GNU grep 2.5.1, but deprecated in later versions
      (setenv "GREP_COLOR" "01;31")
***************
*** 579,592 ****
  			(t
  			 (format "%s . <X> -type f <F> -print | %s %s"
  				 find-program xargs-program gcmd))))))))
!     (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
        (setq grep-highlight-matches
  	    (with-temp-buffer
  	      (and (grep-probe grep-program '(nil t nil "--help"))
  		   (progn
  		     (goto-char (point-min))
  		     (search-forward "--color" nil t))
! 		   t))))
  
      ;; Save defaults for this host.
      (setq grep-host-defaults-alist
--- 588,603 ----
  			(t
  			 (format "%s . <X> -type f <F> -print | %s %s"
  				 find-program xargs-program gcmd))))))))
!     (when (eq grep-highlight-matches 'auto-detect)
        (setq grep-highlight-matches
  	    (with-temp-buffer
  	      (and (grep-probe grep-program '(nil t nil "--help"))
  		   (progn
  		     (goto-char (point-min))
  		     (search-forward "--color" nil t))
! 		   ;; Windows and DOS pipes fail `isatty' detection in Grep.
! 		   (if (memq system-type '(windows-nt ms-dos))
! 		       'always 'auto)))))
  
      ;; Save defaults for this host.
      (setq grep-host-defaults-alist
***************
*** 978,983 ****
--- 993,1030 ----
  	  (if (eq next-error-last-buffer (current-buffer))
  	      (setq default-directory dir)))))))
  
+ ;;;###autoload
+ (defun zrgrep (regexp &optional files dir confirm grep-find-template)
+   "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR.
+ Like `rgrep' but uses `zgrep' for `grep-program', sets the default
+ file name to `*.gz', and sets `grep-highlight-matches' to `always'."
+   (interactive
+    (let ((grep-program "zgrep")
+ 	 (grep-find-command nil)
+ 	 (grep-find-template nil)
+ 	 (grep-host-defaults-alist nil)
+ 	 (grep-files-aliases '(("*.gz" . "*.gz") ; for `grep-read-files'
+ 			       ("all" . "* .*"))))
+      ;; Recompute defaults using let-bound values above.
+      (grep-compute-defaults)
+      (cond
+       ((and grep-find-command (equal current-prefix-arg '(16)))
+        (list (read-from-minibuffer "Run: " grep-find-command
+ 				   nil nil 'grep-find-history)))
+       ((not grep-find-template)
+        (error "grep.el: No `grep-find-template' available"))
+       (t (let* ((regexp (grep-read-regexp))
+ 		(files (grep-read-files regexp))
+ 		(dir (read-directory-name "Base directory: "
+ 					  nil default-directory t))
+ 		(confirm (equal current-prefix-arg '(4))))
+ 	   (list regexp files dir confirm grep-find-template))))))
+   ;; Set `grep-highlight-matches' to `always'
+   ;; since `zgrep' puts filters in the grep output.
+   (let ((grep-highlight-matches 'always))
+     ;; `rgrep' uses the dynamically bound value `grep-find-template'
+     ;; from the argument `grep-find-template' whose value is computed
+     ;; in the `interactive' spec.
+     (rgrep regexp files dir confirm)))
  
;;;###autoload
(defalias 'rzgrep 'zrgrep)
  
  (provide 'grep)
  
-- 
Juri Linkov
http://www.jurta.org/emacs/





  reply	other threads:[~2009-11-24 17:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87y6lu7gq6.fsf@mail.jurta.org>
2009-11-20  1:42 ` bug#4982: rgrep can't deal with .gz files jidanni
2009-11-20  9:31   ` Juri Linkov
2009-11-20 10:07     ` Thierry Volpiatto
2009-11-20 10:43     ` Kevin Rodgers
2009-11-22 10:33       ` Juri Linkov
2009-11-20 21:17     ` jidanni
2009-11-22 10:28       ` Juri Linkov
2009-11-23  1:53         ` jidanni
2009-11-23  9:58           ` Juri Linkov
2009-11-23 20:18             ` jidanni
2009-11-24 17:04               ` Juri Linkov [this message]
2009-11-23  4:26         ` Stefan Monnier
2009-11-23  9:57           ` Juri Linkov
2009-11-25 17:40   ` bug#4982: marked as done (rgrep can't deal with .gz files) Emacs bug Tracking System

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fx84ujec.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=4982@emacsbugs.donarmstrong.com \
    --cc=jidanni@jidanni.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.