defm03

XMonad-StackSet

Jun 26th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 22.66 KB | None | 0 0
  1. {-# LANGUAGE PatternGuards #-}
  2.  
  3. -----------------------------------------------------------------------------
  4. -- |
  5. -- Module      :  XMonad.StackSet
  6. -- Copyright   :  (c) Don Stewart 2007
  7. -- License     :  BSD3-style (see LICENSE)
  8. --
  9. -- Maintainer  :  [email protected]
  10. -- Stability   :  experimental
  11. -- Portability :  portable, Haskell 98
  12. --
  13.  
  14. module XMonad.StackSet (
  15.         -- * Introduction
  16.         -- $intro
  17.  
  18.         -- ** The Zipper
  19.         -- $zipper
  20.  
  21.         -- ** Xinerama support
  22.         -- $xinerama
  23.  
  24.         -- ** Master and Focus
  25.         -- $focus
  26.  
  27.         StackSet(..), Workspace(..), Screen(..), Stack(..), RationalRect(..),
  28.         -- *  Construction
  29.         -- $construction
  30.         new, view, greedyView,
  31.         -- * Xinerama operations
  32.         -- $xinerama
  33.         lookupWorkspace,
  34.         screens, workspaces, allWindows, currentTag,
  35.         -- *  Operations on the current stack
  36.         -- $stackOperations
  37.         peek, index, integrate, integrate', differentiate,
  38.        focusUp, focusDown, focusUp', focusDown', focusMaster, focusWindow,
  39.        tagMember, renameTag, ensureTags, member, findTag, mapWorkspace, mapLayout,
  40.        -- * Modifying the stackset
  41.        -- $modifyStackset
  42.        insertUp, delete, delete', filter,
  43.         -- * Setting the master window
  44.         -- $settingMW
  45.         swapUp, swapDown, swapMaster, shiftMaster, modify, modify', float, sink, -- needed by users
  46.        -- * Composite operations
  47.        -- $composite
  48.        shift, shiftWin,
  49.  
  50.        -- for testing
  51.        abort
  52.    ) where
  53.  
  54. import Prelude hiding (filter)
  55. import Data.Maybe   (listToMaybe,isJust,fromMaybe)
  56. import qualified Data.List as L (deleteBy,find,splitAt,filter,nub)
  57. import Data.List ( (\\) )
  58. import qualified Data.Map  as M (Map,insert,delete,empty)
  59.  
  60. -- $intro
  61. --
  62. -- The 'StackSet' data type encodes a window manager abstraction. The
  63. -- window manager is a set of virtual workspaces. On each workspace is a
  64. -- stack of windows. A given workspace is always current, and a given
  65. -- window on each workspace has focus. The focused window on the current
  66. -- workspace is the one which will take user input. It can be visualised
  67. -- as follows:
  68. --
  69. -- > Workspace  { 0*}   { 1 }   { 2 }   { 3 }   { 4 }
  70. -- >
  71. -- > Windows    [1      []      [3*     [6*]    []
  72. -- >            ,2*]            ,4
  73. -- >                            ,5]
  74. --
  75. -- Note that workspaces are indexed from 0, windows are numbered
  76. -- uniquely. A '*' indicates the window on each workspace that has
  77. -- focus, and which workspace is current.
  78.  
  79. -- $zipper
  80. --
  81. -- We encode all the focus tracking directly in the data structure, with a 'zipper':
  82. --
  83. --    A Zipper is essentially an `updateable' and yet pure functional
  84. --    cursor into a data structure. Zipper is also a delimited
  85. --    continuation reified as a data structure.
  86. --
  87. --    The Zipper lets us replace an item deep in a complex data
  88. --    structure, e.g., a tree or a term, without an  mutation.  The
  89. --    resulting data structure will share as much of its components with
  90. --    the old structure as possible.
  91. --
  92. --      Oleg Kiselyov, 27 Apr 2005, haskell\@, "Zipper as a delimited continuation"
  93. --
  94. -- We use the zipper to keep track of the focused workspace and the
  95. -- focused window on each workspace, allowing us to have correct focus
  96. -- by construction. We closely follow Huet's original implementation:
  97. --
  98. --      G. Huet, /Functional Pearl: The Zipper/,
  99. --      1997, J. Functional Programming 75(5):549-554.
  100. -- and:
  101. --      R. Hinze and J. Jeuring, /Functional Pearl: The Web/.
  102. --
  103. -- and Conor McBride's zipper differentiation paper.
  104. -- Another good reference is:
  105. --
  106. --      The Zipper, Haskell wikibook
  107.  
  108. -- $xinerama
  109. -- Xinerama in X11 lets us view multiple virtual workspaces
  110. -- simultaneously. While only one will ever be in focus (i.e. will
  111. -- receive keyboard events), other workspaces may be passively
  112. -- viewable.  We thus need to track which virtual workspaces are
  113. -- associated (viewed) on which physical screens.  To keep track of
  114. -- this, 'StackSet' keeps separate lists of visible but non-focused
  115. -- workspaces, and non-visible workspaces.
  116.  
  117. -- $focus
  118. --
  119. -- Each stack tracks a focused item, and for tiling purposes also tracks
  120. -- a 'master' position. The connection between 'master' and 'focus'
  121. -- needs to be well defined, particularly in relation to 'insert' and
  122. -- 'delete'.
  123. --
  124.  
  125. ------------------------------------------------------------------------
  126. -- |
  127. -- A cursor into a non-empty list of workspaces.
  128. --
  129. -- We puncture the workspace list, producing a hole in the structure
  130. -- used to track the currently focused workspace. The two other lists
  131. -- that are produced are used to track those workspaces visible as
  132. -- Xinerama screens, and those workspaces not visible anywhere.
  133.  
  134. data StackSet i l a sid sd =
  135.     StackSet { current  :: !(Screen i l a sid sd)    -- ^ currently focused workspace
  136.              , visible  :: [Screen i l a sid sd]     -- ^ non-focused workspaces, visible in xinerama
  137.              , hidden   :: [Workspace i l a]         -- ^ workspaces not visible anywhere
  138.              , floating :: M.Map a RationalRect      -- ^ floating windows
  139.              } deriving (Show, Read, Eq)
  140.  
  141. -- | Visible workspaces, and their Xinerama screens.
  142. data Screen i l a sid sd = Screen { workspace :: !(Workspace i l a)
  143.                                   , screen :: !sid
  144.                                   , screenDetail :: !sd }
  145.     deriving (Show, Read, Eq)
  146.  
  147. -- |
  148. -- A workspace is just a tag, a layout, and a stack.
  149. --
  150. data Workspace i l a = Workspace  { tag :: !i, layout :: l, stack :: Maybe (Stack a) }
  151.     deriving (Show, Read, Eq)
  152.  
  153. -- | A structure for window geometries
  154. data RationalRect = RationalRect Rational Rational Rational Rational
  155.     deriving (Show, Read, Eq)
  156.  
  157. -- |
  158. -- A stack is a cursor onto a window list.
  159. -- The data structure tracks focus by construction, and
  160. -- the master window is by convention the top-most item.
  161. -- Focus operations will not reorder the list that results from
  162. -- flattening the cursor. The structure can be envisaged as:
  163. --
  164. -- >    +-- master:  < '7' >
  165. -- > up |            [ '2' ]
  166. -- >    +---------   [ '3' ]
  167. -- > focus:          < '4' >
  168. -- > dn +----------- [ '8' ]
  169. --
  170. -- A 'Stack' can be viewed as a list with a hole punched in it to make
  171. -- the focused position. Under the zipper\/calculus view of such
  172. -- structures, it is the differentiation of a [a], and integrating it
  173. -- back has a natural implementation used in 'index'.
  174. --
  175. data Stack a = Stack { focus  :: !a        -- focused thing in this set
  176.                      , up     :: [a]       -- clowns to the left
  177.                      , down   :: [a] }     -- jokers to the right
  178.     deriving (Show, Read, Eq)
  179.  
  180.  
  181. -- | this function indicates to catch that an error is expected
  182. abort :: String -> a
  183. abort x = error $ "xmonad: StackSet: " ++ x
  184.  
  185. -- ---------------------------------------------------------------------
  186. -- $construction
  187.  
  188. -- | /O(n)/. Create a new stackset, of empty stacks, with given tags,
  189. -- with physical screens whose descriptions are given by 'm'. The
  190. -- number of physical screens (@length 'm'@) should be less than or
  191. -- equal to the number of workspace tags.  The first workspace in the
  192. -- list will be current.
  193. --
  194. -- Xinerama: Virtual workspaces are assigned to physical screens, starting at 0.
  195. --
  196. new :: (Integral s) => l -> [i] -> [sd] -> StackSet i l a s sd
  197. new l wids m | not (null wids) && length m <= length wids && not (null m)
  198.   = StackSet cur visi unseen M.empty
  199.   where (seen,unseen) = L.splitAt (length m) $ map (\i -> Workspace i l Nothing) wids
  200.         (cur:visi)    = [ Screen i s sd |  (i, s, sd) <- zip3 seen [0..] m ]
  201.                 -- now zip up visibles with their screen id
  202. new _ _ _ = abort "non-positive argument to StackSet.new"
  203.  
  204. -- |
  205. -- /O(w)/. Set focus to the workspace with index \'i\'.
  206. -- If the index is out of range, return the original 'StackSet'.
  207. --
  208. -- Xinerama: If the workspace is not visible on any Xinerama screen, it
  209. -- becomes the current screen. If it is in the visible list, it becomes
  210. -- current.
  211.  
  212. view :: (Eq s, Eq i) => i -> StackSet i l a s sd -> StackSet i l a s sd
  213. view i s
  214.     | i == currentTag s = s  -- current
  215.  
  216.     | Just x <- L.find ((i==).tag.workspace) (visible s)
  217.     -- if it is visible, it is just raised
  218.     = s { current = x, visible = current s : L.deleteBy (equating screen) x (visible s) }
  219.  
  220.     | Just x <- L.find ((i==).tag)           (hidden  s) -- must be hidden then
  221.     -- if it was hidden, it is raised on the xine screen currently used
  222.     = s { current = (current s) { workspace = x }
  223.         , hidden = workspace (current s) : L.deleteBy (equating tag) x (hidden s) }
  224.  
  225.     | otherwise = s -- not a member of the stackset
  226.  
  227.   where equating f = \x y -> f x == f y
  228.  
  229.     -- 'Catch'ing this might be hard. Relies on monotonically increasing
  230.     -- workspace tags defined in 'new'
  231.     --
  232.     -- and now tags are not monotonic, what happens here?
  233.  
  234. -- |
  235. -- Set focus to the given workspace.  If that workspace does not exist
  236. -- in the stackset, the original workspace is returned.  If that workspace is
  237. -- 'hidden', then display that workspace on the current screen, and move the
  238. -- current workspace to 'hidden'.  If that workspace is 'visible' on another
  239. -- screen, the workspaces of the current screen and the other screen are
  240. -- swapped.
  241.  
  242. greedyView :: (Eq s, Eq i) => i -> StackSet i l a s sd -> StackSet i l a s sd
  243. greedyView w ws
  244.      | any wTag (hidden ws) = view w ws
  245.      | (Just s) <- L.find (wTag . workspace) (visible ws)
  246.                             = ws { current = (current ws) { workspace = workspace s }
  247.                                  , visible = s { workspace = workspace (current ws) }
  248.                                            : L.filter (not . wTag . workspace) (visible ws) }
  249.      | otherwise = ws
  250.    where wTag = (w == ) . tag
  251.  
  252. -- ---------------------------------------------------------------------
  253. -- $xinerama
  254.  
  255. -- | Find the tag of the workspace visible on Xinerama screen 'sc'.
  256. -- 'Nothing' if screen is out of bounds.
  257. lookupWorkspace :: Eq s => s -> StackSet i l a s sd -> Maybe i
  258. lookupWorkspace sc w = listToMaybe [ tag i | Screen i s _ <- current w : visible w, s == sc ]
  259.  
  260. -- ---------------------------------------------------------------------
  261. -- $stackOperations
  262.  
  263. -- |
  264. -- The 'with' function takes a default value, a function, and a
  265. -- StackSet. If the current stack is Nothing, 'with' returns the
  266. -- default value. Otherwise, it applies the function to the stack,
  267. -- returning the result. It is like 'maybe' for the focused workspace.
  268. --
  269. with :: b -> (Stack a -> b) -> StackSet i l a s sd -> b
  270. with dflt f = maybe dflt f . stack . workspace . current
  271.  
  272. -- |
  273. -- Apply a function, and a default value for 'Nothing', to modify the current stack.
  274. --
  275. modify :: Maybe (Stack a) -> (Stack a -> Maybe (Stack a)) -> StackSet i l a s sd -> StackSet i l a s sd
  276. modify d f s = s { current = (current s)
  277.                         { workspace = (workspace (current s)) { stack = with d f s }}}
  278.  
  279. -- |
  280. -- Apply a function to modify the current stack if it isn't empty, and we don't
  281. --  want to empty it.
  282. --
  283. modify' :: (Stack a -> Stack a) -> StackSet i l a s sd -> StackSet i l a s sd
  284. modify' f = modify Nothing (Just . f)
  285.  
  286. -- |
  287. -- /O(1)/. Extract the focused element of the current stack.
  288. -- Return 'Just' that element, or 'Nothing' for an empty stack.
  289. --
  290. peek :: StackSet i l a s sd -> Maybe a
  291. peek = with Nothing (return . focus)
  292.  
  293. -- |
  294. -- /O(n)/. Flatten a 'Stack' into a list.
  295. --
  296. integrate :: Stack a -> [a]
  297. integrate (Stack x l r) = reverse l ++ x : r
  298.  
  299. -- |
  300. -- /O(n)/ Flatten a possibly empty stack into a list.
  301. integrate' :: Maybe (Stack a) -> [a]
  302. integrate' = maybe [] integrate
  303.  
  304. -- |
  305. -- /O(n)/. Turn a list into a possibly empty stack (i.e., a zipper):
  306. -- the first element of the list is current, and the rest of the list
  307. -- is down.
  308. differentiate :: [a] -> Maybe (Stack a)
  309. differentiate []     = Nothing
  310. differentiate (x:xs) = Just $ Stack x [] xs
  311.  
  312. -- |
  313. -- /O(n)/. 'filter p s' returns the elements of 's' such that 'p' evaluates to
  314. -- 'True'.  Order is preserved, and focus moves as described for 'delete'.
  315. --
  316. filter :: (a -> Bool) -> Stack a -> Maybe (Stack a)
  317. filter p (Stack f ls rs) = case L.filter p (f:rs) of
  318.     f':rs' -> Just $ Stack f' (L.filter p ls) rs'    -- maybe move focus down
  319.     []     -> case L.filter p ls of                  -- filter back up
  320.                     f':ls' -> Just $ Stack f' ls' [] -- else up
  321.                     []     -> Nothing
  322.  
  323. -- |
  324. -- /O(s)/. Extract the stack on the current workspace, as a list.
  325. -- The order of the stack is determined by the master window -- it will be
  326. -- the head of the list. The implementation is given by the natural
  327. -- integration of a one-hole list cursor, back to a list.
  328. --
  329. index :: StackSet i l a s sd -> [a]
  330. index = with [] integrate
  331.  
  332. -- |
  333. -- /O(1), O(w) on the wrapping case/.
  334. --
  335. -- focusUp, focusDown. Move the window focus up or down the stack,
  336. -- wrapping if we reach the end. The wrapping should model a 'cycle'
  337. -- on the current stack. The 'master' window, and window order,
  338. -- are unaffected by movement of focus.
  339. --
  340. -- swapUp, swapDown, swap the neighbour in the stack ordering, wrapping
  341. -- if we reach the end. Again the wrapping model should 'cycle' on
  342. -- the current stack.
  343. --
  344. focusUp, focusDown, swapUp, swapDown :: StackSet i l a s sd -> StackSet i l a s sd
  345. focusUp   = modify' focusUp'
  346. focusDown = modify' focusDown'
  347.  
  348. swapUp    = modify' swapUp'
  349. swapDown  = modify' (reverseStack . swapUp' . reverseStack)
  350.  
  351. -- | Variants of 'focusUp' and 'focusDown' that work on a
  352. -- 'Stack' rather than an entire 'StackSet'.
  353. focusUp', focusDown' :: Stack a -> Stack a
  354. focusUp' (Stack t (l:ls) rs) = Stack l ls (t:rs)
  355. focusUp' (Stack t []     rs) = Stack x xs [] where (x:xs) = reverse (t:rs)
  356. focusDown'                   = reverseStack . focusUp' . reverseStack
  357.  
  358. swapUp' :: Stack a -> Stack a
  359. swapUp'  (Stack t (l:ls) rs) = Stack t ls (l:rs)
  360. swapUp'  (Stack t []     rs) = Stack t (reverse rs) []
  361.  
  362. -- | reverse a stack: up becomes down and down becomes up.
  363. reverseStack :: Stack a -> Stack a
  364. reverseStack (Stack t ls rs) = Stack t rs ls
  365.  
  366. --
  367. -- | /O(1) on current window, O(n) in general/. Focus the window 'w',
  368. -- and set its workspace as current.
  369. --
  370. focusWindow :: (Eq s, Eq a, Eq i) => a -> StackSet i l a s sd -> StackSet i l a s sd
  371. focusWindow w s | Just w == peek s = s
  372.                | otherwise        = fromMaybe s $ do
  373.                    n <- findTag w s
  374.                    return $ until ((Just w ==) . peek) focusUp (view n s)
  375.  
  376. -- | Get a list of all screens in the 'StackSet'.
  377. screens :: StackSet i l a s sd -> [Screen i l a s sd]
  378. screens s = current s : visible s
  379.  
  380. -- | Get a list of all workspaces in the 'StackSet'.
  381. workspaces :: StackSet i l a s sd -> [Workspace i l a]
  382. workspaces s = workspace (current s) : map workspace (visible s) ++ hidden s
  383.  
  384. -- | Get a list of all windows in the 'StackSet' in no particular order
  385. allWindows :: Eq a => StackSet i l a s sd -> [a]
  386. allWindows = L.nub . concatMap (integrate' . stack) . workspaces
  387.  
  388. -- | Get the tag of the currently focused workspace.
  389. currentTag :: StackSet i l a s sd -> i
  390. currentTag = tag . workspace . current
  391.  
  392. -- | Is the given tag present in the 'StackSet'?
  393. tagMember :: Eq i => i -> StackSet i l a s sd -> Bool
  394. tagMember t = elem t . map tag . workspaces
  395.  
  396. -- | Rename a given tag if present in the 'StackSet'.
  397. renameTag :: Eq i => i -> i -> StackSet i l a s sd -> StackSet i l a s sd
  398. renameTag o n = mapWorkspace rename
  399.     where rename w = if tag w == o then w { tag = n } else w
  400.  
  401. -- | Ensure that a given set of workspace tags is present by renaming
  402. -- existing workspaces and\/or creating new hidden workspaces as
  403. -- necessary.
  404. ensureTags :: Eq i => l -> [i] -> StackSet i l a s sd -> StackSet i l a s sd
  405. ensureTags l allt st = et allt (map tag (workspaces st) \\ allt) st
  406.     where et [] _ s = s
  407.           et (i:is) rn s | i `tagMember` s = et is rn s
  408.           et (i:is) [] s = et is [] (s { hidden = Workspace i l Nothing : hidden s })
  409.           et (i:is) (r:rs) s = et is rs $ renameTag r i s
  410.  
  411. -- | Map a function on all the workspaces in the 'StackSet'.
  412. mapWorkspace :: (Workspace i l a -> Workspace i l a) -> StackSet i l a s sd -> StackSet i l a s sd
  413. mapWorkspace f s = s { current = updScr (current s)
  414.                      , visible = map updScr (visible s)
  415.                      , hidden  = map f (hidden s) }
  416.     where updScr scr = scr { workspace = f (workspace scr) }
  417.  
  418. -- | Map a function on all the layouts in the 'StackSet'.
  419. mapLayout :: (l -> l') -> StackSet i l a s sd -> StackSet i l' a s sd
  420. mapLayout f (StackSet v vs hs m) = StackSet (fScreen v) (map fScreen vs) (map fWorkspace hs) m
  421.  where
  422.     fScreen (Screen ws s sd) = Screen (fWorkspace ws) s sd
  423.     fWorkspace (Workspace t l s) = Workspace t (f l) s
  424.  
  425. -- | /O(n)/. Is a window in the 'StackSet'?
  426. member :: Eq a => a -> StackSet i l a s sd -> Bool
  427. member a s = isJust (findTag a s)
  428.  
  429. -- | /O(1) on current window, O(n) in general/.
  430. -- Return 'Just' the workspace tag of the given window, or 'Nothing'
  431. -- if the window is not in the 'StackSet'.
  432. findTag :: Eq a => a -> StackSet i l a s sd -> Maybe i
  433. findTag a s = listToMaybe
  434.     [ tag w | w <- workspaces s, has a (stack w) ]
  435.     where has _ Nothing         = False
  436.           has x (Just (Stack t l r)) = x `elem` (t : l ++ r)
  437.  
  438. -- ---------------------------------------------------------------------
  439. -- $modifyStackset
  440.  
  441. -- |
  442. -- /O(n)/. (Complexity due to duplicate check). Insert a new element
  443. -- into the stack, above the currently focused element. The new
  444. -- element is given focus; the previously focused element is moved
  445. -- down.
  446. --
  447. -- If the element is already in the stackset, the original stackset is
  448. -- returned unmodified.
  449. --
  450. -- Semantics in Huet's paper is that insert doesn't move the cursor.
  451. -- However, we choose to insert above, and move the focus.
  452. --
  453. insertUp :: Eq a => a -> StackSet i l a s sd -> StackSet i l a s sd
  454. insertUp a s = if member a s then s else insert
  455.   where insert = modify (Just $ Stack a [] []) (\(Stack t l r) -> Just $ Stack a l (t:r)) s
  456.  
  457. -- insertDown :: a -> StackSet i l a s sd -> StackSet i l a s sd
  458. -- insertDown a = modify (Stack a [] []) $ \(Stack t l r) -> Stack a (t:l) r
  459. -- Old semantics, from Huet.
  460. -- >    w { down = a : down w }
  461.  
  462. -- |
  463. -- /O(1) on current window, O(n) in general/. Delete window 'w' if it exists.
  464. -- There are 4 cases to consider:
  465. --
  466. --   * delete on an 'Nothing' workspace leaves it Nothing
  467. --
  468. --   * otherwise, try to move focus to the down
  469. --
  470. --   * otherwise, try to move focus to the up
  471. --
  472. --   * otherwise, you've got an empty workspace, becomes 'Nothing'
  473. --
  474. -- Behaviour with respect to the master:
  475. --
  476. --   * deleting the master window resets it to the newly focused window
  477. --
  478. --   * otherwise, delete doesn't affect the master.
  479. --
  480. delete :: (Ord a, Eq s) => a -> StackSet i l a s sd -> StackSet i l a s sd
  481. delete w = sink w . delete' w
  482.  
  483. -- | Only temporarily remove the window from the stack, thereby not destroying special
  484. -- information saved in the 'Stackset'
  485. delete' :: (Eq a, Eq s) => a -> StackSet i l a s sd -> StackSet i l a s sd
  486. delete' w s = s { current = removeFromScreen        (current s)
  487.                , visible = map removeFromScreen    (visible s)
  488.                , hidden  = map removeFromWorkspace (hidden  s) }
  489.    where removeFromWorkspace ws = ws { stack = stack ws >>= filter (/=w) }
  490.          removeFromScreen scr   = scr { workspace = removeFromWorkspace (workspace scr) }
  491.  
  492. ------------------------------------------------------------------------
  493.  
  494. -- | Given a window, and its preferred rectangle, set it as floating
  495. -- A floating window should already be managed by the 'StackSet'.
  496. float :: Ord a => a -> RationalRect -> StackSet i l a s sd -> StackSet i l a s sd
  497. float w r s = s { floating = M.insert w r (floating s) }
  498.  
  499. -- | Clear the floating status of a window
  500. sink :: Ord a => a -> StackSet i l a s sd -> StackSet i l a s sd
  501. sink w s = s { floating = M.delete w (floating s) }
  502.  
  503. ------------------------------------------------------------------------
  504. -- $settingMW
  505.  
  506. -- | /O(s)/. Set the master window to the focused window.
  507. -- The old master window is swapped in the tiling order with the focused window.
  508. -- Focus stays with the item moved.
  509. swapMaster :: StackSet i l a s sd -> StackSet i l a s sd
  510. swapMaster = modify' $ \c -> case c of
  511.     Stack _ [] _  -> c    -- already master.
  512.     Stack t ls rs -> Stack t [] (xs ++ x : rs) where (x:xs) = reverse ls
  513.  
  514. -- natural! keep focus, move current to the top, move top to current.
  515.  
  516. -- | /O(s)/. Set the master window to the focused window.
  517. -- The other windows are kept in order and shifted down on the stack, as if you
  518. -- just hit mod-shift-k a bunch of times.
  519. -- Focus stays with the item moved.
  520. shiftMaster :: StackSet i l a s sd -> StackSet i l a s sd
  521. shiftMaster = modify' $ \c -> case c of
  522.    Stack _ [] _ -> c     -- already master.
  523.    Stack t ls rs -> Stack t [] (reverse ls ++ rs)
  524.  
  525. -- | /O(s)/. Set focus to the master window.
  526. focusMaster :: StackSet i l a s sd -> StackSet i l a s sd
  527. focusMaster = modify' $ \c -> case c of
  528.     Stack _ [] _  -> c
  529.     Stack t ls rs -> Stack x [] (xs ++ t : rs) where (x:xs) = reverse ls
  530.  
  531. --
  532. -- ---------------------------------------------------------------------
  533. -- $composite
  534.  
  535. -- | /O(w)/. shift. Move the focused element of the current stack to stack
  536. -- 'n', leaving it as the focused element on that stack. The item is
  537. -- inserted above the currently focused element on that workspace.
  538. -- The actual focused workspace doesn't change. If there is no
  539. -- element on the current stack, the original stackSet is returned.
  540. --
  541. shift :: (Ord a, Eq s, Eq i) => i -> StackSet i l a s sd -> StackSet i l a s sd
  542. shift n s = maybe s (\w -> shiftWin n w s) (peek s)
  543.  
  544. -- | /O(n)/. shiftWin. Searches for the specified window 'w' on all workspaces
  545. -- of the stackSet and moves it to stack 'n', leaving it as the focused
  546. -- element on that stack. The item is inserted above the currently
  547. -- focused element on that workspace.
  548. -- The actual focused workspace doesn't change. If the window is not
  549. -- found in the stackSet, the original stackSet is returned.
  550. shiftWin :: (Ord a, Eq a, Eq s, Eq i) => i -> a -> StackSet i l a s sd -> StackSet i l a s sd
  551. shiftWin n w s = case findTag w s of
  552.                     Just from | n `tagMember` s && n /= from -> go from s
  553.                     _                                        -> s
  554.  where go from = onWorkspace n (insertUp w) . onWorkspace from (delete' w)
  555.  
  556. onWorkspace :: (Eq i, Eq s) => i -> (StackSet i l a s sd -> StackSet i l a s sd)
  557.            -> (StackSet i l a s sd -> StackSet i l a s sd)
  558. onWorkspace n f s = view (currentTag s) . f . view n $ s
Advertisement
Add Comment
Please, Sign In to add comment