Skip to main content
22 votes
Accepted

Mark a local variable safe for any value

Set the safe-local-variable property to a function that validates the value. For example, fill-column has a safe-local-variable property set to integerp. Use (lambda (x) t) as the validation function ...
Gilles 'SO- stop being evil''s user avatar
11 votes
Accepted

Prevent folding org files opened by ediff

It sounds like your goal is to have org-mode files always show everything when in ediff. The simplest solution is probably to avoid the local variables approach and just put show-all in the relevant ...
Dan's user avatar
  • 33.1k
10 votes
Accepted

.dir-local.el misunderstanding

When you call add-dir-local-variable, it assumes you are already in the directory you want to apply the directory-local variable to. When it prompts for a directory, it's asking if you want the ...
Tyler's user avatar
  • 22.4k
10 votes
Accepted

How to make sure no global variables have been created in a piece of lisp code?

Is it possible to automatically verify that a piece of lisp code does not create any new global variables? Turn on lexical-binding: ;;; foo.el --- just frobnicating some foo -*- lexical-binding: t -*-...
Basil's user avatar
  • 12.5k
8 votes

How to bind keys to a function which can be defined per-mode?

(defvar my-local-variable nil "Variable used on a bufer-local basis to indicate ...") (make-variable-buffer-local 'my-local-variable) USAGE: (setq my-local-variable (lambda () (message "major-...
lawlist's user avatar
  • 19.2k
8 votes
Accepted

Surprisingly persistent local variable

In case someone else runs into this, I had enabled TeX-auto-save, which saves settings to an auto/ directory and loads them when a file is revisited. https://www.gnu.org/software/auctex/manual/...
Toothrot's user avatar
  • 3,274
7 votes
Accepted

How to reference a local variable inside the definition of a function?

You can use backquotes and commas to build functions dynamically: `(lambda () (interactive) (insert ,(cdr text))) Or you can also enable lexical-binding to make function closures. More ...
zk_phi's user avatar
  • 446
6 votes

Remember permission to execute "risky" local variables

You can disable this "don't remember risky variables" feature by putting the following in your ~/.emacs: ;; allow remembering risky variables (defun risky-local-variable-p (sym &optional _ignored)...
Tommy Jollyboat's user avatar
4 votes
Accepted

How can I automatically add some local variables info to a C-x C-f new (.tex) file?

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-...
JeanPierre's user avatar
  • 7,475
4 votes

Prevent folding org files opened by ediff

Another option can be found on the worg page. The basic idea is to use ediff-select-hook to unfold an element whenever given region is selected in ediff and folding them back in the ediff-unselect-...
Iqbal Ansari's user avatar
  • 7,588
4 votes

How can I create mode-local variables?

I think what you might do is to use properties of the variable symbol to store the history. That is, each symbol can have a property list, which is a list of pairs. The key will be major mode names, ...
Xah Lee's user avatar
  • 1,766
4 votes

Enable .dir-locals.el variables inside just opened buffer

As the new buffer is not saved yet it doesn't know where to look for dir-locals. Have a look at function hack-dir-local-variables-non-file-buffer which uses default-directory to apply dir-locals to ...
dakra's user avatar
  • 406
4 votes

local variable set to nil after switch-to-buffer call

I'm guessing that you really want to use setq, not setq-local in that code. Did you intend to set buffer-local values of those let-bound variables? As @phils points out, setq-local sets a buffer-...
Drew's user avatar
  • 78k
3 votes

Make a variable visible to some functions only

defalias "magically" stores the variables inside its functions, so their values can be reused with subsequent calls. This is not what defalias does; see (elisp) Defining Functions. The means by which ...
Basil's user avatar
  • 12.5k
3 votes

How to customize major mode initialization with local variables?

If you want to set a buffer-local value for a given mode, do so after the mode has been established. You do that by putting the value-assignment on the mode hook. For example, if you want to set ...
Drew's user avatar
  • 78k
3 votes
Accepted

Making a variable window-local

Save your data as a window-parameter. It will force you to use :eval inside your mode-line, but that's not something to be afraid of. If you want to discuss changes to the "core" Emacs features (...
Stefan's user avatar
  • 26.5k
3 votes
Accepted

local variable set to nil after switch-to-buffer call

Annotating parts of your code: (let (left-window edit-window-left edit-window-right) ;; ^ these variables go out of scope at the end of the `let' form, ;; ;; If you were using `setq' below ...
phils's user avatar
  • 51.7k
3 votes

local variable set to nil after switch-to-buffer call

You've been mislead by the unusual terminology in Emacs Lisp. A buffer-local variable is not a local variable! It's a global variable which has (potentially) different values in every buffer. “Buffer-...
Gilles 'SO- stop being evil''s user avatar
2 votes
Accepted

How can I create mode-local variables?

A variable is either buffer-local or not. If it's buffer-local then the value isn't shared with other buffers. There's no built-in feature to directly have a variable that's shared between buffers. So ...
Gilles 'SO- stop being evil''s user avatar
2 votes
Accepted

Using local variables in cl-loop

Thanks to bpalmer in #emacs for helping me solve this. The issue was with the and after for start...; instead, it needed to be another for (or as). I chose to use as instead of the other fors since it ...
aiguofer's user avatar
  • 588
2 votes

How to bind keys to a function which can be defined per-mode?

If a library developer is responsible for definining the mode dependent behavior and you do not plan to allow the user to change it per defcustom you can use mode dependent overloads. The code below ...
Tobias's user avatar
  • 33.3k
2 votes
Accepted

Local variable's state mutated across function calls

A quoted list is a constant thus should not be modified, if you modify a constant inside a function, the function will be self-modifying code, for example, ;; Return how many this function is called (...
xuchunyang's user avatar
  • 14.6k
2 votes

.dir-locals for specific directory but not its children

The Directory Variables section of the emacs manual says: Here’s an example of a ‘.dir-locals.el’ file: ((nil . ((indent-tabs-mode . t) (fill-column . 80) (mode . auto-fill))) (...
Adam Spiers's user avatar
2 votes

Avoiding code duplication with conditional code that uses let bindings

For the sake of providing a more basic version of the answer by @db48x you could do the same thing like this: (defun my-func () (let (v1 v2) (if some-condition (setq v1 2 v2 3) (...
phils's user avatar
  • 51.7k
2 votes

How can I force require-final-newline nil?

The answer in my case turned out to be an .editorconfig file whose existence I was not aware of. It specifies: insert_final_newline = true @dalanicolai's suggestion to use (debug-on-variable-change '...
Croad Langshan's user avatar
2 votes
Accepted

How can I set a buffer-local variable (format-all-mode) to nil within .dir-locals.el? (using Doom emacs)

The real question is about disabling a minor mode, but just to answer the stated question of setting a buffer-local variable, it's as simple as this: ((python-mode . ((format-all-mode . nil)))) I.e. ...
phils's user avatar
  • 51.7k
2 votes

Avoid placing safe-local-variable-values in init file

Yes, possible. You need to change the definition of hack-local-variables-confirm. Find the form (customize-push-and-save (if (= char ?!) ...)) and modify it. I agree with you to some extent: these ...
shynur's user avatar
  • 5,473
2 votes

Avoid placing safe-local-variable-values in init file

Since you said your skills with elisp are very limited, I tried to simplify the solution. Now all you need to do is change the value of your/safe-vars-file. ;;; Put them into your configuration file. ...
shynur's user avatar
  • 5,473
2 votes

cannot use buffer-local variables after defining them

Presumably, since you setq the variable m-buffer-local-variable, that's the variable you want to print - instead you are trying to print a different (and undefined) variable mitmp-request-id. Assuming ...
NickD's user avatar
  • 30.8k
1 vote

Can not read dir-local variable from a hook function

Local variables are set after hooks are run. Try using a timer: (let ((buf (current-buffer))) (run-with-timer 0.01 nil (lambda () (with-current-buffer buf (if (...
Nova's user avatar
  • 1,119

Only top scored, non community-wiki answers of a minimum length are eligible