Emacs语法突出显示可选零件



我正在尝试从基本模式编写派生模式。假设我有此regexp: A ((foo)bar)? B,我该如何告诉emacs使用以下面孔?

  • font-lock-keyword-face上的A
  • font-lock-warning-face上的 CC_4(但不是 bar
  • font-lock-constant-face上的CC_7

我尝试使用以下代码:

(defvar myregexp
  "\(A\) \(?:\(foo\)bar \)?\(B\)")
(setq mylang-font-lock-keywords `(
  (, myregex 1 font-lock-keyword-face)
  (, myregex 2 font-lock-warning-face)
  (, myregex 3 font-lock-constant-face)
))

,但它与字符串A B不起作用(Emacs报告缺失的捕获)。

使用带有laxMatch的subexp Highlighlters启用font-lock-mode忽略匹配中缺少的组:

(setq mylang-font-lock-keywords
      `((,myregexp (1 font-lock-keyword-face)
              (2 font-lock-warning-face nil t)
              (3 font-lock-constant-face))))

每个SubExp Highlighter的第四元素是laxmatch参数。如果t字体模式忽略此荧光笔,如果在myregexp的匹配结果中找不到第一个元素中的相应组。

有关更多信息,请参见EMACS LISP手册中的基于搜索的字体化。

指定式左右的方式,您需要在AB之间两个空间。在可选的foobar零件中拉动第二个空间,或在其之后使用*?

最新更新