module Yi.Buffer.Region
(
module Yi.Region
, swapRegionsB
, deleteRegionB
, replaceRegionB
, replaceRegionClever
, readRegionB
, mapRegionB
, modifyRegionB
, modifyRegionClever
, winRegionB
, inclusiveRegionB
, blockifyRegion
)
where
import Data.Algorithm.Diff
import Yi.Region
import Yi.Buffer.Misc
import Yi.Prelude
import Prelude ()
import Data.List (length, sort)
import Yi.Window (winRegion)
winRegionB :: BufferM Region
winRegionB = askWindow winRegion
deleteRegionB :: Region -> BufferM ()
deleteRegionB r = deleteNAt (regionDirection r) (fromIntegral (regionEnd r ~- regionStart r)) (regionStart r)
readRegionB :: Region -> BufferM String
readRegionB r = nelemsB (fromIntegral (regionEnd r i)) i
where i = regionStart r
replaceRegionB :: Region -> String -> BufferM ()
replaceRegionB r s = do
deleteRegionB r
insertNAt s (regionStart r)
replaceRegionClever :: Region -> String -> BufferM ()
replaceRegionClever region text' = savingExcursionB $ do
text <- readRegionB region
let diffs = getGroupedDiff text text'
moveTo (regionStart region)
forM_ diffs $ \(d,str) -> do
case d of
F -> deleteN $ length str
B -> rightN $ length str
S -> insertN str
mapRegionB :: Region -> (Char -> Char) -> BufferM ()
mapRegionB r f = do
text <- readRegionB r
replaceRegionB r (fmap f text)
swapRegionsB :: Region -> Region -> BufferM ()
swapRegionsB r r'
| regionStart r > regionStart r' = swapRegionsB r' r
| otherwise = do w0 <- readRegionB r
w1 <- readRegionB r'
replaceRegionB r' w0
replaceRegionB r w1
replToMod :: (Region -> a -> BufferM b) -> (String -> a) -> Region -> BufferM b
replToMod replace transform region = replace region =<< transform <$> readRegionB region
modifyRegionB :: (String -> String)
-> Region
-> BufferM ()
modifyRegionB = replToMod replaceRegionB
modifyRegionClever :: (String -> String) -> Region -> BufferM ()
modifyRegionClever = replToMod replaceRegionClever
inclusiveRegionB :: Region -> BufferM Region
inclusiveRegionB r =
if regionStart r <= regionEnd r
then mkRegion (regionStart r) <$> pointAfter (regionEnd r)
else mkRegion <$> pointAfter (regionStart r) <*> pure (regionEnd r)
where pointAfter p = pointAt $ do
moveTo p
rightB
blockifyRegion :: Region -> BufferM [Region]
blockifyRegion r = savingPointB $ do
[lowCol,highCol] <- sort <$> mapM colOf [regionStart r, regionEnd r]
startLine <- lineOf $ regionStart r
endLine <- lineOf $ regionEnd r
when (startLine > endLine) $ fail "blockifyRegion: impossible"
mapM (\line -> mkRegion <$> pointOfLineColB line lowCol <*> pointOfLineColB line (1 + highCol))
[startLine..endLine]