I'd like to use ivy completion to select items from my hydra rather than using the specified keymap. The use case would be for a very large hydra. I could create nested hydras, but I want to be lazy and just keep adding stuff to one large hydra without spending time organizing or formatting it. Technically, a hydra isn't required for the solution. Could also just be a list of functions, preferably with hints, and a method to select them via ivy. Thanks.
1 Answer
Instead of starting from hydra, I would start from counsel-M-x. This function uses ivy-read
, which wants a list of possible inputs, and a function to run once you've chosen one. You can provide it a list of the commands you want to run, and use counsel-M-x-action
as the function to run. Here's an example:
(defvar my/commands '(shell scroll-down text-mode))
(defun my/commands ()
(interactive)
(ivy-read "Command: " my/commands
:caller 'my-commands
:require-match t
:action #'counsel-M-x-action))
For customization, Ivy has "display transformers", which let you display something other than the command name. You could use this to add hints. First, add :caller 'my-commands
to the ivy-read, so that this command has a name. Then, use ivy-configure
to tell Ivy to use your function for displaying text:
(defun my/commands-display (candidate)
(format "%s - add hint here" candidate))
(ivy-configure 'my-commands
:display-transformer-fn #'my/commands-display)
-
Thanks. This is great. Could you elaborate on ivy-set-display-transformer. Can't seem to figure out how to use it for this use case. Commented Aug 26, 2020 at 20:05
-
Sure, l'll edit the answer with an example of ivy-set-display-transformer, which seems to have recently changed its name to ivy-configure.– amitpCommented Aug 27, 2020 at 15:25
-
Thanks. How do I get it to display different hits for each candidate. This is what I tried, but not working. ''' (defun my/commands-display (candidate) (setq x '((scroll-down . "scroll down hint") (text-mode . "text mode hint"))) (cdr(assoc candidate x))) (ivy-configure 'my-commands :display-transformer-fn #'my/commands-display) ''' Commented Aug 31, 2020 at 17:42
-
Looks like the display transformer receives the string that was going to be displayed, and needs to return another string. So if you change the symbols to strings it should work:
(defun my/commands-display (candidate) (let ((x '(("scroll-down" . "scroll down hint") ("text-mode" . "text mode hint")))) (cdr (assoc candidate x))))
– amitpCommented Sep 2, 2020 at 16:25