Ajay Tatachar
Ajay Tatachar

Reputation: 383

Font-Lock an Emacs-Lisp Regexp Group

I'm currently writing a major-mode for Emacs and am trying to figure out how to get syntax highlighting to work. My major-mode is for a lisp-like language that looks like this:

[= a 3]
[= [double x] [* x 2]]
[double a]

Basically, what I'm trying to do is write a regular expression to match all words preceded by [ but not the [ itself to highlight function calls. I've done some googling and have found that emacs does not support regexp look-behind, so how would I do this?

Upvotes: 1

Views: 533

Answers (1)

Rorschach
Rorschach

Reputation: 32446

You can specify the number of the regex group you want to highlight. So, you could construct a regex to capture the [ plus the following function name and then only highlight the function's name by specifying the first capture group, eg.

(defvar my-mode-font-lock-keywords
  '(("\\[\\s-*\\([^\][:space:]]+\\)" (1 font-lock-function-name-face))))

(define-derived-mode my-mode prog-mode "MyMode"
  (setq-local font-lock-defaults '(my-mode-font-lock-keywords)))

Upvotes: 2

Related Questions