views:

83

answers:

3

Hello,

I'm using Emacs/AucTeX for editing LaTeX files. In some of my LaTeX files, I have defined some "file-specific" command (e.g. \todo{...} in one file, \compute{...} in another, and so on), so I can keep track of what open problems I have in my documents.

The Emacs command highlight-regexp does quite a good joob in highlightning all occurences of \todo{...}s resp. \compute{...}s. But up to now I have to execute the highlightning manually every time I open the corresponding file.

Is there a way of telling Emacs/AucTeX to invoke specific commands when opening a particular file? I would like to define these commands inside the corresponding file, so I can easily adjust it for different files (maybe included in local variables).

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "master"
%%%
%%% here I would like to have something like:
%%% execute highlight-regexp for specific arguments
%%%
%%% End: 
+2  A: 

You could use a hook in combination to local vars. For example, you could add a local variable dohighlight or something and then write a function like this:

(defun condhighl ()
 (when (boundp 'dohighlight)
 (highlight-regexp "regexp")))

and then add that function as a hook for AucTex

(add-hook 'tex-mode-hook 'condhighl)

You can then add the dohighlight var in your local vars to buffers you want to highlight the code in.

Joshua Smith
That's a nice, elegant solution.I do wonder if there's a minor mode for just what he describes, though...
Chris R
The solution is elegant. But it seems that Emacs does first switch to `tex-mode` (i.e. executing all hook-stuff) and *after* that, loading the file. That means, it seems that the variable is `void` at the critical point of time.
phimuemue
It looks like text-mode squashes local vars. I can get this to work in other modes using other mode hooks.
Joshua Smith
Yepp, looks so. When I try to get the variable's contents via `describe-variable`, I get an error.
phimuemue
+4  A: 

Try this,

%%% Local Variables: 
%%% eval: (font-lock-add-keywords nil '(("\\\\todo" (0 font-lock-warning-face))))
%%% End: 

See Section 57.3.4.1, Specifying File Variables, for more details.

huaiyuan
I must admit I do not really know how this works, but it does work indeed. I read the manual had the thought to do it with `eval`, but I wasnt able to figure out, how it actually works.However, I read that one shall not include stylistic formatting hints into local variables, but rather into the personal `.emacs`. Nonetheless, it answers the question, so accepted.
phimuemue
You could avoid the lambda by using a hook, (add-hook 'tex-mode-hook '(lambda () (font-lock-add-keywords nil'(("\\\\todo" (0 font-lock-warning-face))))))
Joshua Smith
A: 

After using highlight-regexp to set up your highlighting, hi-lock-write-interactive-patterns (M-s h w) will write the patterns into the buffer as a magic comment.

You probably want to add a mode: hi-lock after the mode: latex line too, to activate the highlighting as soon as you open the file.

slowdog