aef3ee7d2cbe

Avoid destroying record of last visual selection
[view raw] [browse files]
author Daniel Robert Allen <allen.daniel.r@gmail.com>
date Sun, 07 Apr 2013 11:33:46 -0700
parents 56683eb09ecc
children 0baa9f045283
branches/tags (none)
files chapters/34.markdown

Changes

--- a/chapters/34.markdown	Thu Apr 04 01:38:14 2013 -0400
+++ b/chapters/34.markdown	Sun Apr 07 11:33:46 2013 -0700
@@ -9,11 +9,14 @@
 ----------------
 
 By yanking the text into the unnamed register we destroy anything that was
-previously in there.
+previously in there.  Further, by using a visual selection to yank the text in
+the case that our operator is applied with a motion, we also destroy any record
+of the most recent visual selection.
 
-This isn't very nice to our users, so let's save the contents of that register
-before we yank and restore it after we've done.  Change the code to look like
-this:
+This isn't very nice to our users, so let's avoid using a visual selection in
+that case and also save the contents of the unnamed register before we yank in
+all cases so that we can restore it after we're done.  Change the code to look
+like this:
 
     :::vim
     nnoremap <leader>g :set operatorfunc=GrepOperator<cr>g@
@@ -25,7 +28,7 @@
         if a:type ==# 'v'
             normal! `<v`>y
         elseif a:type ==# 'char'
-            normal! `[v`]y
+            normal! `[y`]
         else
             return
         endif
@@ -38,6 +41,8 @@
 
 We've added two `let` statements at the top and bottom of the function.  The
 first saves the contents of `@@` into a variable and the second restores it.
+Additionally, we've applied yank with a motion rather than a visual selection in
+the case that our operator is applied with a motion.
 
 Write and source the file.  Make sure it works by yanking some text, then
 pressing `<leader>giw` to run our operator, then pressing `p` to paste the text