make code more compact in XPrompt.

This commit is contained in:
David Roundy
2007-08-27 19:18:30 +00:00
parent 8935f2aa65
commit 9994baef5d

View File

@@ -265,53 +265,31 @@ data Direction = Prev | Next deriving (Eq,Show,Read)
keyPressHandle :: KeyMask -> KeyStroke -> XP () keyPressHandle :: KeyMask -> KeyStroke -> XP ()
-- commands: ctrl + ... todo -- commands: ctrl + ... todo
keyPressHandle mask _ keyPressHandle mask _
| mask == controlMask = do | mask == controlMask = eventLoop handle -- TODO
-- TODO
eventLoop handle
keyPressHandle _ (ks,_) keyPressHandle _ (ks,_)
-- Return: exit -- Return: exit
| ks == xK_Return = do | ks == xK_Return = do historyPush
historyPush liftIO $ hPutStrLn stderr "Hello world"
return () return ()
-- backspace -- backspace
| ks == xK_BackSpace = do | ks == xK_BackSpace = deleteString Prev >> go
deleteString Prev
go
-- delete -- delete
| ks == xK_Delete = do | ks == xK_Delete = deleteString Next >> go
deleteString Next
go
-- left -- left
| ks == xK_Left = do | ks == xK_Left = moveCursor Prev >> go
moveCursor Prev
go
-- right -- right
| ks == xK_Right = do | ks == xK_Right = moveCursor Next >> go
moveCursor Next
go
-- up -- up
| ks == xK_Up = do | ks == xK_Up = moveHistory Prev >> go
moveHistory Prev
go
-- down -- down
| ks == xK_Down = do | ks == xK_Down = moveHistory Next >> go
moveHistory Next
go
-- escape: exit and discard everything -- escape: exit and discard everything
| ks == xK_Escape = do | ks == xK_Escape = flushString >> return ()
flushString where go = updateWindows >> eventLoop handle
return ()
where
go = do
updateWindows
eventLoop handle
-- insert a character -- insert a character
keyPressHandle _ (_,s) keyPressHandle _ (_,s)
| s == "" = eventLoop handle | s == "" = eventLoop handle
| otherwise = do | otherwise = do insertString s
insertString s
updateWindows updateWindows
eventLoop handle eventLoop handle
@@ -327,8 +305,7 @@ insertString :: String -> XP ()
insertString str = insertString str =
modify (\s -> s { command = c (command s) (offset s), offset = o (offset s)} ) modify (\s -> s { command = c (command s) (offset s), offset = o (offset s)} )
where o oo = oo + length str where o oo = oo + length str
c oc oo c oc oo | oo >= length oc = oc ++ str
| oo >= length oc = oc ++ str
| otherwise = f ++ str ++ ss | otherwise = f ++ str ++ ss
where (f,ss) = splitAt oo oc where (f,ss) = splitAt oo oc