Move my NextWorkspace functionality into CycleWS

Hi,

This patch merges the additional functionality of my NextWorkspace into CycleWS,
using a compatible interface for what was there before.

Greetings,
Joachim
This commit is contained in:
mail
2007-10-07 10:39:33 +00:00
parent a40f8c9c5f
commit af3efea238
3 changed files with 79 additions and 129 deletions

View File

@@ -1,51 +1,99 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- | -- |
-- Module : XMonadContrib.CycleWS -- Module : XMonadContrib.CycleWS
-- Copyright : (C) 2007 Andrea Rossato -- Copyright : (c) Joachim Breitner <mail@joachim-breitner.de>
-- License : BSD3 -- License : BSD3-style (see LICENSE)
-- --
-- Maintainer : andrea.rossato@unibz.it -- Maintainer : Joachim Breitner <mail@joachim-breitner.de>
-- Stability : unstable -- Stability : unstable
-- Portability : unportable -- Portability : unportable
-- --
-- A module to cycle between Workspaces -- Provides bindings to cycle forward or backward through the list
-- of workspaces, and to move windows there.
-- --
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
module XMonadContrib.CycleWS ( module XMonadContrib.CycleWS (
-- * Usage -- * Usage
-- $usage -- $usage
nextWS nextWS,
, prevWS prevWS,
shiftToNext,
shiftToPrev,
) where ) where
import Control.Monad.State ( gets )
import Data.List ( sortBy, findIndex )
import Data.Maybe ( fromMaybe )
import Data.Ord ( comparing )
import XMonad import XMonad
import StackSet hiding (filter, findIndex)
import Operations import Operations
import qualified StackSet as W import {-# SOURCE #-} qualified Config (workspaces)
import {-# SOURCE #-} Config (workspaces)
import Data.List
-- $usage -- $usage
-- Import this module in Config.hs: -- You can use this module with the following in your Config.hs file:
-- --
-- > import XMonadContrib.CycleWS -- > import XMonadContrib.NextWorkspace
-- --
-- And add, in you key bindings: -- > , ((modMask, xK_Right), nextWS)
-- > , ((modMask, xK_Left), prevWWS)
-- > , ((modMask .|. shiftMask, xK_Right), shiftToNext)
-- > , ((modMask .|. shiftMask, xK_Left), shiftToPrev)
--
-- 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)
-- --
-- > , ((modMask , xK_comma ), prevWS )
-- > , ((modMask , xK_period), nextWS )
nextWS, prevWS :: X () -- %import XMonadContrib.NextWorkspace
nextWS = withWindowSet $ \s -> windows $ W.view (workspaces !! (setWS s N)) -- %keybind , ((modMask, xK_Right), nextWS)
prevWS = withWindowSet $ \s -> windows $ W.view (workspaces !! (setWS s P)) -- %keybind , ((modMask, xK_Left), prevWWS)
-- %keybind , ((modMask .|. shiftMask, xK_Right), shiftToNext)
-- %keybind , ((modMask .|. shiftMask, xK_Left), shiftToPrev)
data Dir = P | N deriving Eq
setWS :: WindowSet -> Dir -> Int -- ---------------------
setWS s d -- |
| d == N && cur == (lw - 1) = 0 -- Switch to next workspace
| d == N = cur + 1 nextWS :: X()
| d == P && cur == 0 = lw - 1 nextWS = switchWorkspace (1)
| otherwise = cur - 1
where -- ---------------------
cur = maybe 0 id $ elemIndex (W.tag (W.workspace ((W.current s)))) workspaces -- |
lw = length workspaces -- 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)
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

View File

@@ -55,7 +55,6 @@ import XMonadContrib.Maximize ()
import XMonadContrib.MosaicAlt () import XMonadContrib.MosaicAlt ()
import XMonadContrib.MouseGestures () import XMonadContrib.MouseGestures ()
import XMonadContrib.NamedWindows () import XMonadContrib.NamedWindows ()
import XMonadContrib.NextWorkspace ()
import XMonadContrib.NoBorders () import XMonadContrib.NoBorders ()
import XMonadContrib.ResizableTile () import XMonadContrib.ResizableTile ()
import XMonadContrib.Roledex () import XMonadContrib.Roledex ()

View File

@@ -1,97 +0,0 @@
-----------------------------------------------------------------------------
-- |
-- Module : XMonadContrib.NextWorkspace
-- 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.NextWorkspace (
-- * Usage
-- $usage
nextWorkspace,
prevWorkspace,
shiftToNext,
shiftToPrev,
) 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.NextWorkspace
--
-- > , ((modMask, xK_Right), nextWorkspace)
-- > , ((modMask, xK_Left), prevWorkspace)
-- > , ((modMask .|. shiftMask, xK_Right), shiftToNext)
-- > , ((modMask .|. shiftMask, xK_Left), shiftToPrev)
--
-- If you want to follow the moved window, you can use both actions:
--
-- > , ((modMask .|. shiftMask, xK_Right), shiftToNext >> nextWorkspace)
-- > , ((modMask .|. shiftMask, xK_Left), shiftToPrev >> prevWorkspace)
--
-- %import XMonadContrib.RotView
-- %keybind , ((modMask .|. shiftMask, xK_Right), rotView True)
-- %keybind , ((modMask .|. shiftMask, xK_Left), rotView False)
-- ---------------------
-- |
-- Switch to next workspace
nextWorkspace :: X()
nextWorkspace = switchWorkspace (1)
-- ---------------------
-- |
-- Switch to previous workspace
prevWorkspace :: X()
prevWorkspace = switchWorkspace (-1)
-- |
-- Move focused window to next workspace
shiftToNext :: X()
shiftToNext = shiftBy (1)
-- |
-- Move focused window to previous workspace
shiftToPrev :: X ()
shiftToPrev = shiftBy (-1)
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