module XMonad.Prompt.Window
(
WindowPrompt(..),
windowPrompt,
windowMultiPrompt,
allWindows,
allApplications,
wsWindows,
XWindowMap,
) where
import XMonad.Prelude (forM)
import qualified Data.Map as M
import qualified XMonad.StackSet as W
import XMonad
import XMonad.Prompt
import XMonad.Actions.CopyWindow
import XMonad.Actions.WindowBringer
import XMonad.Util.NamedWindows
data WindowPrompt = Goto | Bring | BringCopy | BringToMaster | WithWindow String (Window -> X())
instance XPrompt WindowPrompt where
showXPrompt :: WindowPrompt -> WorkspaceId
showXPrompt WindowPrompt
Goto = WorkspaceId
"Go to window: "
showXPrompt WindowPrompt
Bring = WorkspaceId
"Bring window: "
showXPrompt WindowPrompt
BringToMaster
= WorkspaceId
"Bring window to master: "
showXPrompt WindowPrompt
BringCopy = WorkspaceId
"Bring a copy: "
showXPrompt (WithWindow WorkspaceId
xs Window -> X ()
_) = WorkspaceId
xs
commandToComplete :: WindowPrompt -> WorkspaceId -> WorkspaceId
commandToComplete WindowPrompt
_ WorkspaceId
c = WorkspaceId
c
nextCompletion :: WindowPrompt -> WorkspaceId -> [WorkspaceId] -> WorkspaceId
nextCompletion WindowPrompt
_ = WorkspaceId -> [WorkspaceId] -> WorkspaceId
getNextCompletion
data WindowModePrompt =
WindowModePrompt WindowPrompt (M.Map String Window) (String -> String -> Bool)
instance XPrompt WindowModePrompt where
showXPrompt :: WindowModePrompt -> WorkspaceId
showXPrompt (WindowModePrompt WindowPrompt
action Map WorkspaceId Window
_ WorkspaceId -> WorkspaceId -> Bool
_) =
WindowPrompt -> WorkspaceId
forall t. XPrompt t => t -> WorkspaceId
showXPrompt WindowPrompt
action
completionFunction :: WindowModePrompt -> ComplFunction
completionFunction (WindowModePrompt WindowPrompt
_ Map WorkspaceId Window
winmap WorkspaceId -> WorkspaceId -> Bool
predicate) WorkspaceId
s =
[WorkspaceId] -> IO [WorkspaceId]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([WorkspaceId] -> IO [WorkspaceId])
-> (Map WorkspaceId Window -> [WorkspaceId])
-> Map WorkspaceId Window
-> IO [WorkspaceId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WorkspaceId -> Bool) -> [WorkspaceId] -> [WorkspaceId]
forall a. (a -> Bool) -> [a] -> [a]
filter (WorkspaceId -> WorkspaceId -> Bool
predicate WorkspaceId
s) ([WorkspaceId] -> [WorkspaceId])
-> (Map WorkspaceId Window -> [WorkspaceId])
-> Map WorkspaceId Window
-> [WorkspaceId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((WorkspaceId, Window) -> WorkspaceId)
-> [(WorkspaceId, Window)] -> [WorkspaceId]
forall a b. (a -> b) -> [a] -> [b]
map (WorkspaceId, Window) -> WorkspaceId
forall a b. (a, b) -> a
fst ([(WorkspaceId, Window)] -> [WorkspaceId])
-> (Map WorkspaceId Window -> [(WorkspaceId, Window)])
-> Map WorkspaceId Window
-> [WorkspaceId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map WorkspaceId Window -> [(WorkspaceId, Window)]
forall k a. Map k a -> [(k, a)]
M.toList (Map WorkspaceId Window -> IO [WorkspaceId])
-> Map WorkspaceId Window -> IO [WorkspaceId]
forall a b. (a -> b) -> a -> b
$ Map WorkspaceId Window
winmap
modeAction :: WindowModePrompt -> WorkspaceId -> WorkspaceId -> X ()
modeAction (WindowModePrompt WindowPrompt
action Map WorkspaceId Window
winmap WorkspaceId -> WorkspaceId -> Bool
_) WorkspaceId
buf WorkspaceId
auto = do
let name :: WorkspaceId
name = if WorkspaceId -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null WorkspaceId
auto then WorkspaceId
buf else WorkspaceId
auto
a :: WorkspaceId -> X ()
a = case WindowPrompt
action of
WindowPrompt
Goto -> WorkspaceId -> X ()
gotoAction
WindowPrompt
Bring -> WorkspaceId -> X ()
bringAction
WindowPrompt
BringCopy -> WorkspaceId -> X ()
bringCopyAction
WindowPrompt
BringToMaster -> WorkspaceId -> X ()
bringToMaster
WithWindow WorkspaceId
_ Window -> X ()
f -> (Window -> X ()) -> WorkspaceId -> X ()
forall {m :: * -> *}.
Monad m =>
(Window -> m ()) -> WorkspaceId -> m ()
withWindow Window -> X ()
f
WorkspaceId -> X ()
a WorkspaceId
name
where
withWindow :: (Window -> m ()) -> WorkspaceId -> m ()
withWindow Window -> m ()
f = (Maybe Window -> (Window -> m ()) -> m ())
-> (Window -> m ()) -> Maybe Window -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Maybe Window -> (Window -> m ()) -> m ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Window -> m ()
f (Maybe Window -> m ())
-> (WorkspaceId -> Maybe Window) -> WorkspaceId -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WorkspaceId -> Map WorkspaceId Window -> Maybe Window)
-> Map WorkspaceId Window -> WorkspaceId -> Maybe Window
forall a b c. (a -> b -> c) -> b -> a -> c
flip WorkspaceId -> Map WorkspaceId Window -> Maybe Window
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Map WorkspaceId Window
winmap
winAction :: (Window -> WindowSet -> WindowSet) -> WorkspaceId -> X ()
winAction Window -> WindowSet -> WindowSet
a = (Window -> X ()) -> WorkspaceId -> X ()
forall {m :: * -> *}.
Monad m =>
(Window -> m ()) -> WorkspaceId -> m ()
withWindow ((WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> (Window -> WindowSet -> WindowSet) -> Window -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Window -> WindowSet -> WindowSet
a)
gotoAction :: WorkspaceId -> X ()
gotoAction = (Window -> WindowSet -> WindowSet) -> WorkspaceId -> X ()
winAction Window -> WindowSet -> WindowSet
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
bringAction :: WorkspaceId -> X ()
bringAction = (Window -> WindowSet -> WindowSet) -> WorkspaceId -> X ()
winAction Window -> WindowSet -> WindowSet
bringWindow
bringCopyAction :: WorkspaceId -> X ()
bringCopyAction = (Window -> WindowSet -> WindowSet) -> WorkspaceId -> X ()
winAction Window -> WindowSet -> WindowSet
bringCopyWindow
bringToMaster :: WorkspaceId -> X ()
bringToMaster = (Window -> WindowSet -> WindowSet) -> WorkspaceId -> X ()
winAction (\Window
w WindowSet
s -> WindowSet -> WindowSet
forall i l a s sd. StackSet i l a s sd -> StackSet i l a s sd
W.shiftMaster (WindowSet -> WindowSet)
-> (WindowSet -> WindowSet) -> WindowSet -> WindowSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Window -> WindowSet -> WindowSet
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 Window
w (WindowSet -> WindowSet) -> WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$ Window -> WindowSet -> WindowSet
bringWindow Window
w WindowSet
s)
allWindows :: XWindowMap
allWindows :: XWindowMap
allWindows = XWindowMap
windowMap
allApplications :: XWindowMap
allApplications :: XWindowMap
allApplications = XWindowMap
windowAppMap
wsWindows :: XWindowMap
wsWindows :: XWindowMap
wsWindows = (WindowSet -> X [Window]) -> X [Window]
forall a. (WindowSet -> X a) -> X a
withWindowSet ([Window] -> X [Window]
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Window] -> X [Window])
-> (WindowSet -> [Window]) -> WindowSet -> X [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet -> [Window]
forall i l a s sd. StackSet i l a s sd -> [a]
W.index) X [Window] -> ([Window] -> XWindowMap) -> XWindowMap
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Window] -> XWindowMap
winmap
where
winmap :: [Window] -> XWindowMap
winmap = ([(WorkspaceId, Window)] -> Map WorkspaceId Window)
-> X [(WorkspaceId, Window)] -> XWindowMap
forall a b. (a -> b) -> X a -> X b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(WorkspaceId, Window)] -> Map WorkspaceId Window
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList (X [(WorkspaceId, Window)] -> XWindowMap)
-> ([Window] -> X [(WorkspaceId, Window)])
-> [Window]
-> XWindowMap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Window -> X (WorkspaceId, Window))
-> [Window] -> X [(WorkspaceId, Window)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Window -> X (WorkspaceId, Window)
pair
pair :: Window -> X (WorkspaceId, Window)
pair Window
w = do WorkspaceId
name <- NamedWindow -> WorkspaceId
forall a. Show a => a -> WorkspaceId
show (NamedWindow -> WorkspaceId) -> X NamedWindow -> X WorkspaceId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window -> X NamedWindow
getName Window
w
(WorkspaceId, Window) -> X (WorkspaceId, Window)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (WorkspaceId
name, Window
w)
type XWindowMap = X (M.Map String Window)
windowPrompt :: XPConfig -> WindowPrompt -> XWindowMap -> X ()
windowPrompt :: XPConfig -> WindowPrompt -> XWindowMap -> X ()
windowPrompt XPConfig
c WindowPrompt
t XWindowMap
winmap = do
Map WorkspaceId Window
wm <- XWindowMap
winmap
let mode :: WindowModePrompt
mode = WindowPrompt
-> Map WorkspaceId Window
-> (WorkspaceId -> WorkspaceId -> Bool)
-> WindowModePrompt
WindowModePrompt WindowPrompt
t Map WorkspaceId Window
wm (XPConfig -> WorkspaceId -> WorkspaceId -> Bool
searchPredicate XPConfig
c)
action :: WorkspaceId -> WorkspaceId -> X ()
action = WindowModePrompt -> WorkspaceId -> WorkspaceId -> X ()
forall t. XPrompt t => t -> WorkspaceId -> WorkspaceId -> X ()
modeAction WindowModePrompt
mode
compList :: ComplFunction
compList = WindowModePrompt -> ComplFunction
forall t. XPrompt t => t -> ComplFunction
completionFunction WindowModePrompt
mode
WindowPrompt
-> XPConfig -> ComplFunction -> (WorkspaceId -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (WorkspaceId -> X ()) -> X ()
mkXPrompt WindowPrompt
t XPConfig
c ComplFunction
compList (\WorkspaceId
s -> WorkspaceId -> WorkspaceId -> X ()
action WorkspaceId
s WorkspaceId
s)
windowMultiPrompt :: XPConfig -> [(WindowPrompt, XWindowMap)] -> X ()
windowMultiPrompt :: XPConfig -> [(WindowPrompt, XWindowMap)] -> X ()
windowMultiPrompt XPConfig
c [(WindowPrompt, XWindowMap)]
modes = do
[XPType]
modes' <- [(WindowPrompt, XWindowMap)]
-> ((WindowPrompt, XWindowMap) -> X XPType) -> X [XPType]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(WindowPrompt, XWindowMap)]
modes (((WindowPrompt, XWindowMap) -> X XPType) -> X [XPType])
-> ((WindowPrompt, XWindowMap) -> X XPType) -> X [XPType]
forall a b. (a -> b) -> a -> b
$ \(WindowPrompt
t, XWindowMap
wm) -> do
Map WorkspaceId Window
wm' <- XWindowMap
wm
XPType -> X XPType
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (XPType -> X XPType)
-> (WindowModePrompt -> XPType) -> WindowModePrompt -> X XPType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowModePrompt -> XPType
forall p. XPrompt p => p -> XPType
XPT (WindowModePrompt -> X XPType) -> WindowModePrompt -> X XPType
forall a b. (a -> b) -> a -> b
$ WindowPrompt
-> Map WorkspaceId Window
-> (WorkspaceId -> WorkspaceId -> Bool)
-> WindowModePrompt
WindowModePrompt WindowPrompt
t Map WorkspaceId Window
wm' (XPConfig -> WorkspaceId -> WorkspaceId -> Bool
searchPredicate XPConfig
c)
[XPType] -> XPConfig -> X ()
mkXPromptWithModes [XPType]
modes' XPConfig
c
bringCopyWindow :: Window -> WindowSet -> WindowSet
bringCopyWindow :: Window -> WindowSet -> WindowSet
bringCopyWindow Window
w WindowSet
ws = Window -> WorkspaceId -> WindowSet -> WindowSet
forall a i s l sd.
(Eq a, Eq i, Eq s) =>
a -> i -> StackSet i l a s sd -> StackSet i l a s sd
copyWindow Window
w (WindowSet -> WorkspaceId
forall i l a s sd. StackSet i l a s sd -> i
W.currentTag WindowSet
ws) WindowSet
ws