-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.DynamicWorkspaces
-- Description :  Provides bindings to add and delete workspaces.
-- Copyright   :  (c) David Roundy <droundy@darcs.net>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  none
-- Stability   :  unstable
-- Portability :  unportable
--
-- Provides bindings to add and delete workspaces.
--
-----------------------------------------------------------------------------

module XMonad.Actions.DynamicWorkspaces (
                                         -- * Usage
                                         -- $usage
                                         addWorkspace, addWorkspacePrompt,
                                         appendWorkspace, appendWorkspacePrompt,
                                         addWorkspaceAt,
                                         removeWorkspace,
                                         removeWorkspaceByTag,
                                         removeEmptyWorkspace,
                                         removeEmptyWorkspaceByTag,
                                         removeEmptyWorkspaceAfter,
                                         removeEmptyWorkspaceAfterExcept,
                                         addHiddenWorkspace, addHiddenWorkspaceAt,
                                         withWorkspace,
                                         selectWorkspace, renameWorkspace,
                                         renameWorkspaceByName,
                                         toNthWorkspace, withNthWorkspace,
                                         setWorkspaceIndex, withWorkspaceIndex,
                                         WorkspaceIndex
                                       ) where

import XMonad.Prelude (find, isNothing, nub, when)
import XMonad hiding (workspaces)
import XMonad.StackSet hiding (filter, modify, delete)
import XMonad.Prompt.Workspace ( Wor(Wor), workspacePrompt )
import XMonad.Prompt ( XPConfig, mkComplFunFromList', mkXPrompt )
import XMonad.Util.WorkspaceCompare ( getSortByIndex )
import qualified Data.Map.Strict as Map
import qualified XMonad.Util.ExtensibleState as XS

-- $usage
-- You can use this module with the following in your @xmonad.hs@ file:
--
-- > import XMonad.Actions.DynamicWorkspaces
-- > import XMonad.Actions.CopyWindow(copy)
--
-- Then add keybindings like the following:
--
-- >   , ((modm .|. shiftMask, xK_BackSpace), removeWorkspace)
-- >   , ((modm .|. shiftMask, xK_v      ), selectWorkspace def)
-- >   , ((modm, xK_m                    ), withWorkspace def (windows . W.shift))
-- >   , ((modm .|. shiftMask, xK_m      ), withWorkspace def (windows . copy))
-- >   , ((modm .|. shiftMask, xK_r      ), renameWorkspace def)
--
-- > -- mod-[1..9]       %! Switch to workspace N in the list of workspaces
-- > -- mod-shift-[1..9] %! Move client to workspace N in the list of workspaces
-- >    ++
-- >    zip (zip (repeat (modm)) [xK_1..xK_9]) (map (withNthWorkspace W.greedyView) [0..])
-- >    ++
-- >    zip (zip (repeat (modm .|. shiftMask)) [xK_1..xK_9]) (map (withNthWorkspace W.shift) [0..])
--
-- Alternatively, you can associate indexes (which don't depend of the
-- workspace list order) to workspaces by using following keybindings:
--
-- > -- mod-[1..9]         %! Switch to workspace of index N
-- > -- mod-control-[1..9] %! Set index N to the current workspace
-- >    ++
-- >    zip (zip (repeat (modm)) [xK_1..xK_9]) (map (withWorkspaceIndex W.greedyView) [1..])
-- >    ++
-- >    zip (zip (repeat (modm .|. controlMask)) [xK_1..xK_9]) (map (setWorkspaceIndex) [1..])
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>. See also the documentation for
-- "XMonad.Actions.CopyWindow", 'windows', 'shift', and 'XPConfig'.

type WorkspaceTag = String
-- | The workspace index is mapped to a workspace tag by the user and
-- can be updated.
type WorkspaceIndex  = Int

-- | Internal dynamic project state that stores a mapping between
--   workspace indexes and workspace tags.
newtype DynamicWorkspaceState = DynamicWorkspaceState {DynamicWorkspaceState -> Map WorkspaceIndex String
workspaceIndexMap :: Map.Map WorkspaceIndex WorkspaceTag}
  deriving (ReadPrec [DynamicWorkspaceState]
ReadPrec DynamicWorkspaceState
WorkspaceIndex -> ReadS DynamicWorkspaceState
ReadS [DynamicWorkspaceState]
forall a.
(WorkspaceIndex -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DynamicWorkspaceState]
$creadListPrec :: ReadPrec [DynamicWorkspaceState]
readPrec :: ReadPrec DynamicWorkspaceState
$creadPrec :: ReadPrec DynamicWorkspaceState
readList :: ReadS [DynamicWorkspaceState]
$creadList :: ReadS [DynamicWorkspaceState]
readsPrec :: WorkspaceIndex -> ReadS DynamicWorkspaceState
$creadsPrec :: WorkspaceIndex -> ReadS DynamicWorkspaceState
Read, WorkspaceIndex -> DynamicWorkspaceState -> ShowS
[DynamicWorkspaceState] -> ShowS
DynamicWorkspaceState -> String
forall a.
(WorkspaceIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DynamicWorkspaceState] -> ShowS
$cshowList :: [DynamicWorkspaceState] -> ShowS
show :: DynamicWorkspaceState -> String
$cshow :: DynamicWorkspaceState -> String
showsPrec :: WorkspaceIndex -> DynamicWorkspaceState -> ShowS
$cshowsPrec :: WorkspaceIndex -> DynamicWorkspaceState -> ShowS
Show)

instance ExtensionClass DynamicWorkspaceState where
  initialValue :: DynamicWorkspaceState
initialValue = Map WorkspaceIndex String -> DynamicWorkspaceState
DynamicWorkspaceState forall k a. Map k a
Map.empty
  extensionType :: DynamicWorkspaceState -> StateExtension
extensionType = forall a. (Read a, Show a, ExtensionClass a) => a -> StateExtension
PersistentExtension

-- | Set the index of the current workspace.
setWorkspaceIndex :: WorkspaceIndex -> X ()
setWorkspaceIndex :: WorkspaceIndex -> X ()
setWorkspaceIndex WorkspaceIndex
widx = do
  String
wtag  <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
  Map WorkspaceIndex String
wmap <- forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets DynamicWorkspaceState -> Map WorkspaceIndex String
workspaceIndexMap
  forall a (m :: * -> *).
(ExtensionClass a, XLike m) =>
(a -> a) -> m ()
XS.modify forall a b. (a -> b) -> a -> b
$ \DynamicWorkspaceState
s -> DynamicWorkspaceState
s {workspaceIndexMap :: Map WorkspaceIndex String
workspaceIndexMap = forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert WorkspaceIndex
widx String
wtag Map WorkspaceIndex String
wmap}

withWorkspaceIndex :: (String -> WindowSet -> WindowSet) -> WorkspaceIndex -> X ()
withWorkspaceIndex :: (String -> WindowSet -> WindowSet) -> WorkspaceIndex -> X ()
withWorkspaceIndex String -> WindowSet -> WindowSet
job WorkspaceIndex
widx = do
  Maybe String
wtag <- WorkspaceIndex -> X (Maybe String)
ilookup WorkspaceIndex
widx
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. Monad m => a -> m a
return ()) ((WindowSet -> WindowSet) -> X ()
windows forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> WindowSet -> WindowSet
job) Maybe String
wtag
    where
      ilookup :: WorkspaceIndex -> X (Maybe WorkspaceTag)
      ilookup :: WorkspaceIndex -> X (Maybe String)
ilookup WorkspaceIndex
idx = forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup WorkspaceIndex
idx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets DynamicWorkspaceState -> Map WorkspaceIndex String
workspaceIndexMap

withWorkspace :: XPConfig -> (String -> X ()) -> X ()
withWorkspace :: XPConfig -> (String -> X ()) -> X ()
withWorkspace XPConfig
c String -> X ()
job = do [Workspace String (Layout Window) Window]
ws <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
workspaces forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
                         WorkspaceSort
sort <- X WorkspaceSort
getSortByIndex
                         let ts :: [String]
ts = forall a b. (a -> b) -> [a] -> [b]
map forall i l a. Workspace i l a -> i
tag forall a b. (a -> b) -> a -> b
$ WorkspaceSort
sort [Workspace String (Layout Window) Window]
ws
                             job' :: String -> X ()
job' String
t | String
t forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
ts = String -> X ()
job String
t
                                    | Bool
otherwise = String -> X ()
addHiddenWorkspace String
t forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> X ()
job String
t
                         forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> Wor
Wor String
"") XPConfig
c (XPConfig -> [String] -> ComplFunction
mkComplFunFromList' XPConfig
c [String]
ts) String -> X ()
job'

renameWorkspace :: XPConfig -> X ()
renameWorkspace :: XPConfig -> X ()
renameWorkspace XPConfig
conf = XPConfig -> (String -> X ()) -> X ()
workspacePrompt XPConfig
conf String -> X ()
renameWorkspaceByName

renameWorkspaceByName :: String -> X ()
renameWorkspaceByName :: String -> X ()
renameWorkspaceByName String
w = do String
old  <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
                             (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ \WindowSet
s -> let sett :: Workspace i l a -> Workspace String l a
sett Workspace i l a
wk = Workspace i l a
wk { tag :: String
tag = String
w }
                                                 setscr :: Screen i l a sid sd -> Screen String l a sid sd
setscr Screen i l a sid sd
scr = Screen i l a sid sd
scr { workspace :: Workspace String l a
workspace = forall {i} {l} {a}. Workspace i l a -> Workspace String l a
sett forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace Screen i l a sid sd
scr }
                                                 sets :: StackSet String l a sid sd -> StackSet String l a sid sd
sets StackSet String l a sid sd
q = StackSet String l a sid sd
q { current :: Screen String l a sid sd
current = forall {i} {l} {a} {sid} {sd}.
Screen i l a sid sd -> Screen String l a sid sd
setscr forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current StackSet String l a sid sd
q }
                                             in forall {l} {a} {sid} {sd}.
StackSet String l a sid sd -> StackSet String l a sid sd
sets forall a b. (a -> b) -> a -> b
$ forall i a l sid sd.
(Eq i, Eq a) =>
i -> StackSet i l a sid sd -> StackSet i l a sid sd
removeWorkspace' String
w WindowSet
s
                             forall {m :: * -> *}. XLike m => String -> String -> m ()
updateIndexMap String
old String
w
  where updateIndexMap :: String -> String -> m ()
updateIndexMap String
oldIM String
newIM = do
          Map WorkspaceIndex String
wmap <- forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets DynamicWorkspaceState -> Map WorkspaceIndex String
workspaceIndexMap
          forall a (m :: * -> *).
(ExtensionClass a, XLike m) =>
(a -> a) -> m ()
XS.modify forall a b. (a -> b) -> a -> b
$ \DynamicWorkspaceState
s -> DynamicWorkspaceState
s {workspaceIndexMap :: Map WorkspaceIndex String
workspaceIndexMap = forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (\String
t -> if String
t forall a. Eq a => a -> a -> Bool
== String
oldIM then String
newIM else String
t) Map WorkspaceIndex String
wmap}

toNthWorkspace :: (String -> X ()) -> Int -> X ()
toNthWorkspace :: (String -> X ()) -> WorkspaceIndex -> X ()
toNthWorkspace String -> X ()
job WorkspaceIndex
wnum = do WorkspaceSort
sort <- X WorkspaceSort
getSortByIndex
                             [String]
ws <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall a b. (a -> b) -> [a] -> [b]
map forall i l a. Workspace i l a -> i
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. WorkspaceSort
sort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
workspaces forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
                             case forall a. WorkspaceIndex -> [a] -> [a]
drop WorkspaceIndex
wnum [String]
ws of
                               (String
w:[String]
_) -> String -> X ()
job String
w
                               [] -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

withNthWorkspace :: (String -> WindowSet -> WindowSet) -> Int -> X ()
withNthWorkspace :: (String -> WindowSet -> WindowSet) -> WorkspaceIndex -> X ()
withNthWorkspace String -> WindowSet -> WindowSet
job WorkspaceIndex
wnum = do WorkspaceSort
sort <- X WorkspaceSort
getSortByIndex
                               [String]
ws <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall a b. (a -> b) -> [a] -> [b]
map forall i l a. Workspace i l a -> i
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. WorkspaceSort
sort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
workspaces forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
                               case forall a. WorkspaceIndex -> [a] -> [a]
drop WorkspaceIndex
wnum [String]
ws of
                                 (String
w:[String]
_) -> (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ String -> WindowSet -> WindowSet
job String
w
                                 [] -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

selectWorkspace :: XPConfig -> X ()
selectWorkspace :: XPConfig -> X ()
selectWorkspace XPConfig
conf = XPConfig -> (String -> X ()) -> X ()
workspacePrompt XPConfig
conf forall a b. (a -> b) -> a -> b
$ \String
w ->
                       do WindowSet
s <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
                          if forall i l a s sd. Eq i => i -> StackSet i l a s sd -> Bool
tagMember String
w WindowSet
s
                            then (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
greedyView String
w
                            else String -> X ()
addWorkspace String
w

-- | Add a new workspace with the given name, or do nothing if a
--   workspace with the given name already exists; then switch to the
--   newly created workspace.
addWorkspace :: String -> X ()
addWorkspace :: String -> X ()
addWorkspace = (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addWorkspaceAt (:)

-- | Same as addWorkspace, but adds the workspace to the end of the list of workspaces
appendWorkspace :: String -> X()
appendWorkspace :: String -> X ()
appendWorkspace = (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addWorkspaceAt (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. [a] -> [a] -> [a]
(++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return)

-- | Adds a new workspace with the given name to the current list of workspaces.
--   This function allows the user to pass a function that inserts an element
--   into a list at an arbitrary spot.
addWorkspaceAt :: (WindowSpace -> [WindowSpace] -> [WindowSpace]) -> String -> X ()
addWorkspaceAt :: (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addWorkspaceAt Workspace String (Layout Window) Window -> WorkspaceSort
add String
newtag = (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addHiddenWorkspaceAt Workspace String (Layout Window) Window -> WorkspaceSort
add String
newtag forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (WindowSet -> WindowSet) -> X ()
windows (forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
greedyView String
newtag)

-- | Prompt for the name of a new workspace, add it if it does not
--   already exist, and switch to it.
addWorkspacePrompt :: XPConfig -> X ()
addWorkspacePrompt :: XPConfig -> X ()
addWorkspacePrompt XPConfig
conf = forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> Wor
Wor String
"New workspace name: ") XPConfig
conf (forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return [])) String -> X ()
addWorkspace

-- | Prompt for the name of a new workspace, appending it to the end of the list of workspaces
--   if it does not already exist, and switch to it.
appendWorkspacePrompt :: XPConfig -> X ()
appendWorkspacePrompt :: XPConfig -> X ()
appendWorkspacePrompt XPConfig
conf = forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> Wor
Wor String
"New workspace name: ") XPConfig
conf (forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return [])) String -> X ()
appendWorkspace

-- | Add a new hidden workspace with the given name, or do nothing if
--   a workspace with the given name already exists. Takes a function to insert
--   the workspace at an arbitrary spot in the list.
addHiddenWorkspaceAt :: (WindowSpace -> [WindowSpace] -> [WindowSpace]) -> String -> X ()
addHiddenWorkspaceAt :: (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addHiddenWorkspaceAt Workspace String (Layout Window) Window -> WorkspaceSort
add String
newtag =
  X Bool -> X () -> X ()
whenX (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. Eq i => i -> StackSet i l a s sd -> Bool
tagMember String
newtag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)) forall a b. (a -> b) -> a -> b
$ do
    Layout Window
l <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (forall (l :: * -> *). XConfig l -> l Window
layoutHook forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config)
    (WindowSet -> WindowSet) -> X ()
windows (forall i l a sid sd.
(Workspace i l a -> [Workspace i l a] -> [Workspace i l a])
-> i -> l -> StackSet i l a sid sd -> StackSet i l a sid sd
addHiddenWorkspace' Workspace String (Layout Window) Window -> WorkspaceSort
add String
newtag Layout Window
l)

-- | Add a new hidden workspace with the given name, or do nothing if
--   a workspace with the given name already exists.
addHiddenWorkspace :: String -> X ()
addHiddenWorkspace :: String -> X ()
addHiddenWorkspace = (Workspace String (Layout Window) Window -> WorkspaceSort)
-> String -> X ()
addHiddenWorkspaceAt (:)

-- | Remove the current workspace if it contains no windows.
removeEmptyWorkspace :: X ()
removeEmptyWorkspace :: X ()
removeEmptyWorkspace = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> X ()
removeEmptyWorkspaceByTag

-- | Remove the current workspace.
removeWorkspace :: X ()
removeWorkspace :: X ()
removeWorkspace = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> X ()
removeWorkspaceByTag

-- | Remove workspace with specific tag if it contains no windows.
removeEmptyWorkspaceByTag :: String -> X ()
removeEmptyWorkspaceByTag :: String -> X ()
removeEmptyWorkspaceByTag String
t = X Bool -> X () -> X ()
whenX (String -> X Bool
isEmpty String
t) forall a b. (a -> b) -> a -> b
$ String -> X ()
removeWorkspaceByTag String
t

-- | Remove workspace with specific tag.
removeWorkspaceByTag :: String -> X ()
removeWorkspaceByTag :: String -> X ()
removeWorkspaceByTag String
torem = do
    WindowSet
s <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
    case WindowSet
s of
        StackSet { current :: forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current = Screen { workspace :: forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace = Workspace String (Layout Window) Window
cur }, hidden :: forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
hidden = (Workspace String (Layout Window) Window
w:[Workspace String (Layout Window) Window]
_) } -> do
                forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (String
toremforall a. Eq a => a -> a -> Bool
==forall i l a. Workspace i l a -> i
tag Workspace String (Layout Window) Window
cur) forall a b. (a -> b) -> a -> b
$ (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
view forall a b. (a -> b) -> a -> b
$ forall i l a. Workspace i l a -> i
tag Workspace String (Layout Window) Window
w
                (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ forall i a l sid sd.
(Eq i, Eq a) =>
i -> StackSet i l a sid sd -> StackSet i l a sid sd
removeWorkspace' String
torem
        WindowSet
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Remove the current workspace after an operation if it is empty and hidden.
--   Can be used to remove a workspace if it is empty when leaving it. The
--   operation may only change workspace once, otherwise the workspace will not
--   be removed.
removeEmptyWorkspaceAfter :: X () -> X ()
removeEmptyWorkspaceAfter :: X () -> X ()
removeEmptyWorkspaceAfter = [String] -> X () -> X ()
removeEmptyWorkspaceAfterExcept []

-- | Like 'removeEmptyWorkspaceAfter' but use a list of sticky workspaces,
--   whose entries will never be removed.
removeEmptyWorkspaceAfterExcept :: [String] -> X () -> X ()
removeEmptyWorkspaceAfterExcept :: [String] -> X () -> X ()
removeEmptyWorkspaceAfterExcept [String]
sticky X ()
f = do
    String
before <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
    X ()
f
    String
after <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> i
currentTag forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (String
beforeforall a. Eq a => a -> a -> Bool
/=String
after Bool -> Bool -> Bool
&& String
before forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [String]
sticky) forall a b. (a -> b) -> a -> b
$ String -> X ()
removeEmptyWorkspaceByTag String
before

isEmpty :: String -> X Bool
isEmpty :: String -> X Bool
isEmpty String
t = do [Workspace String (Layout Window) Window]
wsl <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a -> b) -> a -> b
$ forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
workspaces forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset
               let mws :: Maybe (Workspace String (Layout Window) Window)
mws = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\Workspace String (Layout Window) Window
ws -> forall i l a. Workspace i l a -> i
tag Workspace String (Layout Window) Window
ws forall a. Eq a => a -> a -> Bool
== String
t) [Workspace String (Layout Window) Window]
wsl
               forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> Maybe (Stack a)
stack) Maybe (Workspace String (Layout Window) Window)
mws

addHiddenWorkspace' :: (Workspace i l a -> [Workspace i l a] -> [Workspace i l a]) -> i -> l -> StackSet i l a sid sd -> StackSet i l a sid sd
addHiddenWorkspace' :: forall i l a sid sd.
(Workspace i l a -> [Workspace i l a] -> [Workspace i l a])
-> i -> l -> StackSet i l a sid sd -> StackSet i l a sid sd
addHiddenWorkspace' Workspace i l a -> [Workspace i l a] -> [Workspace i l a]
add i
newtag l
l s :: StackSet i l a sid sd
s@StackSet{ hidden :: forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
hidden = [Workspace i l a]
ws } = StackSet i l a sid sd
s { hidden :: [Workspace i l a]
hidden = Workspace i l a -> [Workspace i l a] -> [Workspace i l a]
add (forall i l a. i -> l -> Maybe (Stack a) -> Workspace i l a
Workspace i
newtag l
l forall a. Maybe a
Nothing) [Workspace i l a]
ws }

-- | Remove the hidden workspace with the given tag from the StackSet, if
--   it exists. All the windows in that workspace are moved to the current
--   workspace.
removeWorkspace' :: (Eq i, Eq a) => i -> StackSet i l a sid sd -> StackSet i l a sid sd
removeWorkspace' :: forall i a l sid sd.
(Eq i, Eq a) =>
i -> StackSet i l a sid sd -> StackSet i l a sid sd
removeWorkspace' i
torem s :: StackSet i l a sid sd
s@StackSet{ current :: forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current = scr :: Screen i l a sid sd
scr@Screen { workspace :: forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace = Workspace i l a
wc }
                                 , hidden :: forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
hidden  = [Workspace i l a]
hs }
    = let ([Workspace i l a]
xs, [Workspace i l a]
ys) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break ((forall a. Eq a => a -> a -> Bool
== i
torem) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> i
tag) [Workspace i l a]
hs
      in [Workspace i l a] -> [Workspace i l a] -> StackSet i l a sid sd
removeWorkspace'' [Workspace i l a]
xs [Workspace i l a]
ys
   where meld :: Maybe (Stack a) -> Maybe (Stack a) -> Maybe (Stack a)
meld Maybe (Stack a)
Nothing Maybe (Stack a)
Nothing = forall a. Maybe a
Nothing
         meld Maybe (Stack a)
x Maybe (Stack a)
Nothing = Maybe (Stack a)
x
         meld Maybe (Stack a)
Nothing Maybe (Stack a)
x = Maybe (Stack a)
x
         meld (Just Stack a
x) (Just Stack a
y) = forall a. [a] -> Maybe (Stack a)
differentiate forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall a. Stack a -> [a]
integrate Stack a
x forall a. [a] -> [a] -> [a]
++ forall a. Stack a -> [a]
integrate Stack a
y
         removeWorkspace'' :: [Workspace i l a] -> [Workspace i l a] -> StackSet i l a sid sd
removeWorkspace'' [Workspace i l a]
xs (Workspace i l a
y:[Workspace i l a]
ys) = StackSet i l a sid sd
s { current :: Screen i l a sid sd
current = Screen i l a sid sd
scr { workspace :: Workspace i l a
workspace = Workspace i l a
wc { stack :: Maybe (Stack a)
stack = forall {a}.
Eq a =>
Maybe (Stack a) -> Maybe (Stack a) -> Maybe (Stack a)
meld (forall i l a. Workspace i l a -> Maybe (Stack a)
stack Workspace i l a
y) (forall i l a. Workspace i l a -> Maybe (Stack a)
stack Workspace i l a
wc) } }
                                         , hidden :: [Workspace i l a]
hidden = [Workspace i l a]
xs forall a. [a] -> [a] -> [a]
++ [Workspace i l a]
ys }
         removeWorkspace'' [Workspace i l a]
_  [Workspace i l a]
_      = StackSet i l a sid sd
s