X.H.ManageHelpers: Make type of ManageHook combinators more general.

This commit is contained in:
sgf
2016-12-15 21:51:39 +03:00
parent 2807935900
commit 8e5931272c
3 changed files with 16 additions and 48 deletions

View File

@@ -92,6 +92,10 @@
Do not overwrite wm name in `handleFocusQuery`, if user has already set it. Do not overwrite wm name in `handleFocusQuery`, if user has already set it.
* `XMonad.Hooks.ManageHelpers`
Make type of ManageHook combinators more general.
## 0.12 (December 14, 2015) ## 0.12 (December 14, 2015)
### Breaking Changes ### Breaking Changes

View File

@@ -149,7 +149,7 @@ import XMonad.Util.EZConfig
-- --
-- > import XMonad -- > import XMonad
-- > -- >
-- > import XMonad.Hooks.ManageHelpers hiding ((-?>), composeOne) -- > import XMonad.Hooks.ManageHelpers
-- > import XMonad.Hooks.Focus -- > import XMonad.Hooks.Focus
-- > -- >
-- > main :: IO () -- > main :: IO ()
@@ -160,32 +160,12 @@ import XMonad.Util.EZConfig
-- > ]) -- > ])
-- > $ def -- > $ def
-- > xmonad xcf -- > xmonad xcf
-- >
-- > composeOne :: (Monoid a, Monad m) => [m (Maybe a)] -> m a
-- > composeOne [] = return mempty
-- > composeOne (mx : xs) = do
-- > x <- mx
-- > case x of
-- > Just y -> return y
-- > Nothing -> composeOne xs
-- >
-- > infixr 0 -?>
-- > (-?>) :: Monad m => m Bool -> m a -> m (Maybe a)
-- > (-?>) mb mx = do
-- > b <- mb
-- > if b
-- > then Just <$> mx
-- > else return Nothing
--
-- --
-- Note: -- Note:
-- --
-- - /mod4Mask+v/ key toggles focus lock (when enabled, focus will not be -- - /mod4Mask+v/ key toggles focus lock (when enabled, focus will not be
-- switched to new window). -- switched to new window).
-- - 'handleFocusQuery' will enable window activation. -- - 'handleFocusQuery' will enable window activation.
-- - I need more generic 'XMonad.Hooks.ManageHelpers.-?>' and
-- 'XMonad.Hooks.ManageHelpers.composeOne', than in the
-- 'XMonad.Hooks.ManageHelpers'.
-- - The order, when constructing final 'FocusHook' in 'handleFocusQuery' -- - The order, when constructing final 'FocusHook' in 'handleFocusQuery'
-- call: 'FocusHook' without 'activated' predicate will match to activated -- call: 'FocusHook' without 'activated' predicate will match to activated
-- windows too, thus i should place it after one with 'activated' (so the -- windows too, thus i should place it after one with 'activated' (so the
@@ -231,7 +211,7 @@ import XMonad.Util.EZConfig
-- > import XMonad -- > import XMonad
-- > import qualified XMonad.StackSet as W -- > import qualified XMonad.StackSet as W
-- > -- >
-- > import XMonad.Hooks.ManageHelpers hiding ((-?>), composeOne) -- > import XMonad.Hooks.ManageHelpers
-- > import XMonad.Hooks.Focus -- > import XMonad.Hooks.Focus
-- > -- >
-- > main :: IO () -- > main :: IO ()
@@ -269,22 +249,6 @@ import XMonad.Util.EZConfig
-- > -- Default behavior for new windows: switch focus. -- > -- Default behavior for new windows: switch focus.
-- > , return True -?> switchFocus -- > , return True -?> switchFocus
-- > ] -- > ]
-- >
-- > composeOne :: (Monoid a, Monad m) => [m (Maybe a)] -> m a
-- > composeOne [] = return mempty
-- > composeOne (mx : xs) = do
-- > x <- mx
-- > case x of
-- > Just y -> return y
-- > Nothing -> composeOne xs
-- >
-- > infixr 0 -?>
-- > (-?>) :: Monad m => m Bool -> m a -> m (Maybe a)
-- > (-?>) mb mx = do
-- > b <- mb
-- > if b
-- > then Just <$> mx
-- > else return Nothing
-- --
-- Note here: -- Note here:
-- --

View File

@@ -73,8 +73,8 @@ data Match a = Match Bool a
-- | An alternative 'ManageHook' composer. Unlike 'composeAll' it stops as soon as -- | An alternative 'ManageHook' composer. Unlike 'composeAll' it stops as soon as
-- a candidate returns a 'Just' value, effectively running only the first match -- a candidate returns a 'Just' value, effectively running only the first match
-- (whereas 'composeAll' continues and executes all matching rules). -- (whereas 'composeAll' continues and executes all matching rules).
composeOne :: [MaybeManageHook] -> ManageHook composeOne :: (Monoid a, Monad m) => [m (Maybe a)] -> m a
composeOne = foldr try idHook composeOne = foldr try (return mempty)
where where
try q z = do try q z = do
x <- q x <- q
@@ -85,17 +85,17 @@ composeOne = foldr try idHook
infixr 0 -?>, -->>, -?>> infixr 0 -?>, -->>, -?>>
-- | q \/=? x. if the result of q equals x, return False -- | q \/=? x. if the result of q equals x, return False
(/=?) :: Eq a => Query a -> a -> Query Bool (/=?) :: (Eq a, Functor m) => m a -> a -> m Bool
q /=? x = fmap (/= x) q q /=? x = fmap (/= x) q
-- | q <==? x. if the result of q equals x, return True grouped with q -- | q <==? x. if the result of q equals x, return True grouped with q
(<==?) :: Eq a => Query a -> a -> Query (Match a) (<==?) :: (Eq a, Functor m) => m a -> a -> m (Match a)
q <==? x = fmap (`eq` x) q q <==? x = fmap (`eq` x) q
where where
eq q' x' = Match (q' == x') q' eq q' x' = Match (q' == x') q'
-- | q <\/=? x. if the result of q notequals x, return True grouped with q -- | q <\/=? x. if the result of q notequals x, return True grouped with q
(</=?) :: Eq a => Query a -> a -> Query (Match a) (</=?) :: (Eq a, Functor m) => m a -> a -> m (Match a)
q </=? x = fmap (`neq` x) q q </=? x = fmap (`neq` x) q
where where
neq q' x' = Match (q' /= x') q' neq q' x' = Match (q' /= x') q'
@@ -103,19 +103,19 @@ q </=? x = fmap (`neq` x) q
-- | A helper operator for use in 'composeOne'. It takes a condition and an action; -- | A helper operator for use in 'composeOne'. It takes a condition and an action;
-- if the condition fails, it returns 'Nothing' from the 'Query' so 'composeOne' will -- if the condition fails, it returns 'Nothing' from the 'Query' so 'composeOne' will
-- go on and try the next rule. -- go on and try the next rule.
(-?>) :: Query Bool -> ManageHook -> MaybeManageHook (-?>) :: (Functor m, Monad m) => m Bool -> m a -> m (Maybe a)
p -?> f = do p -?> f = do
x <- p x <- p
if x then fmap Just f else return Nothing if x then fmap Just f else return Nothing
-- | A helper operator for use in 'composeAll'. It takes a condition and a function taking a grouped datum to action. If 'p' is true, it executes the resulting action. -- | A helper operator for use in 'composeAll'. It takes a condition and a function taking a grouped datum to action. If 'p' is true, it executes the resulting action.
(-->>) :: Query (Match a) -> (a -> ManageHook) -> ManageHook (-->>) :: (Monoid b, Monad m) => m (Match a) -> (a -> m b) -> m b
p -->> f = do p -->> f = do
Match b m <- p Match b m <- p
if b then (f m) else mempty if b then (f m) else return mempty
-- | A helper operator for use in 'composeOne'. It takes a condition and a function taking a groupdatum to action. If 'p' is true, it executes the resulting action. If it fails, it returns 'Nothing' from the 'Query' so 'composeOne' will go on and try the next rule. -- | A helper operator for use in 'composeOne'. It takes a condition and a function taking a groupdatum to action. If 'p' is true, it executes the resulting action. If it fails, it returns 'Nothing' from the 'Query' so 'composeOne' will go on and try the next rule.
(-?>>) :: Query (Match a) -> (a -> ManageHook) -> MaybeManageHook (-?>>) :: (Functor m, Monad m) => m (Match a) -> (a -> m b) -> m (Maybe b)
p -?>> f = do p -?>> f = do
Match b m <- p Match b m <- p
if b then fmap Just (f m) else return Nothing if b then fmap Just (f m) else return Nothing
@@ -179,7 +179,7 @@ transience' :: ManageHook
transience' = maybeToDefinite transience transience' = maybeToDefinite transience
-- | converts 'MaybeManageHook's to 'ManageHook's -- | converts 'MaybeManageHook's to 'ManageHook's
maybeToDefinite :: MaybeManageHook -> ManageHook maybeToDefinite :: (Monoid a, Functor m) => m (Maybe a) -> m a
maybeToDefinite = fmap (fromMaybe mempty) maybeToDefinite = fmap (fromMaybe mempty)