-----------------------------------------------------------------------------
-- |
-- 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.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
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

-- | 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 forall a b. (a -> b) -> a -> b
$ forall a i l s sd.
(Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
modify' 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) -> forall a. a -> [a] -> [a] -> Stack a
Stack Window
r [] (Window
tforall a. a -> [a] -> [a]
:[Window]
rs)
                   Stack Window
t (Window
l:[Window]
ls) [Window]
rs     -> forall a. a -> [a] -> [a] -> Stack a
Stack Window
t [] ([Window]
ys forall a. [a] -> [a] -> [a]
++ Window
y forall a. a -> [a] -> [a]
: [Window]
rs) where (Window
y :| [Window]
ys) = forall a. NonEmpty a -> NonEmpty a
NE.reverse (Window
l forall a. a -> [a] -> NonEmpty a
:| [Window]
ls)