2012-04-23 34 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