summaryrefslogtreecommitdiffstats
path: root/Main.hs
diff options
context:
space:
mode:
authortv <tv@shackspace.de>2014-07-27 19:30:36 +0200
committertv <tv@shackspace.de>2014-07-27 19:30:36 +0200
commitbeac392d39f7a94e8a1f0be366e4c2bf58852e8d (patch)
treec83278da40675d46c613218da67d0fc2f38203b5 /Main.hs
parent6408e2fabcd251861124045f37a040cc7d4ac465 (diff)
modal editing begins
Diffstat (limited to 'Main.hs')
-rw-r--r--Main.hs37
1 files changed, 26 insertions, 11 deletions
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