publicvoit · 2022-05-16 10:04
Hi,
I've recorded a simple macro and I don't want to use it as a named macro. I prefer to use it as a function.
Is there a way to use the output of `last-kbd-macro` and create a function I can call interactively?
I was looking for this on the web and trying to find eval macro functions in Emacs but so far, I wasn't lucky. Maybe there is no possibility and everybody is preferring named macros?
whudwl · 2022-05-16 10:33
M-x name-last-kbd-macro
M-x insert-kbd-macro
publicvoit · 2022-05-16 10:37
OK, so a named macro then.
This results in a `(fset 'myname ...)`.
Does this come with any difference to a function except that there are no parameters?
whudwl · 2022-05-16 10:39
I don't think it's any different to a function.
publicvoit · 2022-05-16 10:50
Well, then I'll start using it as a named macro. Thanks for the confirmation!
[deleted] · 2022-05-16 11:06
[removed]
vifon · 2022-05-16 14:23
In theory: no idea.
In practice: I've been using such kmacro-generated functions as building blocks of larger automation commands with huge success. Basically some buffer navigation logic, kmacro1, some more logic, maybe a loop, then kmacro2, etc.
So I'd say you're relatively safe using them this way.
oantolin · 2022-05-16 15:01
As u/whudwl said, naming keyboard macros is fine. But if you really want to turn a keyboard macro into Emacs Lisp code, there is a package for that: [elmacro](https://github.com/Silex/elmacro).
publicvoit · 2022-05-16 15:53
Oh, this one looks golden. Need to try that.
Phil-Hudson · 2022-05-16 19:45
That `fset` binds your keyboard macro to the "function cell" (or "function property") of symbol `myname`, so in some sense a named macro *is* a function. What we normally think of as a function -- that is, one defined by `defun` -- binds a lambda list to that cell instead, so there is a difference to be found if you dig, it just never shows up in normal usage.