1

I have 2 rules and the second one highlighting text disables the first...

(font-lock-add-keywords 'org-mode '(("^rtfm .*$" 0 '(:foreground "red"))))
(font-lock-add-keywords 'org-mode '(("'.*'"      0 '(:background "OrangeRed"))))

rtfm ... works
and 'this' works
but when I use them together only the second works.
rtfm whatever but then 'xx' and now only 'xx' is highlighted


This maybe sloppy, any tips are appreciated. (quote matching simplified here)

0

Try this:


(font-lock-add-keywords 'org-mode '(("^rtfm .*$" 0 '(:foreground "red"))))
(font-lock-add-keywords 'org-mode '(("'.*'"      0 '(:background "OrangeRed") 'keep)))

See the Elisp manual, node Search-based Fontification - look for keep:

(MATCHER . SUBEXP-HIGHLIGHTER)

In this kind of element, SUBEXP-HIGHLIGHTER is a list which specifies how to highlight matches found by MATCHER. It has the form:

     (SUBEXP FACESPEC [OVERRIDE [LAXMATCH]])

The CAR, SUBEXP, is an integer specifying which subexpression of the match to fontify (0 means the entire matching text). The second subelement, FACESPEC, is an expression whose value specifies the face, as described above.

The last two values in SUBEXP-HIGHLIGHTER, OVERRIDE and LAXMATCH, are optional flags. If OVERRIDE is t, this element can override existing fontification made by previous elements of font-lock-keywords. If it is keep, then each character is fontified if it has not been fontified already by some other element. If it is prepend, the face specified by FACESPEC is added to the beginning of the font-lock-face property. If it is append, the face is added to the end of the font-lock-face property.

If you want both faces used for the text that both regexps match then use prepend or append instead of keep. That will combine the OrangeRed background with the Red foreground. (It's not clear from your question just what behavior you really want.)

1
  • Permutations and combinations... emacs -q many many times. If regexps matching overlap... it does not work. Maybe I need a matching function that can go back to beginning of line (where other matches may have already occurred). Thank you for the help, the documentation for font-lock-add-keywords does refer to font-lock-keywords that documents all that (what you pasted above).
    – 9mjb
    Nov 23 '20 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.