Skip to content

Commit

Permalink
patch 8.0.0748: running Vim in terminal window doesn't use the right …
Browse files Browse the repository at this point in the history
…colors

Problem:    When running Vim in a terminal window it does not detect the right
            number of colors available.
Solution:   Detect the version string that libvterm returns.  Pass the number
            of colors in $COLORS.
  • Loading branch information
brammool committed Jul 22, 2017
1 parent e173fd0 commit b7a8dfe
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
10 changes: 10 additions & 0 deletions src/os_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4063,7 +4063,13 @@ set_child_environment(long rows, long columns, char *term)
static char envbuf_Rows[20];
static char envbuf_Lines[20];
static char envbuf_Columns[20];
static char envbuf_Colors[20];
# endif
long colors =
# ifdef FEAT_GUI
gui.in_use ? 256*256*256 :
# endif
t_colors;

/* Simulate to have a dumb terminal (for now) */
# ifdef HAVE_SETENV
Expand All @@ -4074,6 +4080,8 @@ set_child_environment(long rows, long columns, char *term)
setenv("LINES", (char *)envbuf, 1);
sprintf((char *)envbuf, "%ld", columns);
setenv("COLUMNS", (char *)envbuf, 1);
sprintf((char *)envbuf, "%ld", colors);
setenv("COLORS", (char *)envbuf, 1);
# else
/*
* Putenv does not copy the string, it has to remain valid.
Expand All @@ -4088,6 +4096,8 @@ set_child_environment(long rows, long columns, char *term)
vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
"COLUMNS=%ld", columns);
putenv(envbuf_Columns);
vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
putenv(envbuf_Colors);
# endif
}

Expand Down
93 changes: 60 additions & 33 deletions src/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,6 @@ parse_builtin_tcap(char_u *term)
}
}
#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
static void set_color_count(int nr);

/*
* Set number of colors.
* Store it as a number in t_colors.
Expand All @@ -1447,6 +1445,35 @@ set_color_count(int nr)
*nr_colors = NUL;
set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
}

/*
* Set the color count to "val" and redraw if it changed.
*/
static void
may_adjust_color_count(int val)
{
if (val != t_colors)
{
/* Nr of colors changed, initialize highlighting and
* redraw everything. This causes a redraw, which usually
* clears the message. Try keeping the message if it
* might work. */
set_keep_msg_from_hist();
set_color_count(val);
init_highlight(TRUE, FALSE);
# ifdef DEBUG_TERMRESPONSE
{
char buf[100];
int r = redraw_asap(CLEAR);

sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
log_tr(buf);
}
# else
redraw_asap(CLEAR);
# endif
}
}
#endif

#ifdef HAVE_TGETENT
Expand Down Expand Up @@ -2713,9 +2740,9 @@ term_get_winpos(int *x, int *y)
# endif

void
term_set_winsize(int width, int height)
term_set_winsize(int height, int width)
{
OUT_STR(tgoto((char *)T_CWS, height, width));
OUT_STR(tgoto((char *)T_CWS, width, height));
}
#endif

Expand Down Expand Up @@ -2823,6 +2850,8 @@ term_settitle(char_u *title)
void
ttest(int pairs)
{
char_u *env_colors;

check_options(); /* make sure no options are NULL */

/*
Expand Down Expand Up @@ -2909,8 +2938,16 @@ ttest(int pairs)
}
need_gather = TRUE;

/* Set t_colors to the value of t_Co. */
/* Set t_colors to the value of $COLORS or t_Co. */
t_colors = atoi((char *)T_CCO);
env_colors = mch_getenv((char_u *)"COLORS");
if (env_colors != NULL && isdigit(*env_colors))
{
int colors = atoi((char *)env_colors);

if (colors != t_colors)
set_color_count(colors);
}
}

#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
Expand Down Expand Up @@ -4250,6 +4287,7 @@ check_termcode(
* "<Esc>[" or CSI:
*
* - Xterm version string: <Esc>[>{x};{vers};{y}c
* Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
* Also eat other possible responses to t_RV, rxvt returns
* "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
* mrxvt has been reported to have "+" in the version. Assume
Expand Down Expand Up @@ -4359,10 +4397,8 @@ check_termcode(
/* rxvt sends its version number: "20703" is 2.7.3.
* Ignore it for when the user has set 'term' to xterm,
* even though it's an rxvt. */
if (extra > 0)
extra = atoi((char *)tp + extra);
if (extra > 20000)
extra = 0;
if (col > 20000)
col = 0;

if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
{
Expand All @@ -4371,25 +4407,36 @@ check_termcode(
if (!option_was_set((char_u *)"ttym"))
{
# ifdef TTYM_SGR
if (extra >= 277)
if (col >= 277)
set_option_value((char_u *)"ttym", 0L,
(char_u *)"sgr", 0);
else
# endif
/* if xterm version >= 95 use mouse dragging */
if (extra >= 95)
if (col >= 95)
set_option_value((char_u *)"ttym", 0L,
(char_u *)"xterm2", 0);
}

/* if xterm version >= 141 try to get termcap codes */
if (extra >= 141)
if (col >= 141)
{
LOG_TR("Enable checking for XT codes");
check_for_codes = TRUE;
need_gather = TRUE;
req_codes_from_term();
}

/* libvterm sends 0;100;0 */
if (col == 100
&& STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
{
/* If run from Vim $COLORS is set to the number of
* colors the terminal supports. Otherwise assume
* 256, libvterm supports even more. */
if (mch_getenv((char_u *)"COLORS") == NULL)
may_adjust_color_count(256);
}
}
# ifdef FEAT_EVAL
set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
Expand Down Expand Up @@ -5993,27 +6040,7 @@ got_code_from_term(char_u *code, int len)
{
/* Color count is not a key code. */
i = atoi((char *)str);
if (i != t_colors)
{
/* Nr of colors changed, initialize highlighting and
* redraw everything. This causes a redraw, which usually
* clears the message. Try keeping the message if it
* might work. */
set_keep_msg_from_hist();
set_color_count(i);
init_highlight(TRUE, FALSE);
#ifdef DEBUG_TERMRESPONSE
{
char buf[100];
int r = redraw_asap(CLEAR);

sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
log_tr(buf);
}
#else
redraw_asap(CLEAR);
#endif
}
may_adjust_color_count(i);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
748,
/**/
747,
/**/
Expand Down

0 comments on commit b7a8dfe

Please sign in to comment.