Designating variable references in string literals in Emacs font lock mode

When I type the following code in ruby ​​Emacs mode, "# {foo}" is encrypted in a different color than the wrapper string. How to do this in my own Emacs mode? I tried to decrypt the source code of the ruby ​​mode, but could not understand it for a reasonable amount of time.

"a #{foo} a" 
+4
source share
2 answers

Finally figured it out. The answer is that the override parameter in the sort rule must be set to t, which means that the person will override the line face. See the documentation for the variable "font-lock-keywords" for more details. Here is an example:

 (define-derived-mode temp-mode fundamental-mode "Temp" "Temporary major mode." (set (make-local-variable 'font-lock-defaults) '((temp-mode-font-lock-keywords) nil nil nil nil))) (defconst temp-mode-font-lock-keywords (list (list "$[A-Za-z0-9]+" 0 font-lock-variable-name-face t))) 
+5
source

Find where ruby-mode.el sets font-lock-syntactic-keywords :

 (setq ruby-font-lock-syntactic-keywords '( ;; #{ }, #$hoge, #@foo are not comments ("\\(#\\)[{ $@ ]" 1 (1 . nil)) 

Here is some documentation for a similar font-lock-keywords variable that you should use to create the same type of fonts.

+1
source

All Articles