匹配一个反弹;匹配两个反斜杠;匹配波浪号+两个反斜杠

时间:2013-05-15 05:03:08

标签: regex emacs elisp emacs-faces

我很想知道是否可以只匹配1个反斜杠,只有2个反斜杠,还有一个波浪加上两个反斜杠。我在乳胶模式下使用Emacs,并为font-lock设置关键字。将单个反斜杠定义为关键字会对各种其他定义造成严重破坏。我想要一个反斜杠是红色的;两个反斜杠是蓝色的;并且波浪号+两个反斜杠是绿色的。我不认为波浪号会造成问题,但我希望它本身就是红色的。我有font-lock-add-keywords格式,但不是这种情况的特殊正则表达式。这类似于我们在之前和之后使用\ b的情况,但据我所知,这不适用于反斜杠。

~ - 红色

\ - 红色,except,当触及字母数字字符时。

\\ - 蓝色

~\\ - 绿色

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

(font-lock-add-keywords 'latex-mode '(

("~\\|\\\\" 0 lawlist-face-a prepend)

("\\\\\\\\" 0 lawlist-face-b prepend)

("~\\\\\\\\" 0 lawlist-face-c prepend)

))

在上述示例的上下文中,定义单个反斜杠会使font-latex.elauctex-11.86的预定义警告在第280-285行无效。从第四行代码中删除"\\"并不能解决问题。例如,键入\newpage不再与font-latex-warning-face相关联 - 相反,它会显示为未定义,已分配给font-latex-sedate-face

(defvar font-latex-built-in-keyword-classes
  '(("warning"
     ("nopagebreak" "pagebreak" "newpage" "clearpage" "cleardoublepage"
      "enlargethispage" "nolinebreak" "linebreak" "newline" "-" "\\*" "\\"
      "appendix" "displaybreak" "allowdisplaybreaks" "include")
     'font-latex-warning-face 1 noarg)

BUFFER EXAMPLE - latex-mode:

\newpage - 字体应为font-latex-warning-face

\newpage - 如上所述,在定义单个反斜杠时,字体错误地显示为font-latex-sedate-face


编辑 - 疑难解答 - 测试 - re-builder和LaTeX文档的屏幕截图:

\\(\\\\\\)[^a-zA-Z@]

re-builder http://www.lawlist.com/images/re-builder_01.png

test01 http://www.lawlist.com/images/test01.png

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :background "black" :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

(font-lock-add-keywords 'latex-mode
   '(("~\\|\\(\\\\\\)[^a-zA-Z@]" 0 lawlist-face-a prepend)
     ("\\\\\\\\" 0 lawlist-face-b prepend)
     ("~\\\\\\\\" 0 lawlist-face-c prepend)))

test02 http://www.lawlist.com/images/test02.png

1 个答案:

答案 0 :(得分:1)

LaTeX命令只能由字母(以及库中的@符号)组成。因此,执行以下操作以区分单个斜杠和命令的开头就足够了:

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

(font-lock-add-keywords 'latex-mode
                        '(("~" 0 lawlist-face-a prepend)
                          ("\\(\\\\\\)[^a-zA-Z@]" 1 lawlist-face-a prepend)
                          ("\\\\\\\\" 0 lawlist-face-b prepend)
                          ("~\\\\\\\\" 0 lawlist-face-c prepend)))