reddit / emacs

Tell me how terrible of an idea this is

a-c-d-w · 2022-05-11 03:07
I've been writing some convenience macros, and at times I want to pass a symbol to the macro, and other times the value of that symbol. Think of `mode-line-format`'s `:eval` keyword, etc. I've come up with this macro-helper function, but I feel like it's probably .... cursed. Let me know what you think! (defun +marg (arg) "Return ARG, unless comma-quoted. Then return ARG's value. This function really only makes sense in macros, e.g. (defmacro foo (bar) (let ((bar (+marg bar))) (do-something-with bar))) This will allow a call like (foo thing) to work with the symbol `thing', but (foo ,thing) to work with the value of `thing'. This functionality is probably cursed and probably shouldn't be used." (if (eq (car-safe arg) '\,) `,(cadr arg) `',arg)) The docstring should be fairly descriptive, imo, but I can further explain if need be.
usaoc · 2022-05-11 03:30
Every time you want the value stored in the *value cell* of a symbol, i.e. the dynamic value, you should use `symbol-value`, [which see](https://www.gnu.org/software/emacs/manual/html_node/elisp/Accessing-Variables.html). [*Common Lisp: The Language*](https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node78.html) states very clearly that `symbol-value` is strictly for this purpose. Do *not* ever deal with lexical variables in terms of symbols – Emacs Lisp’s macro system isn’t powerful enough for you to properly do this.
nv-elisp · 2022-05-11 04:31
(eval `(my-macro ,my-symbol)) But with the caveat u/usaoc gave about lexical bindings.