In trying to learn Emacs Lisp on Emacs 25.3.1, I decided to implement a minimal version of fic-mode. Following is the code:
(defun todo/pos-in-doc-or-comment-region-p (pos)
(memq (get-char-property pos 'face)
'(font-lock-doc-face font-lock-comment-face)))
(defun todo/search-for-keyword (limit)
(let ((original-match-data nil))
(save-match-data
(while (and (null original-match-data)
(re-search-forward "TODO([^)]+?):" limit t))
(if (and (todo/pos-in-doc-or-comment-region-p (match-beginning 0))
(todo/pos-in-doc-or-comment-region-p (match-end 0)))
(setq original-match-data (match-data)))))
(when original-match-data
(set-match-data original-match-data)
(goto-char (match-end 0))
t)))
(defvar todo/keywords
'((todo/search-for-keyword . font-lock-warning-face)))
(font-lock-add-keywords nil todo/keywords 'append)
When I run the function todo/search-for-keyword
in a Elisp buffer containing the comment
;; TODO(me): Hey
the function does the right thing (finds the comment and puts cursor after the colon). However, when I put the whole code in my .emacs
, the TODO(me):
does not get colored.
The following even simpler version does work but not in comments (which is the whole point of this exercise):
(defvar todo/keywords
'(("\\(TODO(\\)\\([^)]+?\\)\\():\\)" .
((1 font-lock-warning-face)
(2 font-lock-variable-name-face)
(3 font-lock-warning-face)))))
(font-lock-add-keywords nil todo/keywords 'append)
It seems like Emacs already decides to render the text as a comment before my keyword even comes into picture. How can I override Emacs so that parts of comments get highlighted in different way according to my rules?
PS: Here is the documentation for Search-based fontification.
font-lock-add-keywords
without a mode as argument is a bad idea because it will only work for whatever is the currently active mode. It's better to put your code into a function, then add it to the respective mode hooks you're interested in.