I'm trying to implement user command aliases with a customizable prefix for my package. I wrote two nested macros to accomplish this. The trs-defalias-from-suffix macro returns the correct value as if an alias was created, but none exists when I search for it with M-x. However, when I manually execute the macroexpand output with C-x C-e, the code works. It creates an alias.
That shouldn't be possible, AFAIK. Also, there is probably a better way to accomplish my goal.
I would like to know how to make the macros work, and/or a better way to make the customizable-prefix aliases.
The goal is to take the original function
trs-refile
and make an alias with a different prefix, for example "foobar-"
foobar-refile
If this seems like a strange goal, it's because I want a short prefix, but need to change "trs-" to something longer to be accepted by MELPA.
#+begin_src elisp
(setq trs-alias-prefix "foobar") ; an example value, because I didn't include the defcustom code.
(defun trs-alias-oldname (suffix)
"Reconstruct original function name from SUFFIX."
(make-symbol (concat "trs-" suffix)))
(defun trs-alias-newname (suffix)
"Make new function name from SUFFIX."
(make-symbol (concat trs-alias-prefix "-" suffix)))
(defun trs-alias-name-list (suffix)
"Make a list of new and old function names from SUFFIX."
(list (trs-alias-newname suffix) (trs-alias-oldname suffix)))
(defmacro trs-defalias-from-names (newname oldname)
"Make a defalias with NEWNAME and OLDNAME."
`(defalias ',newname ',oldname))
(defmacro trs-defalias-from-suffix (suffix)
"Make a defalias from SUFFIX."
(let ((trs-alias-name-list (trs-alias-name-list suffix)))
`(trs-defalias-from-names ,(car trs-alias-name-list) ,(nth 1 trs-alias-name-list))))
(trs-defalias-from-suffix "refile")
(trs-defalias-from-suffix "refile-up")
(trs-defalias-from-suffix "delete-this-buffer-and-file")
(trs-defalias-from-suffix "store-link-fold-drawer")
(trs-defalias-from-suffix "dired-zinks")
(trs-defalias-from-suffix "duplicate-heading-to-other-window")
(trs-defalias-from-suffix "region-ends-n-newlines")
(macroexpand '(trs-defalias-from-suffix "refile")) ; this returns the same as below
(defalias (quote leo-refile) (quote trs-refile)) ; this works
#+end_src
These are similar questions, and might already contain the answer: How to give a list as arguments to a macro in elisp? elisp macro to write a function?
Before I dive deeper into learning macros, however, I'd appreciate knowing that I'm headed in the right direction.
make-symbol
returns an uninterned symbol.
Use intern
to get the canonical interned symbol.
The printed representation of the symbol is the same in either case (i.e. the symbol's name) which is why evaluating the printed representation of the macroexpand
output gives a different result (because the lisp reader interns the symbol names).