我正在尝试从基本模式编写派生模式。假设我有此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手册中的基于搜索的字体化。
指定式左右的方式,您需要在A
和B
之间两个空间。在可选的foobar零件中拉动第二个空间,或在其之后使用*
或?
。