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

Bug in defmacro?

There's no bug. Macros are normally expanded ahead of time (typically during byte-compilation or loading, but "not at eval-time" is the key assumption that you need to make), so any code you'...
phils's user avatar
  • 51.5k
8 votes
Accepted

Setf weird expansion

From your comment you've figured this out for yourself, but... In the macro expansion you're seeing the printed representation of two independent symbols with the same name. Most likely both of ...
phils's user avatar
  • 51.5k
7 votes

When to use macro or not to use

From my experience reading, debugging and modifying my and others' Elisp code I'd suggest to avoid macros as much as possible. The obvious thing about macros is that they don't evaluate their ...
abo-abo's user avatar
  • 14.1k
7 votes

Deep eager macroexpansion

(require <foo>) is treated specially when it's at top-level (it causes the compiler to require the file also, rather than only do the require at run-time). If the require is not performed at ...
Stefan's user avatar
  • 26.5k
7 votes
Accepted

One-time advice

You don't need a macro for this. And you don't need (but you can certainly use) lexical binding. ;; Without lexical binding: ;; (defun advise-once (symbol where function &optional props) (...
Drew's user avatar
  • 77.9k
7 votes
Accepted

What is the difference between `with-eval-after-load` and `eval-after-load`?

Start with the first line of the help buffer: with-eval-after-load is a Lisp macro in ‘subr.el’. eval-after-load is a native-compiled Lisp function in ‘subr.el’. See anything different about them? ...
db48x's user avatar
  • 18.2k
6 votes
Accepted

eval-when-compile: defsubst vs defmacro vs define-inline

I'd say stick with defun. If you want to try define-inline, which is new (it doesn't even have a doc string yet!), go ahead. But think about how much you want or need inlining. It is rare to need it,...
Drew's user avatar
  • 77.9k
6 votes
Accepted

eval-when-compile and eval-and-compile identical definitions?

How can eval-when-compile and eval-and-compile have different behavior if their definitions are identical? The hint is in the commentary in the definition of eval-and-compile: ;; When the byte-...
Basil's user avatar
  • 12.4k
5 votes
Accepted

Weird behavior of defmacro and intern - symbolp error

The way you test macros is with macroexpand: (macroexpand-1 '(test-macro "test")) ==> (defun (intern (concat "test-func-called" "test")) nil (message "I do nothing")) This makes the error obvious:...
sds's user avatar
  • 6,124
5 votes

When to use macro or not to use

I'll be blunt: I don't understand what "use for syntax, not for semantics" means. This is why part of this answer will deal with lunaryon's answer, and the other part will be my attempt at answering ...
wvxvw's user avatar
  • 11.4k
5 votes
Accepted

"cl-flet" macro not allowing for recursive functions

Use cl-labels instead of cl-flet. For explanation look at the emacs manual
siehe-falz's user avatar
5 votes

How can I define new places for setf?

Drew's answer is correct, but it should also be mentioned that the reason cl-lib doesn't include a cl-defsetf (or cl-setf) is because the setf machinery has been moved to gv.el. The docstring of ...
npostavs's user avatar
  • 9,203
5 votes
Accepted

Macro breaks only in `eval-expression` (`M-:`)

Error because you use eval. (eval FORM &optional LEXICAL) Evaluate FORM and return its value. If LEXICAL is t, evaluate using lexical scoping. LEXICAL can also be an actual lexical ...
shynur's user avatar
  • 5,318
4 votes
Accepted

How can I define new places for setf?

Where does it say that you're not supposed to use library cl.el? That would be silly (IMHO). The text you quote says that code distributed with GNU Emacs must not load cl at runtime. That does not say ...
Drew's user avatar
  • 77.9k
4 votes
Accepted

Why doesn't this `declare-function` form suppress 'Warning: the function is not known to be defined.'?

You shouldn't quote the first argument to declare-function, as it is a macro; i.e. it receives its arguments implicitly quoted and unevaluated. See also (elisp) Declaring Functions. This should work: ...
Basil's user avatar
  • 12.4k
4 votes
Accepted

Deep eager macroexpansion

@Stefan explained why you see what you see: Your require statement is not at top level of the file. A solution to the byte-compiler not evaluating your (non top-level) require statement is to force ...
Drew's user avatar
  • 77.9k
4 votes

How do you find all macro calls in an elisp file?

I found a way to do it interactively with el-search (http://elpa.gnu.org/packages/el-search.html). M-x el-search `(,(pred macrop) ,_) finds them. Interestingly, it matches backticked s-exps like `(:...
John Kitchin's user avatar
  • 11.9k
4 votes
Accepted

Create a function that returns a new function definition

I would use a macro, it is easier and this is what macros are for: (defmacro lw/define-loadable (fn-name mod-name) `(defun ,fn-name () (interactive) (print (concat "../modules/" ,...
jue's user avatar
  • 4,576
4 votes

Does emacs have compiled/interpreted mode?

Macro expansion happens before evaluation. If you want to see what some code expands to, use macroexpand-1 or macroexpand-all. For example, with the macro defined as given in your question, evaluating ...
db48x's user avatar
  • 18.2k
3 votes
Accepted

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

My crystal ball tells me you're confused by the fact the the generated symbols are not interned: while the symbol's name is G564 it is not the same symbol you get if you (read-from-string "G564") ...
Stefan's user avatar
  • 26.5k
3 votes

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

I think, you can do one of two things: Define your own type to search for via find-func.el. This is how it's done in ert.el (Search for current-load-list and find-function-regexp-alist.) But I don't ...
politza's user avatar
  • 3,346
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

what is the macro "section"?

It enables you to write code like this: (section testing (setq a 1) (setq b 2)) (section configuration (evil-mode) (helm-mode)) This code expands to the following: (progn (setq a 1) (...
wasamasa's user avatar
  • 22.2k
3 votes
Accepted

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

The byte-compiler attempts to catch such cases w.r.t functions that are defined at compile-time but not at run-time (resulting in warnings along the lines of "These function are not known to be ...
Stefan's user avatar
  • 26.5k
3 votes
Accepted

Is there a clean way to splice some args to an existing Lisp macro?

Since you have already a solution for your special use-case (switching from package.el to straight.el) that has nothing to do with macro-expansion I will answer your general question Is there a clean ...
Tobias's user avatar
  • 33.2k
3 votes
Accepted

How to code a command to `write-region` to a particular file?

It's only you're having syntax issues, try this: (defun copybuffer () "function takes current region, and writes it to specified file" (interactive) (write-region (region-beginning) (...
Muihlinn's user avatar
  • 2,634
3 votes
Accepted

compilation sentinel: symbol's value as variable is void for quote, >, <, =, etc

You're missing parentheses around the interior of your cond expression. You also should not have the parentheses around the body forms of the condition (although this part is not what is causing your ...
D. Gillis's user avatar
  • 496

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