-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.PerWorkspaceKeys
-- Description :  Define key-bindings on per-workspace basis.
-- Copyright   :  (c) Roman Cheplyaka, 2008
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Roman Cheplyaka <roma@ro-che.info>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Define key-bindings on per-workspace basis.
--
-----------------------------------------------------------------------------

module XMonad.Actions.PerWorkspaceKeys (
                                 -- * Usage
                                 -- $usage
                                 chooseAction,
                                 bindOn
                                ) where

import XMonad
import XMonad.StackSet as S

-- $usage
--
-- You can use this module with the following in your @xmonad.hs@:
--
-- >  import XMonad.Actions.PerWorkspaceKeys
--
-- >   ,((0, xK_F2), bindOn [("1", spawn "rxvt"), ("2", spawn "xeyes"), ("", spawn "xmessage hello")])
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

-- | Uses supplied function to decide which action to run depending on current workspace name.
chooseAction :: (String->X()) -> X()
chooseAction :: (String -> X ()) -> X ()
chooseAction String -> X ()
f = forall a. (WindowSet -> X a) -> X a
withWindowSet (String -> X ()
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> i
S.currentTag)

-- | If current workspace is listed, run appropriate action (only the first match counts!)
-- If it isn't listed, then run default action (marked with empty string, \"\"), or do nothing if default isn't supplied.
bindOn :: [(String, X())] -> X()
bindOn :: [(String, X ())] -> X ()
bindOn [(String, X ())]
bindings = (String -> X ()) -> X ()
chooseAction String -> X ()
chooser where
    chooser :: String -> X ()
chooser String
ws = case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
ws [(String, X ())]
bindings of
        Just X ()
action -> X ()
action
        Maybe (X ())
Nothing -> case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"" [(String, X ())]
bindings of
            Just X ()
action -> X ()
action
            Maybe (X ())
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()