gnu-emacs-sources
[Top][All Lists]
Advanced

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

Re: Grep with matching lines syntax highlighted [snippet]


From: Markus Triska
Subject: Re: Grep with matching lines syntax highlighted [snippet]
Date: Sat, 09 Dec 2006 21:24:34 +0100

"address@hidden" <address@hidden> writes:

> 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.

Nice idea! A slightly different and probably simpler approach is to
copy matched lines directly from their buffers, if available:


(defun grep-fontify ()
  (interactive)
  (font-lock-mode -1)
  (let ((inhibit-read-only t))
    (save-excursion
      (goto-char (point-min))
      (while (not (eobp))
        ;; naive approach, should use the regexp that compile uses
        (if (looking-at "\\(.*?\\):\\(.*?\\):\\(.*\n?\\)")
            (let* ((file (match-string-no-properties 1))
                   (line-number (string-to-number (match-string-no-properties 
2)))
                   (line (match-string-no-properties 3))
                   (buffer (get-file-buffer file)))
              (if buffer
                  (let (fetched-line)
                    (with-current-buffer buffer
                      (save-excursion
                        (goto-line line-number)
                        (let ((l (thing-at-point 'line)))
                          (if (string= l line)
                              (setq fetched-line l)))))
                    (when fetched-line
                      (re-search-forward ".*?:.*?:")
                      (kill-line)
                      (if (string= (thing-at-point 'char) "\n")
                          (delete-char 1))
                      (save-excursion (insert fetched-line)))))))
        (forward-line)))))


To be invoked in the buffer created and returned by "grep".

All the best,
Markus Triska


reply via email to

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