[Top][All Lists]

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

RE: Grep with matching lines syntax highlighted [snippet]

From: Drew Adams
Subject: RE: Grep with matching lines syntax highlighted [snippet]
Date: Sat, 9 Dec 2006 19:24:21 -0800

> > Nice idea! A slightly different and probably simpler approach is to
> > copy matched lines directly from their buffers, if available:
> > ...
> > To be invoked in the buffer created and returned by "grep".
> Yes, it occurred to me too, but I thought font-lock could clear the
> copied text properties later if it was triggered by something,
> that's why I used overlays instead.

Something like this is useful. I've often wanted to easily distinguish
commented lines in grep output - usually just so that I can ignore them.

I've sometimes used a grep regexp that omits the lines that start with
`comment-start' (e.g. `;') in column 1. That's OK, but it requires typing
the regexp prefix ("^[^;].*"), it only eliminates lines that start with `;'
(not, for example, with whitespace followed by a comment), and it interferes
with highlighting of the match that I really want to see (the ".*" matches
all line starts).

You present interesting implementation ideas (both approaches), but I find
that they interfere with the normal grep functionality too much, besides
relying on the files to be visited and fontified. Also, as I say, I usually
just want to omit the commented lines altogether.

Inspired by what you've done, I've taken a different approach, in the latest
grep+.el: I defined a
command, bound in buffer *grep* to `;', that removes all lines that start
with (possibly whitespace followed by) a comment. IOW, you use `M-x grep' as
usual, then hit `;' to flush the commented lines.

With a prefix arg, the command reads the regexp that recognizes the
commented lines. The default value of the regexp is given by a customizable
variable. With no prefix arg, that variable's value is used directly
(nothing is read).

By default, this regexp is ":[0-9]+: *;", which works for Lisp comments. You
can easily edit the last character (";") of the default value interactively
to, for example, "//" to give ":[0-9]+: *//" for C++. Or you can customize
the regexp variable to that string if you use grep more often for C++ than
for Lisp. The input regexp reading uses `regexp-history', so you can also
quickly retrieve a previous comment regexp.

Since you can use any regexp, this is actually more general than removing
just comments, but that is the raison d'etre for the command. The default
regexp is not fail-safe, of course, but it works well most of the time.

reply via email to

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