To: vim_dev@googlegroups.com Subject: Patch 8.0.0748 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.0.0748 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. Files: src/term.c, src/os_unix.c *** ../vim-8.0.0747/src/term.c 2017-06-29 22:27:20.589824578 +0200 --- src/term.c 2017-07-22 20:31:49.457906183 +0200 *************** *** 1428,1435 **** } } #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. --- 1428,1433 ---- *************** *** 1447,1452 **** --- 1445,1479 ---- *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 *************** *** 2713,2721 **** # endif void ! term_set_winsize(int width, int height) { ! OUT_STR(tgoto((char *)T_CWS, height, width)); } #endif --- 2740,2748 ---- # endif void ! term_set_winsize(int height, int width) { ! OUT_STR(tgoto((char *)T_CWS, width, height)); } #endif *************** *** 2823,2828 **** --- 2850,2857 ---- void ttest(int pairs) { + char_u *env_colors; + check_options(); /* make sure no options are NULL */ /* *************** *** 2909,2916 **** } need_gather = TRUE; ! /* Set t_colors to the value of t_Co. */ t_colors = atoi((char *)T_CCO); } #if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \ --- 2938,2953 ---- } need_gather = TRUE; ! /* 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))) \ *************** *** 4250,4255 **** --- 4287,4293 ---- * "[" or CSI: * * - Xterm version string: [>{x};{vers};{y}c + * Libvterm returns {x} == 0, {vers} == 100, {y} == 0. * Also eat other possible responses to t_RV, rxvt returns * "[?1;2c". Also accept CSI instead of [. * mrxvt has been reported to have "+" in the version. Assume *************** *** 4359,4368 **** /* 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 (tp[1 + (tp[0] != CSI)] == '>' && j == 2) { --- 4397,4404 ---- /* 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 (col > 20000) ! col = 0; if (tp[1 + (tp[0] != CSI)] == '>' && j == 2) { *************** *** 4371,4395 **** if (!option_was_set((char_u *)"ttym")) { # ifdef TTYM_SGR ! if (extra >= 277) set_option_value((char_u *)"ttym", 0L, (char_u *)"sgr", 0); else # endif /* if xterm version >= 95 use mouse dragging */ ! if (extra >= 95) set_option_value((char_u *)"ttym", 0L, (char_u *)"xterm2", 0); } /* if xterm version >= 141 try to get termcap codes */ ! if (extra >= 141) { LOG_TR("Enable checking for XT codes"); check_for_codes = TRUE; need_gather = TRUE; req_codes_from_term(); } } # ifdef FEAT_EVAL set_vim_var_string(VV_TERMRESPONSE, tp, i + 1); --- 4407,4442 ---- if (!option_was_set((char_u *)"ttym")) { # ifdef TTYM_SGR ! if (col >= 277) set_option_value((char_u *)"ttym", 0L, (char_u *)"sgr", 0); else # endif /* if xterm version >= 95 use mouse dragging */ ! if (col >= 95) set_option_value((char_u *)"ttym", 0L, (char_u *)"xterm2", 0); } /* if xterm version >= 141 try to get termcap codes */ ! 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); *************** *** 5993,6019 **** { /* 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 ! } } else { --- 6040,6046 ---- { /* Color count is not a key code. */ i = atoi((char *)str); ! may_adjust_color_count(i); } else { *** ../vim-8.0.0747/src/os_unix.c 2017-07-22 18:38:54.974753840 +0200 --- src/os_unix.c 2017-07-22 19:55:12.457636999 +0200 *************** *** 4063,4069 **** --- 4063,4075 ---- 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 *************** *** 4074,4079 **** --- 4080,4087 ---- 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. *************** *** 4088,4093 **** --- 4096,4103 ---- 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 } *** ../vim-8.0.0747/src/version.c 2017-07-22 19:03:28.184044163 +0200 --- src/version.c 2017-07-22 19:34:31.734543046 +0200 *************** *** 771,772 **** --- 771,774 ---- { /* Add new patch number below this line */ + /**/ + 748, /**/ -- "You mean there really is an answer?" "Yes! But you're not going to like it!" "Oh do please tell us!" "You're really not going to like it!" "but we MUST know - tell us" "Alright, the answer is...." "yes..." "... is ..." "yes... come on!" "is 42!" (Douglas Adams - The Hitchhiker's Guide to the Galaxy) /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///