Yes it is possible to add the fontification of a minor mode to the existing fontification of the major mode.
You add font lock keywords with
font-lock-add-keywords and remove them with
There are many possible structures for the keywords.
See the doc for the variable
font-lock-keywords for the full list.
One such structure is:
(MATCHER SUBEXP FACENAME [OVERRIDE])
MATCHER can be a regexp or a function matching the keyword; The function is called with point at the beginning of the region to be fontified and it receives the end of that region as argument. The function should set the match-data like
SUBEXP is the number of the sub-expression of
MATCHER that should be fontified
FACENAME can be an expression returning the face name to use or a list
(face FACE PROP1 VAL1 PROP2 VAL2 ...); The second form is handy to set text properties on the keyword.
OVERRIDE Overrides all previous fontification if set to t. Other allowed values are
append with the obvious meaning.
Normally, you would use the
OVERRIDE flag with value t,
append to overwrite or combine the fontification of the major and the minor mode.
If the rule for the fontification of the match is more complicated you can also set the face and the text properties directly in
MATCHER. In this case add
MATCHER as follows to
(font-lock-add-keywords nil '((MATCHER)))
That technique is often used by org-mode. See, e.g.,
The following Elisp code gives an example for a minor mode that adds
display text properties to org tables for a fancier presentation:
(defun org+-fancy-table-lines (limit)
"Draw org tables with fancy lines.
This is a face-lock matcher with arg LIMIT."
(let ((p (point))
(while (re-search-forward "^[[:space:]]*|" limit t)
(let* ((hline-p (looking-at org-table-hline-regexp))
(re (if hline-p
(line-limit (min limit (line-end-position))))
(when (< (point) p) (goto-char p))
(while (re-search-forward re line-limit 'noError)
(let ((b (1- (point)))
(setq found t)
(setq e (point))
(put-text-property b e 'display (make-string (- e b) ?─)))
(put-text-property b e 'display "┼"))
(if (eq (char-before b) ?-)
(put-text-property b e 'display "┤")
(put-text-property b e 'display "├"))
(put-text-property b e 'display "│"))))))))
(defconst org+-fancy-table-keywords '((org+-fancy-table-lines))
"Font lock keywords for `org+-fancy-table-mode'.")
"Display tables with fancy lines."
:lighter " ─"
(font-lock-add-keywords nil org+-fancy-table-keywords 'at-end)
(cl-pushnew 'display font-lock-extra-managed-props))
(font-lock-remove-keywords nil org+-fancy-table-keywords))
Note, that here we add
display to the
That gives font-lock the control over those text properties.
It is a quite drastic measure and may interfere with other commands directly using the