-----------------------------------------------------------------------------
-- |
-- Module       : XMonad.Actions.WithAll
-- Description  : Perform a given action on all or certain groups of windows.
-- License      : BSD3-style (see LICENSE)
-- Stability    : unstable
-- Portability  : unportable
--
-- Provides functions for performing a given action on all or certain
-- groups of windows on the current workspace.
-----------------------------------------------------------------------------

module XMonad.Actions.WithAll (
    -- * Usage
    -- $usage
    sinkAll, withAll,
    withAll', killAll,
    killOthers) where

import XMonad.Prelude hiding (foldr)

import XMonad
import XMonad.StackSet

-- $usage
--
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Actions.WithAll
--
-- then add a keybinding; for example:
--
--     , ((modm .|. shiftMask, xK_t), sinkAll)
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

-- | Un-float all floating windows on the current workspace.
sinkAll :: X ()
sinkAll :: X ()
sinkAll = (Window -> WindowSet -> WindowSet) -> X ()
withAll' forall a i l s sd.
Ord a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
sink

-- | Apply a function to all windows on the current workspace.
withAll' :: (Window -> WindowSet -> WindowSet) -> X ()
withAll' :: (Window -> WindowSet -> WindowSet) -> X ()
withAll' Window -> WindowSet -> WindowSet
f = (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> let all' :: [Window]
all' = forall a. Maybe (Stack a) -> [a]
integrate' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> Maybe (Stack a)
stack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current forall a b. (a -> b) -> a -> b
$ WindowSet
ws
                              in forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Window -> WindowSet -> WindowSet
f WindowSet
ws [Window]
all'

-- | Execute an 'X' action for each window on the current workspace.
withAll :: (Window -> X ()) -> X()
withAll :: (Window -> X ()) -> X ()
withAll Window -> X ()
f = forall a. (WindowSet -> X a) -> X a
withWindowSet forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> let all' :: [Window]
all' = forall a. Maybe (Stack a) -> [a]
integrate' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> Maybe (Stack a)
stack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
workspace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current forall a b. (a -> b) -> a -> b
$ WindowSet
ws
                                   in forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Window]
all' Window -> X ()
f

-- | Kill all the windows on the current workspace.
killAll :: X()
killAll :: X ()
killAll = (Window -> X ()) -> X ()
withAll Window -> X ()
killWindow

-- | Kill all the unfocused windows on the current workspace.
killOthers :: X ()
killOthers :: X ()
killOthers = (Window -> X ()) -> X ()
withUnfocused Window -> X ()
killWindow