summaryrefslogtreecommitdiff
path: root/src/KeyMap.zig
blob: 8dca48d26446ce4c3fc5962f4be8094e1b8dbddd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const es = @import("root");
const std = @import("std");

const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Buffer = es.Buffer;
const Editor = es.Editor;
const FormatOptions = std.fmt.FormatOptions;
const HashMap = std.HashMap;
const Key = es.Key;
const KeyMap = @This();
const Wyhash = std.hash.Wyhash;

pub const Error = error{
    MalformedConfig,
    MisformedTerminalResponse,
    StreamTooLong,
} ||
    std.fmt.ParseIntError ||
    std.fs.File.OpenError ||
    std.fs.File.ReadError ||
    std.fs.File.WriteError ||
    std.mem.Allocator.Error ||
    std.os.GetCwdError ||
    std.os.RealPathError;

pub const BoundFn = fn (*Editor, *Buffer, Key) Error!void;
pub const BoundFnPtr = *const BoundFn;

const Context = struct {
    pub fn hash(self: Context, key: []const Key) u64 {
        _ = self;
        var hasher = Wyhash.init(key.len);
        std.hash.autoHashStrat(&hasher, key, .Deep);
        return hasher.final();
    }

    pub fn eql(self: Context, a: []const Key, b: []const Key) bool {
        _ = self;
        return std.mem.eql(Key, a, b);
    }
};

const Keys = struct {
    keys: []const Key,

    pub fn format(
        self: Keys,
        comptime fmt: []const u8,
        options: FormatOptions,
        writer: anytype,
    ) @TypeOf(writer).Error!void {
        var is_first = true;
        for (self.keys) |key| {
            if (is_first) {
                is_first = false;
            } else {
                try std.fmt.formatBuf(" ", options, writer);
            }

            try Key.format(key, fmt, options, writer);
        }
    }
};

const RawMap = HashMap([]const Key, Value, Context, std.hash_map.default_max_load_percentage);

const Value = union(enum) {
    bound_fn: BoundFnPtr,
    chord: void,
};

allocator: Allocator,
current_chord: ArrayList(Key),
default: ?BoundFnPtr,
raw_map: RawMap,

pub fn init(allocator: Allocator) KeyMap {
    return .{
        .allocator = allocator,
        .current_chord = ArrayList(Key).init(allocator),
        .default = null,
        .raw_map = RawMap.init(allocator),
    };
}

pub fn defaultMap(allocator: Allocator) !KeyMap {
    var map = KeyMap.init(allocator);
    errdefer map.deinit();

    map.default = wrapFn(defaultFn);

    // M-g <*>
    try map.bind(&.{ Key.meta('g'), Key.char('g') }, Buffer.goToLine);

    // M-O <*>
    try map.bind(&.{ Key.meta('O'), Key.char('F') }, Buffer.moveEndOfLine);
    try map.bind(&.{ Key.meta('O'), Key.char('H') }, Buffer.moveBeginningOfLine);

    // C-x C-<*>
    try map.bind(&.{ Key.ctrl('x'), Key.ctrl('b') }, Editor.switchBuffer);
    try map.bind(&.{ Key.ctrl('x'), Key.ctrl('c') }, Editor.saveBuffersExit);
    try map.bind(&.{ Key.ctrl('x'), Key.ctrl('f') }, Editor.openFile);
    try map.bind(&.{ Key.ctrl('x'), Key.ctrl('s') }, Buffer.save);

    // C-x <*>
    try map.bind(&.{ Key.ctrl('x'), Key.char('b') }, Editor.switchBuffer);
    // TODO: C-x h for help
    try map.bind(&.{ Key.ctrl('x'), Key.char('k') }, Editor.killCurrentBuffer);

    // M-C-<*>
    try map.bind(&.{Key.meta(Key.ctrl('d'))}, Buffer.backwardDeleteChar);

    // M-<*>
    try map.bind(&.{Key.meta(Key.down)}, Buffer.forwardParagraph);
    try map.bind(&.{Key.meta(Key.left)}, Buffer.backwardWord);
    try map.bind(&.{Key.meta(Key.right)}, Buffer.forwardWord);
    try map.bind(&.{Key.meta(Key.up)}, Buffer.backwardParagraph);
    try map.bind(&.{Key.meta('b')}, Buffer.backwardWord);
    try map.bind(&.{Key.meta('f')}, Buffer.forwardWord);
    // M-g is taken
    try map.bind(&.{Key.meta('n')}, Buffer.forwardParagraph);
    // M-O is taken
    try map.bind(&.{Key.meta('p')}, Buffer.backwardParagraph);
    try map.bind(&.{Key.meta('v')}, Buffer.pageUp);

    // C-<*>
    try map.bind(&.{Key.ctrl(Key.down)}, Buffer.forwardParagraph);
    try map.bind(&.{Key.ctrl(Key.left)}, Buffer.backwardWord);
    try map.bind(&.{Key.ctrl(Key.right)}, Buffer.forwardWord);
    try map.bind(&.{Key.ctrl(Key.up)}, Buffer.backwardParagraph);
    try map.bind(&.{Key.ctrl('a')}, Buffer.moveBeginningOfLine);
    try map.bind(&.{Key.ctrl('b')}, Buffer.backwardChar);
    try map.bind(&.{Key.ctrl('d')}, Buffer.deleteChar);
    try map.bind(&.{Key.ctrl('e')}, Buffer.moveEndOfLine);
    try map.bind(&.{Key.ctrl('f')}, Buffer.forwardChar);
    try map.bind(&.{Key.ctrl('g')}, Editor.clearStatusMessage);
    try map.bind(&.{Key.ctrl('i')}, Buffer.indent); // tab
    try map.bind(&.{Key.ctrl('j')}, Buffer.insertNewline); // line feed
    try map.bind(&.{Key.ctrl('k')}, Buffer.killLine);
    try map.bind(&.{Key.ctrl('l')}, Buffer.recenterTopBottom);
    try map.bind(&.{Key.ctrl('m')}, Buffer.insertNewline); // carriage return
    try map.bind(&.{Key.ctrl('n')}, Buffer.nextLine);
    try map.bind(&.{Key.ctrl('p')}, Buffer.previousLine);
    try map.bind(&.{Key.ctrl('s')}, es.search);
    // TODO: C-q quotedInsert
    try map.bind(&.{Key.ctrl('v')}, Buffer.pageDown);
    // C-x is taken

    // <*>
    try map.bind(&.{Key.backspace}, Buffer.backwardDeleteChar);
    try map.bind(&.{Key.delete}, Buffer.deleteChar);
    try map.bind(&.{Key.down}, Buffer.nextLine);
    try map.bind(&.{Key.end}, Buffer.moveEndOfLine);
    try map.bind(&.{Key.home}, Buffer.moveBeginningOfLine);
    try map.bind(&.{Key.left}, Buffer.backwardChar);
    try map.bind(&.{Key.page_down}, Buffer.pageDown);
    try map.bind(&.{Key.page_up}, Buffer.pageUp);
    try map.bind(&.{Key.right}, Buffer.forwardChar);
    try map.bind(&.{Key.untab}, Buffer.unindent);
    try map.bind(&.{Key.up}, Buffer.previousLine);

    return map;
}

pub fn deinit(self: *KeyMap) void {
    self.current_chord.deinit();

    var it = self.raw_map.keyIterator();
    while (it.next()) |key| {
        self.allocator.free(key.*);
    }
    self.raw_map.deinit();

    self.* = undefined;
}

pub fn bind(self: *KeyMap, keys: []const Key, comptime f: anytype) !void {
    std.debug.assert(keys.len > 0);
    var idx: usize = 0;
    while (idx < keys.len - 1) : (idx += 1) {
        const subseq = try self.allocator.dupe(Key, keys[0 .. idx + 1]);
        errdefer self.allocator.free(subseq);

        const gop = try self.raw_map.getOrPut(subseq);
        if (!gop.found_existing) {
            gop.value_ptr.* = .chord;
        } else {
            defer self.allocator.free(subseq);
            switch (gop.value_ptr.*) {
                .bound_fn => {
                    const longer_keys = Keys{ .keys = keys };
                    const shorter_keys = Keys{ .keys = keys };
                    std.log.err(
                        "Attempting to bind a longer chord ({}) over a shorter one ({})",
                        .{ longer_keys, shorter_keys },
                    );
                    return error.KeyBindError;
                },
                .chord => {},
            }
        }
    }

    const seq = try self.allocator.dupe(Key, keys);
    errdefer self.allocator.free(seq);

    const gop = try self.raw_map.getOrPut(seq);
    if (!gop.found_existing) {
        gop.value_ptr.* = .{ .bound_fn = wrapFn(f) };
    } else {
        defer self.allocator.free(seq);
        // TODO: Maybe I wanna error on rebinding all the time? :thinking:
        switch (gop.value_ptr.*) {
            .bound_fn => gop.value_ptr.* = .{ .bound_fn = wrapFn(f) },
            .chord => {
                const keys_wrap = Keys{ .keys = keys };
                std.log.err(
                    "Attempting to bind a shorter chord ({}) over longer one(s)",
                    .{keys_wrap},
                );
                return error.KeyBindError;
            },
        }
    }
}

pub fn keypress(self: *KeyMap, editor: *Editor, buf: *Buffer, key: Key) !void {
    try self.current_chord.append(key);
    const keys = Keys{ .keys = self.current_chord.items };
    if (self.raw_map.get(self.current_chord.items)) |value| {
        switch (value) {
            .bound_fn => |f| {
                self.current_chord.clearRetainingCapacity();
                editor.clearStatusMessage();
                try f(editor, buf, key);
            },
            .chord => try editor.setStatusMessage("{}-", .{keys}),
        }
    } else if (self.current_chord.items.len > 1) {
        std.log.debug("Unknown chord: {}", .{keys});
        try editor.setStatusMessage("Unknown chord: {}", .{keys});
        self.current_chord.clearRetainingCapacity();
    } else if (self.default) |default| {
        try default(editor, buf, key);
        self.current_chord.clearRetainingCapacity();
    } else {
        std.log.debug("Unknown key: {}", .{key});
        try editor.setStatusMessage("Unknown key: {}", .{key});
        self.current_chord.clearRetainingCapacity();
    }
}

fn defaultFn(editor: *Editor, buffer: *Buffer, key: Key) !void {
    if (@intFromEnum(key) <= @intFromEnum(Key.max_char)) {
        const char: u8 = @intCast(@intFromEnum(key));
        if (std.ascii.isPrint(char)) {
            try buffer.insertChar(char);
            return;
        }
    }

    std.log.debug("Unknown key: {}", .{key});
    try editor.setStatusMessage("Unknown key: {}", .{key});
}

// I think there's a bug in Zig's std.mem.eql
fn typeEql(comptime a: []const type, comptime b: []const type) bool {
    if (a.len != b.len) return false;
    if (a.len == 0 or a.ptr == b.ptr) return true;

    for (a, b) |a_elem, b_elem| {
        if (a_elem != b_elem) return false;
    }
    return true;
}

fn wrapFn(comptime f: anytype) BoundFn {
    comptime {
        if (@TypeOf(f) == BoundFn) {
            return f;
        }

        const fn_info = @typeInfo(@TypeOf(f)).Fn;
        const should_try = if (fn_info.return_type) |return_type| @typeInfo(return_type) == .ErrorUnion else false;

        const params_info = fn_info.params;
        var params = [_]type{undefined} ** params_info.len;
        for (params_info, 0..) |param_info, idx| {
            params[idx] = param_info.type.?;
        }

        if (typeEql(&params, &.{*Buffer})) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    _ = e;
                    _ = k;
                    if (should_try) {
                        _ = try f(b);
                    } else {
                        _ = f(b);
                    }
                }
            }.wf;
        } else if (typeEql(&params, &.{*Editor})) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    _ = b;
                    _ = k;
                    if (should_try) {
                        _ = try f(e);
                    } else {
                        _ = f(e);
                    }
                }
            }.wf;
        } else if (typeEql(&params, &.{ *Editor, *Buffer })) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    _ = k;
                    if (should_try) {
                        _ = try f(e, b);
                    } else {
                        _ = f(e, b);
                    }
                }
            }.wf;
        } else if (typeEql(&params, &.{ *Buffer, *Editor })) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    _ = k;
                    if (should_try) {
                        _ = try f(b, e);
                    } else {
                        _ = f(b, e);
                    }
                }
            }.wf;
        } else if (typeEql(&params, &.{ *Buffer, Editor })) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    _ = k;
                    if (should_try) {
                        _ = try f(b, e.*);
                    } else {
                        _ = f(b, e.*);
                    }
                }
            }.wf;
        } else if (typeEql(&params, &.{ *Editor, *Buffer, Key })) {
            return struct {
                pub fn wf(e: *Editor, b: *Buffer, k: Key) Error!void {
                    if (should_try) {
                        _ = try f(e, b, k);
                    } else {
                        _ = f(e, b, k);
                    }
                }
            }.wf;
        }

        @compileError("How to wrap " ++ @typeName(@TypeOf(f)));
    }
}