0

In many cases emacs doesn't execute kbd macros exactly as it should be. For example

(execute-kbd-macro (read-kbd-macro "C-x C-f"))

Rather than to end execution of this macros with open minibuffer Find file: ... emacs just opens the first available file or dired buffer (depending on the situation). How to execute such kinds of kbd macroses without such misunderstandings?

Upd 2. It is just example of such kind of keyboard macroses that behaves differently from the original behavior (i.e. C-x C-f pressed). Below is another example with grep.

Upd. As @lawlist mentioned I can use

(key-binding (kbd "C-x C-f"))

for transformation key sequence to command name. But it only works when there is the command. In a more complex case, e.g.

(execute-kbd-macro (read-kbd-macro "C-u M-x grep RET"))

this "brute-force" method doesn't work (I want to continue editing pattern for grep but emacs forcedly finishes the interaction).

artscan
  • 2,340
  • 15
  • 26
  • I think this related thread may answer your question?: http://emacs.stackexchange.com/a/7495/2287 – lawlist Feb 08 '15 at 16:55
  • Thank you, but in the most simple case only. It seems I have to manually transform key sequence to command equivalent in each particular case. – artscan Feb 08 '15 at 22:05
  • I don't see what macros have to do with this? Aren't you just running functions here? `C-x C-f` isn't a macro, it defaultly invokes the function `find-file` – Squidly Feb 10 '15 at 12:13
  • But `(execute-kbd-macro (read-kbd-macro "C-x C-f"))` is a macro and it doesn't behave exactly as the function `find-file` call. See Upd 2. – artscan Feb 10 '15 at 12:56

1 Answers1

0

The function kbd-macro-query can help in this situation

(execute-kbd-macro (read-kbd-macro "C-x C-f C-u C-x q"))

But there is a problem: command loop level increases (you can see appearance of the square brackets in modeline). In case of find-file example it doesn't matter, but

(execute-kbd-macro (read-kbd-macro "M-x helm-do-grep RET C-u C-x q"))

gives wrong behavior. I devise interesting workaround

(defun eab/kbd-macro-query ()
  (interactive)
  (let ((executing-kbd-macro defining-kbd-macro))
    (run-with-timer 0.1 nil 'abort-recursive-edit)
    (recursive-edit)))

(global-set-key (kbd "C-x Q") 'eab/kbd-macro-query)

(let ((minibuffer-message-timeout 0))
  (execute-kbd-macro (read-kbd-macro "M-x helm-do-grep RET C-x Q")))

and it works fine.

(let ((minibuffer-message-timeout 0))
  (execute-kbd-macro (read-kbd-macro "C-u M-x grep RET C-x Q")))

also works as it should be.

But now there is another problem with ido-find-file (C-x C-f in ido-mode)

(let ((minibuffer-message-timeout 0))
  (execute-kbd-macro (read-kbd-macro "C-x C-f C-x Q")))

There is something wrong with ido-text and [No match].

Sum. The main idea of this method is the interception of kbd macro execution with the use of recursive-edit. (kbd-macro-query with non-nil arg uses it). There is some problem with ido-mode but in general it works well even with complicated kbd sequences like

(let ((minibuffer-message-timeout 0))
  (execute-kbd-macro (read-kbd-macro "C-u M-x helm-do-grep RET C-x Q")))
artscan
  • 2,340
  • 15
  • 26