3333 * while, if the terminal window is visible, the screen contents is drawn.
3434 *
3535 * TODO:
36+ * - color for GUI
37+ * - color for 'termguicolors'
38+ * - cursor flickers when moving the cursor
3639 * - set buffer options to be scratch, hidden, nomodifiable, etc.
3740 * - set buffer name to command, add (1) to avoid duplicates.
3841 * - Add a scrollback buffer (contains lines to scroll off the top).
@@ -586,6 +589,154 @@ handle_resize(int rows, int cols, void *user)
586589 return 1 ;
587590}
588591
592+ /*
593+ * Reverse engineer the RGB value into a cterm color index.
594+ * First color is 1. Return 0 if no match found.
595+ */
596+ static int
597+ color2index (VTermColor * color )
598+ {
599+ int red = color -> red ;
600+ int blue = color -> blue ;
601+ int green = color -> green ;
602+
603+ if (red == 0 )
604+ {
605+ if (green == 0 )
606+ {
607+ if (blue == 0 )
608+ return 1 ; /* black */
609+ if (blue == 224 )
610+ return 5 ; /* blue */
611+ }
612+ else if (green == 224 )
613+ {
614+ if (blue == 0 )
615+ return 3 ; /* green */
616+ if (blue == 224 )
617+ return 7 ; /* cyan */
618+ }
619+ }
620+ else if (red == 224 )
621+ {
622+ if (green == 0 )
623+ {
624+ if (blue == 0 )
625+ return 2 ; /* red */
626+ if (blue == 224 )
627+ return 6 ; /* magenta */
628+ }
629+ else if (green == 224 )
630+ {
631+ if (blue == 0 )
632+ return 4 ; /* yellow */
633+ if (blue == 224 )
634+ return 8 ; /* white */
635+ }
636+ }
637+ else if (red == 128 )
638+ {
639+ if (green == 128 && blue == 128 )
640+ return 9 ; /* high intensity bladk */
641+ }
642+ else if (red == 255 )
643+ {
644+ if (green == 64 )
645+ {
646+ if (blue == 64 )
647+ return 10 ; /* high intensity red */
648+ if (blue == 255 )
649+ return 14 ; /* high intensity magenta */
650+ }
651+ else if (green == 255 )
652+ {
653+ if (blue == 64 )
654+ return 12 ; /* high intensity yellow */
655+ if (blue == 255 )
656+ return 16 ; /* high intensity white */
657+ }
658+ }
659+ else if (red == 64 )
660+ {
661+ if (green == 64 )
662+ {
663+ if (blue == 255 )
664+ return 13 ; /* high intensity blue */
665+ }
666+ else if (green == 255 )
667+ {
668+ if (blue == 64 )
669+ return 11 ; /* high intensity green */
670+ if (blue == 255 )
671+ return 15 ; /* high intensity cyan */
672+ }
673+ }
674+ if (t_colors >= 256 )
675+ {
676+ if (red == blue && red == green )
677+ {
678+ /* 24-color greyscale */
679+ static int cutoff [23 ] = {
680+ 0x05 , 0x10 , 0x1B , 0x26 , 0x31 , 0x3C , 0x47 , 0x52 ,
681+ 0x5D , 0x68 , 0x73 , 0x7F , 0x8A , 0x95 , 0xA0 , 0xAB ,
682+ 0xB6 , 0xC1 , 0xCC , 0xD7 , 0xE2 , 0xED , 0xF9 };
683+ int i ;
684+
685+ for (i = 0 ; i < 23 ; ++ i )
686+ if (red < cutoff [i ])
687+ return i + 233 ;
688+ return 256 ;
689+ }
690+
691+ /* 216-color cube */
692+ return 17 + ((red + 25 ) / 0x33 ) * 36
693+ + ((green + 25 ) / 0x33 ) * 6
694+ + (blue + 25 ) / 0x33 ;
695+ }
696+ return 0 ;
697+ }
698+
699+ /*
700+ * Convert the attributes of a vterm cell into an attribute index.
701+ */
702+ static int
703+ cell2attr (VTermScreenCell * cell )
704+ {
705+ int attr = 0 ;
706+
707+ if (cell -> attrs .bold )
708+ attr |= HL_BOLD ;
709+ if (cell -> attrs .underline )
710+ attr |= HL_UNDERLINE ;
711+ if (cell -> attrs .italic )
712+ attr |= HL_ITALIC ;
713+ if (cell -> attrs .strike )
714+ attr |= HL_STANDOUT ;
715+ if (cell -> attrs .reverse )
716+ attr |= HL_INVERSE ;
717+ if (cell -> attrs .strike )
718+ attr |= HL_UNDERLINE ;
719+
720+ #ifdef FEAT_GUI
721+ if (gui .in_use )
722+ {
723+ /* TODO */
724+ }
725+ else
726+ #endif
727+ #ifdef FEAT_TERMGUICOLORS
728+ if (p_tgc )
729+ {
730+ /* TODO */
731+ }
732+ #endif
733+ {
734+ return get_cterm_attr_idx (attr , color2index (& cell -> fg ),
735+ color2index (& cell -> bg ));
736+ }
737+ return 0 ;
738+ }
739+
589740/*
590741 * Called to update the window that contains the terminal.
591742 */
@@ -648,7 +799,10 @@ term_update_window(win_T *wp)
648799 VTermScreenCell cell ;
649800 int c ;
650801
651- vterm_screen_get_cell (screen , pos , & cell );
802+ if (vterm_screen_get_cell (screen , pos , & cell ) == 0 )
803+ vim_memset (& cell , 0 , sizeof (cell ));
804+
805+ /* TODO: composing chars */
652806 c = cell .chars [0 ];
653807 if (c == NUL )
654808 {
@@ -672,8 +826,7 @@ term_update_window(win_T *wp)
672826 ScreenLines [off ] = c ;
673827#endif
674828 }
675- /* TODO: use cell.attrs and colors */
676- ScreenAttrs [off ] = 0 ;
829+ ScreenAttrs [off ] = cell2attr (& cell );
677830
678831 ++ pos .col ;
679832 ++ off ;
@@ -731,6 +884,18 @@ create_vterm(term_T *term, int rows, int cols)
731884 vterm_screen_set_callbacks (screen , & screen_callbacks , term );
732885 /* TODO: depends on 'encoding'. */
733886 vterm_set_utf8 (vterm , 1 );
887+
888+ /* Vterm uses a default black background. Set it to white when
889+ * 'background' is "light". */
890+ if (* p_bg == 'l' )
891+ {
892+ VTermColor fg , bg ;
893+
894+ fg .red = fg .green = fg .blue = 0 ;
895+ bg .red = bg .green = bg .blue = 255 ;
896+ vterm_state_set_default_colors (vterm_obtain_state (vterm ), & fg , & bg );
897+ }
898+
734899 /* Required to initialize most things. */
735900 vterm_screen_reset (screen , 1 /* hard */ );
736901}
0 commit comments