Blob Blame History Raw
To: vim_dev@googlegroups.com
Subject: Patch 7.4.757
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.4.757
Problem:    Cannot detect the background color of a terminal.
Solution:   Add T_RBG to request the background color if possible. (Lubomir
            Rintel)
Files:      src/main.c, src/term.c, src/term.h, src/proto/term.pro


*** ../vim-7.4.756/src/main.c	2015-04-17 22:08:10.998772925 +0200
--- src/main.c	2015-06-25 17:01:47.917747345 +0200
***************
*** 837,844 ****
  
      starttermcap();	    /* start termcap if not done by wait_return() */
      TIME_MSG("start termcap");
! #if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
      may_req_ambiguous_char_width();
  #endif
  
  #ifdef FEAT_MOUSE
--- 837,847 ----
  
      starttermcap();	    /* start termcap if not done by wait_return() */
      TIME_MSG("start termcap");
! #if defined(FEAT_TERMRESPONSE)
! # if defined(FEAT_MBYTE)
      may_req_ambiguous_char_width();
+ # endif
+     may_req_bg_color();
  #endif
  
  #ifdef FEAT_MOUSE
*** ../vim-7.4.756/src/term.c	2015-03-31 18:30:09.139370916 +0200
--- src/term.c	2015-06-25 16:52:59.359131386 +0200
***************
*** 124,129 ****
--- 124,134 ----
  #  define U7_SENT	2	/* did send T_U7, waiting for answer */
  #  define U7_GOT	3	/* received T_U7 response */
  static int u7_status = U7_GET;
+ /* Request background color report: */
+ #  define RBG_GET	1	/* send T_RBG when switched to RAW mode */
+ #  define RBG_SENT	2	/* did send T_RBG, waiting for answer */
+ #  define RBG_GOT	3	/* received T_RBG response */
+ static int rbg_status = RBG_GET;
  # endif
  
  /*
***************
*** 949,954 ****
--- 954,960 ----
      {(int)KS_CWP,	IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
  #  endif
      {(int)KS_CRV,	IF_EB("\033[>c", ESC_STR "[>c")},
+     {(int)KS_RBG,	IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
      {(int)KS_U7,	IF_EB("\033[6n", ESC_STR "[6n")},
  
      {K_UP,		IF_EB("\033O*A", ESC_STR "O*A")},
***************
*** 1240,1245 ****
--- 1246,1252 ----
  #  endif
      {(int)KS_CRV,	"[CRV]"},
      {(int)KS_U7,	"[U7]"},
+     {(int)KS_RBG,	"[RBG]"},
      {K_UP,		"[KU]"},
      {K_DOWN,		"[KD]"},
      {K_LEFT,		"[KL]"},
***************
*** 3224,3230 ****
  		 * doesn't work in Cooked mode, an external program may get
  		 * them. */
  		if (tmode != TMODE_RAW && (crv_status == CRV_SENT
! 					 || u7_status == U7_SENT))
  		    (void)vpeekc_nomap();
  		check_for_codes_from_term();
  	    }
--- 3231,3238 ----
  		 * doesn't work in Cooked mode, an external program may get
  		 * them. */
  		if (tmode != TMODE_RAW && (crv_status == CRV_SENT
! 					 || u7_status == U7_SENT
! 					 || rbg_status == RBG_SENT))
  		    (void)vpeekc_nomap();
  		check_for_codes_from_term();
  	    }
***************
*** 3285,3292 ****
  	if (!gui.in_use && !gui.starting)
  # endif
  	{
! 	    /* May need to discard T_CRV or T_U7 response. */
! 	    if (crv_status == CRV_SENT || u7_status == U7_SENT)
  	    {
  # ifdef UNIX
  		/* Give the terminal a chance to respond. */
--- 3293,3301 ----
  	if (!gui.in_use && !gui.starting)
  # endif
  	{
! 	    /* May need to discard T_CRV, T_U7 or T_RBG response. */
! 	    if (crv_status == CRV_SENT || u7_status == U7_SENT
! 						     || rbg_status == RBG_SENT)
  	    {
  # ifdef UNIX
  		/* Give the terminal a chance to respond. */
***************
*** 3398,3403 ****
--- 3407,3447 ----
  }
  # endif
  
+ #if defined(FEAT_TERMRESPONSE) || defined(PROTO)
+ /*
+  * Check how the terminal treats ambiguous character width (UAX #11).
+  * First, we move the cursor to (1, 0) and print a test ambiguous character
+  * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
+  * If the terminal treats \u25bd as single width, the position is (1, 1),
+  * or if it is treated as double width, that will be (1, 2).
+  * This function has the side effect that changes cursor position, so
+  * it must be called immediately after entering termcap mode.
+  */
+     void
+ may_req_bg_color()
+ {
+     if (rbg_status == RBG_GET
+ 	    && cur_tmode == TMODE_RAW
+ 	    && termcap_active
+ 	    && p_ek
+ #  ifdef UNIX
+ 	    && isatty(1)
+ 	    && isatty(read_cmd_fd)
+ #  endif
+ 	    && *T_RBG != NUL
+ 	    && !option_was_set((char_u *)"bg"))
+     {
+ 	LOG_TR("Sending BG request");
+ 	out_str(T_RBG);
+ 	rbg_status = RBG_SENT;
+ 	/* check for the characters now, otherwise they might be eaten by
+ 	 * get_keystroke() */
+ 	out_flush();
+ 	(void)vpeekc_nomap();
+     }
+ }
+ # endif
+ 
  # ifdef DEBUG_TERMRESPONSE
      static void
  log_tr(char *msg)
***************
*** 4222,4233 ****
  	     * - Cursor position report: <Esc>[{row};{col}R
  	     *   The final byte must be 'R'. It is used for checking the
  	     *   ambiguous-width character state.
  	     */
! 	    p = tp[0] == CSI ? tp + 1 : tp + 2;
! 	    if ((*T_CRV != NUL || *T_U7 != NUL)
  			&& ((tp[0] == ESC && tp[1] == '[' && len >= 3)
  			    || (tp[0] == CSI && len >= 2))
! 			&& (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
  	    {
  #ifdef FEAT_MBYTE
  		int col;
--- 4266,4283 ----
  	     * - Cursor position report: <Esc>[{row};{col}R
  	     *   The final byte must be 'R'. It is used for checking the
  	     *   ambiguous-width character state.
+ 	     *
+ 	     * - Background color response:
+ 	     *       <Esc>]11;rgb:{rrrr}/{gggg}/{bbbb}\007
+ 	     *   The final byte must be '\007'.
  	     */
! 	    char_u *argp = tp[0] == CSI ? tp + 1 : tp + 2;
! 
! 	    if ((*T_CRV != NUL || *T_U7 != NUL || *T_RBG != NUL)
  			&& ((tp[0] == ESC && tp[1] == '[' && len >= 3)
+ 			    || (tp[0] == ESC && tp[1] == ']' && len >= 24)
  			    || (tp[0] == CSI && len >= 2))
! 			&& (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
  	    {
  #ifdef FEAT_MBYTE
  		int col;
***************
*** 4363,4368 ****
--- 4413,4439 ----
  		    key_name[1] = (int)KE_IGNORE;
  		    slen = i + 1;
  		}
+ 		else if (*T_RBG != NUL && len >= 24 - (tp[0] == CSI)
+ 			&& argp[0] == '1' && argp[1] == '1'
+ 			&& argp[2] == ';' && argp[3] == 'r' && argp[4] == 'g'
+ 			&& argp[5] == 'b' && argp[6] == ':'
+ 			&& argp[11] == '/' && argp[16] == '/'
+ 			&& argp[21] == '\007')
+ 		{
+ 		    LOG_TR("Received RBG");
+ 		    rbg_status = RBG_GOT;
+ 		    if (!option_was_set((char_u *)"bg"))
+ 		    {
+ 			set_option_value((char_u *)"bg", 0L, (char_u *)(
+ 				    (3 * '6' < argp[7] + argp[12] + argp[17])
+ 						      ? "light" : "dark"), 0);
+ 			reset_option_was_set((char_u *)"bg");
+ 			redraw_asap(CLEAR);
+ 		    }
+ 		    key_name[0] = (int)KS_EXTRA;
+ 		    key_name[1] = (int)KE_IGNORE;
+ 		    slen = 24;
+ 		}
  	    }
  
  	    /* Check for '<Esc>P1+r<hex bytes><Esc>\'.  A "0" instead of the
*** ../vim-7.4.756/src/term.h	2015-03-31 18:30:09.143370872 +0200
--- src/term.h	2015-06-25 16:21:55.222506530 +0200
***************
*** 79,84 ****
--- 79,85 ----
      KS_CWP,	/* set window position in pixels */
      KS_CWS,	/* set window size in characters */
      KS_CRV,	/* request version string */
+     KS_RBG,	/* request background color */
      KS_CSI,	/* start insert mode (bar cursor) */
      KS_CEI,	/* end insert mode (block cursor) */
      KS_CSR,	/* start replace mode (underline cursor) */
***************
*** 162,167 ****
--- 163,169 ----
  #define T_CEI	(term_str(KS_CEI))	/* end insert mode */
  #define T_CSR	(term_str(KS_CSR))	/* start replace mode */
  #define T_CRV	(term_str(KS_CRV))	/* request version string */
+ #define T_RBG	(term_str(KS_RBG))	/* request background RGB */
  #define T_OP	(term_str(KS_OP))	/* original color pair */
  #define T_U7	(term_str(KS_U7))	/* request cursor position */
  
*** ../vim-7.4.756/src/proto/term.pro	2014-07-30 17:21:53.819518506 +0200
--- src/proto/term.pro	2015-06-25 16:39:46.095228111 +0200
***************
*** 36,41 ****
--- 36,42 ----
  void stoptermcap __ARGS((void));
  void may_req_termresponse __ARGS((void));
  void may_req_ambiguous_char_width __ARGS((void));
+ void may_req_bg_color __ARGS((void));
  int swapping_screen __ARGS((void));
  void setmouse __ARGS((void));
  int mouse_has __ARGS((int c));
*** ../vim-7.4.756/src/version.c	2015-06-25 16:13:37.779750062 +0200
--- src/version.c	2015-06-25 16:20:48.475209933 +0200
***************
*** 743,744 ****
--- 743,746 ----
  {   /* Add new patch number below this line */
+ /**/
+     757,
  /**/

-- 
We are the Borg of GNU GPL.  We will assimilate your source code.
Resistance is futile.

 /// 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    ///