Skip to content

Commit 49a613f

Browse files
committed
patch 8.0.1097: background color wrong if job changes background color
Problem: Background color wrong if job changes background color. Solution: Get the background color from vterm.
1 parent 238d43b commit 49a613f

3 files changed

Lines changed: 59 additions & 16 deletions

File tree

src/screen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,7 @@ win_line(
32573257
{
32583258
extra_check = TRUE;
32593259
get_term_attr = TRUE;
3260-
term_attr = term_get_attr(wp->w_buffer, 0, 0);
3260+
term_attr = term_get_attr(wp->w_buffer, lnum, -1);
32613261
}
32623262
#endif
32633263

src/terminal.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ typedef struct {
8585
typedef struct sb_line_S {
8686
int sb_cols; /* can differ per line */
8787
cellattr_T *sb_cells; /* allocated */
88+
cellattr_T sb_fill_attr; /* for short line */
8889
} sb_line_T;
8990

9091
/* typedef term_T in structs.h */
@@ -943,6 +944,28 @@ add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
943944
}
944945
}
945946

947+
static void
948+
cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
949+
{
950+
attr->width = cell->width;
951+
attr->attrs = cell->attrs;
952+
attr->fg = cell->fg;
953+
attr->bg = cell->bg;
954+
}
955+
956+
static int
957+
equal_celattr(cellattr_T *a, cellattr_T *b)
958+
{
959+
/* Comparing the colors should be sufficient. */
960+
return a->fg.red == b->fg.red
961+
&& a->fg.green == b->fg.green
962+
&& a->fg.blue == b->fg.blue
963+
&& a->bg.red == b->bg.red
964+
&& a->bg.green == b->bg.green
965+
&& a->bg.blue == b->bg.blue;
966+
}
967+
968+
946969
/*
947970
* Add the current lines of the terminal to scrollback and to the buffer.
948971
* Called after the job has ended and when switching to Terminal-Normal mode.
@@ -955,21 +978,30 @@ move_terminal_to_buffer(term_T *term)
955978
int lines_skipped = 0;
956979
VTermPos pos;
957980
VTermScreenCell cell;
981+
cellattr_T fill_attr, new_fill_attr;
958982
cellattr_T *p;
959983
VTermScreen *screen;
960984

961985
if (term->tl_vterm == NULL)
962986
return;
963987
screen = vterm_obtain_screen(term->tl_vterm);
988+
fill_attr = new_fill_attr = term->tl_default_color;
989+
964990
for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
965991
{
966992
len = 0;
967993
for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
968994
if (vterm_screen_get_cell(screen, pos, &cell) != 0
969995
&& cell.chars[0] != NUL)
996+
{
970997
len = pos.col + 1;
998+
new_fill_attr = term->tl_default_color;
999+
}
1000+
else
1001+
/* Assume the last attr is the filler attr. */
1002+
cell2cellattr(&cell, &new_fill_attr);
9711003

972-
if (len == 0)
1004+
if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
9731005
++lines_skipped;
9741006
else
9751007
{
@@ -984,14 +1016,19 @@ move_terminal_to_buffer(term_T *term)
9841016

9851017
line->sb_cols = 0;
9861018
line->sb_cells = NULL;
1019+
line->sb_fill_attr = fill_attr;
9871020
++term->tl_scrollback.ga_len;
9881021

9891022
add_scrollback_line_to_buffer(term, (char_u *)"", 0);
9901023
}
9911024
}
9921025

993-
p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
994-
if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
1026+
if (len == 0)
1027+
p = NULL;
1028+
else
1029+
p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
1030+
if ((p != NULL || len == 0)
1031+
&& ga_grow(&term->tl_scrollback, 1) == OK)
9951032
{
9961033
garray_T ga;
9971034
int width;
@@ -1013,10 +1050,7 @@ move_terminal_to_buffer(term_T *term)
10131050
{
10141051
width = cell.width;
10151052

1016-
p[pos.col].width = cell.width;
1017-
p[pos.col].attrs = cell.attrs;
1018-
p[pos.col].fg = cell.fg;
1019-
p[pos.col].bg = cell.bg;
1053+
cell2cellattr(&cell, &p[pos.col]);
10201054

10211055
if (ga_grow(&ga, MB_MAXBYTES) == OK)
10221056
{
@@ -1031,6 +1065,8 @@ move_terminal_to_buffer(term_T *term)
10311065
}
10321066
line->sb_cols = len;
10331067
line->sb_cells = p;
1068+
line->sb_fill_attr = new_fill_attr;
1069+
fill_attr = new_fill_attr;
10341070
++term->tl_scrollback.ga_len;
10351071

10361072
if (ga_grow(&ga, 1) == FAIL)
@@ -1047,6 +1083,10 @@ move_terminal_to_buffer(term_T *term)
10471083
}
10481084
}
10491085

1086+
/* Obtain the current background color. */
1087+
vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1088+
&term->tl_default_color.fg, &term->tl_default_color.bg);
1089+
10501090
FOR_ALL_WINDOWS(wp)
10511091
{
10521092
if (wp->w_buffer == term->tl_buffer)
@@ -2003,11 +2043,14 @@ handle_pushline(int cols, const VTermScreenCell *cells, void *user)
20032043
int col;
20042044
sb_line_T *line;
20052045
garray_T ga;
2046+
cellattr_T fill_attr = term->tl_default_color;
20062047

20072048
/* do not store empty cells at the end */
20082049
for (i = 0; i < cols; ++i)
20092050
if (cells[i].chars[0] != 0)
20102051
len = i + 1;
2052+
else
2053+
cell2cellattr(&cells[i], &fill_attr);
20112054

20122055
ga_init2(&ga, 1, 100);
20132056
if (len > 0)
@@ -2024,10 +2067,7 @@ handle_pushline(int cols, const VTermScreenCell *cells, void *user)
20242067
for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
20252068
ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
20262069
(char_u *)ga.ga_data + ga.ga_len);
2027-
p[col].width = cells[col].width;
2028-
p[col].attrs = cells[col].attrs;
2029-
p[col].fg = cells[col].fg;
2030-
p[col].bg = cells[col].bg;
2070+
cell2cellattr(&cells[col], &p[col]);
20312071
}
20322072
}
20332073
if (ga_grow(&ga, 1) == FAIL)
@@ -2043,6 +2083,7 @@ handle_pushline(int cols, const VTermScreenCell *cells, void *user)
20432083
+ term->tl_scrollback.ga_len;
20442084
line->sb_cols = len;
20452085
line->sb_cells = p;
2086+
line->sb_fill_attr = fill_attr;
20462087
++term->tl_scrollback.ga_len;
20472088
++term->tl_scrollback_scrolled;
20482089
}
@@ -2319,7 +2360,7 @@ term_change_in_curbuf(void)
23192360

23202361
/*
23212362
* Get the screen attribute for a position in the buffer.
2322-
* Use a zero "lnum" to get the default background color.
2363+
* Use a negative "col" to get the filler background color.
23232364
*/
23242365
int
23252366
term_get_attr(buf_T *buf, linenr_T lnum, int col)
@@ -2328,13 +2369,13 @@ term_get_attr(buf_T *buf, linenr_T lnum, int col)
23282369
sb_line_T *line;
23292370
cellattr_T *cellattr;
23302371

2331-
if (lnum == 0 || lnum > term->tl_scrollback.ga_len)
2372+
if (lnum > term->tl_scrollback.ga_len)
23322373
cellattr = &term->tl_default_color;
23332374
else
23342375
{
23352376
line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2336-
if (col >= line->sb_cols)
2337-
cellattr = &term->tl_default_color;
2377+
if (col < 0 || col >= line->sb_cols)
2378+
cellattr = &line->sb_fill_attr;
23382379
else
23392380
cellattr = line->sb_cells + col;
23402381
}

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
1097,
772774
/**/
773775
1096,
774776
/**/

0 commit comments

Comments
 (0)