Convert +snippets/expand-on-region to advice

It's meant to be corrective, not alternative behavior.
This commit is contained in:
Henrik Lissner 2019-05-31 20:11:06 -04:00
parent ff25e3c08f
commit dcdebdb283
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 23 additions and 19 deletions

View file

@ -1,16 +0,0 @@
;;; editor/snippets/autoload/evil.el -*- lexical-binding: t; -*-
;;;###if (featurep! :editor evil)
;;;###autoload
(defun +snippets/expand-on-region ()
"Only use this with `evil-mode'. Expands a snippet around a selected region
and switches to insert mode if there are editable fields."
(interactive)
(when (evil-visual-state-p)
(evil-visual-select evil-visual-beginning evil-visual-end 'inclusive))
(cl-letf (((symbol-function 'region-beginning) (lambda () evil-visual-beginning))
((symbol-function 'region-end) (lambda () evil-visual-end)))
(yas-insert-snippet))
(when (yas-active-snippets)
(evil-insert-state +1)))

View file

@ -110,6 +110,26 @@ buggy behavior when <delete> is pressed in an empty field."
(message "This is a built-in snippet, enabling read only mode. Use `yas-new-snippet' to redefine snippets")))
;;
;;; Advice
;;;###autoload
(defun +snippets*expand-on-region (orig-fn &optional no-condition)
"Fix off-by-one issue with expanding snippets on an evil visual region, and
switches to insert mode.
If evil-local-mode isn't enabled, run ORIG-FN as is."
(if (not (and (bound-and-true-p evil-local-mode)
(evil-visual-state-p)))
(funcall orig-fn no-condition)
(evil-visual-select evil-visual-beginning evil-visual-end 'inclusive)
(cl-letf (((symbol-function 'region-beginning) (lambda () evil-visual-beginning))
((symbol-function 'region-end) (lambda () evil-visual-end)))
(funcall orig-fn no-condition))
(when (yas-active-snippets)
(evil-insert-state +1))))
;;
;; Commands