diff --git a/CHANGELOG b/CHANGELOG index f38c657fb..b3289514d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ ------- * Try to guess and preserve the file encoding (#638) + * Restore cursor position better after deleteTrailingSpacesB (#666) 0.10.0 ------ diff --git a/yi/src/library/Yi/Buffer/HighLevel.hs b/yi/src/library/Yi/Buffer/HighLevel.hs index 801783cae..3252d114d 100644 --- a/yi/src/library/Yi/Buffer/HighLevel.hs +++ b/yi/src/library/Yi/Buffer/HighLevel.hs @@ -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. diff --git a/yi/src/library/Yi/Buffer/Misc.hs b/yi/src/library/Yi/Buffer/Misc.hs index b4678bbe4..58006643f 100644 --- a/yi/src/library/Yi/Buffer/Misc.hs +++ b/yi/src/library/Yi/Buffer/Misc.hs @@ -107,6 +107,7 @@ module Yi.Buffer.Misc , delOverlayLayerB , savingExcursionB , savingPointB + , savingPositionB , pendingUpdatesA , highlightSelectionA , rectangleSelectionA @@ -1035,7 +1036,7 @@ 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 @@ -1043,6 +1044,21 @@ savingPointB f = savingPrefCol $ do 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)