Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use evil commands with avy-dispatch-alist
(setf (alist-get ?w avy-dispatch-alist)
#'my/avy-evil-delete)
(setf (alist-get ?y avy-dispatch-alist)
#'my/avy-evil-copy)
(setf (alist-get ?c avy-dispatch-alist)
#'my/avy-evil-change)
(setf (alist-get ?a avy-dispatch-alist)
#'my/avy-embark-act)
(defun my/avy-embark-act (pt)
(unwind-protect
(save-excursion
(goto-char pt)
(embark-act))
(select-window
(cdr
(ring-ref avy-ring 0))))
t)
(defvar my/evil-extract-count-keys nil)
(defun my/evil-extract-count (orig-fn keys)
"When an evil operation is called from avy `evil-extract-count'
will get the last key given to avy as input. In order to behave
normally we wrap the function so that we can give the input
manually using a let-binding."
(funcall orig-fn
(or my/evil-extract-count-keys keys)))
(advice-add #'evil-extract-count
:around #'my/evil-extract-count)
(defun my/avy-evil-delete (pt)
(interactive)
(save-excursion
(goto-char pt)
(let ((my/evil-extract-count-keys "d"))
(call-interactively #'evil-delete)))
(select-window
(cdr
(ring-ref avy-ring 0)))
t)
(defun my/avy-evil-copy (pt)
(interactive)
(save-excursion
(goto-char pt)
(let ((my/evil-extract-count-keys "y"))
(call-interactively #'evil-yank)))
(select-window
(cdr
(ring-ref avy-ring 0)))
t)
(defvar my/avy-evil-change-marker nil
"The place where the user called ivy from.")
(defun my/avy-evil-change (pt)
(interactive)
(setq my/avy-evil-change-marker (point-marker))
(goto-char pt)
(add-hook 'evil-insert-state-exit-hook #'my/avy-evil-change-h)
(call-interactively #'evil-change)
t)
(defun my/avy-evil-change-h ()
(remove-hook 'evil-insert-state-exit-hook #'my/avy-evil-change-h)
(select-window
(cdr
(ring-ref avy-ring 0)))
(goto-char (marker-position my/avy-evil-change-marker)))
@agenbite
Copy link

agenbite commented Mar 8, 2022

This is great, thank you!!

There's one thing that I don't get, though: when using avy with evil, for example to delete up to certain characters, the delete operation "bites" on the first of the hinted chars... Wouldn't it be better to stop just before these selected chars? I've tried to make sense of your code, but I'm still not that fluent in elisp... :)

@Zetagon
Copy link
Author

Zetagon commented Mar 8, 2022

@agenbite I'm not quite sure what you mean. What avy command are you using this with? I'm using evil-avy-goto-char-timer to do an evil operation from an arbitrary point on the screen.

@agenbite
Copy link

agenbite commented Mar 8, 2022

I'm sorry. I believe I'm not understanding the difference between using evil and then avy or using avy and then evil.

In principle, I see two ways to use avy and evil together: a) call an evil action and then call avy to select the (end of) the motion to which the action is to be applied (starting at point); b) call avy to select a word elsewhere in the buffer and then select from the dispatcher one of the evil options you created, which will wait for a motion and will apply it as if the point was at the selected word, while staying where it first was.

In a), I would, for example, be wanting to delete some words ahead, but be too lazy to count them. Then avy would come to the rescue and help me tell evil the motion I want to perform the action on. In b), I'd want to copy N words from elsewhere in the frame, so I'd call avy to fix the start of my words and then use evil to yank those N words.

I guess the intended behaviour of your code is b), right?

Thank you!!

@Zetagon
Copy link
Author

Zetagon commented Mar 9, 2022

Yeah exactly, my code implements b) and has nothing to do with a). With that said, I tried (using Doom Emacs) to do a) which seems to work. It will delete up until the the character I selected with avy (similar to how t works with vim).

@agenbite
Copy link

agenbite commented Mar 9, 2022

Cool, thanks for the help! :) Your code works beautifully for b).

And regarding a), it bothers me that, in my case, it deletes not "up until" the caracter selected with avy, (as with t), but more as with f: it deletes up until that character and that character. It's not that important, in either case.

Cheers!

@agenbite
Copy link

agenbite commented Mar 14, 2022

Thanks to your help, I've settled down with this config for myself.

It's confusing how avy calls "yank" to yank-and-put and "copy" to yank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment