diff options
Diffstat (limited to 'src/key_state.zig')
| -rw-r--r-- | src/key_state.zig | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/src/key_state.zig b/src/key_state.zig deleted file mode 100644 index 818dded..0000000 --- a/src/key_state.zig +++ /dev/null | |||
| @@ -1,140 +0,0 @@ | |||
| 1 | const es = @import("root"); | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | const Buffer = es.Buffer; | ||
| 5 | const Editor = es.Editor; | ||
| 6 | const Key = es.Key; | ||
| 7 | |||
| 8 | pub const Error = error{ | ||
| 9 | MalformedConfig, | ||
| 10 | MisformedTerminalResponse, | ||
| 11 | StreamTooLong, | ||
| 12 | } || | ||
| 13 | std.mem.Allocator.Error || | ||
| 14 | std.fmt.ParseIntError || | ||
| 15 | std.fs.File.OpenError || | ||
| 16 | std.fs.File.ReadError || | ||
| 17 | std.fs.File.WriteError || | ||
| 18 | std.os.GetCwdError || | ||
| 19 | std.os.RealPathError; | ||
| 20 | pub const KeyState = fn (*Editor, *Buffer, Key) Error!void; | ||
| 21 | |||
| 22 | fn mgState(editor: *Editor, buf: *Buffer, key: Key) Error!void { | ||
| 23 | editor.current_state = defaultState; | ||
| 24 | editor.clearStatusMessage(); | ||
| 25 | |||
| 26 | switch (key) { | ||
| 27 | // ========== <*> ========== | ||
| 28 | Key.char('g') => try buf.goToLine(editor), | ||
| 29 | |||
| 30 | else => { | ||
| 31 | std.log.debug("Unknown chord: M-g {}", .{key}); | ||
| 32 | try editor.setStatusMessage("Unknown chord: M-g {}", .{key}); | ||
| 33 | }, | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | fn mOState(editor: *Editor, buf: *Buffer, key: Key) Error!void { | ||
| 38 | editor.current_state = defaultState; | ||
| 39 | editor.clearStatusMessage(); | ||
| 40 | |||
| 41 | switch (key) { | ||
| 42 | // ========== <*> ========== | ||
| 43 | Key.char('F') => buf.moveEndOfLine(), | ||
| 44 | Key.char('H') => buf.moveBeginningOfLine(), | ||
| 45 | |||
| 46 | else => { | ||
| 47 | std.log.debug("Unknown chord: M-O {}", .{key}); | ||
| 48 | try editor.setStatusMessage("Unknown chord: M-O {}", .{key}); | ||
| 49 | }, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | fn cxState(editor: *Editor, buf: *Buffer, key: Key) Error!void { | ||
| 54 | editor.current_state = defaultState; | ||
| 55 | editor.clearStatusMessage(); | ||
| 56 | |||
| 57 | switch (key) { | ||
| 58 | // ========== C-<*> ========== | ||
| 59 | Key.ctrl('b'), Key.char('b') => try editor.switchBuffer(), | ||
| 60 | Key.ctrl('c') => try editor.saveBuffersExit(), | ||
| 61 | Key.ctrl('f') => try editor.openFile(), | ||
| 62 | Key.ctrl('g') => {}, | ||
| 63 | Key.ctrl('s') => try buf.save(editor), | ||
| 64 | |||
| 65 | // ========== <*> ========== | ||
| 66 | Key.char('k') => _ = try editor.killCurrentBuffer(), | ||
| 67 | |||
| 68 | else => { | ||
| 69 | std.log.debug("Unknown chord: C-x {}", .{key}); | ||
| 70 | try editor.setStatusMessage("Unknown chord: C-x {}", .{key}); | ||
| 71 | }, | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | pub fn defaultState(editor: *Editor, buf: *Buffer, key: Key) Error!void { | ||
| 76 | switch (key) { | ||
| 77 | // ========== M-C-<*> ========== | ||
| 78 | Key.meta(Key.ctrl('d')), Key.backspace => try buf.backwardDeleteChar(), | ||
| 79 | |||
| 80 | // ========== M-<*> ========== | ||
| 81 | Key.meta('g') => { | ||
| 82 | editor.current_state = mgState; | ||
| 83 | try editor.setStatusMessage("M-g-", .{}); | ||
| 84 | }, | ||
| 85 | Key.meta('O') => editor.current_state = mOState, | ||
| 86 | Key.meta('v'), Key.page_up => buf.pageUp(editor.screenrows), | ||
| 87 | |||
| 88 | // ========== C-<*> ========== | ||
| 89 | Key.ctrl('a'), Key.home => buf.moveBeginningOfLine(), | ||
| 90 | Key.ctrl('b'), Key.left => buf.backwardChar(), | ||
| 91 | Key.ctrl('d'), Key.delete => try buf.deleteChar(), | ||
| 92 | Key.ctrl('e'), Key.end => buf.moveEndOfLine(), | ||
| 93 | Key.ctrl('f'), Key.right => buf.forwardChar(), | ||
| 94 | Key.ctrl('g') => editor.clearStatusMessage(), | ||
| 95 | |||
| 96 | // TODO: C-h help | ||
| 97 | |||
| 98 | // tab | ||
| 99 | Key.ctrl('i') => try buf.indent(), | ||
| 100 | // line feed | ||
| 101 | Key.ctrl('j') => try buf.insertNewline(), | ||
| 102 | Key.ctrl('k') => try buf.killLine(), | ||
| 103 | |||
| 104 | Key.ctrl('l') => { | ||
| 105 | try editor.refreshWindowSize(); | ||
| 106 | buf.recenterTopBottom(editor.screenrows); | ||
| 107 | }, | ||
| 108 | |||
| 109 | // carriage return | ||
| 110 | Key.ctrl('m') => try buf.insertNewline(), | ||
| 111 | Key.ctrl('n'), Key.down => buf.nextLine(), | ||
| 112 | Key.ctrl('p'), Key.up => buf.previousLine(), | ||
| 113 | Key.ctrl('s') => try es.search(editor, buf), | ||
| 114 | |||
| 115 | // TODO: C-q quotedInsert | ||
| 116 | |||
| 117 | Key.ctrl('v'), Key.page_down => buf.pageDown(editor.screenrows), | ||
| 118 | |||
| 119 | Key.ctrl('x') => { | ||
| 120 | editor.current_state = cxState; | ||
| 121 | try editor.setStatusMessage("C-x-", .{}); | ||
| 122 | }, | ||
| 123 | |||
| 124 | // ========== <*> ========== | ||
| 125 | Key.untab => try buf.unindent(), | ||
| 126 | |||
| 127 | else => { | ||
| 128 | if (@enumToInt(key) <= @enumToInt(Key.max_char)) { | ||
| 129 | const char = @intCast(u8, @enumToInt(key)); | ||
| 130 | if (std.ascii.isGraph(char) or std.ascii.isSpace(char)) { | ||
| 131 | try buf.insertChar(char); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | std.log.debug("Unknown key: {}", .{key}); | ||
| 137 | try editor.setStatusMessage("Unknown key: {}", .{key}); | ||
| 138 | }, | ||
| 139 | } | ||
| 140 | } | ||