[Top][All Lists]

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

Grep with matching lines syntax highlighted [snippet]

From: address@hidden
Subject: Grep with matching lines syntax highlighted [snippet]
Date: 5 Dec 2006 03:41:01 -0800
User-agent: G2/1.0

The other day I was grepping for something and there were commented
lines in the hits and it occurred to me how easy it would be to
distinguish these lines visually if the matches were syntax
highlighted. At least for those files which are already opened with

I already have open most of the files I'm working on, so I made a
simple filter for grep which does this. Tested with emacs 21.

I'm not yet sure how useful it is, but here's the source if someone's
interested. A possible improvement would be to open automatically the
matched files if they are not opened yet, so that highlighting can be
copied from them.

(require 'cl)

(add-hook 'compilation-filter-hook 'my-grep-filter)
;(remove-hook 'compilation-filter-hook 'my-grep-filter)

(defun my-grep-filter ()
    (goto-char my-grep-filter-pos)
    (while (not (eobp))
      ;; naive approach, should use the regexp what compile uses
      (if (search-forward ":" (line-end-position) t)
          (let ((file (buffer-substring (line-beginning-position) (1-
                (colon (point)))
            (and (search-forward ":" (line-end-position) t)
                 (get-file-buffer file)
                 (let* ((pos (string-to-int (buffer-substring colon (1-
                        (line (buffer-substring (point)
                         (with-current-buffer (get-file-buffer file)
                             (goto-line pos)
                             (let ((faces (my-list-prop-changes)))
                               (if (and faces
                                        (equal line

                   (when faces
                     (dolist (face faces)
                       (let ((overlay (make-overlay (+ (point) (second
                                                    (+ (point) (third
                         (overlay-put overlay 'face (first
  (setq my-grep-filter-pos (point)))

(defun my-grep ()
  (setq my-grep-filter-pos 1)
  (call-interactively 'grep))

(defun my-list-prop-changes ()
  (let* ((pos (line-beginning-position))
         (face (get-text-property pos 'face))
         (start (if face pos))
    (while (< pos (line-end-position))
      (setq pos (next-single-property-change pos 'face nil
      (if start
          (push (list face
                      (- start (line-beginning-position))
                      (- pos (line-beginning-position)))
      (setq face (get-text-property pos 'face))
      (setq start (if face pos)))

reply via email to

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