{-# LANGUAGE PatternGuards #-}
module XMonad.Util.Stack (
Zipper
, emptyZ
, singletonZ
, fromIndex
, toIndex
, fromTags
, toTags
, insertUpZ
, insertDownZ
, swapUpZ
, swapDownZ
, swapMasterZ
, focusUpZ
, focusDownZ
, focusMasterZ
, findS
, findZ
, getFocusZ
, getIZ
, sortZ
, sortByZ
, mapZ
, mapZ_
, mapZM
, mapZM_
, onFocusedZ
, onFocusedZM
, onIndexZ
, onIndexZM
, filterZ
, filterZ_
, deleteFocusedZ
, deleteIndexZ
, foldrZ
, foldlZ
, foldrZ_
, foldlZ_
, elemZ
, getI
, tagBy
, fromE
, mapE
, mapE_
, mapEM
, mapEM_
, reverseS
, reverseZ
) where
import qualified XMonad.StackSet as W
import Control.Applicative ((<|>),(<$>),(<$))
import Control.Monad (guard,liftM)
import Data.List (sortBy)
type Zipper a = Maybe (W.Stack a)
emptyZ :: Zipper a
emptyZ = Nothing
singletonZ :: a -> Zipper a
singletonZ a = Just $ W.Stack a [] []
fromIndex :: [a] -> Int -> Zipper a
fromIndex as i = fromTags $ zipWith ($) (replicate i Left ++ [Right] ++ repeat Left) as
toIndex :: Zipper a -> ([a], Maybe Int)
toIndex Nothing = ([], Nothing)
toIndex (Just s) = (W.integrate s, Just $ length $ W.up s)
fromTags :: [Either a a] -> Zipper a
fromTags = finalize . foldr step ([], Nothing, [])
where step (Right a) (u, Just f, d) = ([], Just a, u++f:d)
step (Right a) (u, Nothing, d) = (u, Just a, d)
step (Left a) (u, Just f, d) = (a:u, Just f, d)
step (Left a) (u, Nothing, d) = (u, Nothing, a:d)
finalize (u, Just f, d) = Just $ W.Stack f (reverse u) d
finalize (u, Nothing, a:d) = Just $ W.Stack a (reverse u) d
finalize (_, Nothing, []) = Nothing
toTags :: Zipper a -> [Either a a]
toTags Nothing = []
toTags (Just s) = map Left (reverse . W.up $ s) ++ [Right . W.focus $ s]
++ map Left (W.down s)
insertUpZ :: a -> Zipper a -> Zipper a
insertUpZ a Nothing = W.differentiate [a]
insertUpZ a (Just s) = Just s { W.focus = a , W.down = W.focus s : W.down s }
insertDownZ :: a -> Zipper a -> Zipper a
insertDownZ a Nothing = W.differentiate [a]
insertDownZ a (Just s) = Just s { W.focus = a, W.up = W.focus s : W.up s }
swapUpZ :: Zipper a -> Zipper a
swapUpZ Nothing = Nothing
swapUpZ (Just s) | u:up <- W.up s = Just s { W.up = up, W.down = u:W.down s}
swapUpZ (Just s) = Just s { W.up = reverse (W.down s), W.down = [] }
swapDownZ :: Zipper a -> Zipper a
swapDownZ Nothing = Nothing
swapDownZ (Just s) | d:down <- W.down s = Just s { W.down = down, W.up = d:W.up s }
swapDownZ (Just s) = Just s { W.up = [], W.down = reverse (W.up s) }
swapMasterZ :: Zipper a -> Zipper a
swapMasterZ Nothing = Nothing
swapMasterZ (Just (W.Stack f up down)) = Just $ W.Stack f [] (reverse up ++ down)
focusUpZ :: Zipper a -> Zipper a
focusUpZ Nothing = Nothing
focusUpZ (Just s) | u:up <- W.up s = Just $ W.Stack u up (W.focus s:W.down s)
focusUpZ (Just s) | null $ W.down s = Just s
focusUpZ (Just (W.Stack f _ down)) = Just $ W.Stack (last down) (reverse (init down) ++ [f]) []
focusDownZ :: Zipper a -> Zipper a
focusDownZ Nothing = Nothing
focusDownZ (Just s) | d:down <- W.down s = Just $ W.Stack d (W.focus s:W.up s) down
focusDownZ (Just s) | null $ W.up s = Just s
focusDownZ (Just (W.Stack f up _)) = Just $ W.Stack (last up) [] (reverse (init up) ++ [f])
focusMasterZ :: Zipper a -> Zipper a
focusMasterZ Nothing = Nothing
focusMasterZ (Just (W.Stack f up down)) | not $ null up
= Just $ W.Stack (last up) [] (reverse (init up) ++ [f] ++ down)
focusMasterZ (Just s) = Just s
findS :: Eq a => (a -> Bool) -> W.Stack a -> Maybe (W.Stack a)
findS p st = st <$ (guard . p . W.focus) st <|> findUp st <|> findDown st
where findDown = reverseZ . findUp . reverseS
findUp s | u:ups <- W.up s = (if p u then Just else findUp)
$ W.Stack u ups (W.focus s : W.down s)
| otherwise = Nothing
findZ :: Eq a => (a -> Bool) -> Zipper a -> Maybe (Zipper a)
findZ _ Nothing = Nothing
findZ p (Just st) = Just <$> findS p st
getFocusZ :: Zipper a -> Maybe a
getFocusZ = fmap W.focus
getIZ :: Int -> Zipper a -> Maybe a
getIZ i = getI i . W.integrate'
sortZ :: Ord a => Zipper a -> Zipper a
sortZ = sortByZ compare
sortByZ :: (a -> a -> Ordering) -> Zipper a -> Zipper a
sortByZ f = fromTags . sortBy (adapt f) . toTags
where adapt g e1 e2 = g (fromE e1) (fromE e2)
mapZ :: (Bool -> a -> b) -> Zipper a -> Zipper b
mapZ f as = fromTags . map (mapE f) . toTags $ as
mapZ_ :: (a -> b) -> Zipper a -> Zipper b
mapZ_ = mapZ . const
mapZM :: Monad m => (Bool -> a -> m b) -> Zipper a -> m (Zipper b)
mapZM f as = fromTags `liftM` (mapM (mapEM f) . toTags) as
mapZM_ :: Monad m => (a -> m b) -> Zipper a -> m (Zipper b)
mapZM_ = mapZM . const
onFocusedZ :: (a -> a) -> Zipper a -> Zipper a
onFocusedZ f = mapZ $ \b a -> if b then f a else a
onFocusedZM :: Monad m => (a -> m a) -> Zipper a -> m (Zipper a)
onFocusedZM f = mapZM $ \b a -> if b then f a else return a
onIndexZ :: Int -> (a -> a) -> Zipper a -> Zipper a
onIndexZ i _ as | i < 0 = as
onIndexZ i f as = case splitAt i $ toTags as of
(before, []) -> fromTags before
(before, a:after) -> fromTags $ before ++ mapE (const f) a : after
onIndexZM :: Monad m => Int -> (a -> m a) -> Zipper a -> m (Zipper a)
onIndexZM i f as = case splitAt i $ toTags as of
(before, []) -> return $ fromTags before
(before, a:after) -> do a' <- mapEM (const f) a
return $ fromTags $ before ++ a' : after
filterZ :: (Bool -> a -> Bool) -> Zipper a -> Zipper a
filterZ _ Nothing = Nothing
filterZ p (Just s) = case ( p True (W.focus s)
, filter (p False) (W.up s)
, filter (p False) (W.down s) ) of
(True, up', down') -> Just s { W.up = up', W.down = down' }
(False, [], []) -> Nothing
(False, f:up', []) -> Just s { W.focus = f, W.up = up', W.down = [] }
(False, up', f:down') -> Just s { W.focus = f
, W.up = up'
, W.down = down' }
filterZ_ :: (a -> Bool) -> Zipper a -> Zipper a
filterZ_ = filterZ . const
deleteFocusedZ :: Zipper a -> Zipper a
deleteFocusedZ = filterZ (\b _ -> not b)
deleteIndexZ :: Int -> Zipper a -> Zipper a
deleteIndexZ i z = let numbered = (fromTags . zipWith number [0..] . toTags) z
number j ea = mapE (\_ a -> (j,a)) ea
in mapZ_ snd $ filterZ_ ((/=i) . fst) numbered
foldrZ :: (Bool -> a -> b -> b) -> b -> Zipper a -> b
foldrZ _ b Nothing = b
foldrZ f b (Just s) = let b1 = foldr (f False) b (W.down s)
b2 = f True (W.focus s) b1
b3 = foldl (flip $ f False) b2 (W.up s)
in b3
foldlZ :: (Bool -> b -> a -> b) -> b -> Zipper a -> b
foldlZ _ b Nothing = b
foldlZ f b (Just s) = let b1 = foldr (flip $ f False) b (W.up s)
b2 = f True b1 (W.focus s)
b3 = foldl (f False) b2 (W.down s)
in b3
foldrZ_ :: (a -> b -> b) -> b -> Zipper a -> b
foldrZ_ = foldrZ . const
foldlZ_ :: (b -> a -> b) -> b -> Zipper a -> b
foldlZ_ = foldlZ . const
elemZ :: Eq a => a -> Zipper a -> Bool
elemZ a as = foldlZ_ step False as
where step True _ = True
step False a' = a' == a
getI :: Int -> [a] -> Maybe a
getI _ [] = Nothing
getI 0 (a:_) = Just a
getI i (_:as) = getI (i-1) as
mapE :: (Bool -> a -> b) -> Either a a -> Either b b
mapE f (Left a) = Left $ f False a
mapE f (Right a) = Right $ f True a
mapE_ :: (a -> b) -> Either a a -> Either b b
mapE_ = mapE . const
mapEM :: Monad m => (Bool -> a -> m b) -> Either a a -> m (Either b b)
mapEM f (Left a) = Left `liftM` f False a
mapEM f (Right a) = Right `liftM` f True a
mapEM_ :: Monad m => (a -> m b) -> Either a a -> m (Either b b)
mapEM_ = mapEM . const
fromE :: Either a a -> a
fromE (Right a) = a
fromE (Left a) = a
tagBy :: (a -> Bool) -> a -> Either a a
tagBy p a = if p a then Right a else Left a
reverseS :: W.Stack a -> W.Stack a
reverseS (W.Stack foc ups downs) = W.Stack foc downs ups
reverseZ :: Zipper a -> Zipper a
reverseZ = (reverseS <$>)