Questions tagged [elisp-macros]

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.

Filter by
Sorted by
Tagged with
13
votes
2answers
2k views

“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 ...
13
votes
2answers
330 views

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 ...
13
votes
1answer
405 views

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 ...
12
votes
3answers
1k views

When to use macro or not to use [closed]

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 ...
12
votes
3answers
2k views

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 ...
11
votes
2answers
318 views

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 ...
11
votes
1answer
185 views

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, ...
8
votes
1answer
446 views

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-...
8
votes
1answer
537 views

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 ...
7
votes
2answers
181 views

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 (...
7
votes
1answer
224 views

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 ...
7
votes
1answer
442 views

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 ...
7
votes
1answer
259 views

“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 ...
7
votes
1answer
270 views

Understanding uninterned symbols and macro expansion?

I want to demonstrate my lack of knowledge with an example. Using the following two macro defintions, (defmacro for (var from init to final do &rest body) "Execute a simple for loop: (for i ...
6
votes
3answers
529 views

Name of Emacs Lisp library that provides `with-gensyms' classic macro

I have the feeling that Emacs Lisp macros are written in some different way than Common Lisp macros. This can't be true, but where is all the gensym things? Recently I was writing a macro and I needed ...
6
votes
1answer
495 views

Alternative to save-excursion that only saves/restores point?

Q: is there an alternative to save-excursion that only saves/restores point? When writing elisp functions, I often need to save point, do some stuff in the current buffer, and then restore point. I ...
6
votes
1answer
138 views

Function to redistribute list into columns

I have a list in an org document that is rather long - alpha a - beta b - gamma - delta d - epsilon e - zeta z - eta - theta - iota - kappa - lambda - mu I would like a function that redistributes ...
6
votes
1answer
837 views

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 ...
6
votes
0answers
71 views

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 (&...
5
votes
2answers
80 views

How do I depend on macros from other packages?

I'm writing a package that provides some Python functionality. I want to use python-rx, which is part of Emacs' python.el. (eval-when-compile (require 'python)) (defun python-foo-starts-defun-p () ...
5
votes
2answers
222 views

Finding the source of a function after macro expansion?

(I'm a rookie and I use Spacemacs) I'm trying to find the source of a certain function*. I can find the .el file in which it's defined by using find-function, however the defun is created ...
5
votes
3answers
627 views

How to disable eager load-time macro expansion in emacs 24.3?

Is it possible to disable eager load-time macro expansion in emacs 24.3.1? For debugging purposes, I would like to expand macros lazily on each invocation at run-time. A working non-standard method ...
4
votes
2answers
195 views

Deep eager macroexpansion

This works: (require 'clojure-mode) (when (member 'clojure-mode my-packages) (define-clojure-indent (-> 1) (->> 1))) This gives the error Wrong type argument: listp, 1: (when (...
4
votes
2answers
128 views

How can I define new places for setf?

Q: how do I teach setf about new places it can set? In common lisp, one uses the macro defsetf to tell setf about new places it can set. In elisp, that macro exists in in the cl library, but, as far ...
4
votes
2answers
1k views

defun vs defmacro and backquote vs list

While looking to exclude the uses of the list function through the backquote substitutes in defmacros, my attempt failed when combined with the let and `let* recipe, together with the gensym ...
4
votes
1answer
220 views

Macro with optional paramaters that checks for whether an optional paramater is defined?

Due to my lazy nature, I made something like this: (defmacro iλ (x) `(lambda () (interactive) ,x)) (defmacro iλd (x y) `(lambda () ,x (interactive) ,y)) (global-set-key (kbd "<f8>") (...
4
votes
1answer
741 views

Order of evaluation of nested macros and backquotes

I read https://github.com/lunaryorn/blog/blob/master/posts/byte-compiling-eval-after-load.md which contains this code: (defmacro stante-after (feature &rest forms) `(,(if (or (not byte-compile-...
4
votes
1answer
343 views

idefun : a macro to turn defun into an 'interactive' defun?

I have a pattern like this scattered all over my .emacs: (defun my/function () (interactive) (....stuff) ) I would like to write a idefun macro so I could write as following instead: (idefun ...
4
votes
3answers
98 views

How do I get byte-compilation warnings about undefined variables?

The following code works fine with M-x eval-buffer, and byte-compiles without any warnings. (eval-when-compile (defconst demo-one 1)) (defvar demo-some-var `(foo bar ,demo-one)) However, if I ...
4
votes
1answer
222 views

Use a variable to determine let bindings

Is there a way to use a variable to hold the set of let bindings for passing to a later call? For example (setq a '((org-export-with-toc nil) (blah 2))) Then (let-wrapper a ;; Perform ...
4
votes
2answers
78 views

How can I record where a function is defined if it's done indirectly?

I've got a few functions in api-wrap.el that I'll refer to as 'macro code', here simplified to clarify the question: function (generate-form prefix kind &rest args) returns an unevaluated form ...
4
votes
1answer
523 views

writing emacs extensions in common lisp or other languages

Some years ago, I was working on a personal project that seemed to work best with Emacs as the user interface, and Common Lisp (SBCL) as the back-end. The accepted answer on How to use SLIME as a ...
3
votes
3answers
368 views

How to avoid use of `lexical-let`

I have a macro that intends to creates a closure: ; -*- lexical-binding: t -*- (defmacro repro () (let ((kmap-sym (gensym "kmap-"))) `(let ((,kmap-sym (make-sparse-keymap))) (define-key ,...
3
votes
2answers
235 views

One-time advice

I've been trying to a macro to put one-time advice on a function. What I mean by one-time advice is advice which removes itself after it is called – so that when you add the advice and run the ...
3
votes
1answer
935 views

How to give a list to cond?

We have a number of clauses (condition body) which we want to give to cond, like so: (cond (condition1 body1) (condition2 body2) (condition3 body3)) But imagine that the clauses were ...
3
votes
1answer
87 views

FORM vs BODY in documentation?

Recently I was trapped by void-variable error on eval-after-load. That is not first time I forget to quote args like: (eval-after-load 'sql '(progn ...)) because eval-after-load docs say: ...
3
votes
1answer
53 views

Inconsistent behavior when calling #'funcall with a macro

I would have thought that both of the funcall's the follow would have yielded the same result, but they don't and I'm trying to understand why. (defmacro test/z () "z") (funcall (function test/z)) ;;...
3
votes
1answer
202 views

Calling a macro from a macro with a variable number of arguments

Say there is a defmacro defined like this: (defmacro do-something (foo &optional bar) (...)) And then there is another one that calls it: (defmacro something-else (wat baz &optional bar) ...
3
votes
0answers
285 views

Byte-compiling a file loads macros with `(eval-and-compile)` but not `(eval-when-compile)`

I created a buffer, test1.el: (eval-when-compile (defmacro dummy-macro (_) nil) (dummy-macro s)) Trying to compile this file, I get the following errors: In toplevel form: test1.el:3:16:Warning:...
3
votes
1answer
570 views

Multiline regex replace within elisp function

I would like to write a elisp function that corrects a few formatting issues in my LaTeX code. Precisely, I would like to transform the following text Some useless text \begin{equation} some math ...
2
votes
1answer
115 views

Dynamic generation of varlist in a let form

Let's say I have a let form and I want to generate the varlist dynamically from another function. Something like this: (let ((x 1) (y 2) (the-dyn-gen)) body... ) I have some local ...
2
votes
1answer
470 views

How to quote for multiple levels of macro expansion

I'm new to Emacs and think I ran into some macro expansion problems. There's a function solarized-with-color-variables which is a macro that creates local variables using let* and then expands the ...
2
votes
3answers
347 views

Eval some arguments in a macro

I want to make a macro that takes a form and returns a string with some parts of the form evaluated. The form can have any number of elements (e.g. additional options for the title font, color, size,.....
2
votes
2answers
170 views

Why does my function-generating macro only work when I evaluate its expansion?

I'm trying to write a macro that will define a function exactly once where I don't really care what the name of the function is. My thought was to generate a symbol, use defun with that, and then put ...
2
votes
1answer
54 views

Macros usage: execute instructions after a backquoted one

I have a question about macros. When I call a macro with an argument, I call a function with it. It works. But if I add another instruction below, the backquoted instruction is not executed. Here is ...
2
votes
1answer
85 views

Why aren't these two otherwise identical forms `equal`?

This requires the let-alist package available on the GNU ELPA (built-in in 25.1). I have the following macro: (defmacro sx-assoc-let (alist &rest body) (declare (indent 1) (debug t)) (...
2
votes
1answer
296 views

Using local variables in cl-loop

This is my first time trying to write my own elisp code, so bear with me. I have the following cl-loop: data contains the following: ((refs . [((file . "api/user.js") (start . 266) (end . 269)) ((...
2
votes
1answer
120 views

improve AUCTeX macros like `\frac` and `\stackrel`

While using AUCTeX, the macro \frac is quite intelligent that when I just type C-c C-m frac RET, it will put the cursor in the first pair of braces, as \frac{(cursor here)}{} However, when I have ...
2
votes
1answer
462 views

Evaluating Macro Arguments in Expansion

In Section 13.5.4 of the Elisp reference, we are shown a problem whereby a macro definition evaluates one of its argument expressions. (defmacro foo (a) (list 'setq (eval a) t)) (setq x 'b) (foo x) ...
1
vote
1answer
216 views

Weird behavior of defmacro and intern - symbolp error

I have the following macro (defmacro test-macro (arg) `(defun (intern (concat "test-func-called" ,arg)) () (message "I do nothing"))) When I call to this macro (test-macro "test") It gives ...