Is it possible to perform a search/isearch
just in comment regions of the text?
2 Answers
Yes. Use library Isearch+, specifically file isearch-prop.el
.
As it says here:
You can search the text of THINGS of various kind (sexps, lists, defuns, lines, pages, sentences, filenames, strings, comments, xml/html elements, symbols,…), using command
isearchp-thing
. This is equivalent to using commandisearchp-thing-define-contexts
, which marks such zones with a text property, and then usingisearchp-property-forward
.
Also:
You can hide or show code comments during Isearch, using
M-;
(commandisearchp-toggle-hiding-comments
). You can toggle ignoring comments during Isearch, usingC-M-;
(commandisearchp-toggle-ignoring-comments
).
And the doc string of command isearchp-thing
says this, which is pertinent for searching comments:
If user option
isearchp-ignore-comments-flag
is nil then include THINGs located within comments. Non-nil (the default value) means to ignore things inside comments for searching. In particular, this means that forcomment
as THING search contexts, be sure to turn off ignoring of comments. You can toggle whether comments are ignored usingC-M-;
during Isearch, but to see the effect you will need to invoke Isearch again.
UPDATE after your comment that you got an infinite loop:
The cause of the looping is Emacs bug #9300, which still has not been fixed. I thought it would be fixed by Emacs 24, so I had a test in the code that does the right thing if the Emacs version is 24 or later, and otherwise does the workaround thing. Unfortunately, if you use the bugged vanilla Emacs version of function bounds-of-thing-at-point
then you can have only the workaround, which is less than ideal. Anyway, the latest version of isearch-prop.el
treats all Emacs versions as bugged, giving you the workaround.
The right thing to do is, as suggested in the isearch-prop.el
file header, to use library Thing At Point + (thingatpt+.el
). It does not have the bug, and it gives you better thing-at-point behavior all around.
Library isearch-prop.el
does not strictly require library thingatpt+.el
, but I highly recommend using both.
(It took me a while to re-debug this, as I had forgotten about the Emacs bug, and the fact that the code made the assumption that the bug would be fixed by Emacs 24. I don't usually do that kind of thing, but as the bug report points out the needed (and trivial) fix, I figured that it would soon be fixed.)
-
Thanks, I installed this package. After
M-x isearchp-this
I answered the queryThing (type)
bycomment
and it was accepted. Then it is askedPredicate to filter search contexts:
I answered byisearch-forward
. The comments became invisible and I can search within non-comment regions. I asked the opposite. What option I should select in the queryThing (type)
in order that only comments remain visible?– NameCommented Jul 28, 2015 at 14:07 -
I updated the answer with a mention that you need to turn off ignoring of comments when you want to search only comments. I also clarified this in the doc string of
isearchp-thing
, in the library. Thx. Also, just answer with the default value ofnil
to the prompt for a predicate (i.e., just hitRET
without typing any input) - no predicate is needed here.– DrewCommented Jul 28, 2015 at 15:36 -
Let me report that when I hit
RET
to the prompt for a predicate, emacs get completely frozen (Emacs 24.5.1 (i686-pc-mingw32) of 2015-04-11 on LEG570).– NameCommented Jul 28, 2015 at 19:15 -
OK, thanks for the report. Please try the latest version, which fixes this. I edited the post to explain more about this. The problem is Emacs bug #9300, which still has not been fixed (or even responded to).– DrewCommented Jul 29, 2015 at 2:08
-
1This was kind of a gotcha. I've updated the command now so that if you pick
comment
as the THING, and you are not searching the complement (i.e., non-comments), andisearchp-ignore-comments-flag
is non-nil
, then that option is automatically set tonil
(and a message tells you this). Hopefully this will help.– DrewCommented Jul 29, 2015 at 14:59
Using a library with this feature as @Drew suggested is probably the best way to go.
Otherwise -- depending on the comment syntax -- you might be able to use isearch-forward-regex
and search using a regular expression that includes leading comment characters. For example in lisp you could do a regex search for ;.*foo
to find lines that include "foo" in a comment.
-
Thanks, your suggestion to use regex is a partial answer. For using the libraries
isearch+/isearch-prop
I cannot yet figure out how to answer the queryPredicate to filter search contexts:
for obtaining the answer to my specific question.– NameCommented Jul 28, 2015 at 15:11 -