2

I use auctex to edit my .tex files. Each time I create a new file .tex file via C-x C-f, I want to insert two local variables informations:

% Local Variables:
% coding: utf-8-unix
% TeX-engine: luatex
% End:

Until now, I do M-x add-file-local-variable two times. Is there a way to automate this?

4
  • First question here. Hope the formating is ok. Thanks :)
    – cjorssen
    Commented Sep 16, 2016 at 9:07
  • Do you prefer setting this as file local variables or setting a global default value?
    – giordano
    Commented Sep 16, 2016 at 11:09
  • 1
    @giordano On my computer, those settings are default settings. But I want to keep those settings in my files, in case I work on an other computer. (If I understand correctly your question)
    – cjorssen
    Commented Sep 16, 2016 at 11:15
  • Yes, that was my question, and your answer is perfectly fine :-).
    – giordano
    Commented Sep 16, 2016 at 11:20

1 Answer 1

4

You can use a hook to achieve this. Use the one you need as described in auctex documentation. Here we check the file extension so that we don't do that for eg .sty files.  Also, we test the buffer-read-only variable before modifying the buffer.

The goto-char at the end ensures point is set at beginning of buffer.

(defun my/add-auctex-file-variables ()
  (interactive)
  (if (and (not buffer-read-only)
           (string= (file-name-extension (buffer-file-name)) "tex"))
   (progn
     (add-file-local-variable 'coding 'utf-8-unix)
     (add-file-local-variable 'TeX-engine 'luatex)
     (goto-char (point-min)))))

(add-hook 'LaTeX-mode-hook 'my/add-auctex-file-variables)

Also, as can be seen on add-file-local-variable docstring (display it with C-h f add-file-local-variable) any setting of the variable is first deleted, so there's no risk of accumulating such definitions.

A possible pitfall is that the file is marked as modified even if nothing has actually changed.

8
  • @cjorssen Sure, just add (goto-char 1) at end of my/... function (before the closing )).
    – JeanPierre
    Commented Sep 16, 2016 at 12:06
  • @cjorssen The docstring of add-file-local-variable (use C-h f add-file-local-variable) says any setting of the variable is first deleted, if that's what you ask.
    – JeanPierre
    Commented Sep 16, 2016 at 12:10
  • @cjorssen Yes, see updated answer, as well as pitfall.
    – JeanPierre
    Commented Sep 16, 2016 at 12:34
  • The first character of a buffer is (point-min), not necessarily 1.
    – giordano
    Commented Sep 18, 2016 at 17:31
  • @giordano Good point.
    – JeanPierre
    Commented Sep 18, 2016 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.