Skip to content

Commit

Permalink
Fix word navigation on lines with tabs, from Alexander Arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicm committed Nov 20, 2024
1 parent a3ede31 commit 273f9b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
17 changes: 7 additions & 10 deletions grid-reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,14 @@ grid_reader_handle_wrap(struct grid_reader *gr, u_int *xx, u_int *yy)
int
grid_reader_in_set(struct grid_reader *gr, const char *set)
{
struct grid_cell gc;

grid_get_cell(gr->gd, gr->cx, gr->cy, &gc);
if (gc.flags & GRID_FLAG_PADDING)
return (0);
return (utf8_cstrhas(set, &gc.data));
return (grid_in_set(gr->gd, gr->cx, gr->cy, set));
}

/* Move cursor to the start of the next word. */
void
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. */
if (grid_get_line(gr->gd, gr->cy)->flags & GRID_LINE_WRAPPED)
Expand Down Expand Up @@ -229,8 +224,8 @@ grid_reader_cursor_next_word(struct grid_reader *gr, const char *separators)
}
}
while (grid_reader_handle_wrap(gr, &xx, &yy) &&
grid_reader_in_set(gr, WHITESPACE))
gr->cx++;
(width = grid_reader_in_set(gr, WHITESPACE)))
gr->cx += width;
}

/* Move cursor to the end of the next word. */
Expand Down Expand Up @@ -425,7 +420,9 @@ grid_reader_cursor_back_to_indentation(struct grid_reader *gr)
xx = grid_line_length(gr->gd, py);
for (px = 0; px < xx; px++) {
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->cy = py;
return;
Expand Down
24 changes: 24 additions & 0 deletions grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1561,3 +1561,27 @@ grid_line_length(struct grid *gd, u_int py)
}
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 changes: 2 additions & 1 deletion tmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ enum tty_code_code {
};

/* Character classes. */
#define WHITESPACE " "
#define WHITESPACE "\t "

/* Mode keys. */
#define MODEKEY_EMACS 0
Expand Down Expand Up @@ -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_unwrap_position(struct grid *, u_int *, u_int *, u_int, 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 */
void grid_reader_start(struct grid_reader *, struct grid *, u_int, u_int);
Expand Down
6 changes: 1 addition & 5 deletions window-copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -5033,12 +5033,8 @@ window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
const char *set)
{
struct window_copy_mode_data *data = wme->data;
struct grid_cell gc;

grid_get_cell(data->backing->grid, px, py, &gc);
if (gc.flags & GRID_FLAG_PADDING)
return (0);
return (utf8_cstrhas(set, &gc.data));
return (grid_in_set(data->backing->grid, px, py, set));
}

static u_int
Expand Down

0 comments on commit 273f9b2

Please sign in to comment.