(defun pretend (keys)
  (dolist (e (reverse (listify-key-sequence (kbd keys))))
    (push e unread-command-events)
    ;; (mystery-function)
    (redisplay t)
    (sleep-for 0.1)))

(pretend "M-x my-function-")

This function is intended to take key events and 'pretend' they were entered by the user at a reasonable pace of 100ms/key. Unfortunately, it doesn't seem like unread-command-events is being processed – probably because emacs is not idle during execution. One way to get around this is to abuse run-with-idle-timer, but this seems like the wrong approach.

How can I process/flush unread-command-events before continuing execution?

(I suspect I won't need the call to reverse after this is fixed.)

bumped to the homepage by Community 5 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

You could used recursive-edit to put yourself into a situation where your events will be consumed. To make control return to your code, you must also add (kbd "C-M-c") (a.k.a exit-recursive-edit) to unread-command-events.

One limitation is that you will have to push complete key sequences, so you can't return to your code between the C-x and 2 in the key sequence C-x 2.

One concrete example where this is used is in the test code for dynamic-spaces (a package that try to make text to the right of the edit location to say in place). (N.B., I was not aware of the function listify-key-sequence when I wrote it, so it could probably be simplified.)

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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