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?

1 Answer 1

5

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).

8
  • 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
    Commented Nov 24, 2017 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
    Commented Nov 24, 2017 at 16:59
  • 1
    I put some sample code you can use in your ~/.emacs which should work around the problem.
    – Stefan
    Commented Nov 25, 2017 at 16:42
  • 1
    I fixed the arglist, which was lacking the actual (ignored) argument. You can try again.
    – Stefan
    Commented Nov 27, 2017 at 5:06
  • 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
    Commented Nov 30, 2017 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.