module Yi.Buffer.Normal (TextUnit(Character, Line, VLine, Document, GenUnit),
outsideUnit,
leftBoundaryUnit,
unitWord,
unitViWord,
unitViWORD,
unitViWordAnyBnd,
unitViWORDAnyBnd,
unitViWordOnLine,
unitViWORDOnLine,
unitDelimited,
unitSentence, unitEmacsParagraph, unitParagraph,
isAnySep, unitSep, unitSepThisLine, isWordChar,
moveB, maybeMoveB,
transformB, transposeB,
regionOfB, regionOfNonEmptyB, regionOfPartB,
regionOfPartNonEmptyB, regionOfPartNonEmptyAtB,
readPrevUnitB, readUnitB,
untilB, doUntilB_, untilB_, whileB, doIfCharB,
atBoundaryB,
numberOfB,
deleteB, genMaybeMoveB,
genMoveB, BoundarySide(..), genAtBoundaryB,
genEnclosingUnit, genUnitBoundary,
checkPeekB
, RegionStyle(..)
, mkRegionOfStyleB
, convertRegionToStyleB
, unitWiseRegion
, extendRegionToBoundaries
, regionStyleA
) where
import Prelude(length, subtract)
import Yi.Prelude
import Data.Binary
import Data.DeriveTH
import Data.List (sort)
import Yi.Buffer.Basic
import Yi.Buffer.Misc
import Yi.Buffer.Region
import Yi.Buffer.TextUnit
import Yi.Dynamic
data RegionStyle = LineWise
| Inclusive
| Exclusive
| Block
deriving (Eq, Typeable, Show)
$(derive makeBinary ''RegionStyle)
instance Initializable RegionStyle where
initial = Inclusive
instance YiVariable RegionStyle
regionStyleA :: Accessor FBuffer RegionStyle
regionStyleA = bufferDynamicValueA
convertRegionToStyleB :: Region -> RegionStyle -> BufferM Region
convertRegionToStyleB r = mkRegionOfStyleB (regionStart r) (regionEnd r)
mkRegionOfStyleB :: Point -> Point -> RegionStyle -> BufferM Region
mkRegionOfStyleB start' stop' regionStyle =
let [start, stop] = sort [start', stop']
region = mkRegion start stop in
case regionStyle of
LineWise -> inclusiveRegionB =<< unitWiseRegion Line region
Inclusive -> inclusiveRegionB region
Exclusive -> return region
Block -> return region
unitWiseRegion :: TextUnit -> Region -> BufferM Region
unitWiseRegion unit = extendRegionToBoundaries unit InsideBound OutsideBound
extendRegionToBoundaries :: TextUnit -> BoundarySide -> BoundarySide -> Region -> BufferM Region
extendRegionToBoundaries unit bs1 bs2 region = savingPointB $ do
moveTo $ regionStart region
genMaybeMoveB unit (Backward, bs1) Backward
start <- pointB
moveTo $ regionEnd region
genMaybeMoveB unit (Forward, bs2) Forward
stop <- pointB
return $ mkRegion' (regionDirection region) start stop