Questions tagged [local-variables]

Use this tag for questions about variables local to a function, defined by let-binding them.

Like most programming languages, Lisp in general and Elisp in particular allow for defining local variables in a function. The scope of such variables is limited to the function within which they are defined.

Local variables are defined by let-binding them. For example, when defining a function func, you can define a local variable v as follows:

(defun func (arg1 arg2)
   (let ((v nil))
      (...)))

Within the body of the function, the variable v can be used as "scratch paper" to calculate and remember partial results. When the function is called and the let-form is evaluated, v is bound (in this case to nil) and can be used as any other variable in the body of the form. But when the let-form evaluation ends, the variable disappears.

It is worth noting that the arguments to the function are also local variables: they disappear when the evaluation of the function body is complete.

There are a few different forms of let - see the links for details.

Useful links

Local variables section in the Elisp Reference Manual

let section in the Introduction to Elisp Manual

49 questions
22
votes
2 answers

Remember permission to execute "risky" local variables

In many of my projects I use .dir-locals.el files to setup project specific variables such as compile commands. Upon opening a file within a directory containing such a file, emacs complains about "risky local variables", specifically: The local…
elemakil
  • 2,517
  • 1
  • 18
  • 26
15
votes
2 answers

Is there a way to daisy chain .dir-locals.el files?

Suppose I have a directory with these files. /foo/bar/baz/.dir-locals.el /foo/bar/.dir-locals.el /foo/.dir-locals.el When I go to create a file in /foo/bar/baz/, I'd like to daisy chain them together such that /foo/.dir-locals.el applies first, and…
Eric Johnson
  • 371
  • 1
  • 9
15
votes
1 answer

Mark a local variable safe for any value

The variable safe-local-variable-values can store name/value pairs that are safe as file-local or directory-local. However sometimes I want to say any value is valid for a given variable. The manual page I linked to says that any integer value is…
14
votes
1 answer

Make a buffer-local variable become global again

I was experimenting with local variables and set: (defvar-local foo nil "Buffer local foo") I later redesigned my program to use a global variable instead using: (defvar foo nil "Not buffer local foo") but the variable is still buffer local and…
Maciej Goszczycki
  • 1,777
  • 13
  • 18
10
votes
2 answers

How can I prevent flycheck-mode from checking certain files?

I've configured Emacs to open files named *.cfg in shell-script-mode. These are config files, of course, rather than shell scripts, but 90% of the time it does what I want (mainly, good guesses about faces for comments and variable assignment). I…
9
votes
2 answers

Can I set a particular file to automatically and silently accept changes?

That is, can I specify for a particular file that I don't want Emacs to notify me that the file has changed on disk and prompt me about whether I want to accept changes or not? I don't want to do this globally, just for files I know will frequently…
9
votes
0 answers

Display all local variables within a edebug session

When using edebug one can get a local variable by command describe-variable (C-h v), but are there any ways to display all local variables? For example in Python you can call locals() in a pdb session, which returns all the local vars in dictionary.…
atevm
  • 928
  • 7
  • 16
9
votes
2 answers

Prevent folding org files opened by ediff

Folding gets in the way when merging two org files using ediff, so I'm trying to disable all folding in org-mode buffers created by ediff. Non-ediff folding behavior should remain unaffected. Naively I think that something akin to the following…
ebpa
  • 7,319
  • 26
  • 53
8
votes
2 answers

Only enable whitespace mode in certain buffers

I would like to have whitespace mode turned on for all buffer except for org-mode ones. It is easy to do this when emacs starts up, but since I use a persistant copy of emacs with emacs daemon mode I can't just do it that way. I tried: (require…
jcv
  • 225
  • 1
  • 6
7
votes
1 answer

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

I am writing some lisp code and I would like it not to mess with the global variables in the system. I am therefore being very careful to only use variables within the scope of the let special form. However, as the code gets longer, it becomes…
Ruy
  • 787
  • 4
  • 11
6
votes
2 answers

Using different theme on tramp buffers (and buffer-related themes in general)?

Is it possible to configure emacs so it uses different theme for tramp buffers than for defaults? Say, I use twilight by default, but while editing sth via ssh I'd like to switch to pastels-on-dark... Changing colors are great at providing a feeling…
Mekk
  • 1,017
  • 7
  • 14
6
votes
1 answer

Surprisingly persistent local variable

A local variable keeps coming back, and I know not whence. In a certain buffer, TeX-command-extra-options is "--synctex=1", even after I have removed this from my init.el and restarted Emacs. I tried setting it to something else by adding %%% Local…
Toothrot
  • 3,204
  • 1
  • 12
  • 30
6
votes
1 answer

How do I use company-mode in some buffers and auto-complete-mode in others?

My init.el loads company with (global-company-mode). I want to make an exception for Javascript buffers, and instead use auto-complete-mode for that mode. Q: I am struggling to find a way to unhook company mode for only a particular mode. How can I…
Chris
  • 699
  • 3
  • 13
5
votes
1 answer

How to get an value from a buffer local variable

I have the following code in the file1.el: (with-current-buffer (get-buffer-create "TestBuffer") (read-only-mode -1) (erase-buffer) (make-local-variable 'source) (setq source (get-source-language)) (my-package-insert text)) In the…
Andrii Tykhonov
  • 452
  • 2
  • 13
5
votes
3 answers

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

Currently I have variables defined with setq-local, this is useful for modes, so I can define variables only for a particular mode. How can this be done for functions? I'd like to have a function, eg:generic-lookup-reference-at-point, bind this to a…
ideasman42
  • 8,375
  • 1
  • 28
  • 105
1
2 3 4