Skip to content

Commit ad6bb49

Browse files
author
stites
committed
implement single-line yank and multi-line paste
1 parent 44641d2 commit ad6bb49

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

yi-keymap-vim/src/Yi/Keymap/Vim/VisualMap.hs

+25-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import Control.Monad (forM_, void, when)
1818
import Data.Char (ord)
1919
import Data.List (group)
2020
import Data.Maybe (fromJust, fromMaybe)
21-
import Data.Monoid ((<>))
22-
import qualified Data.Text as T (unpack, pack, Text)
21+
import qualified Data.Text as T (unpack)
2322
import Yi.Buffer hiding (Insert)
2423
import Yi.Editor
2524
import Yi.Keymap.Vim.Common
@@ -31,8 +30,7 @@ import Yi.Keymap.Vim.TextObject
3130
import Yi.Keymap.Vim.Utils (matchFromBool, mkChooseRegisterBinding, mkMotionBinding, addNewLineIfNecessary, pasteInclusiveB)
3231
import Yi.MiniBuffer (spawnMinibufferE)
3332
import Yi.Monad (whenM)
34-
import Yi.Region
35-
import qualified Yi.Rope as R (toText, countNewLines, YiString)
33+
import qualified Yi.Rope as R (toText, countNewLines)
3634
import Yi.Tag (Tag (Tag))
3735
import Yi.Utils (SemiNum ((-~)))
3836

@@ -252,10 +250,10 @@ pasteBinding = VimBindingE (f . T.unpack . _unEv)
252250

253251
pasteMatch :: RegionStyle -> VisualPaste -> MatchResult (EditorM RepeatToken)
254252
pasteMatch style p = WholeMatch $ do
255-
register <- getRegisterE . vsActiveRegister =<< getEditorDyn
256-
maybe (pure ()) (paste style p) register
257-
escAction
258-
return Finish
253+
register <- getRegisterE . vsActiveRegister =<< getEditorDyn
254+
maybe (pure ()) (paste style p) register
255+
void escAction
256+
return Finish
259257

260258
paste :: RegionStyle -> VisualPaste -> Register -> EditorM ()
261259
paste LineWise = linePaste
@@ -265,20 +263,32 @@ pasteBinding = VimBindingE (f . T.unpack . _unEv)
265263

266264
linePaste :: VisualPaste -> Register -> EditorM ()
267265
linePaste p (Register _style rope) = withCurrentBuffer $ do
268-
region <- regionOfSelectionB
269-
when (p == ReplaceSelection) . void $ deleteRegionWithStyleB region LineWise
270-
insertRopeWithStyleB (addNewLineIfNecessary rope) LineWise
266+
region <- regionOfSelectionB
267+
when (p == ReplaceSelection) . void $ deleteRegionWithStyleB region LineWise
268+
insertRopeWithStyleB (addNewLineIfNecessary rope) LineWise
271269

272270
blockPaste :: VisualPaste -> Register -> EditorM ()
273-
blockPaste p (Register _style rope) =
274-
withCurrentBuffer $ do
271+
blockPaste p (Register _style rope) = withCurrentBuffer $ do
275272
here <- pointB
276273
there <- getSelectionMarkPointB
277274
(here', there') <- flipRectangleB here there
278275
reg <- regionOfSelectionB
279-
when (p == ReplaceSelection) . void $ deleteRegionWithStyleB reg Block
280276
moveTo (minimum [here, there, here', there'])
281-
pasteInclusiveB rope _style
277+
when (p == ReplaceSelection) . void $ deleteRegionWithStyleB reg Block
278+
if R.countNewLines rope == 0
279+
then actionOnLeft reg $ maybe (pure ()) (\_ -> insertRopeWithStyleB rope _style)
280+
else pasteInclusiveB rope _style
281+
where
282+
-- Taken from deleteRegionWithStyleB
283+
actionOnLeft :: Region -> (Maybe Point -> BufferM ()) -> BufferM ()
284+
actionOnLeft reg action = savingPointB $ do
285+
(start, lengths) <- shapeOfBlockRegionB reg
286+
moveTo start
287+
forM_ (zip [0..] lengths) $ \(i, l) -> do
288+
p' <- pointB
289+
moveTo start
290+
void $ lineMoveRel i
291+
action (if l == 0 then Nothing else Just p')
282292

283293
otherPaste :: VisualPaste -> Register -> EditorM ()
284294
otherPaste _ (Register _style rope) = withCurrentBuffer $ do

0 commit comments

Comments
 (0)