-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Hooks.InsertPosition
-- Description :  Configure where new windows should be added and how focus should shift.
-- Copyright   :  (c) 2009 Adam Vogt
-- License     :  BSD-style (see xmonad/LICENSE)
--
-- Maintainer  :  vogt.adam@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Configure where new windows should be added and which window should be
-- focused.
--
-----------------------------------------------------------------------------

module XMonad.Hooks.InsertPosition (
    -- * Usage
    -- $usage
    setupInsertPosition, insertPosition
    ,Focus(..), Position(..)
    ) where

import XMonad (ManageHook, MonadReader (ask), XConfig (manageHook))
import XMonad.Prelude (Endo (Endo), find)
import qualified XMonad.StackSet as W

-- $usage
-- You can use this module by importing it in your @xmonad.hs@:
--
-- > import XMonad.Hooks.InsertPosition
--
-- You then just have to add 'setupInsertPosition' to your @main@ function:
--
-- > main = xmonad $ … $ setupInsertPosition Master Newer $ def { … }
--
-- Alternatively (i.e., you should /not/ do this if you already have set
-- up the above combinator), you can also directly insert
-- 'insertPosition' into your manageHook:
--
-- > xmonad def { manageHook = insertPosition Master Newer <> myManageHook }
--
-- NOTE: You should you put the manageHooks that use 'doShift' to take effect
-- /before/ 'insertPosition', so that the window order will be consistent.
-- Because ManageHooks compose from right to left (like function composition
-- '.'), this means that 'insertPosition' should be the leftmost ManageHook.

data Position = Master | End | Above | Below
data Focus = Newer | Older

-- | A combinator for setting up 'insertPosition'.
setupInsertPosition :: Position -> Focus -> XConfig a -> XConfig a
setupInsertPosition :: forall (a :: * -> *). Position -> Focus -> XConfig a -> XConfig a
setupInsertPosition Position
pos Focus
foc XConfig a
cfg =
  XConfig a
cfg{ manageHook :: ManageHook
manageHook = Position -> Focus -> ManageHook
insertPosition Position
pos Focus
foc forall a. Semigroup a => a -> a -> a
<> forall (l :: * -> *). XConfig l -> ManageHook
manageHook XConfig a
cfg }

-- | insertPosition. A manage hook for placing new windows. XMonad's default is
-- the same as using: @insertPosition Above Newer@.
insertPosition :: Position -> Focus -> ManageHook
insertPosition :: Position -> Focus -> ManageHook
insertPosition Position
pos Focus
foc = forall a. (a -> a) -> Endo a
Endo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {l} {sd}.
Window
-> StackSet WorkspaceId l Window ScreenId sd
-> StackSet WorkspaceId l Window ScreenId sd
g forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r (m :: * -> *). MonadReader r m => m r
ask
  where
    g :: Window
-> StackSet WorkspaceId l Window ScreenId sd
-> StackSet WorkspaceId l Window ScreenId sd
g Window
w = forall a s i l sd.
(Eq a, Eq s, Eq i, Show i) =>
a
-> (StackSet i l a s sd -> StackSet i l a s sd)
-> StackSet i l a s sd
-> StackSet i l a s sd
viewingWs Window
w (forall {l} {sd}.
Window
-> StackSet WorkspaceId l Window ScreenId sd
-> StackSet WorkspaceId l Window ScreenId sd
updateFocus Window
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {s} {a} {i} {l} {sd}.
(Eq s, Eq a, Eq i) =>
a -> StackSet i l a s sd -> StackSet i l a s sd
ins Window
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.delete' Window
w)
    ins :: a -> StackSet i l a s sd -> StackSet i l a s sd
ins a
w = (\StackSet i l a s sd -> StackSet i l a s sd
f StackSet i l a s sd
ws -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id forall {s} {a} {i} {l} {sd}.
(Eq s, Eq a, Eq i) =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.focusWindow (forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek StackSet i l a s sd
ws) forall a b. (a -> b) -> a -> b
$ StackSet i l a s sd -> StackSet i l a s sd
f StackSet i l a s sd
ws) forall a b. (a -> b) -> a -> b
$
        case Position
pos of
            Position
Master -> forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.insertUp a
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> StackSet i l a s sd
W.focusMaster
            Position
End    -> forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
insertDown a
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a i l s sd.
(Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
W.modify' forall a. Stack a -> Stack a
focusLast'
            Position
Above  -> forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.insertUp a
w
            Position
Below  -> forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
insertDown a
w
    updateFocus :: Window
-> StackSet WorkspaceId l Window ScreenId sd
-> StackSet WorkspaceId l Window ScreenId sd
updateFocus =
        case Focus
foc of
            Focus
Older -> forall a b. a -> b -> a
const forall a. a -> a
id
            Focus
Newer -> forall {s} {a} {i} {l} {sd}.
(Eq s, Eq a, Eq i) =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.focusWindow

-- | Modify the StackSet when the workspace containing w is focused
viewingWs :: (Eq a, Eq s, Eq i, Show i) =>a-> (W.StackSet i l a s sd -> W.StackSet i l a s sd)-> W.StackSet i l a s sd-> W.StackSet i l a s sd
viewingWs :: forall a s i l sd.
(Eq a, Eq s, Eq i, Show i) =>
a
-> (StackSet i l a s sd -> StackSet i l a s sd)
-> StackSet i l a s sd
-> StackSet i l a s sd
viewingWs a
w StackSet i l a s sd -> StackSet i l a s sd
f = do
    i
i <- forall i l a. Workspace i l a -> i
W.tag 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
W.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
W.current
    Maybe (Workspace i l a)
ws <- forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe (Stack a) -> [a]
W.integrate' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
W.workspaces
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
W.view i
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackSet i l a s sd -> StackSet i l a s sd
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
W.view forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> i
W.tag) Maybe (Workspace i l a)
ws

-- | 'insertDown' and 'focusLast' belong in XMonad.StackSet?
insertDown :: (Eq a) => a -> W.StackSet i l a s sd -> W.StackSet i l a s sd
insertDown :: forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
insertDown a
w = forall i l a s sd. StackSet i l a s sd -> StackSet i l a s sd
W.swapDown forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.insertUp a
w

focusLast' ::  W.Stack a -> W.Stack a
focusLast' :: forall a. Stack a -> Stack a
focusLast' Stack a
st =
  case forall a. [a] -> [a]
reverse (forall a. Stack a -> [a]
W.integrate Stack a
st) of
    []       -> Stack a
st
    (a
l : [a]
ws) -> forall a. a -> [a] -> [a] -> Stack a
W.Stack a
l [a]
ws []