6

In my ~/.emacs, there are several keyboard macros stored. They appear as (fset 'dhteu-macro-name ... entries. BTW, dhteu is just a random alphabet combination prefixed to avoid collision with any existing commands.

I can C-x C-k e M-x supply a macro name and a buffer opens allowing me to edit the macro. I can edit it and save it with C-c C-c. Now if I C-x C-k e M-x same macro name, I see the modified version. I tried executing the modified macro on a buffer. The madified macro executes successfully.

After all this, I naturally, want to store the macro. So, I visit ~/.emacs and M-x insert-kbd-macro. I expect the name of the macro I just edited to be available at this prompt. It isn't.

Entering the entire name produces a no match error.

I tried C-x C-k n to give it another name. That produces a No keyboard macro defined error.

So, the question is: How do I edit a stored keyboard macro and save it again?

Drew
  • 75,699
  • 9
  • 109
  • 225
deshmukh
  • 1,852
  • 13
  • 29

1 Answers1

4

This is a bug in Emacs. When saving the macro, Emacs fails to add the kmacro property, so that when you start a new session, Emacs doesn't remember that dhteu-macro-name is actually a keyboard macro.

The hack below should workaround the problem.

(defun my-kmacro-hack (&rest _)
  (interactive
   (list (intern (completing-read
                  "Insert kbd macro (name): "
                  obarray
                  (lambda (elt)
                    (and (fboundp elt)
                         (or (stringp (symbol-function elt))
                             (vectorp (symbol-function elt))
                             (kmacro-extract-lambda (symbol-function elt)))))
                  t))
         current-prefix-arg))
  nil)
(advice-add 'insert-kbd-macro :before #'my-kmacro-hack)

I installed a cleaner fix which will into Emacs's master branch (i.e. for Emacs-27).

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • I am a complete newbie. But I think there is a gap between what I have written and your reply. So, this comment. I can very well see all the stored (in ~/.emacs) macros. I can very well edit `C-x C-k e M-x name` any of them and save (`C-c C-c` in the macro editor) it for the session. But when I try to `insert-kbd-macro` it does not recognize this macro. If I record a new macro, there is no problem. Lastly, when do we expect to see Emacs27 in Kubuntu? :) – deshmukh Nov 24 '17 at 14:56
  • 1
    When you create a macro (with `C-x C-k n`), the *name* is marked to remember that it's a keyboard macro. But when you `C-x C-k n` that detail is not written, so when you restart Emacs, the command is defined but the name is not marked as a keyboard macro (and Emacs doesn't recognize the actual definition as a keyboard macro either). My patch makes Emacs recognize the definition instead of checking if the name is marked as a keyboard macro. – Stefan Nov 24 '17 at 16:59
  • Thanks. I think I understand it better now. But if such is the case with emacs as it stands now, what is the alternative? I have been a vim user for the past several years and I am so used to writing quick keyboard macros, editing them, etc. without any trouble. I am planning to migrate to emacs and this may prove to be a big hurdle. Are there any packages/ workarounds that you may suggest? – deshmukh Nov 25 '17 at 10:37
  • 1
    I put some sample code you can use in your `~/.emacs` which should work around the problem. – Stefan Nov 25 '17 at 16:42
  • Thanks a lot for the code. But in my case, it produced a Wrong number of arguments error. The error includes everything upto the advice line. Also, do you think elmacro is a solution? – deshmukh Nov 26 '17 at 05:31
  • 1
    I fixed the arglist, which was lacking the actual (ignored) argument. You can try again. – Stefan Nov 27 '17 at 05:06
  • Thanks a ton. That works perfectly. Can I remove that bit from my ~/.emacs when Emacs-27 is installed on my computer? I am running Kubuntu 17.10 – deshmukh Nov 30 '17 at 09:29
  • 1
    Yes, it should be both useless and harmless in Emacs-27 and it could become harmful in the future, so better remove it once you get there. – Stefan Nov 30 '17 at 13:26