module Yi.Keymap.Vim2.TextObject
( TextObject(..)
, CountedTextObject(..)
, regionOfTextObjectB
, changeTextObjectCount
, changeTextObjectStyle
, stringToTextObject
) where
import Control.Monad
import Yi.Buffer
import Yi.Keymap.Vim2.StyledRegion
data TextObject = TextObject !RegionStyle !TextUnit
data CountedTextObject = CountedTextObject !Int !TextObject
changeTextObjectCount :: Int -> CountedTextObject -> CountedTextObject
changeTextObjectCount n (CountedTextObject _ to) = CountedTextObject n to
regionOfTextObjectB :: CountedTextObject -> BufferM StyledRegion
regionOfTextObjectB = normalizeRegion <=< textObjectRegionB'
textObjectRegionB' :: CountedTextObject -> BufferM StyledRegion
textObjectRegionB' (CountedTextObject count (TextObject style unit)) =
fmap (StyledRegion style) $ regionWithTwoMovesB
(maybeMoveB unit Backward)
(replicateM_ count $ moveB unit Forward)
changeTextObjectStyle :: (RegionStyle -> RegionStyle) -> TextObject -> TextObject
changeTextObjectStyle smod (TextObject s u) = TextObject (smod s) u
stringToTextObject :: String -> Maybe TextObject
stringToTextObject ('i':s) = parseTextObject InsideBound s
stringToTextObject ('a':s) = parseTextObject OutsideBound s
stringToTextObject _ = Nothing
parseTextObject :: BoundarySide -> String -> Maybe TextObject
parseTextObject bs (c:[]) = fmap (TextObject Exclusive . ($ bs == OutsideBound)) mkUnit
where mkUnit = lookup c
[('w', toOuter unitViWord unitViWordAnyBnd)
,('W', toOuter unitViWORD unitViWORDAnyBnd)
,('p', toOuter unitEmacsParagraph unitEmacsParagraph)
,('s', toOuter unitSentence unitSentence)
,('"', unitDelimited '"' '"')
,('`', unitDelimited '`' '`')
,('\'', unitDelimited '\'' '\'')
,('(', unitDelimited '(' ')')
,(')', unitDelimited '(' ')')
,('b', unitDelimited '(' ')')
,('[', unitDelimited '[' ']')
,(']', unitDelimited '[' ']')
,('{', unitDelimited '{' '}')
,('}', unitDelimited '{' '}')
,('B', unitDelimited '{' '}')
,('<', unitDelimited '<' '>')
,('>', unitDelimited '<' '>')
]
parseTextObject _ _ = Nothing
toOuter :: TextUnit -> TextUnit -> Bool -> TextUnit
toOuter outer _ True = leftBoundaryUnit outer
toOuter _ inner False = inner