summaryrefslogtreecommitdiff
path: root/vendor/patches/libghostty-vt/0005-bounded-word-selection.patch
blob: 614d962f12667b8cc3ac970341bedb1361dcb369 (plain)
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
diff --git a/vendor/libghostty-vt/include/ghostty/vt/selection.h b/vendor/libghostty-vt/include/ghostty/vt/selection.h
index 0158965e..3e557ef0 100644
--- a/vendor/libghostty-vt/include/ghostty/vt/selection.h
+++ b/vendor/libghostty-vt/include/ghostty/vt/selection.h
@@ -769,6 +769,21 @@ GHOSTTY_API GhosttyResult ghostty_terminal_select_word(
                                     const GhosttyTerminalSelectWordOptions* options,
                                     GhosttySelection* out_selection);
 
+/**
+ * Derive a word selection with a shared forward/backward cell inspection limit.
+ *
+ * Like ghostty_terminal_select_word(), but returns GHOSTTY_NO_VALUE if
+ * max_cells is zero or the search exhausts that budget. No partial selection
+ * is returned. The existing options structure and unbounded API are unchanged.
+ *
+ * @ingroup selection
+ */
+GHOSTTY_API GhosttyResult ghostty_terminal_select_word_bounded(
+                                    GhosttyTerminal terminal,
+                                    const GhosttyTerminalSelectWordOptions* options,
+                                    size_t max_cells,
+                                    GhosttySelection* out_selection);
+
 /**
  * Derive the nearest word selection snapshot between two terminal grid refs.
  *
diff --git a/vendor/libghostty-vt/src/lib_vt.zig b/vendor/libghostty-vt/src/lib_vt.zig
index a0227aef..48536568 100644
--- a/vendor/libghostty-vt/src/lib_vt.zig
+++ b/vendor/libghostty-vt/src/lib_vt.zig
@@ -331,6 +331,7 @@ comptime {
         @export(&c.terminal_continuation_alloc, .{ .name = "ghostty_terminal_continuation_alloc" });
         if (features.selection) {
             @export(&c.terminal_select_word, .{ .name = "ghostty_terminal_select_word" });
+            @export(&c.terminal_select_word_bounded, .{ .name = "ghostty_terminal_select_word_bounded" });
             @export(&c.terminal_select_word_between, .{ .name = "ghostty_terminal_select_word_between" });
             @export(&c.terminal_select_line, .{ .name = "ghostty_terminal_select_line" });
             @export(&c.terminal_select_all, .{ .name = "ghostty_terminal_select_all" });
diff --git a/vendor/libghostty-vt/src/terminal/Screen.zig b/vendor/libghostty-vt/src/terminal/Screen.zig
index 208e276a..d0d2b929 100644
--- a/vendor/libghostty-vt/src/terminal/Screen.zig
+++ b/vendor/libghostty-vt/src/terminal/Screen.zig
@@ -3350,6 +3350,111 @@ pub fn selectWord(
     return .init(start, end, false);
 }
 
+/// Like selectWord, but returns null instead of a partial selection when the
+/// shared forward/backward cell inspection budget is exhausted.
+pub fn selectWordBounded(
+    self: *Screen,
+    pin: Pin,
+    boundary_codepoints: []const u21,
+    max_cells: usize,
+) ?Selection {
+    _ = self;
+    if (max_cells == 0) return null;
+    var remaining = max_cells - 1;
+
+    // If our cell is empty we can't select a word, because we can't select
+    // areas where the screen is not yet written.
+    const start_cell = pin.rowAndCell().cell;
+    if (!start_cell.hasText()) return null;
+
+    // Determine if we are a boundary or not to determine what our boundary is.
+    const expect_boundary = std.mem.indexOfScalar(
+        u21,
+        boundary_codepoints,
+        start_cell.content.codepoint.data,
+    ) != null;
+
+    // Go forwards to find our end boundary
+    const end: Pin = end: {
+        var it = pin.cellIterator(.right_down, null);
+        var prev = it.next().?; // Consume one, our start
+        while (it.next()) |p| {
+            if (remaining == 0) return null;
+            remaining -= 1;
+            const rac = p.rowAndCell();
+            const cell = rac.cell;
+
+            // Wide-cell spacers are part of the same displayed word.
+            if (cell.wide == .spacer_tail or cell.wide == .spacer_head) {
+                prev = p;
+                if (p.x == p.node.cols() - 1 and !rac.row.wrap) break :end p;
+                continue;
+            }
+
+            // If we reached an empty cell its always a boundary
+            if (!cell.hasText()) break :end prev;
+
+            // If we do not match our expected set, we hit a boundary
+            const this_boundary = std.mem.indexOfScalar(
+                u21,
+                boundary_codepoints,
+                cell.content.codepoint.data,
+            ) != null;
+            if (this_boundary != expect_boundary) break :end prev;
+
+            // If we are going to the next row and it isn't wrapped, we
+            // return the previous.
+            if (p.x == p.node.cols() - 1 and !rac.row.wrap) {
+                break :end p;
+            }
+
+            prev = p;
+        }
+
+        break :end prev;
+    };
+
+    // Go backwards to find our start boundary
+    const start: Pin = start: {
+        var it = pin.cellIterator(.left_up, null);
+        var prev = it.next().?; // Consume one, our start
+        while (it.next()) |p| {
+            if (remaining == 0) return null;
+            remaining -= 1;
+            const rac = p.rowAndCell();
+            const cell = rac.cell;
+
+            // If we are going to the next row and it isn't wrapped, we
+            // return the previous.
+            if (p.x == p.node.cols() - 1 and !rac.row.wrap) {
+                break :start prev;
+            }
+
+            if (cell.wide == .spacer_tail or cell.wide == .spacer_head) {
+                prev = p;
+                continue;
+            }
+
+            // If we reached an empty cell its always a boundary
+            if (!cell.hasText()) break :start prev;
+
+            // If we do not match our expected set, we hit a boundary
+            const this_boundary = std.mem.indexOfScalar(
+                u21,
+                boundary_codepoints,
+                cell.content.codepoint.data,
+            ) != null;
+            if (this_boundary != expect_boundary) break :start prev;
+
+            prev = p;
+        }
+
+        break :start prev;
+    };
+
+    return .init(start, end, false);
+}
+
 /// Select the command output under the given point. The limits of the output
 /// are determined by semantic prompt information provided by shell integration.
 /// A selection can span multiple physical lines if they are soft-wrapped.
diff --git a/vendor/libghostty-vt/src/terminal/c/main.zig b/vendor/libghostty-vt/src/terminal/c/main.zig
index 45b4dc47..0ba1224a 100644
--- a/vendor/libghostty-vt/src/terminal/c/main.zig
+++ b/vendor/libghostty-vt/src/terminal/c/main.zig
@@ -196,6 +196,7 @@ pub const terminal_continuation_write = terminal.continuation_write;
 pub const terminal_continuation_buf = terminal.continuation_buf;
 pub const terminal_continuation_alloc = terminal.continuation_alloc;
 pub const terminal_select_word = selection.word;
+pub const terminal_select_word_bounded = selection.word_bounded;
 pub const terminal_select_word_between = selection.word_between;
 pub const terminal_select_line = selection.line;
 pub const terminal_select_all = selection.all;
diff --git a/vendor/libghostty-vt/src/terminal/c/selection.zig b/vendor/libghostty-vt/src/terminal/c/selection.zig
index 2830149f..63873bed 100644
--- a/vendor/libghostty-vt/src/terminal/c/selection.zig
+++ b/vendor/libghostty-vt/src/terminal/c/selection.zig
@@ -110,6 +110,33 @@ pub fn word(
     return .success;
 }
 
+pub fn word_bounded(
+    terminal: terminal_c.Terminal,
+    options: ?*const SelectWordOptions,
+    max_cells: usize,
+    out_selection: ?*CSelection,
+) callconv(lib.calling_conv) Result {
+    const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
+    const opts = options orelse return .invalid_value;
+    if (opts.size < @sizeOf(SelectWordOptions)) return .invalid_value;
+    const out = out_selection orelse return .invalid_value;
+
+    const boundary_codepoints = codepointSlice(
+        opts.boundary_codepoints,
+        opts.boundary_codepoints_len,
+    ) catch return .invalid_value;
+
+    const screen = t.screens.active;
+    const pin = opts.ref.toPin() orelse return .invalid_value;
+    out.* = .fromZig(screen.selectWordBounded(
+        pin,
+        boundary_codepoints orelse &selection_codepoints.default_word_boundaries,
+        max_cells,
+    ) orelse
+        return .no_value);
+    return .success;
+}
+
 pub fn word_between(
     terminal: terminal_c.Terminal,
     options: ?*const SelectWordBetweenOptions,