Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs: highlighting balanced expressions (eg. LaTeX tags)

What would be a good way to get Emacs to highlight an expression that may include things like balanced brackets -- e.g. something like

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

highlight-regex works nicely for simple things, but I had real trouble writing an emacs regex to recognize line breaks, and of course it matches till the first closing bracket.

(as a secondary question: pointers to any packages that extend emacs regex syntax would be much appreciated -- I am having pretty hard time with it, and I'm fairly familiar with regexes in perl.)

Edit: For my specific purpose (LaTeX tags highlighting in an AUCTeX buffer), I was able to get this to work by customizing an AUCTeX specific variable font-latex-user-keyword-classes, that adds something like this to custom-set-variables in .emacs:

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

A more generic solution would still be nice to have though!

like image 723
laxxy Avatar asked Nov 13 '22 06:11

laxxy


1 Answers

You could use functions acting on s-expressions to work with the region you want to highlight, and use one of the solutions mentionned on this question to actually highlight it.

Here is an example :

(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)))))

EDIT : Here is an example using a similar function with Emacs' standard font locking system, as explained in the search-based fontification section of the emacs-lisp manual :

(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))))
like image 159
François Févotte Avatar answered Dec 07 '22 18:12

François Févotte