Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created December 22, 2015 23:48
Show Gist options
  • Save aavogt/a9f08689b2741bd5a1e3 to your computer and use it in GitHub Desktop.
Save aavogt/a9f08689b2741bd5a1e3 to your computer and use it in GitHub Desktop.
FocusNth swapNth
-----------------------------------------------------------------------------
-- |
-- Module : XMonad.Actions.FocusNth
-- Copyright : (c) Karsten Schoelzel <[email protected]>
-- License : BSD
--
-- Maintainer : Karsten Schoelzel <[email protected]>
-- Stability : stable
-- Portability : unportable
--
-- Focus the nth window of the current workspace.
-----------------------------------------------------------------------------
module XMonad.Actions.FocusNth (
-- * Usage
-- $usage
focusNth,focusNth',
swapNth,swapNth') where
import XMonad.StackSet
import XMonad
-- $usage
-- Add the import to your @~\/.xmonad\/xmonad.hs@:
--
-- > import XMonad.Actions.FocusNth
--
-- Then add appropriate keybindings, for example:
--
-- > -- mod4-[1..9] @@ Switch to window N
-- > ++ [((modm, k), focusNth i)
-- > | (i, k) <- zip [0 .. 8] [xK_1 ..]]
--
-- For detailed instructions on editing your key bindings, see
-- "XMonad.Doc.Extending#Editing_key_bindings".
-- | Give focus to the nth window of the current workspace.
focusNth :: Int -> X ()
focusNth = windows . modify' . focusNth'
focusNth' :: Int -> Stack a -> Stack a
focusNth' n s@(Stack _ ls rs) | (n < 0) || (n > length(ls) + length(rs)) = s
| otherwise = listToStack n (integrate s)
swapNth :: Int -> X ()
swapNth = windows . modify' . swapNth'
swapNth' :: Int -> Stack a -> Stack a
swapNth' n s@(Stack c ls rs)
| (n < 0) || (n > length ls + length rs) || (n == length ls) = s
| n < length ls = let (nl, (nc : nr)) = splitAt (n + 1) ls in Stack nc (nl ++ c : nr) rs
| otherwise = let (nl, (nc : nr)) = splitAt (n - length ls) rs in Stack nc ls (nl ++ c : nr)
listToStack :: Int -> [a] -> Stack a
listToStack n l = Stack t ls rs
where
(t:rs) = drop n l
ls = reverse (take n l)
pattern_match_failure = swapNth' 1 (Stack 0 [1,2] []) :: Stack Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment