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)))
@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

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