1

Summary (TL;DR):

I want to disable Emacs from processing and/or applying any file-local variables, no matter what. However, I still want Emacs to process and apply directory-local variables specified in a .dir-locals.el file (if it exists for any given scenario). Are there any settings or configurations for that would accomplish such a goal? Are there perhaps any alternatives (packages, approaches, etc.) that would be even better suited than relying on Emacs' built-in functionality for configuring Emacs dynamically with/for specific variables/etc. with project specific details only when in any given project?

Details:

I did some research before asking. There were 3 settings that appeared like they could help but they actually don't since they are much broader in scope and either completely enable or disable any type of local variable.

enable-local-variables
enable-local-eval
local-enable-local-variables

Setting them to nil disabled both file-local variables and directory-local variables, unfortunately not what I need. I reviewed the files.el source and it seems this is intentional by design (or at least how they are supposed to work). It also appears local-enable-local-variables is only for an extremely niche obsolete edge-case.

Looking around in files.el, I had the idea of just always forcing file-local-variables-alist to be nil or empty through either various overrides/advice'ing various functions (e.g. hack-local-variables-apply). It's a sort of ugly hack (no pun intended) for sure but seems like it would work.

However, I wondered if there was a better way to achieve the same goal or perhaps an entirely different alternative (i.e. some kind of a package that is better suited than either file-local or directory-local variables entirely)?

Why might I want this?

Well, ignoring that such questions are completely antithetical to Emacs, it's really quite straightforward. All of my projects usually have a .dir-locals.el file in them to configure project specific settings, methods, etc. within Emacs with the particular information only available in that project.

However, from time to time, I visit other files from various repositories, projects, etc. (incl. Emacs' source code). Sometimes people have put file-local variables in those files which are really just an annoying nuisance when I want to explore around the file or what have you.

I then have to make sure they don't do anything I don't want as well as make sure my original settings and any .dir-locals.el changes are correctly re-applied afterward. It's not a huge deal but just frustrating enough that it causes just enough friction that I want to finally fix it.

1 Answer 1

1

See function inhibit-local-variables-p:

Return non-nil if file local variables should be ignored. This checks the file (or buffer) name against inhibit-local-variables-regexps and inhibit-local-variables-suffixes. If inhibit-local-variables-ignore-case is non-nil, this ignores case.

Function hack-local-variables also notes:

If inhibit-local-variables-regexps applies to the file in question, the file is not scanned for local variables, but directory-local variables may still be applied.

And C-hig (elisp)File Local Variables says:

Variable: inhibit-local-variables-regexps
This is a list of regular expressions. If a file has a name matching an element of this list, then it is not scanned for any form of file-local variable. For examples of why you might want to use this, *note Auto Major Mode::.

So if you want to inhibit file-local variables for all files, you could use:

(add-to-list 'inhibit-local-variables-regexps ".")
1
  • 1
    Thanks. I saw that method but must have glazed over the documentation and just assumed it was for all local variables (since the method name sort-of implies that). I figured there had to be a better way of achieving this goal. Commented Jul 6, 2024 at 2:48

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.