emacssyntax-highlightingemacs-faces

Highlight function calls but not first in quoted list in emacs


In emacs lisp mode, I want to highlight all function calls one color, and all quoted symbols another color. For example, in (foo 1 2 'bar), foo should be highlighted color 1 and 'bar should be highlighted color 2.

I was able to do this with the code below, however, it is also highlighting the first symbol in a list when the list is quoted. For example, in '(nil a b c), nil should not be highlighted as a function call, and all items in that list should be highlighted as quoted symbols (color 2).

(defface font-lock-func-face 
    '((nil (:foreground "#6fc2ef"))
      (t (:bold t :italic t)))
  "Font Lock mode face used for function calls."
  :group 'font-lock-highlighting-faces)

(defface font-lock-quoted-face 
    '((nil (:foreground "#e1a3ee"))
      (t (:bold t :italic t)))
  "Font Lock mode face used for function calls."
  :group 'font-lock-highlighting-faces)

(font-lock-add-keywords 
 'emacs-lisp-mode
 '(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
    1 'font-lock-func-face)))

(font-lock-add-keywords
 'emacs-lisp-mode
 '(("'[-a-zA-Z_][-a-zA-Z0-9_]*\\>" 0 'font-lock-quoted-face)))

There's also an image of what I'm looking at here: https://i.sstatic.net/RGZe5.jpg


Solution

  • First part of the answer: The package lisp-extra-font-lock highlights, among else, quoted expressions.

    It also highlights:

    For example: enter image description here

    Second part of the answer:

    If you use the lisp-extra-font-lock package, your rule for highlighting functions work as you posted it, if you add it as the last rule, after you have enabled the package. That way it will not overwrite any other highlight:

    (font-lock-add-keywords 
     'emacs-lisp-mode
     '(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
        1 'font-lock-func-face))
     'append)    ;; <-- Add after all other rules