Emacs mode: how to indicate that a thing in square brackets should be colored

I am writing a simple emacs mode. As explicitly indicate that all things, for example. square brackets should be painted. It should be something like:

( (if thing is in square brackets) . font-lock-string-face) 
+6
source share
3 answers

I assume that you write the main mode, but font-lock-add-keywords also works in minor modes. Check your documentation with Ch f RET font-lock-add-keywords .

 (define-derived-mode my-mode text-mode "mymode" ;; some init code (font-lock-add-keywords nil '(("\\[\\(.*\\)\\]" 1 font-lock-warning-face prepend))) ;; some more init code ) 
+6
source

You will need to expand the mode in which you are going to include a new syntax rule, or simply use highlight-regexp for quick and dirty highlighting.

+2
source

So here's the summary: To add new keywords to the mode

 (font-lock-add-keywords 'emacs-lisp-mode '(("foo" . font-lock-keyword-face))) 

It can have regular expressions:

 (font-lock-add-keywords 'emacs-lisp-mode '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face))) 

(this makes the font all in square brackets of a given color)

For the current mode and current emacs session, you can simply evaluate the following:

 (font-lock-add-keywords nil '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face))) 

(note - mode is not specified here)

To make it permanent, you can add it as a mode binding:

 (add-hook 'bk-grmx-mode-hook (lambda () (font-lock-add-keywords nil '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face))) ) ) 

You can also add it to the mode specification:

 (define-derived-mode bk-grmx-mode fundamental-mode (setq font-lock-defaults '(bk-grmx-keyWords)) ;; the next line is added: (font-lock-add-keywords nil '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face))) (setq mode-name "bk-grmx-mode") 
+2
source

Source: https://habr.com/ru/post/897229/


All Articles