diff --git a/CHANGES.md b/CHANGES.md index 2d3983dbc..f6274bffc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -65,6 +65,11 @@ (See also `XMonad.Hooks.FloatConfigureReq` and/or `XMonad.Util.Hacks` for additional Steam client workarounds.) + * `XMonad.Actions.Submap` + + - Added `visualSubmapSorted` to enable sorting of the keymap + descriptions. + ### Other changes ## 0.18.0 (February 3, 2024) diff --git a/XMonad/Actions/Submap.hs b/XMonad/Actions/Submap.hs index 0c6a80d8b..896af7240 100644 --- a/XMonad/Actions/Submap.hs +++ b/XMonad/Actions/Submap.hs @@ -18,6 +18,7 @@ module XMonad.Actions.Submap ( -- $usage submap, visualSubmap, + visualSubmapSorted, submapDefault, submapDefaultWithKey, @@ -88,15 +89,32 @@ visualSubmap :: WindowConfig -- ^ The config for the spawned window. -> M.Map (KeyMask, KeySym) (String, X ()) -- ^ A map @keybinding -> (description, action)@. -> X () -visualSubmap wc keys = +visualSubmap = visualSubmapSorted id + +-- | Like 'visualSubmap', but is able to sort the descriptions. +-- For example, +-- +-- > import Data.Ord (comparing, Down) +-- > +-- > visualSubmapSorted (sortBy (comparing Down)) def +-- +-- would sort the @(key, description)@ pairs by their keys in descending +-- order. +visualSubmapSorted :: ([((KeyMask, KeySym), String)] -> [((KeyMask, KeySym), String)]) + -- ^ A function to resort the descriptions + -> WindowConfig -- ^ The config for the spawned window. + -> M.Map (KeyMask, KeySym) (String, X ()) + -- ^ A map @keybinding -> (description, action)@. + -> X () +visualSubmapSorted sorted wc keys = withSimpleWindow wc descriptions waitForKeyPress >>= \(m', s) -> maybe (pure ()) snd (M.lookup (m', s) keys) where descriptions :: [String] descriptions = - zipWith (\key desc -> keyToString key <> ": " <> desc) - (M.keys keys) - (map fst (M.elems keys)) + map (\(key, desc) -> keyToString key <> ": " <> desc) + . sorted + $ zip (M.keys keys) (map fst (M.elems keys)) -- | Give a name to an action. subName :: String -> X () -> (String, X ())