Fix word navigation on lines with tabs, from Alexander Arch.

This commit is contained in:
nicm 2024-11-20 20:54:02 +00:00
parent a3ede3106a
commit 273f9b2027
4 changed files with 34 additions and 16 deletions

View File

@ -180,19 +180,14 @@ grid_reader_handle_wrap(struct grid_reader *gr, u_int *xx, u_int *yy)
int int
grid_reader_in_set(struct grid_reader *gr, const char *set) grid_reader_in_set(struct grid_reader *gr, const char *set)
{ {
struct grid_cell gc; return (grid_in_set(gr->gd, gr->cx, gr->cy, set));
grid_get_cell(gr->gd, gr->cx, gr->cy, &gc);
if (gc.flags & GRID_FLAG_PADDING)
return (0);
return (utf8_cstrhas(set, &gc.data));
} }
/* Move cursor to the start of the next word. */ /* Move cursor to the start of the next word. */
void void
grid_reader_cursor_next_word(struct grid_reader *gr, const char *separators) grid_reader_cursor_next_word(struct grid_reader *gr, const char *separators)
{ {
u_int xx, yy; u_int xx, yy, width;
/* Do not break up wrapped words. */ /* Do not break up wrapped words. */
if (grid_get_line(gr->gd, gr->cy)->flags & GRID_LINE_WRAPPED) if (grid_get_line(gr->gd, gr->cy)->flags & GRID_LINE_WRAPPED)
@ -229,8 +224,8 @@ grid_reader_cursor_next_word(struct grid_reader *gr, const char *separators)
} }
} }
while (grid_reader_handle_wrap(gr, &xx, &yy) && while (grid_reader_handle_wrap(gr, &xx, &yy) &&
grid_reader_in_set(gr, WHITESPACE)) (width = grid_reader_in_set(gr, WHITESPACE)))
gr->cx++; gr->cx += width;
} }
/* Move cursor to the end of the next word. */ /* Move cursor to the end of the next word. */
@ -425,7 +420,9 @@ grid_reader_cursor_back_to_indentation(struct grid_reader *gr)
xx = grid_line_length(gr->gd, py); xx = grid_line_length(gr->gd, py);
for (px = 0; px < xx; px++) { for (px = 0; px < xx; px++) {
grid_get_cell(gr->gd, px, py, &gc); grid_get_cell(gr->gd, px, py, &gc);
if (gc.data.size != 1 || *gc.data.data != ' ') { if ((gc.data.size != 1 || *gc.data.data != ' ') &&
~gc.flags & GRID_FLAG_TAB &&
~gc.flags & GRID_FLAG_PADDING) {
gr->cx = px; gr->cx = px;
gr->cy = py; gr->cy = py;
return; return;

24
grid.c
View File

@ -1561,3 +1561,27 @@ grid_line_length(struct grid *gd, u_int py)
} }
return (px); return (px);
} }
/* Check if character is in set. */
int
grid_in_set(struct grid *gd, u_int px, u_int py, const char *set)
{
struct grid_cell gc, tmp_gc;
u_int pxx;
grid_get_cell(gd, px, py, &gc);
if (strchr(set, '\t')) {
if (gc.flags & GRID_FLAG_PADDING) {
pxx = px;
do
grid_get_cell(gd, --pxx, py, &tmp_gc);
while (pxx > 0 && tmp_gc.flags & GRID_FLAG_PADDING);
if (tmp_gc.flags & GRID_FLAG_TAB)
return (tmp_gc.data.width - (px - pxx));
} else if (gc.flags & GRID_FLAG_TAB)
return (gc.data.width);
}
if (gc.flags & GRID_FLAG_PADDING)
return (0);
return (utf8_cstrhas(set, &gc.data));
}

3
tmux.h
View File

@ -605,7 +605,7 @@ enum tty_code_code {
}; };
/* Character classes. */ /* Character classes. */
#define WHITESPACE " " #define WHITESPACE "\t "
/* Mode keys. */ /* Mode keys. */
#define MODEKEY_EMACS 0 #define MODEKEY_EMACS 0
@ -2944,6 +2944,7 @@ void grid_reflow(struct grid *, u_int);
void grid_wrap_position(struct grid *, u_int, u_int, u_int *, u_int *); void grid_wrap_position(struct grid *, u_int, u_int, u_int *, u_int *);
void grid_unwrap_position(struct grid *, u_int *, u_int *, u_int, u_int); void grid_unwrap_position(struct grid *, u_int *, u_int *, u_int, u_int);
u_int grid_line_length(struct grid *, u_int); u_int grid_line_length(struct grid *, u_int);
int grid_in_set(struct grid *, u_int, u_int, const char *);
/* grid-reader.c */ /* grid-reader.c */
void grid_reader_start(struct grid_reader *, struct grid *, u_int, u_int); void grid_reader_start(struct grid_reader *, struct grid *, u_int, u_int);

View File

@ -5033,12 +5033,8 @@ window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
const char *set) const char *set)
{ {
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
struct grid_cell gc;
grid_get_cell(data->backing->grid, px, py, &gc); return (grid_in_set(data->backing->grid, px, py, set));
if (gc.flags & GRID_FLAG_PADDING)
return (0);
return (utf8_cstrhas(set, &gc.data));
} }
static u_int static u_int