From 548f4bad33b2aaa427f25126a92b12f3ab5268fe Mon Sep 17 00:00:00 2001 From: Don Stewart Date: Thu, 19 Apr 2007 00:42:36 +0200 Subject: Promote now swaps focused window with master window This means other windows are unaffected. The change from the previous cycling behaviour was felt necessary, since cycling isn't a terribly useful operation. Some properties that hold: focus is unchanged by promotion promote is idempotent (promoting twice does nothing) the focused and master window will swap their positions in the stack darcs-hash:20070418224236-9c5c1-4539f9f397e181f91dc9743e3ffac4b5796c0fac.gz --- StackSet.hs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'StackSet.hs') diff --git a/StackSet.hs b/StackSet.hs index 89a8484..a7bc376 100644 --- a/StackSet.hs +++ b/StackSet.hs @@ -24,7 +24,7 @@ module StackSet where import Data.Maybe -import qualified Data.List as L (delete,genericLength) +import qualified Data.List as L (delete,genericLength,elemIndex) import qualified Data.Map as M ------------------------------------------------------------------------ @@ -216,12 +216,35 @@ raiseFocus k w = case M.lookup k (cache w) of Nothing -> w Just i -> (view i w) { focus = M.insert i k (focus w) } --- | Cycle the current stack ordering. In tiled mode has the effect of --- moving a new window into the master position, without changing focus. -promote :: StackSet a -> StackSet a +-- | Swap the currently focused window with the master window (the +-- window on top of the stack). Focus moves to the master. +promote :: Ord a => StackSet a -> StackSet a +promote w = maybe w id $ do + a <- peek w -- fail if null + let w' = w { stacks = M.adjust (\s -> swap a (head s) s) (current w) (stacks w) } + return $ insert a (current w) w' -- and maintain focus + +-- +-- | Swap first occurences of 'a' and 'b' in list. +-- If both elements are not in the list, the list is unchanged. +-- +swap :: Eq a => a -> a -> [a] -> [a] +swap a b xs + | a == b = xs -- do nothing + | Just ai <- L.elemIndex a xs + , Just bi <- L.elemIndex b xs = insertAt bi a (insertAt ai b xs) + where + insertAt n x ys = as ++ x : tail bs + where (as,bs) = splitAt n ys + +swap _ _ xs = xs -- do nothing + +{- +-- cycling: promote w = w { stacks = M.adjust next (current w) (stacks w) } where next [] = [] next xs = last xs : init xs +-} -- | elemAfter :: Eq a => a -> [a] -> Maybe a -- cgit v1.2.3