{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.CopyWindow
-- Description :  Duplicate a window on multiple workspaces.
-- Copyright   :  (c) David Roundy <droundy@darcs.net>, Ivan Veselov <veselov@gmail.com>, Lanny Ripple <lan3ny@gmail.com>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  ???
-- Stability   :  unstable
-- Portability :  unportable
--
-- Provides bindings to duplicate a window on multiple workspaces,
-- providing dwm-like tagging functionality.
--
-----------------------------------------------------------------------------

module XMonad.Actions.CopyWindow (
                                 -- * Usage
                                 -- $usage
                                 copy, copyToAll, copyWindow, runOrCopy
                                 , killAllOtherCopies, kill1, taggedWindows, copiesOfOn
                                 -- * Highlight workspaces containing copies in logHook
                                 -- $logHook
                                 , wsContainingCopies, copiesPP
                                ) where

import XMonad
import XMonad.Prelude
import Control.Arrow ((&&&))
import qualified Data.List as L

import XMonad.Actions.WindowGo
import XMonad.Hooks.StatusBar.PP (PP(..), WS(..), isHidden)
import qualified XMonad.StackSet as W

-- $usage
--
-- You can use this module with the following in your @xmonad.hs@ file:
--
-- > import XMonad.Actions.CopyWindow
--
-- Then add something like this to your keybindings:
--
-- > -- mod-[1..9] @@ Switch to workspace N
-- > -- mod-shift-[1..9] @@ Move client to workspace N
-- > -- mod-control-shift-[1..9] @@ Copy client to workspace N
-- > [((m .|. modm, k), windows $ f i)
-- >     | (i, k) <- zip (workspaces x) [xK_1 ..]
-- >     , (f, m) <- [(W.view, 0), (W.shift, shiftMask), (copy, shiftMask .|. controlMask)]]
--
-- To use the above key bindings you need also to import
-- "XMonad.StackSet":
--
-- > import qualified XMonad.StackSet as W
--
-- You may also wish to redefine the binding to kill a window so it only
-- removes it from the current workspace, if it's present elsewhere:
--
-- >  , ((modm .|. shiftMask, xK_c     ), kill1) -- @@ Close the focused window
--
-- Instead of copying a window from one workspace to another maybe you don't
-- want to have to remember where you placed it.  For that consider:
--
-- >  , ((modm, xK_b    ), runOrCopy "firefox" (className =? "Firefox")) -- @@ run or copy firefox
--
-- Another possibility which this extension provides is 'making window
-- always visible' (i.e. always on current workspace), similar to corresponding
-- metacity functionality. This behaviour is emulated through copying given
-- window to all the workspaces and then removing it when it's unneeded on
-- all workspaces any more.
--
-- Here is the example of keybindings which provide these actions:
--
-- >  , ((modm, xK_v ), windows copyToAll) -- @@ Make focused window always visible
-- >  , ((modm .|. shiftMask, xK_v ),  killAllOtherCopies) -- @@ Toggle window state back
--
-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

-- $logHook
--
-- To distinguish workspaces containing copies of the focused window, use 'copiesPP'.
-- 'copiesPP' takes a pretty printer and makes it aware of copies of the focused window.
-- It can be applied when creating a 'XMonad.Hooks.StatusBar.StatusBarConfig'.
--
-- A sample config looks like this:
--
-- > mySB = statusBarProp "xmobar" (copiesPP (pad . xmobarColor "red" "black") xmobarPP)
-- > main = xmonad $ withEasySB mySB defToggleStrutsKey def

-- | Take a pretty printer and make it aware of copies by using the provided function
-- to show hidden workspaces that contain copies of the focused window.
copiesPP :: (WorkspaceId -> String) -> PP -> X PP
copiesPP :: (WorkspaceId -> WorkspaceId) -> PP -> X PP
copiesPP WorkspaceId -> WorkspaceId
wtoS PP
pp = do
    [WorkspaceId]
copies <- X [WorkspaceId]
wsContainingCopies
    let check :: WS -> Bool
check WS{[Window]
WindowSet
WindowSpace
PP
wsPP :: WS -> PP
wsWS :: WS -> WindowSpace
wsWindowSet :: WS -> WindowSet
wsUrgents :: WS -> [Window]
wsPP :: PP
wsWS :: WindowSpace
wsWindowSet :: WindowSet
wsUrgents :: [Window]
..} = forall i l a. Workspace i l a -> i
W.tag WindowSpace
wsWS forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [WorkspaceId]
copies
    let printer :: ReaderT WS Maybe (WorkspaceId -> WorkspaceId)
printer = (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WS -> Bool
isHidden forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
<&&> WS -> Bool
check) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *). Alternative f => Bool -> f ()
guard) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> WorkspaceId -> WorkspaceId
wtoS
    forall (m :: * -> *) a. Monad m => a -> m a
return PP
pp{ ppPrinters :: ReaderT WS Maybe (WorkspaceId -> WorkspaceId)
ppPrinters = ReaderT WS Maybe (WorkspaceId -> WorkspaceId)
printer forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PP -> ReaderT WS Maybe (WorkspaceId -> WorkspaceId)
ppPrinters PP
pp }

-- | Copy the focused window to a workspace.
copy :: (Eq s, Eq i, Eq a) => i -> W.StackSet i l a s sd -> W.StackSet i l a s sd
copy :: forall s i a l sd.
(Eq s, Eq i, Eq a) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
copy i
n StackSet i l a s sd
s | Just a
w <- forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek StackSet i l a s sd
s = 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 a
w i
n StackSet i l a s sd
s
         | Bool
otherwise = StackSet i l a s sd
s

-- | Copy the focused window to all workspaces.
copyToAll :: (Eq s, Eq i, Eq a) => W.StackSet i l a s sd -> W.StackSet i l a s sd
copyToAll :: forall s i a l sd.
(Eq s, Eq i, Eq a) =>
StackSet i l a s sd -> StackSet i l a s sd
copyToAll StackSet i l a s sd
s = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall s i a l sd.
(Eq s, Eq i, Eq a) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
copy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> i
W.tag) StackSet i l a s sd
s (forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
W.workspaces StackSet i l a s sd
s)

-- | Copy an arbitrary window to a workspace.
copyWindow :: (Eq a, Eq i, Eq s) => a -> i -> W.StackSet i l a s sd -> W.StackSet i l a s sd
copyWindow :: 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 a
w i
n = forall {s} {l} {sd}.
Eq s =>
StackSet i l a s sd -> StackSet i l a s sd
copy'
    where copy' :: StackSet i l a s sd -> StackSet i l a s sd
copy' StackSet i l a s sd
s = if i
n forall i l a s sd. Eq i => i -> StackSet i l a s sd -> Bool
`W.tagMember` StackSet i l a s sd
s
                    then 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 i l a s sd. StackSet i l a s sd -> i
W.currentTag StackSet i l a s sd
s) forall a b. (a -> b) -> a -> b
$ forall {a} {i} {l} {s} {sd}.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
insertUp' a
w forall a b. (a -> b) -> a -> b
$ 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
n StackSet i l a s sd
s
                    else StackSet i l a s sd
s
          insertUp' :: a -> StackSet i l a s sd -> StackSet i l a s sd
insertUp' a
a = forall a i l s sd.
Maybe (Stack a)
-> (Stack a -> Maybe (Stack a))
-> StackSet i l a s sd
-> StackSet i l a s sd
W.modify (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
a [] [])
                          (\(W.Stack a
t [a]
l [a]
r) -> if a
a forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` a
tforall a. a -> [a] -> [a]
:[a]
lforall a. [a] -> [a] -> [a]
++[a]
r
                                             then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
t [a]
l [a]
r
                                             else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a] -> Stack a
W.Stack a
a (forall a. Eq a => a -> [a] -> [a]
L.delete a
a [a]
l) (forall a. Eq a => a -> [a] -> [a]
L.delete a
a (a
tforall a. a -> [a] -> [a]
:[a]
r)))


-- | runOrCopy will run the provided shell command unless it can
--  find a specified window in which case it will copy the window to
--  the current workspace.  Similar to (i.e., stolen from) "XMonad.Actions.WindowGo".
runOrCopy :: String -> Query Bool -> X ()
runOrCopy :: WorkspaceId -> Query Bool -> X ()
runOrCopy = X () -> Query Bool -> X ()
copyMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadIO m => WorkspaceId -> m ()
spawn

-- | Copy a window if it exists, run the first argument otherwise.
copyMaybe :: X () -> Query Bool -> X ()
copyMaybe :: X () -> Query Bool -> X ()
copyMaybe X ()
f Query Bool
qry = Query Bool -> ManageHook -> X () -> X ()
ifWindow Query Bool
qry forall {l} {sd}.
Query (Endo (StackSet WorkspaceId l Window ScreenId sd))
copyWin X ()
f
    where copyWin :: Query (Endo (StackSet WorkspaceId l Window ScreenId sd))
copyWin = forall r (m :: * -> *). MonadReader r m => m r
ask forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Window
w -> forall s. (s -> s) -> Query (Endo s)
doF (\StackSet WorkspaceId l Window ScreenId sd
ws -> 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 (forall i l a s sd. StackSet i l a s sd -> i
W.currentTag StackSet WorkspaceId l Window ScreenId sd
ws) StackSet WorkspaceId l Window ScreenId sd
ws)

-- | Remove the focused window from this workspace.  If it's present in no
-- other workspace, then kill it instead. If we do kill it, we'll get a
-- delete notify back from X.
--
-- There are two ways to delete a window. Either just kill it, or if it
-- supports the delete protocol, send a delete event (e.g. firefox).
kill1 :: X ()
kill1 :: X ()
kill1 = do WindowSet
ss <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
           forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust (forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ss) forall a b. (a -> b) -> a -> b
$ \Window
w -> if forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Bool
W.member Window
w forall a b. (a -> b) -> a -> b
$ forall {a} {i} {l} {s} {sd}.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
delete'' Window
w WindowSet
ss
                                      then (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$ forall {a} {i} {l} {s} {sd}.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
delete'' Window
w
                                      else X ()
kill
    where delete'' :: a -> StackSet i l a s sd -> StackSet i l a s sd
delete'' a
w = forall a i l s sd.
Maybe (Stack a)
-> (Stack a -> Maybe (Stack a))
-> StackSet i l a s sd
-> StackSet i l a s sd
W.modify forall a. Maybe a
Nothing (forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
W.filter (forall a. Eq a => a -> a -> Bool
/= a
w))

-- | Kill all other copies of focused window (if they're present).
-- 'All other' means here 'copies which are not on the current workspace'.
killAllOtherCopies :: X ()
killAllOtherCopies :: X ()
killAllOtherCopies = do WindowSet
ss <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
                        forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust (forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ss) forall a b. (a -> b) -> a -> b
$ \Window
w -> (WindowSet -> WindowSet) -> X ()
windows forall a b. (a -> b) -> a -> b
$
                                                   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 i l a s sd. StackSet i l a s sd -> i
W.currentTag WindowSet
ss) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                                                   forall {sid} {b} {a} {l} {sd}.
(Eq sid, Eq b, Eq a) =>
a -> StackSet b l a sid sd -> StackSet b l a sid sd
delFromAllButCurrent Window
w
    where
      delFromAllButCurrent :: a -> StackSet b l a sid sd -> StackSet b l a sid sd
delFromAllButCurrent a
w StackSet b l a sid sd
ss = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall {s} {i} {a} {l} {sd}.
(Eq s, Eq i, Eq a) =>
a -> i -> StackSet i l a s sd -> StackSet i l a s sd
delWinFromWorkspace a
w forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a. Workspace i l a -> i
W.tag)
                                  StackSet b l a sid sd
ss
                                  (forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
W.hidden StackSet b l a sid sd
ss forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible StackSet b l a sid sd
ss))
      delWinFromWorkspace :: a -> i -> StackSet i l a s sd -> StackSet i l a s sd
delWinFromWorkspace a
w i
wid = forall {s} {i} {s} {l} {a} {sd} {l} {a} {sd}.
(Eq s, Eq i, Eq s) =>
i
-> (StackSet i l a s sd -> StackSet i l a s sd)
-> StackSet i l a s sd
-> StackSet i l a s sd
viewing i
wid forall a b. (a -> b) -> a -> b
$ forall a i l s sd.
Maybe (Stack a)
-> (Stack a -> Maybe (Stack a))
-> StackSet i l a s sd
-> StackSet i l a s sd
W.modify forall a. Maybe a
Nothing (forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
W.filter (forall a. Eq a => a -> a -> Bool
/= a
w))

      viewing :: i
-> (StackSet i l a s sd -> StackSet i l a s sd)
-> StackSet i l a s sd
-> StackSet i l a s sd
viewing i
wis StackSet i l a s sd -> StackSet i l a s sd
f StackSet i l a s sd
ss = 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 i l a s sd. StackSet i l a s sd -> i
W.currentTag StackSet i l a s sd
ss) forall a b. (a -> b) -> a -> b
$ StackSet i l a s sd -> StackSet i l a s sd
f forall a b. (a -> b) -> a -> b
$ 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
wis StackSet i l a s sd
ss

-- | A list of hidden workspaces containing a copy of the focused window.
wsContainingCopies :: X [WorkspaceId]
wsContainingCopies :: X [WorkspaceId]
wsContainingCopies = do
    WindowSet
ws <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a i. Eq a => Maybe a -> [(i, [a])] -> [i]
copiesOfOn (forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ws) (forall i l a. [Workspace i l a] -> [(i, [a])]
taggedWindows forall a b. (a -> b) -> a -> b
$ forall i l a s sd. StackSet i l a s sd -> [Workspace i l a]
W.hidden WindowSet
ws)

-- | Get a list of tuples (tag, [Window]) for each workspace.
taggedWindows :: [W.Workspace i l a] -> [(i, [a])]
taggedWindows :: forall i l a. [Workspace i l a] -> [(i, [a])]
taggedWindows = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a -> b) -> a -> b
$ forall i l a. Workspace i l a -> i
W.tag forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, 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

-- | Get tags with copies of the focused window (if present.)
copiesOfOn :: (Eq a) => Maybe a -> [(i, [a])] -> [i]
copiesOfOn :: forall a i. Eq a => Maybe a -> [(i, [a])] -> [i]
copiesOfOn Maybe a
foc [(i, [a])]
tw = forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] a -> [i]
hasCopyOf Maybe a
foc
  where hasCopyOf :: a -> [i]
hasCopyOf a
f = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter ((a
f forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(i, [a])]
tw