0

In other words, are the text-part-categories which can be highlighted limited in amount and hard-wired into Emacs (keywords, functions, strings, digits, numbers, etc.) or is it possible to define any number of additional own categories like for example highlighting of emoticons or special words with different fonts/faces/decorations as any other ones?

If it can be done ... What would be the steps for defining an own category and a font face for highlighting it along with a file type for which this highlighting should work?

5
  • 1
    See font-lock-add-keywords, read the Font Lock Mode section in the Emacs manual, search this site for the tag font-lock, read a mode's implementation and see how they do it (I suggest lisp/shell.el which is relatively simple).
    – NickD
    Mar 26 at 21:24
  • I mean I am through the manuals. It seems to be not possible. It seems I can define additional/other keywords for a given highlighting face type, but not an own one. And reading free through source code understanding in detail what it does ... hmmm ... my experience with it is that the code I have seen is not well structured and the explanations it comes with not really useful. So you have yourself not yet tried to extend highlighting?
    – Claudio
    Mar 26 at 21:34
  • 1
    You don't need to examine/understand all the code: just the parts that mention font-lock.
    – NickD
    Mar 27 at 0:37
  • 1
    This is a common feature of editors, and I think there's a 99% chance that Emacs can do it, although I haven't read the relevant chapter in the manual.
    – shynur
    Mar 27 at 2:45
  • Correction: 100%. Here's a basic example.
    – NickD
    Mar 27 at 3:03

1 Answer 1

1

Yes, font lock mode can fontify arbitrary regexes. See chapter 24.6.2 Search–based Fontification in the Emacs Lisp manual. In fact, the whole of chapter 24.6 has information you will need.

3
  • I have been through the first part of this chapter already (and was confused to see "\\<foo\\>" as a regex. What for are the \\< \\> there? It is not a regex searching for 'foo'). Convinced it will highlight with the font-lock-keyword-face only. OK. Now with your hint I have read it all. Difficult to read and understand. It seems that it is possible to specify own face. But there is no code example of all what is needed to highlight own keywords for a specific file type like e.g. .py Python scripts. A short complete code example with hint where to copy/paste it to make it work is missing.
    – Claudio
    Mar 26 at 22:56
  • 1
    To quote myself, "...read a mode's implementation and see how they do it (I suggest lisp/shell.el which is relatively simple)." You are looking for examples: where do you think you will find them if not in the code that is used in Emacs?
    – NickD
    Mar 27 at 0:14
  • 1
    For the syntax of regexes, see Regular Expressions And remember, comments are not for asking additional questions.
    – NickD
    Mar 27 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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