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
//! Public, byte-offset text motion helpers shared with the vim-mode `TextArea` widget.
//!
//! Every function takes a `&str` and a byte offset and returns a new byte offset; offsets are
//! always clamped to a UTF-8 char boundary. These are the exact algorithms `TextArea`'s vim mode
//! uses internally for `w`/`b`/`e`/`0`/`^`/`$`, promoted to a stable public surface so apps that
//! render their own text grids (for example a terminal emulator's scrollback copy mode) can reuse
//! vim-style word/line motions instead of reimplementing them.
//!
//! # Cursor convention
//!
//! Offsets follow the same "insertion point" convention as [`crate::TextEditor`]: a cursor value
//! `N` sits *between* the bytes at `N - 1` and `N`, not "on" the character at `N`. This matters
//! most for [`word_end`] and [`big_word_end`], which land the cursor one byte **past** the last
//! character of the word (so the returned offset can equal `text.len()` and is not always a valid
//! index to read a character *from*).
//!
//! If your own cursor model instead tracks a selected *cell* (as in a terminal grid, where the
//! cursor always occupies a visible character), convert to an insertion point by adding the byte
//! width of the character under the cursor before calling [`word_end`] / [`big_word_end`], then
//! map the result back down to the cell at `offset - 1`. Feeding a cell's own start byte directly
//! into [`word_end`] breaks the case where the cursor already sits on a word's last character:
//! since that byte still belongs to the current word, the motion re-finds the same word's end
//! instead of advancing to the next word.
//!
//! The cell-cursor adapters below take **character columns**, because they operate on text rows.
//! Convert those columns to rendered display columns with the span helpers in
//! [`crate::utils::spans`] before using them against a styled grid.
//!
//! ```
//! use tui_lipan::text_motion::word_end;
//!
//! let text = "cat dog";
//! // Insertion point 3 sits right after 'cat' (between the 't' and the space).
//! assert_eq!(word_end(text, 3), 7); // -> end of "dog"
//!
//! // Feeding the *cell* start byte of 't' (2) instead re-finds the same word's end (3),
//! // which looks like "no progress" to a cell-based caller expecting to land on 'g' (6).
//! assert_eq!(word_end(text, 2), 3);
//! ```
/// Move forward to the start of the next vim "word" (`w`): skip the remainder of the current
/// word/punctuation run, then any following whitespace.
///
/// ```
/// use tui_lipan::text_motion::word_forward_start;
///
/// assert_eq!(word_forward_start("cat dog", 0), 4);
/// ```
pub use cratevim_word_forward_start as word_forward_start;
/// Move backward to the start of the previous vim "word" (`b`).
///
/// ```
/// use tui_lipan::text_motion::word_backward_start;
///
/// assert_eq!(word_backward_start("cat dog", 7), 4);
/// ```
pub use cratevim_word_backward_start as word_backward_start;
/// Move to the end of the current or next vim "word" (`e`).
///
/// Returns an insertion-point offset one byte past the word's last character — see the
/// "Cursor convention" section of the [`crate::text_motion`] module docs before feeding in a
/// cell-based cursor.
///
/// ```
/// use tui_lipan::text_motion::word_end;
///
/// assert_eq!(word_end("cat dog", 0), 3);
/// ```
pub use cratevim_word_end as word_end;
/// Move forward to the start of the next vim WORD (`W`): a whitespace-delimited run that
/// includes punctuation, unlike a "word".
///
/// ```
/// use tui_lipan::text_motion::big_word_forward_start;
///
/// assert_eq!(big_word_forward_start("foo.bar baz", 0), 8);
/// ```
pub use cratevim_big_word_forward_start as big_word_forward_start;
/// Move backward to the start of the previous vim WORD (`B`).
///
/// ```
/// use tui_lipan::text_motion::big_word_backward_start;
///
/// assert_eq!(big_word_backward_start("foo.bar baz", 11), 8);
/// ```
pub use cratevim_big_word_backward_start as big_word_backward_start;
/// Move to the end of the current or next vim WORD (`E`).
///
/// Returns an insertion-point offset one byte past the WORD's last character — see the
/// "Cursor convention" section of the [`crate::text_motion`] module docs.
///
/// ```
/// use tui_lipan::text_motion::big_word_end;
///
/// assert_eq!(big_word_end("foo.bar baz", 0), 7);
/// ```
pub use cratevim_big_word_end as big_word_end;
/// Byte offset of the start of the line containing `cursor` (`0` motion target).
///
/// ```
/// use tui_lipan::text_motion::line_start_at;
///
/// assert_eq!(line_start_at("foo\nbar", 5), 4);
/// ```
pub use crateline_start_at;
/// Byte offset one past the end of the line containing `cursor` (`$` motion target, exclusive).
///
/// ```
/// use tui_lipan::text_motion::line_end_at;
///
/// assert_eq!(line_end_at("foo\nbar", 5), 7);
/// ```
pub use crateline_end_at;
/// Byte offset of the first non-blank character in `text[line_start..line_end]` (`^` motion
/// target), or `line_end` if the line is entirely blank.
///
/// ```
/// use tui_lipan::text_motion::first_nonblank_in_line;
///
/// assert_eq!(first_nonblank_in_line(" bar", 0, 5), 2);
/// ```
pub use cratefirst_nonblank_in_line;
// ─────────────────────────────────────────────────────────────────────────────
// Cell-cursor adapters
// ─────────────────────────────────────────────────────────────────────────────
/// Convert a character-column cursor into a UTF-8 byte offset.
///
/// These adapters use **character columns**, not rendered display columns. They are for text
/// rows (`&str`); convert the result with a span helper before using it to index a rendered grid.
/// Columns past the end of the row map to `row.len()`.
///
/// ```
/// use tui_lipan::text_motion::char_col_to_byte;
///
/// assert_eq!(char_col_to_byte("hé", 1), 1);
/// assert_eq!(char_col_to_byte("hé", 2), 3);
/// ```
/// Convert a UTF-8 byte offset into a character-column cursor.
///
/// The offset is clamped to the preceding UTF-8 character boundary, so callers that receive an
/// arbitrary byte offset do not panic while converting it.
///
/// ```
/// use tui_lipan::text_motion::byte_to_char_col;
///
/// assert_eq!(byte_to_char_col("hél", 3), 2);
/// ```
/// Apply a byte-offset motion to a character-column cursor.
/// Apply a word-end byte motion to a character-column cursor.
///
/// Word-end motions use an insertion point just after the current character, then map the result
/// back to the character before it. Without this `e`/`E` would re-find the current word's end
/// when the cursor is already on its last character.
/// Move a character-column cursor to the next vim word start (`w`).
///
/// This operates on character columns, not display columns; it is intended for `&str` rows.
///
/// ```
/// use tui_lipan::text_motion::cell_word_forward_start;
/// assert_eq!(cell_word_forward_start("héllo world", 0), 6);
/// ```
/// Move a character-column cursor to the previous vim word start (`b`).
///
/// This operates on character columns, not display columns; it is intended for `&str` rows.
///
/// ```
/// use tui_lipan::text_motion::cell_word_backward_start;
/// assert_eq!(cell_word_backward_start("héllo world", 11), 6);
/// ```
/// Move a character-column cursor to the current or next vim word end (`e`).
///
/// This operates on character columns, not display columns. The insertion-point conversion keeps
/// repeated `e` presses moving forward when the cursor is already on a word's last character.
///
/// ```
/// use tui_lipan::text_motion::cell_word_end;
/// assert_eq!(cell_word_end("héllo world", 0), 4);
/// ```
/// Move a character-column cursor to the next vim WORD start (`W`).
///
/// This operates on character columns, not display columns; it is intended for `&str` rows.
///
/// ```
/// use tui_lipan::text_motion::cell_big_word_forward_start;
/// assert_eq!(cell_big_word_forward_start("foo.bar baz", 0), 8);
/// ```
/// Move a character-column cursor to the previous vim WORD start (`B`).
///
/// This operates on character columns, not display columns; it is intended for `&str` rows.
///
/// ```
/// use tui_lipan::text_motion::cell_big_word_backward_start;
/// assert_eq!(cell_big_word_backward_start("foo.bar baz", 11), 8);
/// ```
/// Move a character-column cursor to the current or next vim WORD end (`E`).
///
/// This operates on character columns, not display columns. The insertion-point conversion keeps
/// repeated `E` presses moving forward when the cursor is already on a WORD's last character.
///
/// ```
/// use tui_lipan::text_motion::cell_big_word_end;
/// assert_eq!(cell_big_word_end("foo.bar baz", 0), 6);
/// ```
/// Move a character-column cursor to the first non-blank character (`^`).
///
/// This operates on character columns, not display columns; it is intended for `&str` rows.
/// An all-blank row returns its character length.
///
/// ```
/// use tui_lipan::text_motion::cell_line_first_nonblank;
/// assert_eq!(cell_line_first_nonblank(" hé"), 2);
/// ```
/// Move a character-column cursor to the last character on the row (`$`).
///
/// This operates on character columns, not display columns; it returns `0` for an empty row.
///
/// ```
/// use tui_lipan::text_motion::cell_line_last;
/// assert_eq!(cell_line_last("hé"), 1);
/// ```