字符串之间的emacs主模式字体锁定(括号,引号等)

时间:2013-10-21 16:20:22

标签: emacs syntax lisp syntax-highlighting mode

我正在尝试设置一个emacs主模式,它基本上只是以不同颜色突出显示许多不同字符之间的文本。我有方括号:

(font-lock-add-keywords nil '(("\\[\\(.*\\)\\]"
                             1 font-lock-keyword-face prepend)))

但是当我尝试用其他字符替换[]时,它就会停止工作。例如,圆括号'()'执行工作:

(font-lock-add-keywords nil '(("\\(\\(.*\\)\\)"
                             1 font-lock-function-name-face prepend)))

尝试单引号,双引号或后引号等也不起作用。我对lisp语法完全不熟悉 - 我做错了什么?另外:有没有办法包括将表达括起来的字符?

1 个答案:

答案 0 :(得分:2)

你正在混合正则表达式和常规字符串。

试试这些:

;; square brackets - escape the first one so you don't get a [..] regexp
(font-lock-add-keywords nil '(("\\(\\[.*]\\)"
                         1 font-lock-keyword-face prepend)))

;; parentheses - don't escape the parentheses you want to match!
(font-lock-add-keywords nil '(("\\((.*)\\)"
                         1 font-lock-keyword-face prepend)))

;; quotes - single escape so you don't break your string:
(font-lock-add-keywords nil '(("\\(\".*\"\\)"
                         1 font-lock-keyword-face prepend)))

;; other characters - not regexps, so don't escape them:
(font-lock-add-keywords nil '(("\\('.*'\\)"
                         1 font-lock-keyword-face prepend)))
(font-lock-add-keywords nil '(("\\(<.*>\\)"
                         1 font-lock-keyword-face prepend)))