In Emacs, how do I work (i.e., search) only in areas of code?

I would like to search / search-replace / occur only in code (this is not in comments or lines).

This should be a common problem, so which packages have already done this?

If none of the existing packages does this, I think of a minor mode, when lines and comments are hidden based on markup, they will do the trick. There is one?

+7
source share
4 answers

Finding juices gives you several ways to do this. Here are two:

  • Search for "things" (for example, defun s, sexp s, xml elements, etc.) ignoring comments ( icicle-ignore-comments-flag option). That is, use the selected code segments as search contexts, but ignore any comments inside the code or code inside the comments.

  • Find the addition of comments. For example, define search contexts as a complement to text areas that are locked with a font-lock-comment-face using face font-lock-comment-face or face font-lock-comment-delimiter-face (which means finding all code outside of comments).

After defining the search contexts, simply enter the text to filter the contexts step by step. And you can replace any matches on demand.

+2
source

Yes you are right. HideShow minor mode allows you to hide / show a block of text, in particular multi-line comments.

Hide / show comments is not included in the standard package, but on the wiki page you will find the code that does the trick.

Then, the isearch command ignores hidden comments.

HOWEVER: replace works in the entire buffer, including hidden blocks.

0
source

Check out narrowing .

0
source

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

You can define the contexts that Isearch is looking for using any of the following:

  • Matching regular expression.
  • Specified text or overlay property --- Search contexts are text areas that have a property (for example, a specific face value or a combination of values).
  • This Emacs THING (sexp, defun, list, string, comment, etc.) --- Search contexts are text areas of this type of THING .

By defining search contexts, you can also search for add-ons: non-contexts. You can switch between search contexts and non-contexts at any time in Isearch using CM-~ .

In the default search, the areas that are not executed are slightly shaded so that the highlighted search areas are highlighted.

For contextual searches using Isearch, you will need these two libraries:

0
source

All Articles