开发者

In Emacs how to operate (i.e. search) only in code regions?

开发者 https://www.devze.com 2023-04-07 08:38 出处:网络
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?

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 wo开发者_如何学Culd do the trick. Is there one?


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.


Check out narrowing.


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.


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:

  • isearch+.el
  • isearch-prop.el
0

精彩评论

暂无评论...
验证码 换一张
取 消