-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.Promote
-- Description :  Alternate promote function for xmonad.
-- Copyright   :  (c) Miikka Koskinen 2007
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  xmonad@s001.ethrael.com
-- Stability   :  unstable
-- Portability :  unportable
--
-- Alternate promote function for xmonad.
--
-- Moves the focused window to the master pane. All other windows
-- retain their order. If focus is in the master, swap it with the
-- next window in the stack. Focus stays in the master.
--
-----------------------------------------------------------------------------

module XMonad.Actions.Promote (
                                 -- * Usage
                                 -- $usage
                                 promote
                                ) where

import XMonad
import XMonad.StackSet

-- $usage
--
-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
--
-- >    import XMonad.Actions.Promote
--
-- then add a keybinding or substitute 'promote' in place of swapMaster:
--
-- >   , ((modm,               xK_Return), promote)
--
-- For detailed instructions on editing your key bindings, see
-- "XMonad.Doc.Extending#Editing_key_bindings".

-- | Move the focused window to the master pane. All other windows
--   retain their order. If focus is in the master, swap it with the
--   next windo in the stack. Focus stays in the master.
promote :: X ()
promote :: X ()
promote = (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' ((Stack Window -> Stack Window) -> WindowSet -> WindowSet)
-> (Stack Window -> Stack Window) -> WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$
          \Stack Window
c -> case Stack Window
c of
                Stack Window
_ [] []     -> Stack Window
c
                Stack Window
t [] (Window
x:[Window]
rs) -> Window -> [Window] -> [Window] -> Stack Window
forall a. a -> [a] -> [a] -> Stack a
Stack Window
x [] (Window
tWindow -> [Window] -> [Window]
forall a. a -> [a] -> [a]
:[Window]
rs)
                Stack Window
t [Window]
ls [Window]
rs     -> Window -> [Window] -> [Window] -> Stack Window
forall a. a -> [a] -> [a] -> Stack a
Stack Window
t [] ([Window] -> [Window]
forall a. [a] -> [a]
reverse [Window]
ls [Window] -> [Window] -> [Window]
forall a. [a] -> [a] -> [a]
++ [Window]
rs)