Is it possible to perform a
search/isearch just in comment regions of the text?
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 command
isearchp-thing-define-contexts, which marks such zones with a text property, and then using
You can hide or show code comments during Isearch, using
isearchp-toggle-hiding-comments). You can toggle ignoring comments during Isearch, using
And the doc string of command
isearchp-thing says this, which is pertinent for searching comments:
If user option
isearchp-ignore-comments-flagis 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 for
commentas THING search contexts, be sure to turn off ignoring of comments. You can toggle whether comments are ignored using
C-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.
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.)
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.