Emacs Lisp macros enable you to define new control constructs and other language features. A macro is defined much like a function, but instead of telling how to compute a value, it tells how to compute another Lisp expression, which in turn is evaluated to compute the value. We call this expression the "expansion" of the macro call. The macro call is first expanded and then evaluated, returning the value of that evaluation.
Questions tagged [elisp-macros]
123 questions
17
votes
2 answers
"and" vs "when" for conditionals
This is a follow-up on the comments on this answer. The following bits of code seem to be equivalent:
(and a b)
(when a b)
Of course and lets you put more conditions: (and a b c d) means (when (and a b c) d)
I tend to use when only to express…

Clément
- 3,924
- 1
- 22
- 37
14
votes
1 answer
Macro with defcustom leads to "Symbol's value as variable is void" error when byte-compiled
In two of my packages I have a macro where the body depends on a variable defined in a defcustom form.
When installing the packages from Melpa, the byte-compilation ends with:
Error: Symbol's value as variable is void: my-defcustom-variable
Then,…

syl20bnr
- 2,095
- 11
- 21
13
votes
2 answers
Are macros expanded when the file is compiled?
I have a macro that needs to be expanded at every single instance of
its use compile-time. Is there a way I can specify this to be so
without going through the codebase and carefully wrapping each call
with eval-when-compile?

Sean Allred
- 6,861
- 16
- 85
12
votes
3 answers
When to use macro or not to use
When should I use macro in my program or not to?
This question is inspired by an informative answer by @tarsius. I believe many have great experiences to share others. I hope this question become one of Great Subjective Questions and help Emacs…

Yasushi Shoji
- 2,151
- 15
- 35
12
votes
3 answers
How can I create multiple defuns by looping through a list?
I am working on optimizing my emacs config where I can dynamically create interactive functions for all themes I have in a list.
Below is a simplified version of the construct I am trying to make work.
;; List containing names of functions that I…

Kaushal Modi
- 25,203
- 3
- 74
- 179
11
votes
1 answer
How is the variable scoping for macros determined?
Take the following example macro, defined in macro.el.
(defmacro some-macro (&rest body)
`(let ((some-variable 1))
,@body))
And take the following function, defined in a different file, function.el.
(defun some-function ()
(some-macro…

Malabarba
- 22,878
- 6
- 78
- 163
11
votes
2 answers
How to use while-no-input?
From the docstring:
Execute BODY only as long as there's no pending input.
If input arrives, that ends the execution of BODY,
and while-no-input returns t. Quitting makes it return nil.
If BODY finishes, while-no-input returns whatever value…

Malabarba
- 22,878
- 6
- 78
- 163
8
votes
1 answer
Evaluate some arguments to macro immediately
I am trying to call a macro (defined somewhere else; I can't change it), but I want to evaluate some of the arguments before the macro is expanded. Is this possible?
For example:
(defmacro foo (&rest args)
(mapcar (lambda (a) (message "arg: %s"…

0x5453
- 319
- 1
- 7
8
votes
1 answer
"cl-flet" macro not allowing for recursive functions
I am used to the deprecated elisp macro flet and I was told to change to cl-flet. However upon making this change some of my elisp programs stopped working and I realized the reason is that, unlike flet, cl-flet does not allow for recursive…

Ruy
- 787
- 4
- 11
8
votes
1 answer
eval-when-compile: defsubst vs defmacro vs define-inline
I defined some simple functions in init.el, for example my-cache-file:
(defconst my-cache-directory
(expand-file-name ".cache" user-emacs-directory)) ; ~/.emacs/.cache
(defun my-cache-file (x)
(expand-file-name x my-cache-directory)) ;…

ivan
- 1,928
- 10
- 20
8
votes
1 answer
Make flycheck's "reference to free variable" work with macros
The use-package is a useful and popular macro for installing packages, but flycheck always complains about it since it hides variable definitions. For example, if I attempt (use-package org), Flycheck will warn of a reference to free variable 'org.…

Matthew Piziak
- 5,958
- 3
- 29
- 77
8
votes
1 answer
Autoloads and variables
I understand what autoload does for functions (register file to load when
such function is called or its documentation string is retrieved). However,
it's not clear how to use autoload facility in conjunction with variables
and macros.
I have two…

Mark Karpov
- 4,893
- 1
- 24
- 53
7
votes
2 answers
How can I modify a macro I call frequently?
I use use-package to organize my init.el. I noticed that all my declarations use :ensure t. An example declaration is:
(use-package auto-complete
:ensure t
:diminish auto-complete-mode
:init (global-auto-complete-mode t))
Since all my…

bsamek
- 213
- 1
- 3
7
votes
1 answer
Defining a macro that exists only in one .el file
Is there a way to define a macro in an emacs-lisp file in such a way that the macro would not be visible anywhere else once the file is read and evaluated?
Kind of like a temporary macro that goes out of scope at the end of a file?

Kirill
- 1,019
- 7
- 19
7
votes
1 answer
Setf weird expansion
Trying to understand what setf can do, I called
(macroexpand '(setf (aref vec i) val))
⇒ (let* ((v vec) (v i)) (aset v v val))
This seems obviously wrong.
However I couldn't create an actual instance where (setf (aref .. fails. E.g.
(setq vec…

phs
- 1,095
- 6
- 13