Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 70 lines (60 sloc) 2.294 kB
13104d3 @michaelfeathers first commit
authored
1
2 import EditBuffer
3 import Rendering
4 import Input
5 import Char
6
7 data EditMode = Command | Insert deriving (Eq,Show)
8
9 vih :: IO ()
10 vih =
11 do initInput
12 mainLoop Command emptyBuffer
13
14 mainLoop :: EditMode -> EditBuffer -> IO ()
15 mainLoop mode buffer@(EditBuffer location contents) =
16 do cls
17 render $ getBufferContents buffer
18 -- writeAt commandHome (showRepresentation buffer)
19 goto location
20 ch <- getInputChar
21 if mode == Insert
22 then
23 case ch of
24 '\ESC' -> mainLoop Command (enterCommandMode buffer)
c3676ba @michaelfeathers Added `replace char' support with 'r'
authored
25 _ -> if isInputChar ch
26 then mainLoop mode (insertChar ch buffer)
27 else mainLoop mode buffer
13104d3 @michaelfeathers first commit
authored
28 else
29 case ch of
30 ':' -> handleCommandLine buffer
31 'i' -> mainLoop Insert buffer
32 'h' -> mainLoop mode (moveLeft buffer)
33 'j' -> mainLoop mode (moveDown buffer)
34 'k' -> mainLoop mode (moveUp buffer)
35 'l' -> mainLoop mode (moveRight buffer)
36 'd' -> do nextCh <- getInputChar
37 if nextCh == 'd'
38 then mainLoop mode (deleteLine buffer)
39 else mainLoop mode buffer
40 'g' -> do nextCh <- getInputChar
41 if nextCh == 'g'
42 then mainLoop mode (moveToHome buffer)
43 else mainLoop mode buffer
44 'G' -> mainLoop mode (moveToEnd buffer)
45 '0' -> mainLoop mode (moveToLineStart buffer)
46 '$' -> mainLoop mode (moveToLineEnd buffer)
47 'o' -> mainLoop Insert (insertLineAfter buffer)
c3676ba @michaelfeathers Added `replace char' support with 'r'
authored
48 'r' -> do nextCh <- getInputChar
49 if isInputChar nextCh
50 then mainLoop mode (replaceChar nextCh buffer)
51 else mainLoop mode buffer
d31b8ba @michaelfeathers Added support for `word forward' with 'w'. Works only on alnums curre…
authored
52 'w' -> mainLoop mode (wordForward buffer)
13104d3 @michaelfeathers first commit
authored
53 'x' -> mainLoop mode (deleteChar buffer)
54 _ -> mainLoop mode buffer
55
56
57 handleCommandLine :: EditBuffer -> IO ()
58 handleCommandLine buffer =
59 do goto commandHome
60 putStr ":"
61 command <- getLine
62 if head command == 'q'
63 then return ()
64 else mainLoop Command buffer
c3676ba @michaelfeathers Added `replace char' support with 'r'
authored
65
66
67 isInputChar :: Char -> Bool
68 isInputChar ch = (not (isControl ch)) || (ch == '\n')
69
Something went wrong with that request. Please try again.