Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evil interacts badly with yasnippet when text selected with visual-line state #254

Open
TheBB opened this issue Feb 18, 2013 · 4 comments
Open

Comments

@TheBB
Copy link
Member

TheBB commented Feb 18, 2013

Originally reported by: Mark Edgington (Bitbucket: edgimar, GitHub: edgimar)


When using yasnippet (https://github.com/capitaomorte/yasnippet), if I have a snippet like the following:

# name: \begin{environment} ... \end{environment}
# key: begin
# --
\begin{${1:environment}}
  `yas/selected-text`$0
\end{$1}

then if I select some lines in visual-line state and do M-x yas-insert-snippet, choosing the snippet above, then the first line of the snippet is highlighted, with the cursor at the beginning of 'environment'. At this point, evil is still in visual-state for a few seconds, and then returns (without any user-interaction) to normal-state, whereupon the cursor-position is moved to the beginning of the line.

The desired behavior would be like it behaves if evil-mode is disabled, and I select a couple of lines and then execute the yas-insert-snippet function. In this case, the final cursor-position remains on the first character of 'environment', and subsequent typing will be mirrored in the \end{} line.

I don't know if this is a bug in Evil or in yasnippet, but I don't know how to determine this. In any case, as it currently is, it seems impossible to use Evil's visual-line state selections with yasnippet.

Note that I am able to get visual-state selections do work if I use:

(add-hook 'yas-before-expand-snippet-hook (lambda ()
    (evil-insert-state)))

But, if this hook is active, and I try the snippet with visual-line state selections, then the selections are deleted.


@TheBB
Copy link
Member Author

TheBB commented Feb 18, 2013

Original comment by Frank Fischer (Bitbucket: lyro, GitHub: lyro):


Hm, it seems as if Evil's region adjustment does not work well with yasnippet. The problem is that Evil restores the original position of point and mark after the command yas-snippet-insert finishes. This needs some additional work, because currently there is no way to inhibit this behaviour. But if you modify your hook so that it sets the (Emacs) region appropriately after switching to insert state, it should work as expected:

#!common-lisp
(add-hook 'yas-before-expand-snippet-hook
		  #'(lambda()
			  (when (evil-visual-state-p)
				(let ((p (point))
					  (m (mark)))
				  (evil-insert-state)
				  (goto-char p)
				  (set-mark m)))))

@TheBB
Copy link
Member Author

TheBB commented Feb 19, 2013

Original comment by Mark Edgington (Bitbucket: edgimar, GitHub: edgimar):


Just to confirm, I've tested your modified hook, and it does work as a 'workaround' for the present time.

@amosbird
Copy link
Contributor

hmm, that workaround doesn't work anymore. Selection isn't cleared when entering yasnippet state. And there is one more issue. The indentation is wrong after inserting the wrapping snippet.

@amosbird
Copy link
Contributor

I finally resolve this by

  (setq yas-wrap-around-region ?y)
  (defun evil-visual-insert-snippet ()
    (interactive)
    (when (evil-visual-state-p)
      (call-interactively #'narrow-to-region)
      (execute-kbd-macro "gv")
      (setq evil-this-register ?y)
      (execute-kbd-macro "y")
      (call-interactively #'widen)
      (execute-kbd-macro "gv")
      (setq evil-this-register ?n)
      (call-interactively #'evil-substitute)
      (yas-insert-snippet)))

along with https://github.com/emallson/narrow-reindent.el/blob/master/narrow-reindent.el

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

No branches or pull requests

2 participants