emacs-devel
[Top][All Lists]
Advanced

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

Re: Highlighting in grep buffer


From: Juri Linkov
Subject: Re: Highlighting in grep buffer
Date: Thu, 06 May 2004 11:55:49 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

I was thinking about the best solution and it seems that using
font-lock machinery for fontification of grep matches is the simplest
and most consistent with principles which compile.el is based on:
a new rule is added to `grep-mode-font-lock-keywords' which fontifies
the text inside the grep markers and deletes them afterwards.

I also made changes in `compilation-goto-locus' to highlight the
matching string in the source buffer.  The variable `highlight-regexp'
is set to a string found in `compilation-locus' face.  But perhaps
this is not the best way to achieve the goal.  Could someone familiar
with compile.el suggest a better method, for example, something like
setting the `message' property with calculated `col' and `end-col' on
grep markers in `grep-mode-font-lock-keywords' and using them in
`compilation-goto-locus'?

Index: emacs/lisp/progmodes/grep.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/grep.el,v
retrieving revision 1.12
diff -u -r1.12 grep.el
--- emacs/lisp/progmodes/grep.el        20 Apr 2004 21:41:50 -0000      1.12
+++ emacs/lisp/progmodes/grep.el        6 May 2004 07:33:14 -0000
@@ -64,6 +64,21 @@
   :version "21.4"
   :group 'grep)
 
+(defcustom grep-highlight-matches t
+  "*Non-nil to use grep markers to highlight matching strings.
+
+Some grep programs are able to surround matching strings with
+special markers in grep output.  Such markers can be used to
+highlight matching strings in grep mode.
+
+This option sets the environment variable GREP_COLOR to specify
+markers for highlighting and GREP_OPTIONS to place the --color
+option in front of any explicit grep options before starting
+the grep."
+  :type 'boolean
+  :version "21.4"
+  :group 'grep)
+
 (defcustom grep-scroll-output nil
   "*Non-nil to scroll the *grep* buffer window as output appears.
 
@@ -244,7 +259,12 @@
      ("^Grep \\(exited abnormally\\) with code \\([0-9]+\\).*"
       (0 '(face nil message nil help-echo nil mouse-face nil) t)
       (1 compilation-warning-face)
-      (2 compilation-line-face)))
+      (2 compilation-line-face))
+     ("\033\\[01;41m\\([^\033\n]*\\)\033\\[00m"
+      (1 'compilation-locus)
+      ((lambda (p))
+       (progn (delete-region (match-end       1) (match-end       0))
+              (delete-region (match-beginning 0) (match-beginning 1))))))
    "Additional things to highlight in grep output.
 This gets tacked on the end of the generated expressions.")
 
@@ -281,6 +301,10 @@
 (defun grep-process-setup ()
   "Setup compilation variables and buffer for `grep'.
 Set up `compilation-exit-message-function' and run `grep-setup-hook'."
+  (when grep-highlight-matches
+    ;; Modify `process-environment' locally bound in `compilation-start'
+    (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always"))
+    (setenv "GREP_COLOR" "01;41"))
   (set (make-local-variable 'compilation-exit-message-function)
        (lambda (status code msg)
         (if (eq status 'exit)

Index: emacs/lisp/progmodes/compile.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/compile.el,v
retrieving revision 1.316
diff -u -r1.316 compile.el
--- emacs/lisp/progmodes/compile.el     2 May 2004 20:45:36 -0000       1.316
+++ emacs/lisp/progmodes/compile.el     6 May 2004 07:33:16 -0000
@@ -1541,10 +1541,17 @@
         (w (or (get-buffer-window (marker-buffer msg) 'visible)
                ;; Pop up a window.
                (display-buffer (marker-buffer msg))))
+         p
         (highlight-regexp (with-current-buffer (marker-buffer msg)
                             ;; also do this while we change buffer
                             (compilation-set-window w msg)
-                            compilation-highlight-regexp)))
+                             (if (setq p (text-property-any
+                                          (point) (line-end-position)
+                                          'face 'compilation-locus))
+                                 (buffer-substring-no-properties
+                                  p (next-single-char-property-change
+                                     p 'face))
+                               compilation-highlight-regexp))))
     (compilation-set-window-height w)
 
     (when (and highlight-regexp
@@ -1552,8 +1559,9 @@
       (unless compilation-highlight-overlay
        (setq compilation-highlight-overlay
              (make-overlay (point-min) (point-min)))
-       (overlay-put compilation-highlight-overlay 'face 'region))
+       (overlay-put compilation-highlight-overlay 'face 'compilation-locus))
       (with-current-buffer (marker-buffer mk)
+        (setq p nil)
        (save-excursion
          (end-of-line)
          (let ((end (point)))
@@ -1561,11 +1569,42 @@
            (if (and (stringp highlight-regexp)
                     (re-search-forward highlight-regexp end t))
                (progn
-                 (goto-char (match-beginning 0))
-                 (move-overlay compilation-highlight-overlay (match-beginning 
0) (match-end 0)))
-             (move-overlay compilation-highlight-overlay (point) end))
-           (sit-for 0.5)
-           (delete-overlay compilation-highlight-overlay)))))))
+                 (goto-char (setq p (match-beginning 0)))
+                 (move-overlay compilation-highlight-overlay (match-beginning 
0) (match-end 0) (current-buffer)))
+             (move-overlay compilation-highlight-overlay (point) end 
(current-buffer)))
+           (sit-for 0.5)
+           (delete-overlay compilation-highlight-overlay)))
+        (if p (goto-char p))))))
+
+(defface compilation-locus
+  '((t (:inherit region)))
+  "Face used to highlight compilation locus."
+  :group 'font-lock-highlighting-faces
+  :version "21.4")

-- 
Juri Linkov
http://www.jurta.org/emacs/





reply via email to

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