2012-04-23 68 views
5

如何让Emacs突出显示可能包含平衡括号等表达式的好方法 - 例如,像emacs:突出显示平衡表达式(例如,LaTeX标记)

\highlightthis{some \textit{text} here 
some more text 
done now} 

highlight-regex很好地工作了简单的事情,但我有真正的麻烦写一个emacs的正则​​表达式来识别换行符,当然它匹配直到第一个右括号。

(作为次要的问题:指针扩展emacs的正则​​表达式的语法,将不胜感激的任何包 - 我有相当困难的时间与它,我相当熟悉,在Perl的正则表达式)

编辑:对于我的特定目的(LaTeX的标签在AUCTeX缓冲高亮),我能得到这个通过定制的AUCTeX特定变量font-latex-user-keyword-classes工作,,增加了在.emacs中这样的事情custom-set-variables

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

更通用的解决方案将直到很高兴虽然!

回答

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的-口齿不清手册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)))) 
+0

不错的主意,但我还不能完成这项工作。首先,来自“区域突出”的解决方案似乎对我没有任何作用 - 即。单独执行(add-text-properties 1 10 ...)语句不会突出显示符号1到10.其次,即使我使用它,我也有点担心突出显示将保留固定在缓冲。如果这可以自动工作,比如其他语法突出显示的功能,它也会很好...... – laxxy 2012-04-24 11:53:12

+0

“我有点担心突出显示会留在缓冲区中的那个位置”事实并非如此:文本属性保留附加到他们的文本,而不是缓冲区中的特定位置。 – Francesco 2012-04-24 12:48:24

+0

为您另外两个问题,我认为[基于搜索的建立](http://www.gnu.org/software/emacs/manual/html_node/elisp/Search_002dbased-Fontification.html#Search_002dbased-Fontification)部分elisp手册应该可以帮助你。您可以特别在'font-lock-keywords'变量中添加一个'function'元素。 – Francesco 2012-04-24 12:52:33