Skip to content

Commit

Permalink
Reposition cursor after deleteTrailingSpaceB
Browse files Browse the repository at this point in the history
Fixes #666
  • Loading branch information
Fuuzetsu committed Oct 24, 2014
1 parent 00deca4 commit 1bd9e02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,7 @@
-------

* Try to guess and preserve the file encoding (#638)
* Restore cursor position better after deleteTrailingSpacesB (#666)

0.10.0
------
Expand Down
6 changes: 4 additions & 2 deletions yi/src/library/Yi/Buffer/HighLevel.hs
Expand Up @@ -447,10 +447,12 @@ swapB = do eol <- atEol
when eol leftB
transposeB Character Forward

-- | Delete trailing whitespace from all lines
-- | Delete trailing whitespace from all lines. Uses 'savingPositionB'
-- to get back to where it was.
deleteTrailingSpaceB :: BufferM ()
deleteTrailingSpaceB =
regionOfB Document >>= modifyRegionB (tru . mapLines stripEnd)
regionOfB Document >>=
savingPositionB . modifyRegionB (tru . mapLines stripEnd)
where
-- Strips the space from the end of each line, preserving
-- newlines.
Expand Down
18 changes: 17 additions & 1 deletion yi/src/library/Yi/Buffer/Misc.hs
Expand Up @@ -107,6 +107,7 @@ module Yi.Buffer.Misc
, delOverlayLayerB
, savingExcursionB
, savingPointB
, savingPositionB
, pendingUpdatesA
, highlightSelectionA
, rectangleSelectionA
Expand Down Expand Up @@ -1035,14 +1036,29 @@ markPointA mark = lens getter setter where
getter b = markPoint $ getMarkValueRaw mark b
setter b pos = modifyMarkRaw mark (\v -> v {markPoint = pos}) b

-- | perform an @BufferM a@, and return to the current point
-- | Perform an @BufferM a@, and return to the current point.
savingPointB :: BufferM a -> BufferM a
savingPointB f = savingPrefCol $ do
p <- pointB
res <- f
moveTo p
return res

-- | Perform an @BufferM a@, and return to the current line and column
-- number. The difference between this and 'savingPointB' is that here
-- we attempt to return to the specific line and column number, rather
-- than a specific number of characters from the beginning of the
-- buffer.
--
-- In case the column is further away than EOL, the point is left at
-- EOL: 'moveToLineColB' is used internally.
savingPositionB :: BufferM a -> BufferM a
savingPositionB f = savingPrefCol $ do
(c, l) <- (,) <$> curCol <*> curLn
res <- f
moveToLineColB l c
return res

pointAt :: BufferM a -> BufferM Point
pointAt f = savingPointB (f *> pointB)

Expand Down

0 comments on commit 1bd9e02

Please sign in to comment.