Skip to main content
Share Your Experience: Take the 2024 Developer Survey
46 votes
Accepted

what does "let*" ( let asterisk) mean in elisp

It means that when there is more than one binding the variables are bound sequentially, so that each can depend on the values bound to those that are bound before it, i.e., those that come before it ...
Drew's user avatar
  • 77.9k
13 votes

Is there any difference between '(let (var) ...)' and '(let ((var nil)) ...)'?

@amitp provided the answer. They do have the same behavior. However, IMO they can indicate something slightly different to a human reader of the code -- at least according to an informal convention. ...
Drew's user avatar
  • 77.9k
13 votes
Accepted

Is there any difference between '(let (var) ...)' and '(let ((var nil)) ...)'?

The documentation for let says: Each element of VARLIST is a symbol (which is bound to nil) or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM). The let (var) variant matches ...
amitp's user avatar
  • 2,541
13 votes

what does "let*" ( let asterisk) mean in elisp

let* is like a recursive let meaning: (let* ((var1 VAL1) (var2 VAL2)) ...) is like (let ((var1 VAL1)) (let ((var2 VAL2)) ...)) Thereby the usage of capital letters for VAL1 and VAL2 ...
Tobias's user avatar
  • 33.2k
11 votes
Accepted

Define a function alias within a let binding?

Definitions This topic confuses me sometimes, so let's start with some definitions, namely those of let: let is a special form in ‘C source code’. (let VARLIST BODY...) Bind variables according to ...
Basil's user avatar
  • 12.4k
10 votes
Accepted

Difference between pcase-let & cl-destructuring-bind?

cl-destructuring-bind was designed more or less specifically to destructure data made of cons cells. pcase-let on the other hand is just a special case of pcase which was designed to handle arbitrary ...
Stefan's user avatar
  • 26.5k
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.4k
7 votes
Accepted

How to let-bind multiple variables conditional on X?

I think a reasonable way is to collect all the values into a list and then use a destructuring bind, e.g., pcase-let: (pcase-let ((`(,a ,b ,c) (if t (list 1 2 3) (list 4 5 6)))) (list ...
npostavs's user avatar
  • 9,203
7 votes
Accepted

How to define a variable in a custom function?

You are looking to create a local variable, which can be done with a let-binding. Here's an example: (defun myfun-create-file () "Doc-string." (interactive) (let ((stamp (format-time-string "%...
Dan's user avatar
  • 33.1k
5 votes
Accepted

What's really behind an assignment in Emacs lisp?

setq is doing like expected, the thing here is, that (1 2 3 4) is not a value, so it is not what you think it is. a Place is a location in memory. x and y are Symbols. a Symbol merely points to a ...
jue's user avatar
  • 4,576
5 votes
Accepted

why is a let binding is ignored in compiled function?

You're compiling the file using lexical binding, so I suspect the problem is that your file does neither of the following: (defvar bibtex-completion-bibliography) (require 'bibtex-completion) either ...
phils's user avatar
  • 51.5k
5 votes

Bind multiple values directly from list without binding the list itself

Beside relying on the cl-lib compatibility package, the recommended way in Elisp for that is to use pcase: (defun test-fun (let ((a '(a b)) (b '(c d))) `(,a ,b))) (defun other-test-fun ...
Stefan's user avatar
  • 26.5k
5 votes
Accepted

Reliable lexical binding?

Just use an uninterned symbol that cannot be reached outside of the let. You need that uninterned symbol two times, one time for the let and one time within foo. The purpose of the additional outer ...
Tobias's user avatar
  • 33.2k
5 votes
Accepted

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

(defvar wtf 10) made wtf bound dynamically so the variable g would get a closure which captured nothing. This is correct. However, this cannot explain what (f 20) returned. It can if, under lexical ...
phils's user avatar
  • 51.5k
4 votes
Accepted

Is it possible to use let-binding for setting keys?

You can let-bind the keymap variable (presumably tex-file-mode-map) to a new keymap: (let ((tex-file-mode-map (make-sparse-keymap))) ...) Its value before binding it is restored after the let. ...
Drew's user avatar
  • 77.9k
4 votes

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

A long comment instead of an answer. (SE limits the length of a comment.) Hi phils, this is my comment on your answer. potentially causing other functions, called later in the stack, to obtain an ...
shynur's user avatar
  • 5,318
3 votes

Backup a variable and store it back

Just use let. Write something like this: (let ((foo 323)) …) Where the ellipsis is, place whatever code you wanted to run with foo set to 323. Once that code finishes, foo will go back to having ...
db48x's user avatar
  • 18.2k
3 votes

Why does a let-bound huge list survive garbage collection after the let form?

From the docstring of memory-use-counts, emphasis mine: Return a list of counters that measure how much consing there has been. Each of these counters increments for a certain kind of object. ...
legoscia's user avatar
  • 6,072
3 votes

How to avoid use of `lexical-let`

Your example is likely not representative of the actual code you're using, but a good solution might be to create the closure in the macro and return the closure, instead of returning code which may ...
Stefan's user avatar
  • 26.5k
3 votes
Accepted

Why I can't use `let` in this example?

(let (cur-lengths-list (lengths-list-file filename)) Here I first used let to bind (lengths-list-file filename) to a symbol cur-lengths-list No, you bound cur-lengths-list to nil (hence &...
phils's user avatar
  • 51.5k
3 votes
Accepted

Why doesn't my keymap let-binding work?

The problem is in the let-binding of your defvar form. The let will return the value of its last form. Currently, the last form is define-key, which returns the function symbol which you bound to ...
Dan's user avatar
  • 33.1k
3 votes

Is it possible to use let-binding for setting keys?

[This was going to be a comment to @Drew's answer, but the code will not be rendered correctly.] I gave this a try and wrote a macro using unwind-protect. (defmacro with-keybinding (map key command &...
YoungFrog's user avatar
  • 3,526
3 votes
Accepted

Let bound face-attribute locally

No, the attributes of a face are global. Using face-remapping-alist you can make them buffer local, which won't make you happy. In your case, I would recommend that you define a new face, say my-...
Lindydancer's user avatar
  • 6,170
3 votes
Accepted

Calling locally defined function via function parameter

It looks like you want to alias a function name within the scope of the my-run-command body, so I'd suggest something more like: (defun my-run-command (command) (cl-letf (((symbol-function 'my-...
phils's user avatar
  • 51.5k
3 votes
Accepted

Enable Lexical Binding by Binding “lexical-binding” to t

Op missed the expected behavior of the code snippet, and does not provide the full code to reproduce the issue, which seriously obscurified the problem. Short answer is, custom-set-variables used eval ...
LdBeth's user avatar
  • 171
3 votes
Accepted

“named-let” bug?

Refer to C-hig (cl)Function Bindings: -- Macro: cl-labels (bindings...) forms... The ‘cl-labels’ form is like ‘cl-flet’, except that the function bindings can be recursive. The scoping is lexical, ...
phils's user avatar
  • 51.5k
2 votes

How to avoid use of `lexical-let`

There is an ugly workaround using eval with the optional argument LEXICAL set to t. (defmacro repro () (let ((kmap-sym (gensym "kmap-"))) `(eval '(let ((,kmap-sym (make-sparse-keymap))) ...
Tobias's user avatar
  • 33.2k
2 votes

How to let-bind multiple variables conditional on X?

There is no idiomatic way to do it, IMO. Do what you like. If you want to be sure to test the condition only once that you are currently retesting, then here is another possibility, where I've used ...
Drew's user avatar
  • 77.9k
2 votes
Accepted

Identities between let/lambda/defun expressions and simplifying nested progn

lambda seems to have an "inherent" progn as well, such that (funcall (lambda (x y z) A B C) 1 2 3) is equivalent to (let ((x 1) (y 2) (z 3)) A B C). "Equivalent" is a strong word. Calling a lambda ...
Basil's user avatar
  • 12.4k
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

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