    -----------------------------------------------------------------------------
    -- |
    -- Module       : XMonad.Actions.RotSlaves
    -- Copyright    : (c) Hans Philipp Annen <haphi@gmx.net>, Mischa Dieterle <der_m@freenet.de>
    -- License      : BSD3-style (see LICENSE)
    --
    -- Maintainer   : Hans Philipp Annen <haphi@gmx.net>
    -- Stability    : stable
    -- Portability  : unportable
    --
    -- Rotate all windows except the master window and keep the focus in
    -- place.
    -----------------------------------------------------------------------------
    module XMonad.Actions.RotSlaves (
            -- $usage
            rotSlaves', rotSlavesUp, rotSlavesDown,
            rotAll', rotAllUp, rotAllDown
            ) where

    import XMonad.StackSet
    import XMonad

    -- $usage
    --
    -- To use this module, import it with:
    --
    -- > import XMonad.Actions.RotSlaves
    --
    -- and add whatever keybindings you would like, for example:
    --
    -- > , ((modm .|. shiftMask, xK_Tab   ), rotSlavesUp)
    --
    -- This operation will rotate all windows except the master window,
    -- while the focus stays where it is. It is useful together with the
    -- TwoPane layout (see "XMonad.Layout.TwoPane").
    --
    -- For detailed instructions on editing your key bindings, see
    -- "XMonad.Doc.Extending#Editing_key_bindings".

    -- | Rotate the windows in the current stack, excluding the first one
    --   (master).
    rotSlavesUp,rotSlavesDown :: X ()
    rotSlavesUp :: X ()
    rotSlavesUp   = (WindowSet -> WindowSet) -> X ()
    windows ((WindowSet -> WindowSet) -> X ())
    -> (WindowSet -> WindowSet) -> X ()
    forall a b. (a -> b) -> a -> b
    $ (Stack Window -> Stack Window) -> WindowSet -> WindowSet
    forall a i l s sd.
    (Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
    modify' (([Window] -> [Window]) -> Stack Window -> Stack Window
    forall a. ([a] -> [a]) -> Stack a -> Stack a
    rotSlaves' (\l :: [Window]
    l -> ([Window] -> [Window]
    forall a. [a] -> [a]
    tail [Window]
    l)[Window] -> [Window] -> [Window]
    forall a. [a] -> [a] -> [a]
    ++[[Window] -> Window
    forall a. [a] -> a
    head [Window]
    l]))
    rotSlavesDown :: X ()
    rotSlavesDown = (WindowSet -> WindowSet) -> X ()
    windows ((WindowSet -> WindowSet) -> X ())
    -> (WindowSet -> WindowSet) -> X ()
    forall a b. (a -> b) -> a -> b
    $ (Stack Window -> Stack Window) -> WindowSet -> WindowSet
    forall a i l s sd.
    (Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
    modify' (([Window] -> [Window]) -> Stack Window -> Stack Window
    forall a. ([a] -> [a]) -> Stack a -> Stack a
    rotSlaves' (\l :: [Window]
    l -> [[Window] -> Window
    forall a. [a] -> a
    last [Window]
    l][Window] -> [Window] -> [Window]
    forall a. [a] -> [a] -> [a]
    ++([Window] -> [Window]
    forall a. [a] -> [a]
    init [Window]
    l)))

    -- | The actual rotation, as a pure function on the window stack.
    rotSlaves' :: ([a] -> [a]) -> Stack a -> Stack a
    rotSlaves' :: ([a] -> [a]) -> Stack a -> Stack a
    rotSlaves' _ s :: Stack a
    s@(Stack _ [] []) = Stack a
    s
    rotSlaves' f :: [a] -> [a]
    f   (Stack t :: a
    t [] rs :: [a]
    rs) = a -> [a] -> [a] -> Stack a
    forall a. a -> [a] -> [a] -> Stack a
    Stack a
    t [] ([a] -> [a]
    f [a]
    rs)                -- Master has focus
    rotSlaves' f :: [a] -> [a]
    f s :: Stack a
    s@(Stack _ ls :: [a]
    ls _ ) = a -> [a] -> [a] -> Stack a
    forall a. a -> [a] -> [a] -> Stack a
    Stack a
    t' ([a] -> [a]
    forall a. [a] -> [a]
    reverse [a]
    revls') [a]
    rs'    -- otherwise
        where  (master :: a
    master:ws :: [a]
    ws)     = Stack a -> [a]
    forall a. Stack a -> [a]
    integrate Stack a
    s
               (revls' :: [a]
    revls',t' :: a
    t':rs' :: [a]
    rs') = Int -> [a] -> ([a], [a])
    forall a. Int -> [a] -> ([a], [a])
    splitAt ([a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [a]
    ls) (a
    mastera -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :([a] -> [a]
    f [a]
    ws))

    -- | Rotate all the windows in the current stack.
    rotAllUp,rotAllDown :: X ()
    rotAllUp :: X ()
    rotAllUp   = (WindowSet -> WindowSet) -> X ()
    windows ((WindowSet -> WindowSet) -> X ())
    -> (WindowSet -> WindowSet) -> X ()
    forall a b. (a -> b) -> a -> b
    $ (Stack Window -> Stack Window) -> WindowSet -> WindowSet
    forall a i l s sd.
    (Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
    modify' (([Window] -> [Window]) -> Stack Window -> Stack Window
    forall a. ([a] -> [a]) -> Stack a -> Stack a
    rotAll' (\l :: [Window]
    l -> ([Window] -> [Window]
    forall a. [a] -> [a]
    tail [Window]
    l)[Window] -> [Window] -> [Window]
    forall a. [a] -> [a] -> [a]
    ++[[Window] -> Window
    forall a. [a] -> a
    head [Window]
    l]))
    rotAllDown :: X ()
    rotAllDown = (WindowSet -> WindowSet) -> X ()
    windows ((WindowSet -> WindowSet) -> X ())
    -> (WindowSet -> WindowSet) -> X ()
    forall a b. (a -> b) -> a -> b
    $ (Stack Window -> Stack Window) -> WindowSet -> WindowSet
    forall a i l s sd.
    (Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
    modify' (([Window] -> [Window]) -> Stack Window -> Stack Window
    forall a. ([a] -> [a]) -> Stack a -> Stack a
    rotAll' (\l :: [Window]
    l -> [[Window] -> Window
    forall a. [a] -> a
    last [Window]
    l][Window] -> [Window] -> [Window]
    forall a. [a] -> [a] -> [a]
    ++([Window] -> [Window]
    forall a. [a] -> [a]
    init [Window]
    l)))

    -- | The actual rotation, as a pure function on the window stack.
    rotAll' :: ([a] -> [a]) -> Stack a -> Stack a
    rotAll' :: ([a] -> [a]) -> Stack a -> Stack a
    rotAll' f :: [a] -> [a]
    f s :: Stack a
    s = a -> [a] -> [a] -> Stack a
    forall a. a -> [a] -> [a] -> Stack a
    Stack a
    r ([a] -> [a]
    forall a. [a] -> [a]
    reverse [a]
    revls) [a]
    rs
        where (revls :: [a]
    revls,r :: a
    r:rs :: [a]
    rs) = Int -> [a] -> ([a], [a])
    forall a. Int -> [a] -> ([a], [a])
    splitAt ([a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length (Stack a -> [a]
    forall a. Stack a -> [a]
    up Stack a
    s)) ([a] -> [a]
    f (Stack a -> [a]
    forall a. Stack a -> [a]
    integrate Stack a
    s))