While there are packages that handle this, I would like to highlight a single word in source code comments, using for example the
warning face, when a function call returns
t, using the word as input.
How would this be achieved?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
First study the doc-string of
There you have
MATCHER which can be a regexp but also can be a function that is called with one arg and should act like
MATCHER function you have the chance to consider other conditions, as that one that your function that should return t.
You can use the form
(MATCHER . (SUBEXP FACENAME OVERRIDE)) for the keyword entry and append it to
font-lock-keywords with the help of
(defun my-highlight-fixme-p (s) "Example predicate function for `my-highlight-mode'." (string-equal s "FIXME")) (defvar my-highlight-regexp (regexp-opt '("FIXME" "TODO") 'words) "Regexp matching in comments.") (defvar my-highlight-predicate #'my-highlight-fixme-p "Register your predicate function here.") (defvar my-highlight-keywords '((my-highlight-highlight . ( 0 ;; subexpression 'warning ;; facename t ;; override syntactic fontification for comments ))) "One of the possible constructs of keywords in `font-lock-keywords'. The HIGHLIGHT element of the keyword should always be `my-highlight-highlight'.") (defun my-highlight-highlight (bound) "MATCHER for the font lock keyword in `my-highlight-mode'." (let (found) (while (and (setq found (re-search-forward my-highlight-regexp bound t)) (null (and (nth 4 (syntax-ppss)) ;; inside comment? (funcall my-highlight-predicate (match-string 0)))))) found)) (define-minor-mode my-highlight-mode "Highlight matches for `my-highlight-regexp' in comments if they also fulfill `my-highlight-predicate'." :lighter " κ" (if my-highlight-mode (font-lock-add-keywords nil my-highlight-keywords 'append) ;; append and override (font-lock-remove-keywords nil my-highlight-keywords)) (font-lock-flush))