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
|
//! docs/typ/guide.typ, The mouse <mouse>.
const std = @import("std");
const Session = @import("session").Session;
/// The mouse as `/mousein` lines say it, `m col row buttons` each.
fn mouse(s: *Session, comptime lines: []const u8, args: anytype) !void {
try s.write("/mousein", s.at(lines, args));
}
test "mouse: a double-click selects a word, a line, a bracketed or quoted text, as acme's" {
var s = try Session.initFile(std.testing.allocator, "call foo_bar(a, b)\nsay \"hi there\"\n");
defer s.deinit();
const n = try s.focused();
const sel = s.at("/pane/{d}/sel", .{n});
for ([_]struct { at: []const u8, into: usize, want: []const u8 }{
.{ .at = "foo_bar", .into = 2, .want = "foo_bar" },
.{ .at = "(a, b)", .into = 1, .want = "a, b" },
.{ .at = "call", .into = 0, .want = "call foo_bar(a, b)\n" },
.{ .at = "\"hi", .into = 1, .want = "hi there" },
}) |w| {
try s.click(w.at, w.into, 1);
try s.click(w.at, w.into, 1);
try s.expect(sel, w.want);
// A third click is a plain click again: the caret, no selection.
try s.click(w.at, w.into, 1);
try s.expect(sel, "");
}
// In the tag too: what it selected is what a yank takes.
try s.click("Collapse Del", "Collapse D".len, 1);
try s.click("Collapse Del", "Collapse D".len, 1);
try s.keys("\x1by:ggP");
try s.expect(s.at("/pane/{d}/body", .{n}), "Delcall foo_bar(a, b)\nsay \"hi there\"\n");
}
test "mouse: everything a B1 chord does while B1 is down is one undo step" {
var s = try Session.initFile(std.testing.allocator, "one two three\n");
defer s.deinit();
const n = try s.focused();
const body = s.at("/pane/{d}/body", .{n});
const x, const y = try s.where("one two");
// B1-B2 then B1-B3: cut, then paste back: a copy, one step.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 5\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y });
try s.expect(body, "one two three\n");
// Undone in one step, it is the text before the cut, not after it.
try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
try s.expect(body, "one two three\n");
// A cut alone comes back with one Undo.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y });
try s.expect(body, "one three\n");
try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
try s.expect(body, "one two three\n");
}
test "mouse: a click past a line's or the file's end puts the caret where insertion types" {
for ([_]bool{ false, true }) |already_insert| {
for ([_]bool{ false, true }) |bottom_tag| {
for ([_]struct { text: []const u8, click_row: usize, dot: u32, col: usize, row: usize, result: []const u8 }{
.{ .text = "abc\nxy", .click_row = 0, .dot = 3, .col = 3, .row = 0, .result = "abcZ\nxy" },
.{ .text = "abc\nxy", .click_row = 5, .dot = 6, .col = 2, .row = 1, .result = "abc\nxyZ" },
.{ .text = "abc\n", .click_row = 5, .dot = 4, .col = 0, .row = 1, .result = "abc\nZ" },
.{ .text = "", .click_row = 5, .dot = 0, .col = 0, .row = 0, .result = "Z" },
}) |case| {
var s = try Session.initFile(std.testing.allocator, case.text);
defer s.deinit();
const n = try s.focused();
if (bottom_tag) try s.write("/ctl", "Tagbottom on\n");
if (already_insert) try s.keys("i");
// The first line's number, then a blank, then its text.
const x, const y = try s.where("1 ");
try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2 + 15, y + case.click_row, x + 2 + 15, y + case.click_row });
try std.testing.expectEqual(.{ case.dot, case.dot }, try s.range(s.at("/pane/{d}/dot", .{n})));
// The caret is drawn where it is, and insertion draws it there too.
try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?);
if (!already_insert) try s.keys("i");
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:insert "));
try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?);
try s.keys("Z");
try s.expect(s.at("/pane/{d}/body", .{n}), case.result);
}
}
}
}
test "mouse: a click on a tab's or a wrapped wide glyph's cells puts the caret on it" {
for ([_]bool{ false, true }) |already_insert| {
for ([_]bool{ false, true }) |wrap| {
const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n";
var s = try Session.initFile(std.testing.allocator, source);
defer s.deinit();
s.detach();
try s.write("/ctl", "size 24 12\n");
const n = try s.focused();
if (!wrap) try s.write("/ctl", "Wrap off\n");
if (already_insert) try s.keys("i");
const x, const y = try s.where("界");
if (wrap) try std.testing.expect(y > (try s.where("1 "))[1]);
// The glyph's second cell.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y });
const at: u32 = @intCast(try std.unicode.utf8CountCodepoints(source[0..std.mem.indexOf(u8, source, "界").?]));
try std.testing.expectEqual(.{ at, at }, try s.range(s.at("/pane/{d}/dot", .{n})));
// Drawn on the glyph's first cell, in insert mode too.
try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?);
if (!already_insert) try s.keys("i");
try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?);
try s.keys("Z");
const byte = std.mem.indexOf(u8, source, "界").?;
try s.expect(s.at("/pane/{d}/body", .{n}), s.at("{s}Z{s}", .{ source[0..byte], source[byte..] }));
}
}
}
test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and a tag" {
var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\n");
defer s.deinit();
const n = try s.focused();
const body = s.at("/pane/{d}/body", .{n});
const x, const y = try s.where("alpha");
// From the press to the end of its line, the line between whole, the
// last from its start: no line numbers, no rectangle. B2 cuts it.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 2, y, x + 1, y + 2, x + 1, y + 2, x + 1, y + 2 });
try s.expect(body, "alarlie\n");
try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
// Upward, the same text.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 1, y + 2, x + 2, y, x + 2, y, x + 2, y });
try s.expect(body, "alarlie\n");
// A tag of several rows sweeps the same way.
const tag = s.at("/pane/{d}/tag", .{n});
try s.write(tag, "\nLook Edit");
const tx, const ty = try s.where("Collapse");
const lx, const ly = try s.where("Look Edit");
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ tx + 3, ty, lx + 1, ly, lx + 1, ly, lx + 1, ly });
try s.expect(tag, "/test.txt Save Tty Colok Edit");
}
test "mouse: a click on either half of a wide character lands before it, in the text and on a tag's path, and the caret steps over it whole" {
var s = try Session.initFile(std.testing.allocator, "a界e\u{301}z\n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
const x, const y = try s.where("a界");
// 界 is two cells, bytes 1 to 4; é two runes, bytes 4 to 7.
for ([_]usize{ 1, 2 }) |into| {
try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + into, y, x + into, y });
try std.testing.expectEqual(.{ @as(u32, 1), @as(u32, 1) }, try s.range(dot));
}
try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 4, y });
try std.testing.expectEqual(.{ @as(u32, 7), @as(u32, 7) }, try s.range(dot));
// On the tag's path, the second half of 界 drafts the name with its
// caret before it; Right steps a whole grapheme, two cells then one.
try s.write(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n");
const px, const py = try s.where("/界");
try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ px + 2, py, px + 2, py });
try std.testing.expectEqual(.{ px + 1, py }, (try s.cursor()).?);
try s.keys("\u{F012}");
try std.testing.expectEqual(.{ px + 3, py }, (try s.cursor()).?);
try s.keys("\u{F012}");
try std.testing.expectEqual(.{ px + 4, py }, (try s.cursor()).?);
try s.keys("\x1b\x1b");
try s.expect(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n");
// Typed past the tag's words, a two-byte λ goes in and one backspace
// takes it out whole.
const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
try s.click("Collapse Del", "Collapse Del".len + 1, 1);
const caret = (try s.cursor()).?;
// A clicked tag is in normal mode: `i` types at the caret.
try s.keys("iλ");
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "λ"));
try std.testing.expectEqual(.{ caret[0] + 1, caret[1] }, (try s.cursor()).?);
try s.keys("\x08");
try std.testing.expectEqual(caret, (try s.cursor()).?);
try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
}
test "mouse: a sweep pressed on the line numbers takes only the text: drawn while held and once let go, looked for by a click in it, cut by a chord" {
var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nalpha beta\ngamma\n");
defer s.deinit();
const n = try s.focused();
const x, const y = try s.where("alpha");
const plain = try s.bg(x - 2, y);
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\n", .{ x - 2, y, x + 4, y + 1 });
for (0..2) |released| {
// The number pressed on, and the room after the line, stay unlit.
try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y));
try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y + 1));
const lit = try s.bg(x, y);
try std.testing.expect(!std.mem.eql(u8, plain, lit));
try std.testing.expectEqualStrings(lit, try s.bg(x + 4, y + 1));
try std.testing.expectEqualStrings(plain, try s.bg(x + 5, y + 1));
if (released == 0) try mouse(&s, "m {d} {d} 0\n", .{ x + 4, y + 1 });
}
// A Look clicked in it looks for the text swept: its next place, the
// third and fourth lines.
try mouse(&s, "m {d} {d} 4\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y });
try std.testing.expectEqual(.{ @as(u32, 23), @as(u32, 39) }, try s.range(s.at("/pane/{d}/dot", .{n})));
// A chord cuts what was swept: the text, no number.
try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x - 2, y, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1 });
try s.expect(s.at("/pane/{d}/body", .{n}), " delta\nalpha beta\ngamma\n");
}
|