[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
emacs.
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 ()
(save-excursion
(goto-char my-grep-filter-pos)
(while (not (eobp))
(beginning-of-line)
;; naive approach, should use the regexp what compile uses
(if (search-forward ":" (line-end-position) t)
(let ((file (buffer-substring (line-beginning-position) (1-
(point))))
(colon (point)))
(and (search-forward ":" (line-end-position) t)
(get-file-buffer file)
(let* ((pos (string-to-int (buffer-substring colon (1-
(point)))))
(line (buffer-substring (point)
(line-end-position)))
(faces
(with-current-buffer (get-file-buffer file)
(save-excursion
(goto-line pos)
(let ((faces (my-list-prop-changes)))
(if (and faces
(equal line
(buffer-substring
(line-beginning-position)
(line-end-position))))
faces))))))
(when faces
(dolist (face faces)
(let ((overlay (make-overlay (+ (point) (second
face))
(+ (point) (third
face)))))
(overlay-put overlay 'face (first
face)))))))))
(forward-line)))
(setq my-grep-filter-pos (point)))
(defun my-grep ()
(interactive)
(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))
faces)
(while (< pos (line-end-position))
(setq pos (next-single-property-change pos 'face nil
(line-end-position)))
(if start
(push (list face
(- start (line-beginning-position))
(- pos (line-beginning-position)))
faces))
(setq face (get-text-property pos 'face))
(setq start (if face pos)))
faces))