reddit / emacs

How to retrieve argument value passed to macros?

EmilySeville7cfg · 2022-12-28 10:17
For instance I wanna create a macro which accepts variable names and prints some output in the following form: `variable-1 = value-1, variable-2 = value-2`. Syntax for call must be: `(my-macro variable-1 variable-2)`. I don't know how to retrieve variable name and whether macros is the right way to implement what I want.
deaddyfreddy · 2022-12-28 10:37
the simplest case for one argument (defmacro my-macro (arg) `(format "%s = %S" ,(substring-no-properties (symbol-name arg)) ,arg))
eleven_cupfuls · 2022-12-28 17:35
The key here is that the arguments to macros are the _symbols_ themselves. They are unevaluated. You can think about them as if they are quoted: in `(my-macro variable-1 variable-2)` the args are like the args in `(my-normal-function 'variable-1 'variable-2)`. And given that, you can do to them whatever you would do with a symbol anywhere else. For example, you can call `symbol-name` on it, as u/deaddyfreddy demonstrated.