在 Emacs 中添加并引用新面孔

eat*_*hil 1 emacs emacs-faces

我在 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)))
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*fan 5

font-lock-keywords 中的元素(0 sml-highlight-operator-face)并没有说“使用面 sml-highlight-operator-face 进行子匹配 0”,而是“使用计算表达式的结果sml-highlight-operator-face作为面来进行子匹配 0”。

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

顺便说一句,现在的惯例是不使用-face面孔后缀(尽管这样的后缀当然仍然用于保存面孔的变量),但我们还没有费心将面孔重命名font-lock-foo-facefont-lock-foo(尽管这将非常有助于你会看到很多字体锁定规则的混乱,(0 font-lock-foo-face)人们认为它指的是font-lock-foo-face面孔,而它指的是font-lock-foo-face变量(其值包含font-lock-foo-face面孔)。