I want to improve my Elisp, but despite reading about the differences between functions and macros (including discussions here in Emacs SE and on Reddit), I still find examples where the distinction confuses me.
For example, in the "starter-pack" Radian, a macro can be found which wraps use-package
ensuring some sort of partial application:
(defmacro use-feature (name &rest args)
"Like `use-package', but with `straight-use-package-by-default' disabled."
(declare (indent defun))
`(use-package ,name
:straight nil
,@args))
As a case study, would it not be possible to achieve the same with a defun
? After all, this seems like a simple case of partial application, or am I missing something? Do we need a macro here just because use-package
itself is a macro which we don't want a defun
body to evaluate?
And would there be a safe way to improve this macro to take care of a possibly unwanted case (use-feature foo :straight t)
?
use-package
is a functions, user will have to quote the arguments which is inconvenient and ugly, e.g.,(use-package 'foo :if '(<= 1 2) :commands #'foo-bar :bind '("C-c C-c" . foo))
, the same applies touse-feature
, and ifuse-feature
is not a macro, the use of it won't be likeuse-package
.C-h i
, then chooseEmacs Lisp Intro
. In that intro-to-Elisp manual you'll find node Lisp macro, which will help. Or chooseElisp
, the Emacs-Lisp reference and guide. There you'll find node Macros, which fills you in about Elisp macros. Much better than getting ad hoc answers here or on Reddit (IMHO). You're not the first person to learn Lisp macros - trust Emacs to help.