emacs-devel
[Top][All Lists]
Advanced

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

Re: Changes to emacs/lisp/progmodes/grep.el


From: Juri Linkov
Subject: Re: Changes to emacs/lisp/progmodes/grep.el
Date: Wed, 30 Jun 2004 08:08:59 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

address@hidden (Daniel Pfeiffer) writes:
> The interesting question is what to do with M-x grep for
> highlighting the matched string:
>
> - Is it better to delete the Escape sequences as Juri suggested and thus
> irreversibly change the buffer contents such that it can't be rehighlighted?
>
> - Or do we maintain save-to-file and editability, by making the Escape
> sequences invisible (and maybe solving the drawbacks somehow)?

Solving the drawbacks of invisible text is a separate issue.  There is
a `search-invisible' option in isearch.el, but it can't find a string
with invisible text inside it.  To discard invisible text when yanking
it's possible to implement an option like `yank-excluded-properties'.
Another problem with invisible text is that C-M-f or C-M-b on
invisible parens like ^[[01;41m signals an error "Unbalanced parentheses".

But perhaps these drawbacks are negligible for grep mode.  If not,
they might be addressed later, or we can add an option to choose
between hiding and deleting escape sequences.

Anyhow, there is a new patch below which puts an `invisible' property
on matched strings.  I tested it, and I find it acceptable even with
invisible text since I don't operate too much on the grep buffer.

Daniel, you added a new option `grep-error-screen-columns' recently.
But I don't see where it is used.  Could you tell how do you plan to
use it?  Currently I set `compilation-error-screen-columns' to nil in
`grep-regexp-alist', but perhaps I should use `grep-error-screen-columns'?

BTW, as you can see below I also changed handling of `end-mk' in
`compilation-goto-locus'.  Currently it activates the region around a
matched string.  This is very inconvenient.  I changed this to put a
highlighted overlay on a matched string just as it does now for the
whole line.

Index: lisp/progmodes/grep.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/grep.el,v
retrieving revision 1.16
diff -u -r1.16 grep.el
--- lisp/progmodes/grep.el      23 Jun 2004 23:10:33 -0000      1.16
+++ lisp/progmodes/grep.el      30 Jun 2004 03:27:24 -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.
 
@@ -227,6 +242,22 @@
   '(("^\\(.+?\\)[:( \t]+\
 \\([0-9]+\\)\\([.:]?\\)\\([0-9]+\\)?\
 \\(?:-\\(?:\\([0-9]+\\)\\3\\)?\\.?\\([0-9]+\\)?\\)?[:) \t]" 1 (2 . 5) (4 . 6))
+    
("^\\(.+?\\)[:(]+\\([0-9]+\\)\\([:)]\\).*?\\(\033\\[01;41m\\)\\(.*?\\)\\(\033\\[00m\\)"
+     1 2
+     ((lambda ()
+        (setq compilation-error-screen-columns nil)
+        (- (match-beginning 5) (match-end 3) 8))
+      .
+      (lambda () (- (match-end 5) (match-end 3) 8)))
+     nil nil
+     (4 (list 'face nil 'invisible t 'intangible t))
+     (5 (list 'face compilation-column-face))
+     (6 (list 'face nil 'invisible t 'intangible t))
+     ;; highlight other matches on the same line
+     ("\\(\033\\[01;41m\\)\\(.*?\\)\\(\033\\[00m\\)"
+      nil nil
+      (1 (list 'face nil 'invisible t 'intangible t))
+      (2 (list 'face compilation-column-face) t)
+      (3 (list 'face nil 'invisible t 'intangible t))))
     ("^Binary file \\(.+\\) matches$" 1 nil nil 1))
   "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")
 
@@ -291,6 +322,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: lisp/progmodes/compile.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/compile.el,v
retrieving revision 1.322
diff -u -r1.322 compile.el
--- lisp/progmodes/compile.el   18 Jun 2004 23:00:46 -0000      1.322
+++ lisp/progmodes/compile.el   30 Jun 2004 03:35:13 -0000
@@ -579,12 +579,17 @@
     (and end-line
         (setq end-line (match-string-no-properties end-line))
         (setq end-line (string-to-number end-line)))
-    (and col
-        (setq col (match-string-no-properties col))
-        (setq col (- (string-to-number col) compilation-first-column)))
-    (if (and end-col (setq end-col (match-string-no-properties end-col)))
-       (setq end-col (- (string-to-number end-col) compilation-first-column 
-1))
-      (if end-line (setq end-col -1)))
+    (if col
+        (if (functionp col)
+            (setq col (funcall col))
+          (and
+           (setq col (match-string-no-properties col))
+           (setq col (- (string-to-number col) compilation-first-column)))))
+    (if (and end-col (functionp end-col))
+        (setq end-col (funcall end-col))
+      (if (and end-col (setq end-col (match-string-no-properties end-col)))
+          (setq end-col (- (string-to-number end-col) compilation-first-column 
-1))
+        (if end-line (setq end-col -1))))
     (if (consp type)                   ; not a static type, check what it is.
        (setq type (or (and (car type) (match-end (car type)) 1)
                       (and (cdr type) (match-end (cdr type)) 0)
@@ -726,9 +731,9 @@
              ,@(when end-line
                  `((,end-line compilation-line-face nil t)))
 
-             ,@(when col
+             ,@(when (integerp col)
                  `((,col compilation-column-face nil t)))
-             ,@(when end-col
+             ,@(when (integerp end-col)
                  `((,end-col compilation-column-face nil t)))
 
              ,@(nthcdr 6 item)
@@ -1535,9 +1542,7 @@
   (unless (eq (goto-char mk) (point))
     (widen)
     (goto-char mk))
-  (if end-mk
-      (push-mark end-mk nil t)
-    (if mark-active (setq mark-active)))
+  (if mark-active (setq mark-active))
   ;; If hideshow got in the way of
   ;; seeing the right place, open permanently.
   (dolist (ov (overlays-at (point)))
@@ -1557,26 +1562,31 @@
                             compilation-highlight-regexp)))
     (compilation-set-window-height w)
 
-    (when (and highlight-regexp
-              (not (and end-mk transient-mark-mode)))
+    (when highlight-regexp
       (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 next-error-face))
       (with-current-buffer (marker-buffer mk)
        (save-excursion
-         (end-of-line)
+         (if end-mk (goto-char end-mk) (end-of-line))
          (let ((end (point)))
-           (beginning-of-line)
+           (if mk (goto-char mk) (beginning-of-line))
            (if (and (stringp highlight-regexp)
                     (re-search-forward highlight-regexp end t))
                (progn

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





reply via email to

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