在 Emacs 中添加和引用新面孔

Add and reference new face in Emacs

我在Emacs 中定义了一个新面孔,但是着色没有生效。这是~/.emacs中的面和模式定义:

(defface sml-highlight-operator-face
  '((t (:foreground "red")))
  "SML operator highlighting"
  :group 'basic-faces)

(defvar sml-font-lock-keywords
   ((,(regexp-opt '("fun" "fn" "let" "val" "datatype" "type" "case" "of" "end" "structure" "struct" "signature" "sig"))
     (0 font-lock-keyword-face))
    ("[][=|><-+;,{}():]" (0 sml-highlight-operator-face))))

;;;###autoload
(define-derived-mode sml-mode prog-mode "SML"
  "SML major mode."
  (set (make-local-variable 'comment-start) "(* ")
  (set (make-local-variable 'comment-end) " *)")
  (set (make-local-variable 'font-lock-defaults)
       '(sml-font-lock-keywords)))

但是,当我使用 font-lock-builtin-face 而不是 sml-highlight-operator-face 时,这些字符会突出显示(尽管使用了我不想要的颜色)。我做错了什么?

你的 font-lock-keywords 中的元素 (0 sml-highlight-operator-face) 不是 "use face sml-highlight-operator-face for sub-match 0" 而是 "use the result of evaluating the expression sml-highlight-operator-face as the face to put on sub-match 0".

IOW,你需要使用(0 'sml-highlight-operator-face)

顺便说一句,现在的惯例是不对面孔使用 -face 后缀(当然这样的后缀仍然用于 变量 保持面孔),虽然我们还没有费心将 font-lock-foo-face 面孔重命名为 font-lock-foo (尽管这会极大地帮助您在许多字体锁定规则中看到诸如 (0 font-lock-foo-face) 人们认为它指的是 font-lock-foo-face 面孔,而它指的是 font-lock-foo-face 变量(其值包含 font-lock-foo-face 面孔)。