aboutsummaryrefslogtreecommitdiffstats
path: root/CycleWS.hs
blob: a123370ae33ef4ea1483a27d991f1e856f9a9dbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonadContrib.CycleWS
-- Copyright   :  (c) Joachim Breitner <mail@joachim-breitner.de>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Joachim Breitner <mail@joachim-breitner.de>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Provides bindings to cycle forward or backward through the list
-- of workspaces, and to move windows there.
--
-----------------------------------------------------------------------------

module XMonadContrib.CycleWS (
                              -- * Usage
                              -- $usage
                              nextWS,
                              prevWS,
			      shiftToNext,
			      shiftToPrev,
                              toggleWS,
                             ) where

import Control.Monad.State ( gets )
import Data.List ( sortBy, findIndex )
import Data.Maybe ( fromMaybe )
import Data.Ord ( comparing )

import XMonad
import StackSet hiding (filter, findIndex)
import Operations
import {-# SOURCE #-} qualified Config (workspaces)

-- $usage
-- You can use this module with the following in your Config.hs file:
--
-- > import XMonadContrib.CycleWS
--
-- >   , ((modMask,               xK_Right), nextWS)
-- >   , ((modMask,               xK_Left),  prevWS)
-- >   , ((modMask .|. shiftMask, xK_Right), shiftToNext)
-- >   , ((modMask .|. shiftMask, xK_Left),  shiftToPrev)
-- >   , ((modMask,               xK_t),     toggleWS)
--
-- If you want to follow the moved window, you can use both actions:
--
-- >   , ((modMask .|. shiftMask, xK_Right), shiftToNext >> nextWS)
-- >   , ((modMask .|. shiftMask, xK_Left),  shiftToPrev >> prevWS)
--

-- %import XMonadContrib.CycleWS
-- %keybind , ((modMask,               xK_Right), nextWS)
-- %keybind , ((modMask,               xK_Left),  prevWS)
-- %keybind , ((modMask .|. shiftMask, xK_Right), shiftToNext)
-- %keybind , ((modMask .|. shiftMask, xK_Left),  shiftToPrev)
-- %keybind , ((modMask,               xK_t),     toggleWS)


-- | Switch to next workspace
nextWS :: X ()
nextWS = switchWorkspace 1

-- | Switch to previous workspace
prevWS :: X ()
prevWS = switchWorkspace (-1)

-- | Move focused window to next workspace
shiftToNext :: X ()
shiftToNext = shiftBy 1

-- | Move focused window to previous workspace
shiftToPrev :: X ()
shiftToPrev = shiftBy (-1)

-- | Toggle to the workspace displayed previously
toggleWS :: X ()
toggleWS = windows $ view =<< tag . head . hidden

switchWorkspace :: Int -> X ()
switchWorkspace d = wsBy d >>= windows . greedyView

shiftBy :: Int -> X ()
shiftBy d = wsBy d >>= windows . shift

wsBy :: Int -> X (WorkspaceId)
wsBy d = do
    ws <- gets windowset
    let orderedWs = sortBy (comparing wsIndex) (workspaces ws)
    let now = fromMaybe 0 $ findWsIndex (workspace (current ws)) orderedWs
    let next = orderedWs !! ((now + d) `mod` length orderedWs)
    return $ tag next

wsIndex :: WindowSpace -> Maybe Int
wsIndex ws = findIndex (== tag ws) Config.workspaces

findWsIndex :: WindowSpace -> [WindowSpace] -> Maybe Int
findWsIndex ws wss = findIndex ((== tag ws) . tag) wss