I would like to set the major mode for a file as you would using file-local variables
Local Variables: mode: text End:
.dir-locals.el in order not to “pollute” the file in question. Is this possible?
It's possible to specify a string instead of a mode, but this only works for subdirectories. So I'll use
nil instead to match all modes, then the
eval key to change the major mode conditionally:
((nil (eval (lambda () (when (string= (file-name-nondirectory buffer-file-name) "file-name.extension") (my-mode))))))
A downside of this approach is that the
eval key is unsafe, so you'll need to confirm the variable permanently for this to have an effect.
.dir-locals.el works just fine for me to set the major mode of all files in a directory to
((nil . ((mode . shell-script))))
The obvious downside is that you can't specify the major mode for only a given subset of files in the directory.
Addendum: Amusingly, it also has the side effect of setting the major mode of
.dir-locals.el itself to
Addendum 2: Rather more amusingly, it also has the side effect of setting the major mode of Dired buffers to
shell-script-mode, making it impossible to open any (as an error is thrown during the initialization).