@@ -106,6 +106,14 @@ static char_u *tgetent_error __ARGS((char_u *, char_u *));
106106char * tgetstr __ARGS ((char * , char * * ) );
107107
108108# ifdef FEAT_TERMRESPONSE
109+ /* Change this to "if 1" to debug what happens with termresponse. */
110+ # if 0
111+ # define DEBUG_TERMRESPONSE
112+ static void log_tr (char * msg );
113+ # define LOG_TR (msg ) log_tr(msg)
114+ # else
115+ # define LOG_TR (msg )
116+ # endif
109117/* Request Terminal Version status: */
110118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
111119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
@@ -1936,6 +1944,7 @@ set_termname(term)
19361944 full_screen = TRUE; /* we can use termcap codes from now on */
19371945 set_term_defaults (); /* use current values as defaults */
19381946#ifdef FEAT_TERMRESPONSE
1947+ LOG_TR ("setting crv_status to CRV_GET" );
19391948 crv_status = CRV_GET ; /* Get terminal version later */
19401949#endif
19411950
@@ -3326,6 +3335,7 @@ may_req_termresponse()
33263335# endif
33273336 && * T_CRV != NUL )
33283337 {
3338+ LOG_TR ("Sending CRV" );
33293339 out_str (T_CRV );
33303340 crv_status = CRV_SENT ;
33313341 /* check for the characters now, otherwise they might be eaten by
@@ -3338,10 +3348,10 @@ may_req_termresponse()
33383348# if defined(FEAT_MBYTE ) || defined(PROTO )
33393349/*
33403350 * Check how the terminal treats ambiguous character width (UAX #11).
3341- * First, we move the cursor to (0 , 0) and print a test ambiguous character
3351+ * First, we move the cursor to (1 , 0) and print a test ambiguous character
33423352 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
3343- * If the terminal treats \u25bd as single width, the position is (0 , 1),
3344- * or if it is treated as double width, that will be (0 , 2).
3353+ * If the terminal treats \u25bd as single width, the position is (1 , 1),
3354+ * or if it is treated as double width, that will be (1 , 2).
33453355 * This function has the side effect that changes cursor position, so
33463356 * it must be called immediately after entering termcap mode.
33473357 */
@@ -3361,7 +3371,10 @@ may_req_ambiguous_character_width()
33613371 {
33623372 char_u buf [16 ];
33633373
3364- term_windgoto (0 , 0 );
3374+ LOG_TR ("Sending U7 request" );
3375+ /* Do this in the second row. In the first row the returned sequence
3376+ * may be CSI 1;2R, which is the same as <S-F3>. */
3377+ term_windgoto (1 , 0 );
33653378 buf [mb_char2bytes (0x25bd , buf )] = 0 ;
33663379 out_str (buf );
33673380 out_str (T_U7 );
@@ -3376,6 +3389,29 @@ may_req_ambiguous_character_width()
33763389 }
33773390}
33783391# endif
3392+
3393+ # ifdef DEBUG_TERMRESPONSE
3394+ static void
3395+ log_tr (char * msg )
3396+ {
3397+ static FILE * fd_tr = NULL ;
3398+ static proftime_T start ;
3399+ proftime_T now ;
3400+
3401+ if (fd_tr == NULL )
3402+ {
3403+ fd_tr = fopen ("termresponse.log" , "w" );
3404+ profile_start (& start );
3405+ }
3406+ now = start ;
3407+ profile_end (& now );
3408+ fprintf (fd_tr , "%s: %s %s\n" ,
3409+ profile_msg (& now ),
3410+ must_redraw == NOT_VALID ? "NV"
3411+ : must_redraw == CLEAR ? "CL" : " " ,
3412+ msg );
3413+ }
3414+ # endif
33793415#endif
33803416
33813417/*
@@ -3847,6 +3883,7 @@ switch_to_8bit()
38473883 need_gather = TRUE; /* need to fill termleader[] */
38483884 }
38493885 detected_8bit = TRUE;
3886+ LOG_TR ("Switching to 8 bit" );
38503887}
38513888#endif
38523889
@@ -4156,14 +4193,20 @@ check_termcode(max_offset, buf, bufsize, buflen)
41564193 if (tp [i ] == ';' && ++ j == 1 )
41574194 extra = i + 1 ;
41584195 if (i == len )
4159- return -1 ; /* not enough characters */
4196+ {
4197+ LOG_TR ("Not enough characters for CRV" );
4198+ return -1 ;
4199+ }
41604200
41614201#ifdef FEAT_MBYTE
4162- /* eat it when it has 2 arguments and ends in 'R' */
4163- if (j == 1 && tp [i ] == 'R' )
4202+ /* Eat it when it has 2 arguments and ends in 'R'. Ignore it
4203+ * when u7_status is not "sent", <S-F3> sends something
4204+ * similar. */
4205+ if (j == 1 && tp [i ] == 'R' && u7_status == U7_SENT )
41644206 {
41654207 char * aw = NULL ;
41664208
4209+ LOG_TR ("Received U7 status" );
41674210 u7_status = U7_GOT ;
41684211# ifdef FEAT_AUTOCMD
41694212 did_cursorhold = TRUE;
@@ -4174,8 +4217,24 @@ check_termcode(max_offset, buf, bufsize, buflen)
41744217 aw = "single" ;
41754218 else if (extra == 3 )
41764219 aw = "double" ;
4177- if (aw != NULL )
4220+ if (aw != NULL && STRCMP (aw , p_ambw ) != 0 )
4221+ {
4222+ /* Setting the option causes a screen redraw. Do that
4223+ * right away if possible, keeping any messages. */
41784224 set_option_value ((char_u * )"ambw" , 0L , (char_u * )aw , 0 );
4225+ #ifdef DEBUG_TERMRESPONSE
4226+ {
4227+ char buf [100 ];
4228+ int r = redraw_asap (CLEAR );
4229+
4230+ sprintf (buf , "set 'ambiwidth', redraw_asap(): %d" ,
4231+ r );
4232+ log_tr (buf );
4233+ }
4234+ #else
4235+ redraw_asap (CLEAR );
4236+ #endif
4237+ }
41794238 key_name [0 ] = (int )KS_EXTRA ;
41804239 key_name [1 ] = (int )KE_IGNORE ;
41814240 slen = i + 1 ;
@@ -4185,6 +4244,7 @@ check_termcode(max_offset, buf, bufsize, buflen)
41854244 /* eat it when at least one digit and ending in 'c' */
41864245 if (* T_CRV != NUL && i > 2 + (tp [0 ] != CSI ) && tp [i ] == 'c' )
41874246 {
4247+ LOG_TR ("Received CRV" );
41884248 crv_status = CRV_GOT ;
41894249# ifdef FEAT_AUTOCMD
41904250 did_cursorhold = TRUE;
@@ -4224,6 +4284,7 @@ check_termcode(max_offset, buf, bufsize, buflen)
42244284 /* if xterm version >= 141 try to get termcap codes */
42254285 if (extra >= 141 )
42264286 {
4287+ LOG_TR ("Enable checking for XT codes" );
42274288 check_for_codes = TRUE;
42284289 need_gather = TRUE;
42294290 req_codes_from_term ();
@@ -4262,7 +4323,10 @@ check_termcode(max_offset, buf, bufsize, buflen)
42624323 }
42634324
42644325 if (i == len )
4326+ {
4327+ LOG_TR ("not enough characters for XT" );
42654328 return -1 ; /* not enough characters */
4329+ }
42664330 }
42674331 }
42684332#endif
@@ -5207,6 +5271,10 @@ check_termcode(max_offset, buf, bufsize, buflen)
52075271 return retval == 0 ? (len + extra + offset ) : retval ;
52085272 }
52095273
5274+ #ifdef FEAT_TERMRESPONSE
5275+ LOG_TR ("normal character" );
5276+ #endif
5277+
52105278 return 0 ; /* no match found */
52115279}
52125280
@@ -5661,6 +5729,13 @@ req_more_codes_from_term()
56615729 * many, there can be a buffer overflow somewhere. */
56625730 while (xt_index_out < xt_index_in + 10 && key_names [xt_index_out ] != NULL )
56635731 {
5732+ # ifdef DEBUG_TERMRESPONSE
5733+ char dbuf [100 ];
5734+
5735+ sprintf (dbuf , "Requesting XT %d: %s" ,
5736+ xt_index_out , key_names [xt_index_out ]);
5737+ log_tr (dbuf );
5738+ # endif
56645739 sprintf (buf , "\033P+q%02x%02x\033\\" ,
56655740 key_names [xt_index_out ][0 ], key_names [xt_index_out ][1 ]);
56665741 out_str_nf ((char_u * )buf );
@@ -5707,6 +5782,14 @@ got_code_from_term(code, len)
57075782 break ;
57085783 }
57095784 }
5785+ # ifdef DEBUG_TERMRESPONSE
5786+ {
5787+ char buf [100 ];
5788+
5789+ sprintf (buf , "Received XT %d: %s" , xt_index_in , (char * )name );
5790+ log_tr (buf );
5791+ }
5792+ # endif
57105793 if (key_names [i ] != NULL )
57115794 {
57125795 for (i = 8 ; (c = hexhex2nr (code + i )) >= 0 ; i += 2 )
@@ -5725,7 +5808,17 @@ got_code_from_term(code, len)
57255808 set_keep_msg_from_hist ();
57265809 set_color_count (i );
57275810 init_highlight (TRUE, FALSE);
5728- redraw_later (CLEAR );
5811+ #ifdef DEBUG_TERMRESPONSE
5812+ {
5813+ char buf [100 ];
5814+ int r = redraw_asap (CLEAR );
5815+
5816+ sprintf (buf , "Received t_Co, redraw_asap(): %d" , r );
5817+ log_tr (buf );
5818+ }
5819+ #else
5820+ redraw_asap (CLEAR );
5821+ #endif
57295822 }
57305823 }
57315824 else
0 commit comments