I am writing a mode to color a few common syntax in a symbolic language called FORM. I am a new user of emacs and I took the help of this website to write down the following code. I modified the original code changing the syntax that I want to be colored. But keywords that have special characters (hash # / dot . /underscore _ ), such as "#define", "...", "(dot)sort", "(dot)end", "d_" etc, do not show color.
(setq form-font-lock-keywords
(let* (
;; define several category of keywords
(x-keywords '("S" "s" "V" "v" "I" "i" "F" "f" "CF" "cf"))
(x-types '("#define" "..."))
(x-constants '(".sort" ".end" ".store" ".global" ".clear" ))
(x-events '("break" "bracket" ))
(x-functions '("d_" "g_" "g5_" "g6_" "g7_" "gi_"))
;; generate regex string for each category of keywords
(x-keywords-regexp (regexp-opt x-keywords 'words))
(x-types-regexp (regexp-opt x-types 'words))
(x-constants-regexp (regexp-opt x-constants 'words))
(x-events-regexp (regexp-opt x-events 'words))
(x-functions-regexp (regexp-opt x-functions 'words)))
`(
(,x-types-regexp . font-lock-type-face)
(,x-constants-regexp . font-lock-constant-face)
(,x-events-regexp . font-lock-builtin-face)
(,x-functions-regexp . font-lock-function-name-face)
(,x-keywords-regexp . font-lock-keyword-face)
;; note: order above matters, because once colored, that part won't change.
;; in general, put longer words first
)))
(define-generic-mode form-mode nil "form mode"
"Major mode for editing FORM symbolic tool."
;; code for syntax highlighting
(setq font-lock-defaults '((form-font-lock-keywords))))
(provide 'form-mode)
To my understanding, what I did was:
I have defined categories of the keywords --> x-name of the category
Generated regular expression - regex (I am not sure of its function in this context) for each category
assigned regex to a font that has unique color
set nil as parent mode
At first I thought that my parent mode was not accurate to show color for #/./_ symbols, but now I think that's not the main issue. I think the problem comes from 1-3 options. Please provide your valuable suggestion on this problem. Many thanks!