module Text.LaTeX.LambdaTeX.Selection.Internal where
import Data.List (isPrefixOf)
import Text.LaTeX.LambdaTeX.Selection.Types
import Text.LaTeX.LambdaTeX.Types
selects :: Part -> [Selector] -> Bool
selects (Part ps) ss = go ps ss False
where
go :: [Text] -> [Selector] -> Bool -> Bool
go _ [] b = b
go ps (All:ss) _ = go ps ss True
go ps ((Match s):ss) b = if ps `matches` s
then go ps ss True
else go ps ss b
go ps ((Ignore s):ss) True = if ps `matches` s
then go ps ss False
else go ps ss True
go ps ((Ignore _):ss) False = go ps ss False
isSelected :: Monad m => ΛTeXT m Bool
isSelected = do
part <- currentPart
sels <- λasks configSelection
return $ selects part sels
currentPart :: Monad m => ΛTeXT m Part
currentPart = λgets stateCurrentPart
inPart :: Monad m => Text -> ΛTeXT m () -> ΛTeXT m ()
inPart partname func = do
pushCurrentPart partname
func
popCurrentPart
pushCurrentPart :: Monad m => Text -> ΛTeXT m ()
pushCurrentPart partname = λmodify (\s -> s { stateCurrentPart = pushPart (stateCurrentPart s) partname})
popCurrentPart :: Monad m => ΛTeXT m ()
popCurrentPart = λmodify (\s -> s { stateCurrentPart = popPart $ stateCurrentPart s })
split :: String -> [String]
split = splitOn '.'
splitOn :: Char -> String -> [String]
splitOn c s = go s []
where
go :: String -> String -> [String]
go [] s = [s]
go (sc:ss) acc | sc == c = acc : go [] ss
| otherwise = go ss (acc ++ [sc])
matches :: [Text] -> [Text] -> Bool
matches a b = (a `isPrefixOf` b) || (b `isPrefixOf` a)