@@ -232,7 +232,8 @@ static guicolor_T store_console_bg_rgb;
232232static guicolor_T store_console_fg_rgb ;
233233static int default_console_color_bg = 0x000000 ; // black
234234static int default_console_color_fg = 0xc0c0c0 ; // white
235- # define USE_VTP (vtp_working && is_term_win32())
235+ # define USE_VTP (vtp_working && is_term_win32() \
236+ && (p_tgc || t_colors >= 256))
236237# define USE_WT (wt_working)
237238# else
238239# define USE_VTP 0
@@ -3463,6 +3464,9 @@ mch_init_c(void)
34633464 ui_get_shellsize ();
34643465
34653466 vtp_init ();
3467+ // Switch to a new alternate screen buffer.
3468+ if (use_alternate_screen_buffer )
3469+ vtp_printf ("\033[?1049h" );
34663470
34673471# ifdef MCH_WRITE_DUMP
34683472 fdDump = fopen ("dump" , "wt" );
@@ -4437,6 +4441,9 @@ ResizeConBuf(
44374441 HANDLE hConsole ,
44384442 COORD coordScreen )
44394443{
4444+ if (use_alternate_screen_buffer )
4445+ return ;
4446+
44404447 if (!SetConsoleScreenBufferSize (hConsole , coordScreen ))
44414448 {
44424449# ifdef MCH_WRITE_DUMP
@@ -6189,12 +6196,6 @@ termcap_mode_start(void)
61896196 if (g_fTermcapMode )
61906197 return ;
61916198
6192- // VTP uses alternate screen buffer.
6193- // Switch to a new alternate screen buffer.
6194- // But, not if running in a nested terminal
6195- if (use_alternate_screen_buffer )
6196- vtp_printf ("\033[?1049h" );
6197-
61986199 SaveConsoleBuffer (& g_cbNonTermcap );
61996200
62006201 if (g_cbTermcap .IsValid )
@@ -6273,7 +6274,6 @@ termcap_mode_end(void)
62736274 RestoreConsoleBuffer (cb , p_rs );
62746275 restore_console_color_rgb ();
62756276
6276- // VTP uses alternate screen buffer.
62776277 // Switch back to main screen buffer.
62786278 if (exiting && use_alternate_screen_buffer )
62796279 vtp_printf ("\033[?1049l" );
@@ -6285,9 +6285,8 @@ termcap_mode_end(void)
62856285 */
62866286 coord .X = 0 ;
62876287 coord .Y = (SHORT ) (p_rs ? cb -> Info .dwCursorPosition .Y : (Rows - 1 ));
6288- if (!vtp_working )
6289- FillConsoleOutputCharacter (g_hConOut , ' ' ,
6290- cb -> Info .dwSize .X , coord , & dwDummy );
6288+ FillConsoleOutputCharacter (g_hConOut , ' ' ,
6289+ cb -> Info .dwSize .X , coord , & dwDummy );
62916290 /*
62926291 * The following is just for aesthetics. If we are exiting without
62936292 * restoring the screen, then we want to have a prompt string
@@ -6497,11 +6496,7 @@ insert_lines(unsigned cLines)
64976496 clip .Bottom = g_srScrollRegion .Bottom ;
64986497
64996498 fill .Char .AsciiChar = ' ' ;
6500- if (!(vtp_working
6501- # ifdef FEAT_TERMGUICOLORS
6502- && (p_tgc || t_colors >= 256 )
6503- # endif
6504- ))
6499+ if (!USE_VTP )
65056500 fill .Attributes = g_attrCurrent ;
65066501 else
65076502 fill .Attributes = g_attrDefault ;
@@ -6625,11 +6620,7 @@ gotoxy(
66256620 if (x < 1 || x > (unsigned )Columns || y < 1 || y > (unsigned )Rows )
66266621 return ;
66276622
6628- if (!(vtp_working
6629- # ifdef FEAT_TERMGUICOLORS
6630- && (p_tgc || t_colors >= 256 )
6631- # endif
6632- ))
6623+ if (!USE_VTP )
66336624 {
66346625 // There are reports of double-width characters not displayed
66356626 // correctly. This workaround should fix it, similar to how it's done
@@ -6883,11 +6874,7 @@ write_chars(
68836874 }
68846875 }
68856876
6886- if (!(vtp_working
6887- # ifdef FEAT_TERMGUICOLORS
6888- && (p_tgc || t_colors >= 256 )
6889- # endif
6890- ))
6877+ if (!USE_VTP )
68916878 {
68926879 FillConsoleOutputAttribute (g_hConOut , g_attrCurrent , cells ,
68936880 coord , & written );
@@ -6927,11 +6914,7 @@ write_chars(
69276914 }
69286915
69296916 // Cursor under VTP is always in the correct position, no need to reset.
6930- if (!(vtp_working
6931- # ifdef FEAT_TERMGUICOLORS
6932- && (p_tgc || t_colors >= 256 )
6933- # endif
6934- ))
6917+ if (!USE_VTP )
69356918 gotoxy (g_coord .X + 1 , g_coord .Y + 1 );
69366919
69376920 return written ;
@@ -7227,11 +7210,7 @@ mch_write(
72277210 normvideo ();
72287211 else if (argc == 1 )
72297212 {
7230- if (vtp_working
7231- # ifdef FEAT_TERMGUICOLORS
7232- && (p_tgc || t_colors >= 256 )
7233- # endif
7234- )
7213+ if (USE_VTP )
72357214 textcolor ((WORD )arg1 );
72367215 else
72377216 textattr ((WORD )arg1 );
@@ -8440,6 +8419,11 @@ vtp_flag_init(void)
84408419 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING );
84418420 if (SetConsoleMode (out , mode ) == 0 )
84428421 vtp_working = 0 ;
8422+
8423+ // VTP uses alternate screen buffer.
8424+ // But, not if running in a nested terminal
8425+ use_alternate_screen_buffer = win10_22H2_or_later && p_rs && vtp_working
8426+ && !mch_getenv ("VIM_TERMINAL" );
84438427 }
84448428#endif
84458429
@@ -8485,8 +8469,6 @@ vtp_init(void)
84858469 fg = (GetRValue (fg ) << 16 ) | (GetGValue (fg ) << 8 ) | GetBValue (fg );
84868470 default_console_color_fg = fg ;
84878471# endif
8488- use_alternate_screen_buffer = win10_22H2_or_later && p_rs && vtp_working
8489- && !mch_getenv ("VIM_TERMINAL" );
84908472 set_console_color_rgb ();
84918473}
84928474
@@ -8707,6 +8689,9 @@ set_console_color_rgb(void)
87078689 return ;
87088690 }
87098691
8692+ if (use_alternate_screen_buffer )
8693+ return ;
8694+
87108695 fg = (GetRValue (fg ) << 16 ) | (GetGValue (fg ) << 8 ) | GetBValue (fg );
87118696 bg = (GetRValue (bg ) << 16 ) | (GetGValue (bg ) << 8 ) | GetBValue (bg );
87128697
@@ -8781,6 +8766,8 @@ get_default_console_color(
87818766reset_console_color_rgb (void )
87828767{
87838768# ifdef FEAT_TERMGUICOLORS
8769+ if (use_alternate_screen_buffer )
8770+ return ;
87848771
87858772 CONSOLE_SCREEN_BUFFER_INFOEX csbi ;
87868773
@@ -8803,6 +8790,8 @@ reset_console_color_rgb(void)
88038790restore_console_color_rgb (void )
88048791{
88058792# ifdef FEAT_TERMGUICOLORS
8793+ if (use_alternate_screen_buffer )
8794+ return ;
88068795
88078796 CONSOLE_SCREEN_BUFFER_INFOEX csbi ;
88088797
@@ -8875,6 +8864,9 @@ get_conpty_fix_type(void)
88758864 void
88768865resize_console_buf (void )
88778866{
8867+ if (use_alternate_screen_buffer )
8868+ return ;
8869+
88788870 CONSOLE_SCREEN_BUFFER_INFO csbi ;
88798871 COORD coord ;
88808872 SMALL_RECT newsize ;
0 commit comments