Skip to content

Commit

Permalink
fix(tui): reposition cursor after printing ambiguous-width character
Browse files Browse the repository at this point in the history
  • Loading branch information
zeertzjq committed Aug 9, 2022
1 parent e6680ea commit 075807e
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/nvim/tui/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,23 @@ static void cursor_goto(UI *ui, int row, int col)
ugrid_goto(grid, row, col);
}

/// Move cursor to the position given by `row` and `col` and print `cell`.
static void print_cell_at(UI* ui, int row, int col, UCell *cell)
{
TUIData *data = ui->data;
UGrid *grid = &data->grid;
if (grid->row == -1 && cell->data[0] == NUL) {
// If cursor needs to repositioned and there is nothing to print, don't move cursor.
return;
}
cursor_goto(ui, row, col);
print_cell(ui, cell);
if (utf_ambiguous_width(utf_ptr2char(cell->data))) {
// Force repositioning cursor after printing an ambiguous-width character.
grid->row = -1;
}
}

static void clear_region(UI *ui, int top, int bot, int left, int right, int attr_id)
{
TUIData *data = ui->data;
Expand Down Expand Up @@ -1302,8 +1319,7 @@ static void tui_flush(UI *ui)
}

UGRID_FOREACH_CELL(grid, row, r.left, clear_col, {
cursor_goto(ui, row, curcol);
print_cell(ui, cell);
print_cell_at(ui, row, curcol, cell);
});
if (clear_col < r.right) {
clear_region(ui, row, row + 1, clear_col, r.right, clear_attr);
Expand Down Expand Up @@ -1439,8 +1455,7 @@ static void tui_raw_line(UI *ui, Integer g, Integer linerow, Integer startcol, I
grid->cells[linerow][c].attr = attrs[c - startcol];
}
UGRID_FOREACH_CELL(grid, (int)linerow, (int)startcol, (int)endcol, {
cursor_goto(ui, (int)linerow, curcol);
print_cell(ui, cell);
print_cell_at(ui, (int)linerow, curcol, cell);
});

if (clearcol > endcol) {
Expand All @@ -1458,8 +1473,8 @@ static void tui_raw_line(UI *ui, Integer g, Integer linerow, Integer startcol, I
if (endcol != grid->width) {
// Print the last char of the row, if we haven't already done so.
int size = grid->cells[linerow][grid->width - 1].data[0] == NUL ? 2 : 1;
cursor_goto(ui, (int)linerow, grid->width - size);
print_cell(ui, &grid->cells[linerow][grid->width - size]);
print_cell_at(ui, (int)linerow, grid->width - size,
&grid->cells[linerow][grid->width - size]);
}

// Wrap the cursor over to the next line. The next line will be
Expand Down

0 comments on commit 075807e

Please sign in to comment.