让Emacs突出显示可能包括平衡括号之类的表达式的好方法是什么 – 例如就像是
\highlightthis{some \textit{text} here some more text done now}
highlight-regex很适合简单的事情,但我在编写emacs正则表达式以识别换行符时遇到了麻烦,当然它匹配到第一个结束括号.
(作为第二个问题:指向任何扩展emacs正则表达式语法的包的指针将非常感激 – 我很难用它,我对perl中的正则表达式非常熟悉.)
编辑:为了我的特定目的(在AUCTeX缓冲区中突出显示的LaTeX标签),我能够通过自定义AUCTeX特定变量font-latex-user-keyword-classes来实现这一点,这样就可以将这样的内容添加到自定义设置中 – .emacs中的变量:
'(font-latex-user-keyword-classes (quote (("mycommands" (("highlightthis" "{")) (:slant italic :foreground "red") command))))
虽然更通用的解决方案仍然很好!
解决方法
您可以使用作用于s表达式的函数来处理要突出显示的区域,并使用
this question中提到的其中一个解决方案来实际突出显示它.
这是一个例子:
(defun my/highlight-function () (interactive) (save-excursion (goto-char (point-min)) (search-forward "\highlightthis") (let ((end (scan-sexps (point) 1))) (add-text-properties (point) end '(comment t face highlight)))))
编辑:这是一个使用与Emacs的标准字体锁定系统类似的功能的示例,如emacs-lisp手册的search-based fontification部分所述:
(defun my/highlight-function (bound) (if (search-forward "\highlightthis" bound 'noerror) (let ((begin (match-end 0)) (end (scan-sexps (point) 1))) (set-match-data (list begin end)) t) nil)) (add-hook 'LaTeX-mode-hook (lambda () (font-lock-add-keywords nil '(my/highlight-function))))