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'...
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 ...
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 ...
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)
(...
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 ...
6
votes
"and" vs "when" for conditionals
Let me start off by saying that (and a b) and (when a b) in your example do the same thing: First a is evaluated. b is evaluated if a is true#.
But and and when are used for different things.
You ...
6
votes
Accepted
How to give a list to cond?
You want to splice the list into the new form:
`(cond ,@clauses)
See C-hig (elisp) Backquote RET
You can also "splice" an evaluated value into the resulting list,
using the special marker ,@. ...
6
votes
Accepted
Name of Emacs Lisp library that provides `with-gensyms' classic macro
Take a look at Missing Macro Tools. It's available on MELPA.
The package provides the following classics:
mmt-gensym
mmt-make-gensym-list
mmt-with-gensyms
mmt-with-unique-names
mmt-once-only
6
votes
Accepted
Dynamic generation of varlist in a let form
Looks like a macro could do that:
(defmacro mylet (defs fun body)
(list 'let (append defs (funcall fun)) body))
;; example uses
(defun the-dyn-gen ()
'((a 3) (b 3)))
(mylet ((x 1) (y 2)) the-...
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,...
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:...
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 ...
5
votes
Accepted
Understanding uninterned symbols and macro expansion?
As mentioned in the comments, you have to turn on this setting to see how macro-expansion works more precisely.
Note that it only changes how macroexpand is displayed, the macros still work the same ...
5
votes
How can I modify a macro I call frequently?
The newer version of use-package has the following to enable this behavior for all packages:
(setq use-package-always-ensure t)
Just in case anyone else had the same question.
5
votes
Accepted
How do I depend on macros from other packages?
Firstly, that eval-when-compile method works fine for macros. It is a perfectly good way to use macros (not functions) from another package.
The problems you report are two different things.
the ...
5
votes
Accepted
How to quote for multiple levels of macro expansion
What should work is to drop the eval-after-load:
(use-package solarized-theme
:ensure t
:config
...
;; solarized-with-color-variables is a macro
(solarized-with-color-variables 'light
.....
5
votes
How to disable eager load-time macro expansion in emacs 24.3?
Looking at the code (specifically the readevalloop function in lread.c) shows that the following prevents eager macroexpansion from being attempted:
(fmakunbound 'internal-macroexpand-for-load)
...
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
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 ...
5
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-...
4
votes
Accepted
How to disable eager load-time macro expansion in emacs 24.3?
Here's something that does what you want, however, I doubt that that is what you really need. This doesn't really help debugging, if anything, it makes debugging more complicated:
(defmacro lazy-...
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 ...
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 ...
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 `(:...
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/" ,...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
elisp-macros × 124elisp × 25
byte-compilation × 9
functions × 7
eval × 5
backquote × 5
latex × 4
symbols × 4
lexical-scoping × 4
defmacro × 4
flycheck × 3
replace × 3
common-lisp × 3
doc-strings × 3
macroexpansion × 3
org-mode × 2
key-bindings × 2
auctex × 2
regular-expressions × 2
variables × 2
quote × 2
documentation × 2
require × 2
let-binding × 2
arguments × 2