From 94b549f93e8cc4372e5ab7dc9c832219f337819c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 5 Jun 2021 22:48:20 +0200 Subject: vt: emit a tab character if all cells between cursor and tab stop are empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TAB (\t) move the cursor to the next tab stop. That’s it, according to the specification. However, many terminal emulators try to keep tabs in the grid, to be able to e.g. copy them. That is, copying a text chunk containing tabs should result in tabs being pasted, not spaces. In order to do that, we need to print a tab character to the grid. To improve text reflow of tabs, we also print spaces to the subsequent cells, up until (but not including) the next tab stop. However, we can only do this if all the cells between the cursor and the next tab stop are empty, since (obviously), we cannot overwrite pre-existing characters. Finally, while some fonts render tabs as spaces (i.e. an empty glyph), some use a glyph representing “unprintable” characters, or similar. Thus, we need to exclude cells with tab characters when rendering. --- vt.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'vt.c') diff --git a/vt.c b/vt.c index d0d215f1..fe7ec52a 100644 --- a/vt.c +++ b/vt.c @@ -162,20 +162,57 @@ action_execute(struct terminal *term, uint8_t c) case '\t': { /* HT - horizontal tab */ + int start_col = term->grid->cursor.point.col; int new_col = term->cols - 1; + tll_foreach(term->tab_stops, it) { - if (it->item > term->grid->cursor.point.col) { + if (it->item > start_col) { new_col = it->item; break; } } - xassert(new_col >= term->grid->cursor.point.col); + xassert(new_col >= start_col); + xassert(new_col < term->cols); + + + bool emit_tab_char = true; + struct row *row = term->grid->cur_row; + + /* Check if all cells from here until the next tab stop are empty */ + for (const struct cell *cell = &row->cells[start_col]; + cell < &row->cells[new_col]; + cell++) + { + if (!(cell->wc == 0 || cell->wc == L' ')) { + emit_tab_char = false; + break; + } + } + + /* + * Emit a tab in current cell, and write spaces to the + * subsequent cells, all the way until the next tab stop. + */ + if (emit_tab_char) { + row->dirty = true; + + row->cells[start_col].wc = '\t'; + row->cells[start_col].attrs.clean = 0; + + for (struct cell *cell = &row->cells[start_col + 1]; + cell < &row->cells[new_col]; + cell++) + { + cell->wc = L' '; + cell->attrs.clean = 0; + } + } /* According to the specification, HT _should_ cancel LCF. But * XTerm, and nearly all other emulators, don't. So we follow * suit */ bool lcf = term->grid->cursor.lcf; - term_cursor_right(term, new_col - term->grid->cursor.point.col); + term_cursor_right(term, new_col - start_col); term->grid->cursor.lcf = lcf; break; } -- cgit v1.3 From 9d3351472d9d6ef9a4314906fe71bf5fa3ed77bf Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 7 Jun 2021 21:16:38 +0200 Subject: vt: TAB: don’t print a ‘\t’ to the grid if the *current* cell isn’t empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the cursor is already at the right edge, our logic that checked for non-empty cells failed; it didn’t check the current cell. Fix by initializing ‘emit_tab_char’ to true/false, depending on the contents of the current cell. --- vt.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'vt.c') diff --git a/vt.c b/vt.c index fe7ec52a..7c884cca 100644 --- a/vt.c +++ b/vt.c @@ -174,12 +174,13 @@ action_execute(struct terminal *term, uint8_t c) xassert(new_col >= start_col); xassert(new_col < term->cols); - - bool emit_tab_char = true; struct row *row = term->grid->cur_row; + bool emit_tab_char = (row->cells[start_col].wc == 0 || + row->cells[start_col].wc == L' '); + /* Check if all cells from here until the next tab stop are empty */ - for (const struct cell *cell = &row->cells[start_col]; + for (const struct cell *cell = &row->cells[start_col + 1]; cell < &row->cells[new_col]; cell++) { -- cgit v1.3