6

I'd like to isearch/search-replace/occur only in code (that is not in comments or strings).

This must be a common problem, so what packages do this already?

If no existing packages do this, I'm thinking a minor-mode where strings and comments are hidden based on fontification would do the trick. Is there one?

2

4 Answers 4

2

Icicles search gives you several ways to do this. Here are two:

  • Search "things" (e.g. defuns, sexps, xml elements, etc.), ignoring comments (option icicle-ignore-comments-flag). That is, use selected code segments as search contexts, but ignore any comments within code or code inside comments.

  • Search the complement of the comments. E.g., define the search contexts as the complement of the zones of text that are font-locked with either face font-lock-comment-face or face font-lock-comment-delimiter-face (which means search all code outside of comments).

After defining the search contexts, just type text to incrementally filter the contexts. And you can replace any matches on demand.

Sign up to request clarification or add additional context in comments.

Comments

1

Check out narrowing.

2 Comments

Whoops, i missed the word "code" from that question. Well, since narrowing is so awesome, ill shamelessly leave the answer here. Perhaps you could use narrowing and the syntax table with some elisp do do the trick.
The problem with narrowing it can only narrow the buffer to a contiguous block. It would be very nice if emacs would support multiple regions for narrowing.
0

Yes, you are right. The HideShow minor mode allows you to hide/show block of text, in particular multiline comments.

The hide/show comments is not part of the standard package but on the wiki page you will find the code which does the trick.

Then isearch command does not take into account the hidden comments.

HOWEVER: replace operates on the whole buffer, including hidden blocks.

2 Comments

Tried it with emacs 23.2, hid some java code and search-replace modified the contents of the hidden block as well, so it's not really an answer to the question.
Yes, the replace operates on the whole buffer. isearch, however, does not look into hidden blocks
0

Isearch+ does what you ask (as does Icicles --- see other answer, above).

You can define the contexts that Isearch searches, using any of the following:

  • A regexp to be matched.
  • A given text or overlay property --- The search contexts are the text zones that have the property (e.g. a particular `face' value or combination of values)
  • A given Emacs THING (sexp, defun, list, string, comment, etc.) --- The search contexts are the text zones of the given THING type.

Having defined the search contexts, you can also search the complement: the non-contexts. You can toggle between searching contexts and non-contexts anytime in Isearch, using C-M-~.

When searching, by default the zones not being searched are dimmed slightly, to make the searchable areas stand out.

For context-searching with Isearch you need these two libraries:

2 Comments

Beside of the files given here, where to load the whole icicle-stuff? Could see only github.com/emacsmirror/icicles/archive/master.zip
@AndreasRöhler: There are many ways to download Icicles. They are described here. You can, for instance, get Icicles from MELPA. (But to be clear, the Isearch+ files mentioned here are independent of Icicles.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.