diff options
| author | Josh Rahm <rahm@google.com> | 2022-11-21 12:05:03 -0700 |
|---|---|---|
| committer | Josh Rahm <rahm@google.com> | 2022-11-21 12:05:03 -0700 |
| commit | ee9be16599f20aef6d1d3fd15666c00452f85aba (patch) | |
| tree | 1aed66c1de2ce201463e3becc2d452d4a8aa2992 /src/Rahm/Desktop/Keys/Dsl.hs | |
| parent | a1636c65e05d02f7d4fc408137e1d37b412ce890 (diff) | |
| download | rde-ee9be16599f20aef6d1d3fd15666c00452f85aba.tar.gz rde-ee9be16599f20aef6d1d3fd15666c00452f85aba.tar.bz2 rde-ee9be16599f20aef6d1d3fd15666c00452f85aba.zip | |
Format with ormolu.
Diffstat (limited to 'src/Rahm/Desktop/Keys/Dsl.hs')
| -rw-r--r-- | src/Rahm/Desktop/Keys/Dsl.hs | 182 |
1 files changed, 94 insertions, 88 deletions
diff --git a/src/Rahm/Desktop/Keys/Dsl.hs b/src/Rahm/Desktop/Keys/Dsl.hs index 55912f8..adb2668 100644 --- a/src/Rahm/Desktop/Keys/Dsl.hs +++ b/src/Rahm/Desktop/Keys/Dsl.hs @@ -1,27 +1,28 @@ -- Domain-specific language for configuring key/button bindings. module Rahm.Desktop.Keys.Dsl where -import Data.List -import Data.Bits ((.&.)) -import Control.Monad.Writer -import Text.Printf -import Control.Arrow (second, first) +import Control.Arrow (first, second) import Control.Monad (void) -import Control.Monad.State (State(..), modify', get, execState) -import XMonad +import Control.Monad.State (State (..), execState, get, modify') +import Control.Monad.Writer +import Data.Bits ((.&.)) +import Data.List import Data.Map (Map) import qualified Data.Map as Map +import Text.Printf +import XMonad data Documented t = Documented String t -data KeyBinding = - Action (X ()) | - Submap KeyBindings | - Repeat KeyBindings +data KeyBinding + = Action (X ()) + | Submap KeyBindings + | Repeat KeyBindings type KeyBindings = Map (KeyMask, KeySym) (Documented KeyBinding) type ButtonBinding = Window -> X () + type ButtonBindings = Map (KeyMask, Button) ButtonBinding {- Module that defines a DSL for binding keys. -} @@ -42,13 +43,14 @@ class Bindable k where type BindableMonad k :: (* -> *) -> * -> * bind :: k -> BindingBuilder (BindableValue k) a -> BindableMonad k l () - -- section :: String -> BindableMonad k l () -> BindableMonad k l () + +-- section :: String -> BindableMonad k l () -> BindableMonad k l () class Binding k b where toB :: k -> b rawMask :: KeyMask -> k -> BindingBuilder b () - rawMask m x = BindingBuilder $ modify' (second ((m, toB x):)) + rawMask m x = BindingBuilder $ modify' (second ((m, toB x) :)) instance Binding (X ()) (Documented KeyBinding) where toB = Documented "" . Action @@ -112,9 +114,10 @@ instance Bindable KeySym where m <- modMask <$> getConfig let (_, values) = execState stM (m, []) - KeysM $ modify' $ second $ - flip (<>) (Map.fromList (map (\(m, v) -> ((m, key), v)) values)) - + KeysM $ + modify' $ + second $ + flip (<>) (Map.fromList (map (\(m, v) -> ((m, key), v)) values)) instance Bindable Button where type BindableValue Button = ButtonBinding @@ -125,8 +128,10 @@ instance Bindable Button where m <- modMask <$> getConfig let (_, values) = execState stM (m, []) - ButtonsM $ modify' $ second $ - flip (<>) (Map.fromList (map (\(m, v) -> ((m, button), v)) values)) + ButtonsM $ + modify' $ + second $ + flip (<>) (Map.fromList (map (\(m, v) -> ((m, button), v)) values)) shiftControlAltSuperHyperAltgrMod :: (Binding k b) => k -> BindingBuilder b () shiftControlAltSuperHyperAltgrMod = @@ -376,10 +381,12 @@ altgrMod = maskMod altgrMask {- Can combine two or more of the functions above to apply the same action to - multiple masks. -} -(-|-) :: (Binding k b) => - (k -> BindingBuilder b ()) -> - (k -> BindingBuilder b ()) -> - k -> BindingBuilder b () +(-|-) :: + (Binding k b) => + (k -> BindingBuilder b ()) -> + (k -> BindingBuilder b ()) -> + k -> + BindingBuilder b () (-|-) fn1 fn2 f = fn1 f >> fn2 f {- Meant for submapping, binds all alphanumeric charactes to (fn c). -} @@ -392,63 +399,65 @@ mapNumbersAndAlpha km fn = do - pressed and fn is the function provided. -} mapNumbers :: KeyMask -> (Char -> X ()) -> KeysM l () mapNumbers km fn = do - mapM_ (\(key, ch) -> bind key $ rawMask km (fn ch)) - [ (xK_0, '0') - , (xK_1, '1') - , (xK_2, '2') - , (xK_3, '3') - , (xK_4, '4') - , (xK_5, '5') - , (xK_6, '6') - , (xK_7, '7') - , (xK_8, '8') - , (xK_9, '9') - -- Programmer Dvorak shifts the numbers so I have to map to their unshifted - -- form. - , (xK_bracketright, '6') - , (xK_exclam, '8') - , (xK_bracketleft, '7') - , (xK_braceleft, '5') - , (xK_braceright, '3') - , (xK_parenleft, '1') - , (xK_equal, '9') - , (xK_asterisk, '0') - , (xK_parenright, '2') - , (xK_plus, '4') ] + mapM_ + (\(key, ch) -> bind key $ rawMask km (fn ch)) + [ (xK_0, '0'), + (xK_1, '1'), + (xK_2, '2'), + (xK_3, '3'), + (xK_4, '4'), + (xK_5, '5'), + (xK_6, '6'), + (xK_7, '7'), + (xK_8, '8'), + (xK_9, '9'), + -- Programmer Dvorak shifts the numbers so I have to map to their unshifted + -- form. + (xK_bracketright, '6'), + (xK_exclam, '8'), + (xK_bracketleft, '7'), + (xK_braceleft, '5'), + (xK_braceright, '3'), + (xK_parenleft, '1'), + (xK_equal, '9'), + (xK_asterisk, '0'), + (xK_parenright, '2'), + (xK_plus, '4') + ] {- Meant for submapping. This binds all alpha charactes to (fn c) where c is the - character pressed and fn is the function provided. -} mapAlpha :: KeyMask -> (Char -> X ()) -> KeysM l () mapAlpha km fn = - mapM_ (\(key, ch) -> bind key $ rawMask km (fn ch)) [ - (xK_a, 'a') - , (xK_b, 'b') - , (xK_c, 'c') - , (xK_d, 'd') - , (xK_e, 'e') - , (xK_f, 'f') - , (xK_g, 'g') - , (xK_h, 'h') - , (xK_i, 'i') - , (xK_j, 'j') - , (xK_k, 'k') - , (xK_l, 'l') - , (xK_m, 'm') - , (xK_n, 'n') - , (xK_o, 'o') - , (xK_p, 'p') - , (xK_q, 'q') - , (xK_r, 'r') - , (xK_s, 's') - , (xK_t, 't') - , (xK_u, 'u') - , (xK_v, 'v') - , (xK_w, 'w') - , (xK_x, 'x') - , (xK_y, 'y') - , (xK_z, 'z') - ] - + mapM_ + (\(key, ch) -> bind key $ rawMask km (fn ch)) + [ (xK_a, 'a'), + (xK_b, 'b'), + (xK_c, 'c'), + (xK_d, 'd'), + (xK_e, 'e'), + (xK_f, 'f'), + (xK_g, 'g'), + (xK_h, 'h'), + (xK_i, 'i'), + (xK_j, 'j'), + (xK_k, 'k'), + (xK_l, 'l'), + (xK_m, 'm'), + (xK_n, 'n'), + (xK_o, 'o'), + (xK_p, 'p'), + (xK_q, 'q'), + (xK_r, 'r'), + (xK_s, 's'), + (xK_t, 't'), + (xK_u, 'u'), + (xK_v, 'v'), + (xK_w, 'w'), + (xK_x, 'x'), + (xK_y, 'y'), + (xK_z, 'z') + ] documentation :: KeyBindings -> String documentation = execWriter . document' "" @@ -467,8 +476,8 @@ documentation = execWriter . document' "" keyBindingsToList :: KeyBindings -> Map String (KeyBinding, [(KeyMask, KeySym)]) keyBindingsToList b = - (\list -> ((\(_, Documented _ t) -> t) (head list), map fst list)) <$> - group (\(_, Documented doc _) -> doc) (sortOn (snd . fst) $ Map.toList b) + (\list -> ((\(_, Documented _ t) -> t) (head list), map fst list)) + <$> group (\(_, Documented doc _) -> doc) (sortOn (snd . fst) $ Map.toList b) prettyShow :: (KeyMask, KeySym) -> String prettyShow (mask, key) = printf "%s%s" (showMask mask) (keysymToString key) @@ -477,20 +486,17 @@ documentation = execWriter . document' "" Action _ -> False _ -> True - showMask :: KeyMask -> String showMask mask = - let masks = [(shiftMask, "S"), - (altMask, "A"), - (mod3Mask, "H"), - (mod4Mask, "M"), - (altgrMask, "AGr"), - (controlMask, "C")] in - - concatMap ((++"-") . snd) $ filter ((/=0) . (.&.mask) . fst) masks - + let masks = + [ (shiftMask, "S"), + (altMask, "A"), + (mod3Mask, "H"), + (mod4Mask, "M"), + (altgrMask, "AGr"), + (controlMask, "C") + ] + in concatMap ((++ "-") . snd) $ filter ((/= 0) . (.&. mask) . fst) masks group :: (Ord b) => (a -> b) -> [a] -> Map b [a] group fn = Map.fromListWith (++) . map (first fn . (\a -> (a, [a]))) - - |