1

In my init file, I have these bindings:

(global-set-key (kbd "C-x S-SPC") (kbd ")"))
(global-set-key (kbd "S-SPC") (kbd "("))

Now, starting with empty buffer, if I define this keyboard macro:

S-SPC f o o C-x S-SPC RET

edit-kbd-macro shows:

Command: last-kbd-macro
Key: none

Macro:

S-SPC
(foo            ;; self-insert-command * 4
C-x S-SPC
)           ;; self-insert-command
RET         ;; newline

Then when I repeat my macro, I end up with the following buffer contents:

(foo)
((foo))

Obviously I want the second line to be exactly like the first one, how can I fix it?

  • 2
    I can confirm what you see. Looks like a bug to me. Please consider filing a bug report: M-x report-emacs-bug. But wait a little while, to see if someone else here thinks the behavior is to be expected for some reason. (I also checked that the particular keys bound to the two commands don't matter.) – Drew Jun 8 '17 at 18:10
  • Also confirmed. When a keyboard macro is invoked by a key during the recording of another keyboard macro, both the key and the output of the invoked macro are recorded. I agree that it seems unexpected. – phils Jun 8 '17 at 22:32
  • Do you know if the solution I posted is equivalent/safe to use and why I need to use the timer to set the keys? – mkcms Jun 10 '17 at 10:44
1

I solved(?) my problem by binding my keys in init file like this:

(run-with-timer 2.0 nil
        (lambda ()
          (define-key input-decode-map (kbd "S-SPC") (kbd "("))
          (define-key input-decode-map (kbd "C-x S-SPC") (kbd ")"))))

With these bindings, edit-kbd-macro shows (for the macro in question):

Command: last-kbd-macro
Key: none

Macro:

S-SPC
foo         ;; self-insert-command * 3
C-x S-SPC
RET         ;; newline

I don't know why, but I need the timer to define the keys some time after initialization or otherwise they're not set. I tried hooking the lambda to after-init-hook, emacs-startup-hook etc. but it doesn't work.

  • Is it the timer, or the fact that you're using input-decode-map (or both)? – npostavs Aug 9 '17 at 12:19
  • It's input-decode-map. I'm just using the timer to set those bindings after emacs initialization. – mkcms Aug 9 '17 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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