8

I'm looking through a very large hydrodynamics code in c which has, often, some very poor variables choices. Including a global variable named just 'g'. Similarly, there's a file with a variable named 'geom' and lots of other variables which contain the substring 'geom' (e.g. geometry, geomAL, geom_arb, etc.).

Is there any way to search for variables that exactly match a regex, instead of partially?

For example: searching for 'geom' does not match 'geomAL'. Obviously emacs doesn't a priori know where a variable starts or ends, but could this be constructed as a function for c-mode?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119

4 Answers4

8

The Emacs regular expression engine (C-M-s <regexp>) has various operands for this sort of thing, such as the word boundary \< and \> zero-width assertions. So \<geom\> would match geom alone and (depending on your mode's syntax table) perhaps also the prefix in geom_something. Try \<geom\>[^_] if you need to exclude the underscore suffix.

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Sorry folks, I edited this answer instead of my own by accident. Is there a way to cancel edits? – mvw Aug 30 '13 at 13:52
  • @mvw: It was rejected by the reviewers. You can also rollback an edit by clicking on the "edited" link and looking for the rollback link in the header of the previous edit before the unwanted one. – tripleee Aug 30 '13 at 14:04
  • I personally prefer the `\b` operand, which I find a little easier to type then, < or >. – Malabarba Aug 30 '13 at 20:38
  • @BruceConnor Thanks! That's indeed a little more pleasant – DilithiumMatrix Aug 31 '13 at 14:04
3

You can use C-u C-s \_<g\_> which will search for the symbol g using a regular expression search with symbol-boundary markers. Or in a recent enough Emacs you can do M-s _ g which will do essentially the same (M-s is the "search prefix key" in which M-s _ is isearch-forward-symbol).

Stefan
  • 27,908
  • 4
  • 53
  • 82
1

Have you tried out the Emacs TAGS system? It should be able to parse the vars out and it might offer exact lookups. See here: http://www.emacswiki.org/emacs/EmacsTags

Generate the tags table with the etags helper:

etags *.c  

Look for a tag with

M-. your-var-name
mvw
  • 5,075
  • 1
  • 28
  • 34
0

http://www.emacswiki.org/emacs/RegularExpression has pretty much everything you need if you want to use emacs regexps.

blsmfrth
  • 9
  • 1