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

52 questions
16
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
  • 381
  • 1
  • 9
2
votes
1 answer

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

Is it possible to reference a local variable inside the definition of a function? Here's an example: I have an alist text-alist consisting of keys and text snippets. I want each text snippet bound to a key in the prefix map my-text-prefix-map. The…
Erik Sjöstrand
  • 826
  • 4
  • 15
0
votes
1 answer

Returning the value of a buffer variable

Is this a good way to return the value of a buffer variable (if (local-variable-p variable buffer) (buffer-local-value variable buffer) (default-value variable))
Dilna
  • 1
  • 3
  • 11
0
votes
2 answers

write-file-functions: Clarification as to permanent local behavior with add-hook

According to the write-file-functions doc-string, "[t]his variable's value is permanent if it is given a local binding." Step 1: Start from emacs -q using Emacs 25.2.1. Step 2: Switch to the *scratch* buffer. Step 3: Type the following in the…
lawlist
  • 19,106
  • 5
  • 38
  • 120
0
votes
1 answer

Idiomatic way to improve `insert-pair`

I have the following custom implementation of insert-pair: (defun *-insert-pair (&optional arg open close) "Wrap next ARG sexps in parentheses. If there is no next sexp, insert a new pair of parentheses." (interactive "p") (let ((arg (or arg…
Tianxiang Xiong
  • 3,878
  • 18
  • 28
0
votes
2 answers

Combine setq and concat

Could you help me with smarter way of handling package-archives? At the moment I have the following in my init.el ;; This is dumb but... (if (eq system-type 'windows-nt) (setq package-archives '(("elpa" . "http://elpa.gnu.org/packages/") …
Maxim Kim
  • 1,766
  • 11
  • 19