You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FocusNth.hs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. -----------------------------------------------------------------------------
  2. -- |
  3. -- Module : XMonad.Actions.FocusNth
  4. -- Copyright : (c) Karsten Schoelzel <kuser@gmx.de>
  5. -- License : BSD
  6. --
  7. -- Maintainer : Karsten Schoelzel <kuser@gmx.de>
  8. -- Stability : stable
  9. -- Portability : unportable
  10. --
  11. -- Focus the nth window of the current workspace.
  12. -----------------------------------------------------------------------------
  13. module XMonad.Actions.FocusNth (
  14. -- * Usage
  15. -- $usage
  16. focusNth,focusNth',
  17. swapNth,swapNth') where
  18. import XMonad.StackSet
  19. import XMonad
  20. -- $usage
  21. -- Add the import to your @~\/.xmonad\/xmonad.hs@:
  22. --
  23. -- > import XMonad.Actions.FocusNth
  24. --
  25. -- Then add appropriate keybindings, for example:
  26. --
  27. -- > -- mod4-[1..9] @@ Switch to window N
  28. -- > ++ [((modm, k), focusNth i)
  29. -- > | (i, k) <- zip [0 .. 8] [xK_1 ..]]
  30. --
  31. -- For detailed instructions on editing your key bindings, see
  32. -- "XMonad.Doc.Extending#Editing_key_bindings".
  33. -- | Give focus to the nth window of the current workspace.
  34. focusNth :: Int -> X ()
  35. focusNth = windows . modify' . focusNth'
  36. focusNth' :: Int -> Stack a -> Stack a
  37. focusNth' n s@(Stack _ ls rs) | (n < 0) || (n > length(ls) + length(rs)) = s
  38. | otherwise = listToStack n (integrate s)
  39. -- | Swap current window with nth. Focus stays in the same position
  40. swapNth :: Int -> X ()
  41. swapNth = windows . modify' . swapNth'
  42. swapNth' :: Int -> Stack a -> Stack a
  43. swapNth' n s@(Stack c l r)
  44. | (n < 0) || (n > length l + length r) || (n == length l) = s
  45. | n < length l = let (nl, nc:nr) = splitAt (length l - n - 1) l in Stack nc (nl ++ c : nr) r
  46. | otherwise = let (nl, nc:nr) = splitAt (n - length l - 1) r in Stack nc l (nl ++ c : nr)
  47. listToStack :: Int -> [a] -> Stack a
  48. listToStack n l = Stack t ls rs
  49. where
  50. (t:rs) = drop n l
  51. ls = reverse (take n l)