-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.DwmPromote
-- Description :  DWM-like swap function for xmonad.
-- Copyright   :  (c) Miikka Koskinen 2007
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  arcatan@kapsi.fi
-- Stability   :  stable
-- Portability :  unportable
--
-- Dwm-like swap function for xmonad.
--
-- Swaps focused window with the master window. If focus is in the
-- master, swap it with the next window in the stack. Focus stays in the
-- master.
--
-----------------------------------------------------------------------------

module XMonad.Actions.DwmPromote (
                                 -- * Usage
                                 -- $usage
                                 dwmpromote
                                ) where

import XMonad
import XMonad.StackSet
import XMonad.Prelude

import qualified Data.List.NonEmpty as NE

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

-- | Swap the focused window with the master window. If focus is in
--   the master, swap it with the next window in the stack. Focus
--   stays in the master.
dwmpromote :: X ()
dwmpromote :: X ()
dwmpromote = (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
r:[Window]
rs) -> Window -> [Window] -> [Window] -> Stack Window
forall a. a -> [a] -> [a] -> Stack a
Stack Window
r [] (Window
tWindow -> [Window] -> [Window]
forall a. a -> [a] -> [a]
:[Window]
rs)
                   Stack Window
t (Window
l:[Window]
ls) [Window]
rs     -> Window -> [Window] -> [Window] -> Stack Window
forall a. a -> [a] -> [a] -> Stack a
Stack Window
t [] ([Window]
ys [Window] -> [Window] -> [Window]
forall a. [a] -> [a] -> [a]
++ Window
y Window -> [Window] -> [Window]
forall a. a -> [a] -> [a]
: [Window]
rs) where (Window
y :| [Window]
ys) = NonEmpty Window -> NonEmpty Window
forall a. NonEmpty a -> NonEmpty a
NE.reverse (Window
l Window -> [Window] -> NonEmpty Window
forall a. a -> [a] -> NonEmpty a
:| [Window]
ls)