From beac392d39f7a94e8a1f0be366e4c2bf58852e8d Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 27 Jul 2014 19:30:36 +0200 Subject: modal editing begins --- Main.hs | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'Main.hs') diff --git a/Main.hs b/Main.hs index df6da8d..7028998 100644 --- a/Main.hs +++ b/Main.hs @@ -27,7 +27,7 @@ data VTState = VTState , mode :: Mode } -emptyState = VTState emptyBuffer (NormalMode nmap) +emptyState = VTState emptyBuffer InsertMode @@ -47,7 +47,7 @@ main = do hFlush stdout forkIO $ (dateThread 1000000) lock - uiThread (NormalMode nmap) lock + uiThread InsertMode lock dateThread delay lock = forever $ do @@ -107,6 +107,7 @@ data Command | ExecuteInputBuffer | UnboundSequence String String | MotionCommand LeftRightMotion + | ChangeMode Mode -- TODO Move Count Motion -- Delete Count Register Motion -- etc. @@ -185,6 +186,9 @@ execCommand (MotionCommand x) = execCommand (InsertChar c) = modifyBuffer (insertChar c) +execCommand (ChangeMode m) = + modify $ \ q -> q { mode = m } + execCommand (InsertCharThenChangeMode c m) = modify $ \ q -> q { mode = m @@ -290,10 +294,16 @@ charToCode c = "\\x" ++ showIntAtBase 16 intToDigit (ord c) "" +nmap = + [ ("i", ChangeMode InsertMode) + , ("0", MotionCommand GotoFirstChar) + , ("$", MotionCommand GotoEndOfLine) + ] -nmap = - [ ("\x01", MotionCommand GotoFirstChar) +imap = + [ ("\x1b", ChangeMode NormalMode) + , ("\x01", MotionCommand GotoFirstChar) , ("\x05", MotionCommand GotoEndOfLine) , ("\x1b[3~", KillNextChar) , ("\x1b[C", MotionCommand $ GotoRight 1) @@ -376,28 +386,33 @@ unboundSequence seq name = (seq, UnboundSequence seq name) +type Keymap = [(String, Command)] + data Mode - = NormalMode [(String, Command)] + = InsertMode + | NormalMode | VerbatimMode instance Show Mode where - show (NormalMode _) = "normal" + show NormalMode = "normal" + show InsertMode = "insert" show VerbatimMode = "verbatim" getCommand :: Mode -> IO Command -getCommand (NormalMode map) = getMappedCommand map +getCommand InsertMode = getMappedCommand imap InsertChar +getCommand NormalMode = getMappedCommand nmap (AlertBadInput . (:[])) getCommand VerbatimMode = verbatimKeymap -getMappedCommand :: [(String, Command)] -> IO Command -getMappedCommand xs = do +getMappedCommand :: Keymap -> (Char -> Command) -> IO Command +getMappedCommand xs defCmd = do c <- getChar if any (isPrefixOf [c] . fst) xs then rec [c] else if isPrint c - then return $ InsertChar c + then return $ defCmd c else return $ AlertBadInput [c] where rec :: String -> IO Command @@ -417,7 +432,7 @@ verbatimKeymap :: IO Command verbatimKeymap = do c <- getChar --return $ InsertCharThenChangeMode c defaultGetCommand - return $ InsertCharThenChangeMode c (NormalMode nmap) + return $ InsertCharThenChangeMode c NormalMode -- TODO Control.Monad.whenLeft -- cgit v1.2.3