Emacs Lisp: How to Color Comment in Major Mode
This page shows how to implement comment coloring for writing a major mode.
You are writing a major mode for a language. You want comment be syntax colored.
(For how to write a command to comment/uncomment, see: Emacs Lisp: How to Write Comment Command in Major Mode)
The typical way to syntax color comment is via emacs syntax table. Two things you need to do:
- Set syntax table for the comment characters.
- Set font-lock-defaults to non-nil.
Comment AND string will then automatically be syntax colored, when
font-lock-mode is on. (it is on by default)
Let's do a example.
Here's complete code of a major mode:
;; sample mode that does coloring of python style commment syntax (defvar xpy-mode-syntax-table nil "Syntax table for `xpy-mode'.") (setq xpy-mode-syntax-table (let ( (synTable (make-syntax-table))) ;; python style comment: “# …” (modify-syntax-entry ?# "<" synTable) (modify-syntax-entry ?\n ">" synTable) synTable)) (define-derived-mode xpy-mode prog-mode "xpy" "xpy-mode is a major mode for editing language xpy." (setq font-lock-defaults (list nil nil)) (set-syntax-table xpy-mode-syntax-table) ;; actually no need, because our syntax table name is “xpy-mode” + “-syntax-table”, so define-derived-mode will find it and set it )
Now, copy paste the above into a buffer then call
to evaluate the code.
Now, open a new buffer, type the following:
some "thing" # wow
xpy-mode, you see that the string and comment are colored.
How Does Syntax Coloring Works
Here's how it works.
font-lock-modeis a buffer-local minor mode. (that is, it can be on or off for each buffer, independent of each other.)
font-lock-modeis on by default for all buffers since 2007 or so.
font-lock-modeis a high-level “API” to syntax color buffer.
font-lock-modecolor text in two ways: ① by syntactic parsing based on the syntax table. This basically means, lookup the delimiter characters for string in syntax table, and color text betwen it in buffer. Same for comment. ② search by regular expressions. This is how keywords, function names, variable names, html title text, Markdown text, Org Mode text, etc, are colored.
- Syntactic fontification happens first. It finds comments and string and color them. Search-based fontification happens second. Once a text is colored, it is not changed. For example, if a text is colored as string or comment, subsequent searh by regex for coloring will skip those parts.
font-lock-modeneed 2 things to do the coloring job. ① Syntax table. ② the value of font-lock-defaults. It uses those info to go thru buffer and do syntax coloring.
- Vast majority of programing language major modes do syntax color by: ① set up the proper syntax table. (in particular, the characters for string and character for comment.) ② Set up proper value for font-lock-defaults. (then, turn on
font-lock-mode, which is on by default)
- font-lock-defaults is a buffer local variable. (buffer local variable means, each buffer has its own “copy”, may have different values, independent of each other.)
- font-lock-defaults variable is designed as a config for the purpose of syntax coloring. When
font-lock-modeis on, it will use the value of font-lock-defaults to color buffer.
- font-lock-defaults's value should be a list. If it's nil (nil is empty list), no coloring will happen, not even syntax table based one.
- First element of the list should be a list of keywords (regexes) to color. We set to nil because we don't need it here. (nil is empty list)
- Second element should be t or nil. t means don't color by syntax table. We do want to color by syntax table, namely the comments syntax (and string syntax), so we use nil (or, don't provide any argument)
Here's the appropriate syntax table setup for popular languages.
C++ Style Comments
(defvar xcpp-mode-syntax-table nil "Syntax table for `xcpp-mode'.") (setq xcpp-mode-syntax-table (let ( (synTable (make-syntax-table))) ;; C++ style comment “// …” (modify-syntax-entry ?\/ ". 12b" synTable) (modify-syntax-entry ?\n "> b" synTable) synTable)) (define-derived-mode xcpp-mode prog-mode "xcpp" "xcpp-mode is a major mode for editing language xcpp." (setq font-lock-defaults (list nil)))
Java Style Comments
(defvar xjv-mode-syntax-table nil "Syntax table for `xjv-mode'.") (setq xjv-mode-syntax-table (let ( (synTable (make-syntax-table))) ;; comment style “/* … */” (modify-syntax-entry ?\/ ". 14" synTable) (modify-syntax-entry ?* ". 23" synTable) synTable)) (define-derived-mode xjv-mode prog-mode "xjv" "xjv-mode is a major mode for editing language xjv." (setq font-lock-defaults (list nil)))
Wolfram Language Style Comments
(defvar xwl-mode-syntax-table nil "Syntax table for `xwl-mode'.") (setq xwl-mode-syntax-table (let ((synTable (make-syntax-table))) ;; Wolfram Language style comment “(* … *)” (modify-syntax-entry ?\( ". 1" synTable) (modify-syntax-entry ?\) ". 4" synTable) (modify-syntax-entry ?* ". 23" synTable) synTable)) (define-derived-mode xwl-mode prog-mode "xwl" "xwl-mode is a major mode for editing language xwl." (setq font-lock-defaults (list nil)))
Syntax Table Supports Limited Comment Syntax
Emacs's syntax table only support comment syntaxes that are used in mainstream languages.
|Start with a char to newline char.|
|Start with 2 identical chars to newline char.|
|A matching pair chars with another char.|
|Two chars used in a ad hoc way as matching pair.|
If your language's comment syntax is not one of the above, then emacs syntax table is not able to capture it.
To handle non-mainstream comment syntax, you have 2 ways:
- Use advanced features of
font-lock-modeto color comment. See (info "(elisp) Font Lock Mode")
- Write your own parser, then use text properties to color them. See (info "(elisp) Text Properties")
Now, continue to Emacs Lisp: How to Write Comment Command in Major Mode
or, buy something from Best Keyboard for Emacs