aboutsummaryrefslogtreecommitdiff
path: root/src/Internal/Keys.hs
blob: 89b4e0fe2a7f17616e82110edec1e02748fc965b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
module Internal.Keys where

import Debug.Trace
import Control.Applicative
import Prelude hiding ((!!))
import Control.Monad
import Data.Char
import Data.List hiding ((!!))
import Data.List.Safe ((!!))
import Data.Map (Map)
import Internal.Layout
import Internal.Marking
import Internal.PromptConfig
import System.IO
import Text.Printf
import XMonad
import XMonad.Actions.Submap
import XMonad.Actions.WindowNavigation
import XMonad.Prompt
import XMonad.Prompt.Input
import XMonad.Prompt.Shell
import XMonad.Util.CustomKeys
import XMonad.Util.Scratchpad
import qualified Data.Map as Map
import qualified XMonad.StackSet as W

type KeyMap l = XConfig l -> Map (KeyMask, KeySym) (X ())

applyKeys :: XConfig l -> IO (XConfig l)
applyKeys config@(XConfig {modMask = modm}) = do
   ks <- newKeys
   withWindowNavigation (xK_k, xK_h, xK_j, xK_l) $
     config { keys = ks }

data WinPrompt = WinPrompt

instance XPrompt WinPrompt where
    showXPrompt _ = "[Window] "
    commandToComplete _ = id

getHorizontallyOrderedScreens ::
  W.StackSet wid l a ScreenId ScreenDetail ->
    [W.Screen wid l a ScreenId ScreenDetail]
-- ^ Returns a list of screens ordered from leftmost to rightmost.
getHorizontallyOrderedScreens windowSet =
    flip sortBy screens $ \sc1 sc2 ->
      let (SD (Rectangle x1 _ _ _)) = W.screenDetail sc1
          (SD (Rectangle x2 _ _ _)) = W.screenDetail sc2
          in x1 `compare` x2
    where
      screens = W.current windowSet : W.visible windowSet

newKeys :: IO (KeyMap l)
newKeys =
  withNewMarkContext $ \markContext ->
    return $ \config@(XConfig {modMask = modm}) ->
      let workspacesByInt =
            Map.fromList $
              zip ['1'..] (XMonad.workspaces config)

          gotoWorkspace ch = do
            saveLastMark markContext
            windows $ W.greedyView $ return ch

          shiftToWorkspace ch = do
            windows $ W.shift $ return ch

          swapWs f t (W.Workspace t' l s) | t' == f = W.Workspace t l s
          swapWs f t (W.Workspace t' l s) | t' == t = W.Workspace f l s
          swapWs _ _ ws = ws

          swapSc f t (W.Screen ws a b) = W.Screen (swapWs f t ws) a b

          swapWorkspace :: Char -> X ()
          swapWorkspace toChar = do
              windows $ \ss -> do
                let from  = W.tag $ W.workspace $ W.current ss
                    to = [toChar] in
                  (W.StackSet (swapSc from to $ W.current ss)
                              (map (swapSc from to) $ W.visible ss)
                              (map (swapWs from to) $ W.hidden ss)
                              (W.floating ss))

          fuzzyCompletion s1 s0 =
            let ws = filter (not . all isSpace) $ words (map toLower s1)
                l0 = map toLower s0 in
                 all (`isInfixOf`l0) ws

          getString = runQuery $ do
              t <- title
              a <- appName
              return $
                if map toLower a `isInfixOf` map toLower t
                  then t
                  else printf "%s - %s" a t

          withScreen :: (WorkspaceId -> WindowSet -> WindowSet) -> Int -> X ()
          withScreen fn n = do
            saveLastMark markContext
            windows $ \windowSet ->
              case (getHorizontallyOrderedScreens windowSet !! n) of
                Nothing -> windowSet
                Just screen -> fn (W.tag $ W.workspace screen) windowSet

          windowJump = do
              windowTitlesToWinId <- withWindowSet $ \ss ->
                Map.fromList <$>
                  mapM (\wid -> (,) <$> getString wid <*> return wid)
                       (W.allWindows ss)

              mkXPrompt
                WinPrompt
                xpConfig
                (\input -> do
                  return $ filter (fuzzyCompletion input) (Map.keys windowTitlesToWinId)) $ \str -> do
                    saveLastMark markContext
                    case Map.lookup str windowTitlesToWinId of
                        Just w -> focus w
                        Nothing ->
                          case filter (fuzzyCompletion str) (Map.keys windowTitlesToWinId) of
                              [s] ->
                                mapM_ focus (Map.lookup s windowTitlesToWinId)
                              _ -> return ()

          in

      Map.fromList
        [ ((modm, xK_F12), (void $ spawn "spotify-control next"))
        , ((modm, xK_F11), (void $ spawn "spotify-control prev"))
        , ((modm, xK_semicolon), scratchpadSpawnActionTerminal "scratchpad")
        , ((modm, xK_F10), (void $ spawn "spotify-control play"))
        , ((modm, xK_r),   (void $ spawn "dmenu_run"))
        , ((modm .|. shiftMask, xK_r),   (void $ spawn "gmrun"))
        , ((modm .|. mod1Mask, xK_l), (void $ spawn "xsecurelock"))
        , ((modm .|. mod1Mask, xK_s), (void $ spawn "xsecurelock && pm-suspend"))
        , ((modm .|. shiftMask, xK_c), kill)
        , ((modm .|. shiftMask, xK_t), withFocused $ windows . W.sink)
        , ((modm, xK_t),  (void $ spawn (terminal config)))
        , ((modm, xK_m), (submap $ mapAlpha modm (markCurrentWindow markContext)))
        , ((modm, xK_w), windowJump)
        , ((modm, xK_apostrophe), (submap $
              Map.insert
                (modm, xK_apostrophe)
                (jumpToLast markContext)
                (mapAlpha modm (jumpToMark markContext))))
        , ((modm, xK_g), (submap $ mapNumbersAndAlpha 0 gotoWorkspace))
        , ((modm .|. shiftMask, xK_g), (submap $ mapNumbersAndAlpha 0 shiftToWorkspace))
        , ((modm .|. shiftMask .|. mod1Mask, xK_g), (submap $ mapNumbersAndAlpha 0 swapWorkspace))

        , ((modm .|. shiftMask, xK_bracketleft), sendMessage (IncMasterN (-1)))
        , ((modm .|. shiftMask, xK_bracketright), sendMessage (IncMasterN 1))
        , ((modm, xK_bracketleft), sendMessage ShrinkZoom)
        , ((modm, xK_bracketright), sendMessage ExpandZoom)

        , ((modm, xK_space), sendMessage NextLayout)

        , ((modm, xK_q), spawn "xmonad --recompile && xmonad --restart")
        , ((modm, xK_z), sendMessage ToggleZoom)

        , ((modm, xK_Tab), windows W.focusDown)
        , ((modm .|. shiftMask, xK_Tab), windows W.focusUp)

        , ((modm, xK_a), withScreen W.view 0)
        , ((modm, xK_o), withScreen W.view 1)
        , ((modm, xK_e), withScreen W.view 2)

        , ((modm .|. shiftMask, xK_a), withScreen W.shift 0)
        , ((modm .|. shiftMask, xK_o), withScreen W.shift 1)
        , ((modm .|. shiftMask, xK_e), withScreen W.shift 2)
        ]

mapNumbersAndAlpha :: KeyMask -> (Char -> X ()) -> Map (KeyMask, KeySym) (X ())
mapNumbersAndAlpha km fn =
    mapNumbers km fn
      <> mapAlpha km fn

mapNumbers :: KeyMask -> (Char -> X ()) -> Map (KeyMask, KeySym) (X ())
mapNumbers km fn =
  Map.fromList [
    ((km, xK_0), fn '0')
  , ((km, xK_1), fn '1')
  , ((km, xK_2), fn '2')
  , ((km, xK_3), fn '3')
  , ((km, xK_4), fn '4')
  , ((km, xK_5), fn '5')
  , ((km, xK_6), fn '6')
  , ((km, xK_7), fn '7')
  , ((km, xK_8), fn '8')
  , ((km, xK_9), fn '9')
  ]

mapAlpha :: KeyMask -> (Char -> X ()) -> Map (KeyMask, KeySym) (X ())
mapAlpha km fn =
  Map.fromList [
    ((km, xK_a), fn 'a')
  , ((km, xK_b), fn 'b')
  , ((km, xK_c), fn 'c')
  , ((km, xK_d), fn 'd')
  , ((km, xK_e), fn 'e')
  , ((km, xK_f), fn 'f')
  , ((km, xK_g), fn 'g')
  , ((km, xK_h), fn 'h')
  , ((km, xK_i), fn 'i')
  , ((km, xK_j), fn 'j')
  , ((km, xK_k), fn 'k')
  , ((km, xK_l), fn 'l')
  , ((km, xK_m), fn 'm')
  , ((km, xK_n), fn 'n')
  , ((km, xK_o), fn 'o')
  , ((km, xK_p), fn 'p')
  , ((km, xK_q), fn 'q')
  , ((km, xK_r), fn 'r')
  , ((km, xK_s), fn 's')
  , ((km, xK_t), fn 't')
  , ((km, xK_u), fn 'u')
  , ((km, xK_v), fn 'v')
  , ((km, xK_w), fn 'w')
  , ((km, xK_x), fn 'x')
  , ((km, xK_y), fn 'y')
  , ((km, xK_z), fn 'z')
  ]