summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorJason Creighton <jcreigh@gmail.com>2007-05-27 08:29:14 +0200
committerJason Creighton <jcreigh@gmail.com>2007-05-27 08:29:14 +0200
commitaf77755668b7a3dc3f6bcf9c0bbf7e01852ab670 (patch)
tree9dc33d9d98841a33cbf4d35ed6b4f331602b94fa /util
parentf80572c4c2d27db83f759b584f6c7a84d33a6041 (diff)
downloadmetatile-af77755668b7a3dc3f6bcf9c0bbf7e01852ab670.tar
metatile-af77755668b7a3dc3f6bcf9c0bbf7e01852ab670.zip
Generate keybindings section in manpage from Config.hs
darcs-hash:20070527062914-b9aa7-d28805de3a198f81ca54cb800250bb16f65ab036
Diffstat (limited to 'util')
-rw-r--r--util/GenerateManpage.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/util/GenerateManpage.hs b/util/GenerateManpage.hs
new file mode 100644
index 0000000..540c74c
--- /dev/null
+++ b/util/GenerateManpage.hs
@@ -0,0 +1,47 @@
+--
+-- Generates man/xmonad.1 from man/xmonad.1.in by filling the list of
+-- keybindings with values scraped from Config.hs
+--
+-- Format for the docstrings in Config.hs takes the following form:
+--
+-- -- mod-x @@ Frob the whatsit
+--
+-- "Frob the whatsit" will be used as the description for keybinding "mod-x"
+--
+-- If the keybinding name is omitted, it will try to guess from the rest of the
+-- line. For example:
+--
+-- [ ((modMask .|. shiftMask, xK_Return), spawn "xterm") -- @@ Launch an xterm
+--
+-- Here, mod-shift-return will be used as the keybinding name.
+--
+import Control.Monad
+import Text.Regex.Posix
+import Data.Char
+import Data.List
+
+trim :: String -> String
+trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
+
+guessKeys line = concat $ intersperse "-" (modifiers ++ [map toLower key])
+ where modifiers = map (!!1) (line =~ "(mod|shift|control)Mask")
+ (_, _, _, [key]) = line =~ "xK_(\\w+)" :: (String, String, String, [String])
+
+binding :: [String] -> (String, String)
+binding [ _, bindingLine, "", desc ] = (guessKeys bindingLine, desc)
+binding [ _, _, keyCombo, desc ] = (keyCombo, desc)
+
+allBindings :: String -> [(String, String)]
+allBindings xs = map (binding . map trim) (xs =~ "(.*)--(.*)@@(.*)")
+
+-- FIXME: What escaping should we be doing on these strings?
+troff :: (String, String) -> String
+troff (key, desc) = ".IP \\fB" ++ key ++ "\\fR\n" ++ desc ++ "\n"
+
+replace :: Eq a => a -> a -> [a] -> [a]
+replace x y = map (\a -> if a == x then y else a)
+
+main = do
+ troffBindings <- (concatMap troff . allBindings) `liftM` readFile "./Config.hs"
+ let sed = unlines . replace "___KEYBINDINGS___" troffBindings . lines
+ readFile "./man/xmonad.1.in" >>= return . sed >>= writeFile "./man/xmonad.1"