6

Is it possible to perform a search/isearch just in comment regions of the text?

2 Answers 2

4

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 command isearchp-thing-define-contexts, which marks such zones with a text property, and then using isearchp-property-forward.

Also:

You can hide or show code comments during Isearch, using M-; (command isearchp-toggle-hiding-comments). You can toggle ignoring comments during Isearch, using C-M-; (command isearchp-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 for comment as 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.

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

7
  • Thanks, I installed this package. After M-x isearchp-this I answered the query Thing (type) by commentand it was accepted. Then it is asked Predicate to filter search contexts: I answered by isearch-forward. The comments became invisible and I can search within non-comment regions. I asked the opposite. What option I should select in the query Thing (type) in order that only comments remain visible?
    – Name
    Commented 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 of nil to the prompt for a predicate (i.e., just hit RET without typing any input) - no predicate is needed here.
    – Drew
    Commented 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).
    – Name
    Commented 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).
    – Drew
    Commented Jul 29, 2015 at 2:08
  • 1
    This 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), and isearchp-ignore-comments-flag is non-nil, then that option is automatically set to nil (and a message tells you this). Hopefully this will help.
    – Drew
    Commented Jul 29, 2015 at 14:59
1

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.

2
  • 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 query Predicate to filter search contexts: for obtaining the answer to my specific question.
    – Name
    Commented Jul 28, 2015 at 15:11
  • @Name: See my reply. Should be OK now.
    – Drew
    Commented Jul 29, 2015 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.