Following code will visually replace "hello world" with "HW" by passing a progn form to font lock keywords.
(font-lock-add-keywords
nil '(("\\(hello world\\)"
(0 (progn (put-text-property (match-beginning 1) (match-end 1)
'display "HW")
nil)))))
I've look into C-h v font-lock-keywords
to see if this is a documented feature of font lock. The hello world element seemed to be of this form:
(MATCHER HIGHLIGHT ...)
which would mean that (0 ...)
is HIGHLIGHT and the doc says
HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
and
MATCH-HIGHLIGHT should be of the form:
(SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
So I guessed 0 was SUBEXP and (progn ...)
was FACENAME. But if (progn ..)
were a valid FACENAME, the following code would work but it doesn't work.
;; (MATCHER . FACENAME)
(font-lock-add-keywords
nil '(("goodbye lenin"
. (progn (put-text-property (match-beginning 1) (match-end 1)
'display "GL")
nil))))
That brings me to the question of how the first code works and whether it is relying on an undocumented feature.
Update:
Side note: simpler way of visual replacement without font lock errors
(font-lock-add-keywords
nil '(("my llama"
(0 (progn (put-text-property (match-beginning 0) (match-end 0)
'display "ML")
nil)))))
("\\(goodbye lenin\\)" . (progn..))
example works but somehow it generatesError during redisplay: (jit-lock-function 1) signaled (invalid-function progn)
in Messages, and when I type(defun goodbye lenin)
, the word defun stops getting highlighted with font-lock-keyword-face as soon as I finish the closing paren. – Brenneman