0

是否可以在 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) "//") (set (make-local-variable 'comment2-start) "||")

这样线

//测试注释:||第二个测试注释

以两种颜色呈现?

我需要在其他地方定义“comment2”吗?

谢谢!

4

1 回答 1

0

注释的着色一般由 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即使 // 出现在字符串中也会突出显示

在此处输入图像描述

如果您希望这些辅助注释的字体化“更智能”,您将需要研究更高级的字体化技术。您使用挂钩jit-lock-functions来执行此操作。在您的 jit 锁定功能中,您需要扫描 || 文本模式并根据它是否已经在注释中突出显示它,syntax-ppss假设您的语法表设置正确,您可以找到它。

于 2014-12-09T14:39:30.273 回答