I've found I can make some comments use a different face by customizing font-lock-syntactic-face-function
.
To give some context, I'm setting some comments to use font-lock-doc-face
, in situations the major mode isn't setting it.
Currently I'm swapping in a new function, while it works using setq
seems clunky.
;; Just an example.
(defconst mycustom-comment-prefix "////")
(defvar mycustom-font-lock-syntactic-face-function-original nil)
(defun mycustom-font-lock-syntactic-face-function (state)
(cond
;; Check if this comment starts with `mycustom-comment-prefix'.
((and (nth 4 state)
(let ((p (nth 8 state)))
(string-equal
mycustom-comment-prefix
(buffer-substring-no-properties p (+ p (length mycustom-comment-prefix)))))
'font-lock-doc-face)
(t
;; Default behavior.
(funcall mycustom-font-lock-syntactic-face-function-original state))))
;; --- snip ---
;; When enabling...
(setq mycustom-font-lock-syntactic-face-function-original
font-lock-syntactic-face-function)
;; Restore when disabling...
(setq font-lock-syntactic-face-function
mycustom-font-lock-syntactic-face-function-original))
Is there a better way to handle the function override?