在Emacs中定义第二个font-lock-comment-face

时间:2014-12-08 23:08:37

标签: emacs emacs-faces

是否可以在Emacs中定义第二个评论面和评论开始指示符,以便即使从第一个评论面内调用,也会显示第二个评论突出显示?

我目前有一个自定义Emacs模式,其中包含以下内容:

.emacs文件:: (set-face-attribute' font-lock-comment-face nil:foreground" gray70")

custom_mode.el :: (set(make-local-variable' comment-start)" //")

是否可以添加:

.emacs文件:: (set-face-attribute' font-lock-comment-face nil:foreground" gray70") (set-face-attribute' font-lock-comment2-face nil:foreground" forestgreen")

custom_mode.el :: (set(make-local-variable' comment-start)" //") (设置(make-local-variable' comment2-start)" ||")

这样的行

//测试评论:||第二次测试评论

以两种颜色呈现?

我是否需要定义评论2'别处?

谢谢!

1 个答案:

答案 0 :(得分:0)

着色注释通常由font-lock处理,引用模式的语法表,这只支持一个面用于注释。

但是,您可以为font-lock指定正则表达式以强制突出显示您想要的任何颜色,这样我们就可以指定匹配“//”后跟文本的正则表达式,后跟“||”后跟更多文字,并使用font-lock突出显示“||”以及以下文字。

以下是主要模式的基本实现。

;; your custom face to color secondary comments blue
(defface my-second-comment-face '((t (:foreground "blue"))) "its a face")

(define-derived-mode testy-mode fundamental-mode
  "test"
  (modify-syntax-entry ?/ "<1")
  (modify-syntax-entry ?/ "<2")
  (modify-syntax-entry 10 ">")
  (font-lock-add-keywords
   'testy-mode
   ;; this highlights || followed by anything to the end of the line as long as there is
   ;; a // before it on the same line.
   ;; the 2 means only highlight the second regexp group, and the t means highlight it
   ;; even if it has been highlighted already, which is should have been.
   '(("\\(//.*\\)\\(||.*$\\)" 2 'my-second-comment-face t))))

您应该注意,这是一个不能处理所有情况的基本解决方案。正常情况如

foobar // comment || more comments

将被正确处理,但类似

foobar "string//string" || text 

将无法正常工作,即使//出现在字符串

中,|| text也会突出显示

enter image description here

如果你想要使这些次要评论更加“聪明”,你需要研究更为先进的技巧。您可以在jit-lock-functions中使用钩子来执行此操作。在你的jit lock函数中,你需要扫描||文本模式并根据它是否已经在注释中突出显示它,你可以使用syntax-ppss找到,假设你的语法表设置正确