【发布时间】:2020-10-07 18:11:21
【问题描述】:
我想为 emacs 编写一个主要模式,它应该对 mml(音乐宏语言)关键字进行语法突出显示。我遵循了本教程:
http://ergoemacs.org/emacs/elisp_syntax_coloring.html
这是我当前的代码 (在x-events下还有占位符,x-functions我还没调整,从教程里接过来了):
;;
;; to install this mode, put the following lines
;; (add-to-list 'load-path "~/.emacs.d/lisp/")
;; (load "mml-mode.el")
;; into your init.el file and activate it with
;; ALT+X mml-mode RET
;;
;; create the list for font-lock.
;; each category of keyword is given a particular face
(setq mml-font-lock-keywords
(let* (
;; define several category of keywords
(x-keywords '("#author" "#title" "#game" "#comment"))
(x-types '("&" "?" "/" "=" "[" "]" "^" "<" ">"))
(x-constants '("w" "t" "o" "@" "v" "y" "h" "q" "p" "n" "*" "!"))
(x-events '("@" "@@" "ooo" "oooo"))
(x-functions '("llAbs" "llAcos" "llAddToLandBanList"
"llAddToLandPassList"))
;; 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)
)))
;;;###autoload
(define-derived-mode mml-mode text-mode "mml mode"
"Major mode for editing mml (Music Macro Language)"
;; code for syntax highlighting
(setq font-lock-defaults '((mml-font-lock-keywords))))
;; add the mode to the `features' list
(provide 'mml-mode)
但是现在有两个问题:
首先,我有几个以# 开头的关键字(例如#author)。但是# 似乎不起作用,因为如果我忽略它,它就会起作用。
(x-keywords '("#author"))
不工作。
(x-keywords '("author"))
有效,但 # 没有着色。 @ 也会出现同样的问题。可能也和其他人一起工作,但我会尽量让他们一个一个地工作。
其次,一个关键字似乎至少需要两个字母。
(x-keywords '("o"))
不工作。
(x-keywords '("oo"))
有效。
但我有几个“关键字”,后面只有一个字母和两个(任意)十六进制数字(0-F)(例如o7D)
如何指定找到这些单字母关键字? (最好和数字一起,但不是必须的)。
【问题讨论】: