0

I would like to programmatically call an ivy or counsel function and then immediately execute ivy-occur to get the ivy-occur buffer.

Example:
Instead of doing M-x counsel-describe-function and C-c C-o manually to get the ivy-occur buffer I would to do this with a function.


Tried the following (the last three solution work but there has to be a better way):

Minibuffer-setup-hook

Works for some (counsel-M-x) but most commands give me an ivy-occur buffer with 0 candidates. So this is probably called too early:

(unwind-protect 
    (progn
      (add-hook 'minibuffer-setup-hook #'ivy-occur)
      (ignore-errors (counsel-faces)))
  (remove-hook 'minibuffer-setup-hook #'ivy-occur))

Minibuffer-with-setup-hook

Same problem as with the hook.

(ignore-errors
  (minibuffer-with-setup-hook
    (:append #'ivy-occur)
  (counsel-faces)))

Execute-kbd-macro

This actually works, but there has to be a better way to do this. This just simulates the keys pressed:

(execute-kbd-macro (kbd "M-x counsel-faces RET C-c C-o"))

;; Can even change the initial input like this (`ivy-`).
(execute-kbd-macro (kbd "M-x counsel-faces RET ivy- C-c C-o"))

Run-with-idle-timer

This works as well but is sort of strange too. Not even sure if this might be run before calling counsel-faces in some cases.

(progn
  (run-with-idle-timer 0.01 nil #'ivy-occur)
  (counsel-faces))

Unread-command-events

Found this in the question Clean way to perform commands in the Emacs minibuffer. But this also feels akward.

(progn
  (setq unread-command-events
      (append (listify-key-sequence (kbd "C-c C-o")) unread-command-events))
  (counsel-faces))

Thanks for any solution or a hint on how to do this properly.

0

Finally found a working solution that feels ok:

run-at-time with the argument nil will run the code as soon as possible, which is almost exactly after we're back into the command loop.

(progn
  (run-at-time nil nil #'ivy-occur)
  (counsel-faces))

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.