6

I keep auto-fill-mode enabled by default for all LaTeX files.

This is usually nice, but occasionally one latex file contains mostly tables and I would like to disable auto-fill-mode whenever I edit that particular file.

Is it possible to specify at the top of a .tex file that I would like it to be the exception? Alternatively, is it possible to specify in .emacs the paths/names of these files?

Calaf
  • 10,113
  • 15
  • 57
  • 120

4 Answers4

4

look onto "Local Variables in Files" section in official Emacs documentation. There is eval variable, that allows to evaluate any code. So it will look something like (put this into end of file, comment char should be mode-specific):

# Local Variables:
# eval: (auto-fill-mode -1)
# End:
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • 2
    -1. By enabling `eval` as a safe local variable, you allow anyone who can persuade you to open a file of their choice in Emacs to run arbitrary Emacs Lisp code. See [Safety of file variables](http://www.gnu.org/software/libtool/manual/emacs/Safe-File-Variables.html) in the manual. – Gareth Rees Jul 12 '11 at 19:54
  • 1
    @GarethRees This is still a good solution: `eval` is normally set to ask for confirmation, and adding *this particular* eval form to `safe-local-eval-forms` is pretty save I'd say. – halloleo Jun 07 '16 at 02:18
3

This approach does not technically disable fill mode, but it is safe, unlike enabling eval, and achieves the desired result. Put a line like this as the first line of your file. Just specify the correct major mode, probably text, and set the fill-column to an appropriately large value:

-*- mode: text; fill-column: 99999 -*-

As previously suggested, you could write a hook that decides if auto-fill should be enabled, but I felt that was more work than necessary.

  • This is a nice solution, especially since something in Emacs 27.2 has broken Trey's solution. The trouble here though is that it _really_ disables auto-fill. With Trey's solution it is still possible to sometimes choose to auto-fill (M-q) when one wants. – Calaf May 17 '21 at 16:08
3

I like the solution of looking for something in the file to determine whether or not to have autofill.

If you want to put an identifier at the top, you can use code like this:

(defun my-auto-fill-disabling-hook ()
  "Check to see if we should disable autofill."
  (save-excursion
    (when (re-search-forward "Some Unique Identifier" 1000 t)
      (auto-fill-mode -1))))

(add-hook 'find-file-hooks 'my-auto-fill-disabling-hook)

And obviously change "Some Unique Identifier" to something reasonable - such as a search for the tables themselves. Then you'd get exactly what you want, LaTeX files with tables wouldn't be auto-filled.

Note: @Alex suggested using a file-local variable, but this is a mistake according to the manual itself:

Often, however, it is a mistake to enable minor modes this way. Most minor modes, like Auto Fill mode, represent individual user preferences. If you want to use a minor mode, it is better to set up major mode hooks with your init file to turn that minor mode on for yourself alone (see Init File), instead of using a local variable list to impose your taste on everyone.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 2
    if you work alone, and you have too many files, local variables works better - that's why I suggested this solution... – Alex Ott Jul 12 '11 at 19:54
  • @Alex True, but (I just amended the answer), if you search for exactly the condition he describes (tables), then you don't actually have to modify any of the files. – Trey Jackson Jul 12 '11 at 19:59
  • Hmm.. After upgrading from Emacs 27.2-2 to 28.0.50, this is broken. Any hints? – Calaf May 19 '21 at 19:47
1

Not sure which Emacs version this was added with, but you can also 'neuter' the auto-fill-function variable on a per-file basis:

# Local Variables:
# auto-fill-function: (lambda nil nil)
# End:
user117529
  • 663
  • 8
  • 16