I want to add better support for expl3 to AUCTeX. I have regular expressions that properly group according to the naming conventions for:

How can I apply faces to both

  • the match as a whole and
  • the individual groups (overriding the whole match where appropriate)?

Just a proof-of-concept using the function-regexp would be very helpful :)

rx Forms

(setq LaTeX-expl3-function-regexp
      (rx symbol-start
          (group "\\" (optional "__") (+ alpha) "_")
          (group (+ (any alpha "_")))
          (group ":" (* (any "nNpTFwcVvxofD")))

      (rx symbol-start
          (group "\\" (any "cgl") "_" (optional "_"))
          (group (or (seq (any alpha) "_") "@@_")) 
          (group (any alpha) (+ (any alpha "_")))
          (group "_" (+ alpha))

      (rx (* whitespace)
          (group (any alpha "-"))
          (+ whitespace)
          (group "." (+ (any alpha)))
          (group ":" (* (any "nNpTFwcVvxofD")))
          (+ whitespace) "="))
  • Are you doing font-locking, i.e., setting font-lock-keywords? If so, just use an OVERRIDE value of t for the group pattern, and place it after the whole-match pattern. See C-h v font-lock-keywords. – Drew Apr 2 '15 at 3:56
  • @Drew I had vague thoughts along that line as well, but I'm pitifully under-educated when it comes to font-lock-mode. Specifically, I'm not sure how to go about using a single matching regexp and supplying multiple group/face/override values. – Sean Allred Apr 2 '15 at 4:44
  • (info "(elisp) Search-based Fontification") ? – politza Apr 2 '15 at 5:31
  • Try first using two separate font-lock-keywords entries. You can worry about combining them later. The point is to make the second one override anything done by the first one. – Drew Apr 2 '15 at 14:10
  • 1
    @SeanAllred See the docstring of the font-lock-keywords variable. Use the (MATCHER HIGHLIGHT ...) format, where HIGHLIGHT stands for (SUBEXP FACENAME [OVERRIDE [LAXMATCH]]). – Malabarba Apr 2 '15 at 20:32

You can use font-lock-add-keywords in a major mode hook to add new rules. The format of the rules is described in the help text to the variable font-lock-keywords. In short, your code would look something like:

(defun my-LaTeX-expl3-hook ()
  (font-lock-add-keywords nil
                             (0 face-for-whole-match prepend)
                             (1 face-for-group-one prepend)
                             (2 face-for-group-two prepend))
                             (0 font-lock-variable-name-face prepend))
                             (0 font-lock-keyword-face prepend)))
                          t)) ;; Append
(add-hook 'latex-mode-hook 'my-LaTeX-expl3-hook)

Here, I've used 0 to highlight the entire match. If you would like to highlight only parts, supply a integer 1, 2, 3 etc. that corresponds the the parenthesis you would like to highlight. You can supply more than one such rule to each regexp.

The prepend flag indicates that this takes precedence over already present fontification. (However, unlike passing t, it leaves, for example, the background intact even if it overrides the foreground.)

Finally, if you find that you keywords doesn't work as intended, I would recommend using font-lock-studio, an interactive debugger that lets you step the font-lock machinery one step at a time, it highlights matches in the source buffer and in the regexps using a palette of background colors etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.