Search code examples
c++regexemacsfont-lock

Regular Expression to match member variables


I am trying to improve emacs c++-mode syntax highlighting and need to find a regular expression to match member variables in the code.

atom's syntax highlighting

What I want to match with the regular expression is the red keywords in the screenshot and not the blue ones.

To find function calls(blue ones), I am using the expression ([a-zA-Z_]+[a-zA-Z0-9_]*)+[(]

I want to modify this so that if there is any parentheses after the keyword, it does not match.


Solution

  • I think this should do what you want.

    (font-lock-add-keywords
     'c++-mode
     '(("\\(\\_<[a-zA-Z_][a-zA-Z0-9_]*\\_>\\)[(]" 1 font-lock-function-name-face)
       ("\\.\\(\\_<[a-zA-Z_][a-zA-Z0-9_]*\\_>\\)" 1 font-lock-variable-name-face)))
    

    I modified your first regexp slightly in an attempt to address your performance issues (see below for explanation). The second regexp just exchanges the trailing parenthesis for a leading period; we're relying on the ordering of the two regexps to handle method calls which would match both.

    As others have mentioned, your performance issues are probably due to a regexp that can match in too many ways. Anchoring with the symbol-boundary markers \_< and \_> should help. I was also troubled by the adjacent + and * groups on non-disjoint character sets, but I don't know if this was actually a problem.

    Since you don't appear to want highlighting in strings and comments, you should leave append off. If you wanted string and comment highlighting you would use prepend, while append would only be useful if you were using a face that sets a property that your string or comment faces didn't and you wanted to combine the two. To see what I'm talking about, try replacing font-lock-function-name-face with 'hi-yellow. (The quote is relevant here, since the highlight faces don't have variable aliases like the font lock faces.)