emacs:突出显示平衡表达式(例如,LaTeX标签)

时间:2012-04-23 16:47:55

标签: regex emacs latex elisp auctex

让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中的custom-set-variables

'(font-latex-user-keyword-classes (quote (("mycommands" (("highlightthis" "{")) (:slant italic :foreground "red") command))))

虽然更通用的解决方案仍然很好!

1 个答案:

答案 0 :(得分:1)

您可以使用作用于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))))