emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Highlighting in grep buffer


From: Drew Adams
Subject: RE: Highlighting in grep buffer
Date: Mon, 5 Apr 2004 16:05:35 -0700

FYI - This was part of the Emacs 20 code I sent previously (files
compile-.el and compile+.el).

My code treated the common-case grep patterns using simple regexps (see #3,
below).

My code did *not* try to:

 - treat possible occurrences of grep as part of another command sequence
(e.g. piped)
 - treat grep options: -i for case insensitivity, -E etc. for other grep
flavors etc.
 - treat environment variable substitution

It just grabbed the regexp (quoted or not) from an Emacs M-x grep command,
and used it for highlighting.

Y'all decided that "this is pretty much the same feature that has now been
integrated into Emacs".  I don't have Emacs 21, so I took your word for it;
I know Emacs 21 has lots of great stuff.

I personally find it saves time and eye fatigue to have the search pattern
stand out from the context. The feature is simple, but helpful. If you turn
on Google highlighting, then you will appreciate this same feature for grep.

>From my code:

1. The grep pattern is used here to highlight occurrences in the *grep*
buffer (in function compilation-mode-font-lock-keywords):

   ;; NOTE: No account is taken here of case-insensitivity options to grep
   ;; (e.g. `-i'). This is not generally possible, as different grep's may
use
   ;; different options. Here, only the literal `grep-pattern' string is
   ;; highlighted.
   (and grep-pattern ; Does nothing if this is not a grep command.
        (list
         (list (concat "\\(" (regexp-quote grep-pattern) "\\)")
               1 grep-regexp-face)))

2. The grep pattern is used here to highlight the particulat occurrence in
the line visited by compilation-goto-locus (in function
compilation-goto-locus):

   (set-window-point w (car next-error))
   (set-window-start w (car next-error))
   (highlight-regexp-region (save-excursion (beginning-of-line) (point))
                            (save-excursion (end-of-line) (point))
                            grep-pattern grep-regexp-face)

3. The grep pattern is saved here (in command grep):

(cond (;; Quoted pattern (either "..." or '...')
       (string-match
        (concat
         grep-program
         "[ \t]*\\(-[a-zA-Z]+\\s-+\\)*[ \t]*\\('[^']+'\\|\"[^\"]+\"\\)") ;"
        command-args)
       (setq grep-pattern
             (substring command-args
                        (1+ (match-beginning 2)) (1- (match-end 2)))))
      (;; Unquoted pattern.
       (string-match
        (concat grep-program
                "[ \t]*\\(-[a-zA-Z]+\\s-+\\)*[ \t]*\\([^ \n\t'\"]+\\)") ; "
        command-args)
       (setq grep-pattern
             (substring command-args (match-beginning 2) (match-end 2))))
      (t;; Bad pattern.
       (setq grep-pattern nil))

4. Function compile resets the grep pattern (from last grep) to nil.

HTH,

   Drew

-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf Of
Richard Stallman
Sent: Monday, April 05, 2004 3:03 PM
To: address@hidden
Subject: Highlighting in grep buffer


It would be useful to highlight the string in each line that matched
the specified regexp.  It may be nontrivial to parse the command line
that was run, but it would be quite useful, even if it only works
in the most common cases.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]