comparison src/term.c @ 12632:b1a7e3968a31 v8.0.1194

patch 8.0.1194: actual fg and bg colors of terminal are unknown commit https://github.com/vim/vim/commit/65e4c4f6868882a380c319632a1728a5e7d274ad Author: Bram Moolenaar <Bram@vim.org> Date: Sat Oct 14 23:24:25 2017 +0200 patch 8.0.1194: actual fg and bg colors of terminal are unknown Problem: Actual fg and bg colors of terminal are unknown. Solution: Add t_RF. Store response to t_RB and t_RF, use for terminal.
author Christian Brabandt <cb@256bit.org>
date Sat, 14 Oct 2017 23:30:05 +0200
parents 895da0cbb16e
children 94566ecb55f0
comparison
equal deleted inserted replaced
12631:3e8f6b06e414 12632:b1a7e3968a31
122 /* Request Terminal Version status: */ 122 /* Request Terminal Version status: */
123 static int crv_status = STATUS_GET; 123 static int crv_status = STATUS_GET;
124 124
125 /* Request Cursor position report: */ 125 /* Request Cursor position report: */
126 static int u7_status = STATUS_GET; 126 static int u7_status = STATUS_GET;
127
128 #ifdef FEAT_TERMINAL
129 /* Request foreground color report: */
130 static int rfg_status = STATUS_GET;
131 static int fg_r = 0;
132 static int fg_g = 0;
133 static int fg_b = 0;
134 static int bg_r = 255;
135 static int bg_g = 255;
136 static int bg_b = 255;
137 #endif
127 138
128 /* Request background color report: */ 139 /* Request background color report: */
129 static int rbg_status = STATUS_GET; 140 static int rbg_status = STATUS_GET;
130 141
131 /* Request cursor blinking mode report: */ 142 /* Request cursor blinking mode report: */
880 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")}, 891 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
881 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")}, 892 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
882 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")}, 893 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
883 # endif 894 # endif
884 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")}, 895 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
896 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
885 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")}, 897 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
886 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")}, 898 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
887 # ifdef FEAT_TERMGUICOLORS 899 # ifdef FEAT_TERMGUICOLORS
888 /* These are printf strings, not terminal codes. */ 900 /* These are printf strings, not terminal codes. */
889 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")}, 901 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
1183 {(int)KS_CWS, "[%dCWS%d]"}, 1195 {(int)KS_CWS, "[%dCWS%d]"},
1184 {(int)KS_CWP, "[%dCWP%d]"}, 1196 {(int)KS_CWP, "[%dCWP%d]"},
1185 # endif 1197 # endif
1186 {(int)KS_CRV, "[CRV]"}, 1198 {(int)KS_CRV, "[CRV]"},
1187 {(int)KS_U7, "[U7]"}, 1199 {(int)KS_U7, "[U7]"},
1200 {(int)KS_RFG, "[RFG]"},
1188 {(int)KS_RBG, "[RBG]"}, 1201 {(int)KS_RBG, "[RBG]"},
1189 {K_UP, "[KU]"}, 1202 {K_UP, "[KU]"},
1190 {K_DOWN, "[KD]"}, 1203 {K_DOWN, "[KD]"},
1191 {K_LEFT, "[KL]"}, 1204 {K_LEFT, "[KL]"},
1192 {K_RIGHT, "[KR]"}, 1205 {K_RIGHT, "[KR]"},
1606 {KS_CIS, "IS"}, {KS_CIE, "IE"}, 1619 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1607 {KS_CSC, "SC"}, {KS_CEC, "EC"}, 1620 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1608 {KS_TS, "ts"}, {KS_FS, "fs"}, 1621 {KS_TS, "ts"}, {KS_FS, "fs"},
1609 {KS_CWP, "WP"}, {KS_CWS, "WS"}, 1622 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1610 {KS_CSI, "SI"}, {KS_CEI, "EI"}, 1623 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1611 {KS_U7, "u7"}, {KS_RBG, "RB"}, 1624 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1612 {KS_8F, "8f"}, {KS_8B, "8b"}, 1625 {KS_8F, "8f"}, {KS_8B, "8b"},
1613 {KS_CBE, "BE"}, {KS_CBD, "BD"}, 1626 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1614 {KS_CPS, "PS"}, {KS_CPE, "PE"}, 1627 {KS_CPS, "PS"}, {KS_CPE, "PE"},
1615 {(enum SpecialKey)0, NULL} 1628 {(enum SpecialKey)0, NULL}
1616 }; 1629 };
3318 /* May need to check for T_CRV response and termcodes, it 3331 /* May need to check for T_CRV response and termcodes, it
3319 * doesn't work in Cooked mode, an external program may get 3332 * doesn't work in Cooked mode, an external program may get
3320 * them. */ 3333 * them. */
3321 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT 3334 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3322 || u7_status == STATUS_SENT 3335 || u7_status == STATUS_SENT
3336 #ifdef FEAT_TERMINAL
3337 || rfg_status == STATUS_SENT
3338 #endif
3323 || rbg_status == STATUS_SENT 3339 || rbg_status == STATUS_SENT
3324 || rbm_status == STATUS_SENT 3340 || rbm_status == STATUS_SENT
3325 || rcs_status == STATUS_SENT)) 3341 || rcs_status == STATUS_SENT))
3326 (void)vpeekc_nomap(); 3342 (void)vpeekc_nomap();
3327 check_for_codes_from_term(); 3343 check_for_codes_from_term();
3389 # endif 3405 # endif
3390 { 3406 {
3391 /* May need to discard T_CRV, T_U7 or T_RBG response. */ 3407 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3392 if (crv_status == STATUS_SENT 3408 if (crv_status == STATUS_SENT
3393 || u7_status == STATUS_SENT 3409 || u7_status == STATUS_SENT
3410 # ifdef FEAT_TERMINAL
3411 || rfg_status == STATUS_SENT
3412 # endif
3394 || rbg_status == STATUS_SENT 3413 || rbg_status == STATUS_SENT
3395 || rbm_status == STATUS_SENT 3414 || rbm_status == STATUS_SENT
3396 || rcs_status == STATUS_SENT) 3415 || rcs_status == STATUS_SENT)
3397 { 3416 {
3398 # ifdef UNIX 3417 # ifdef UNIX
3506 void 3525 void
3507 may_req_bg_color(void) 3526 may_req_bg_color(void)
3508 { 3527 {
3509 if (can_get_termresponse() && starting == 0) 3528 if (can_get_termresponse() && starting == 0)
3510 { 3529 {
3511 /* Only request background if t_RB is set and 'background' wasn't 3530 int didit = FALSE;
3512 * changed. */ 3531
3513 if (rbg_status == STATUS_GET 3532 #ifdef FEAT_TERMINAL
3514 && *T_RBG != NUL 3533 /* Only request foreground if t_RF is set. */
3515 && !option_was_set((char_u *)"bg")) 3534 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3535 {
3536 LOG_TR("Sending FG request");
3537 out_str(T_RFG);
3538 rfg_status = STATUS_SENT;
3539 didit = TRUE;
3540 }
3541 #endif
3542
3543 /* Only request background if t_RB is set. */
3544 if (rbg_status == STATUS_GET && *T_RBG != NUL)
3516 { 3545 {
3517 LOG_TR("Sending BG request"); 3546 LOG_TR("Sending BG request");
3518 out_str(T_RBG); 3547 out_str(T_RBG);
3519 rbg_status = STATUS_SENT; 3548 rbg_status = STATUS_SENT;
3520 3549 didit = TRUE;
3550 }
3551
3552 if (didit)
3553 {
3521 /* check for the characters now, otherwise they might be eaten by 3554 /* check for the characters now, otherwise they might be eaten by
3522 * get_keystroke() */ 3555 * get_keystroke() */
3523 out_flush(); 3556 out_flush();
3524 (void)vpeekc_nomap(); 3557 (void)vpeekc_nomap();
3525 } 3558 }
4686 return -1; 4719 return -1;
4687 } 4720 }
4688 } 4721 }
4689 } 4722 }
4690 4723
4691 /* Check for background color response from the terminal: 4724 /* Check for fore/background color response from the terminal:
4692 * 4725 *
4693 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail} 4726 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
4694 * 4727 *
4728 * {code} is 10 for foreground, 11 for background
4695 * {lead} can be <Esc>] or OSC 4729 * {lead} can be <Esc>] or OSC
4696 * {tail} can be '\007', <Esc>\ or STERM. 4730 * {tail} can be '\007', <Esc>\ or STERM.
4697 * 4731 *
4698 * Consume any code that starts with "{lead}11;", it's also 4732 * Consume any code that starts with "{lead}11;", it's also
4699 * possible that "rgba" is following. 4733 * possible that "rgba" is following.
4700 */ 4734 */
4701 else if (*T_RBG != NUL 4735 else if ((*T_RBG != NUL || *T_RFG != NUL)
4702 && ((tp[0] == ESC && len >= 2 && tp[1] == ']') 4736 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4703 || tp[0] == OSC)) 4737 || tp[0] == OSC))
4704 { 4738 {
4705 j = 1 + (tp[0] == ESC); 4739 j = 1 + (tp[0] == ESC);
4706 if (len >= j + 3 && (argp[0] != '1' 4740 if (len >= j + 3 && (argp[0] != '1'
4707 || argp[1] != '1' || argp[2] != ';')) 4741 || (argp[1] != '1' && argp[1] != '0')
4742 || argp[2] != ';'))
4708 i = 0; /* no match */ 4743 i = 0; /* no match */
4709 else 4744 else
4710 for (i = j; i < len; ++i) 4745 for (i = j; i < len; ++i)
4711 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM 4746 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4712 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\'))) 4747 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
4713 { 4748 {
4749 int is_bg = argp[1] == '1';
4750
4714 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0 4751 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4715 && tp[j + 11] == '/' && tp[j + 16] == '/' 4752 && tp[j + 11] == '/' && tp[j + 16] == '/')
4716 && !option_was_set((char_u *)"bg"))
4717 { 4753 {
4718 char *newval = (3 * '6' < tp[j+7] + tp[j+12] 4754 int rval = hexhex2nr(tp + j + 7);
4755 int gval = hexhex2nr(tp + j + 12);
4756 int bval = hexhex2nr(tp + j + 17);
4757
4758 if (is_bg)
4759 {
4760 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4719 + tp[j+17]) ? "light" : "dark"; 4761 + tp[j+17]) ? "light" : "dark";
4720 4762
4721 LOG_TR("Received RBG response"); 4763 LOG_TR("Received RBG response");
4722 rbg_status = STATUS_GOT; 4764 rbg_status = STATUS_GOT;
4723 if (STRCMP(p_bg, newval) != 0) 4765 #ifdef FEAT_TERMINAL
4766 bg_r = rval;
4767 bg_g = gval;
4768 bg_b = bval;
4769 #endif
4770 if (!option_was_set((char_u *)"bg")
4771 && STRCMP(p_bg, newval) != 0)
4772 {
4773 /* value differs, apply it */
4774 set_option_value((char_u *)"bg", 0L,
4775 (char_u *)newval, 0);
4776 reset_option_was_set((char_u *)"bg");
4777 redraw_asap(CLEAR);
4778 }
4779 }
4780 #ifdef FEAT_TERMINAL
4781 else
4724 { 4782 {
4725 /* value differs, apply it */ 4783 LOG_TR("Received RFG response");
4726 set_option_value((char_u *)"bg", 0L, 4784 rfg_status = STATUS_GOT;
4727 (char_u *)newval, 0); 4785 fg_r = rval;
4728 reset_option_was_set((char_u *)"bg"); 4786 fg_g = gval;
4729 redraw_asap(CLEAR); 4787 fg_b = bval;
4730 } 4788 }
4789 #endif
4731 } 4790 }
4732 4791
4733 /* got finished code: consume it */ 4792 /* got finished code: consume it */
4734 key_name[0] = (int)KS_EXTRA; 4793 key_name[0] = (int)KS_EXTRA;
4735 key_name[1] = (int)KE_IGNORE; 4794 key_name[1] = (int)KE_IGNORE;
4736 slen = i + 1 + (tp[i] == ESC); 4795 slen = i + 1 + (tp[i] == ESC);
4737 # ifdef FEAT_EVAL 4796 # ifdef FEAT_EVAL
4738 set_vim_var_string(VV_TERMRGBRESP, tp, slen); 4797 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4798 : VV_TERMRFGRESP, tp, slen);
4739 # endif 4799 # endif
4740 break; 4800 break;
4741 } 4801 }
4742 if (i == len) 4802 if (i == len)
4743 { 4803 {
5766 #endif 5826 #endif
5767 5827
5768 return 0; /* no match found */ 5828 return 0; /* no match found */
5769 } 5829 }
5770 5830
5831 #if defined(FEAT_TERMINAL) || defined(PROTO)
5832 /*
5833 * Get the text foreground color, if known.
5834 */
5835 void
5836 term_get_fg_color(uint8_t *r, uint8_t *g, uint8_t *b)
5837 {
5838 if (rfg_status == STATUS_GOT)
5839 {
5840 *r = fg_r;
5841 *g = fg_g;
5842 *b = fg_b;
5843 }
5844 }
5845
5846 /*
5847 * Get the text background color, if known.
5848 */
5849 void
5850 term_get_bg_color(uint8_t *r, uint8_t *g, uint8_t *b)
5851 {
5852 if (rbg_status == STATUS_GOT)
5853 {
5854 *r = bg_r;
5855 *g = bg_g;
5856 *b = bg_b;
5857 }
5858 }
5859 #endif
5860
5771 /* 5861 /*
5772 * Replace any terminal code strings in from[] with the equivalent internal 5862 * Replace any terminal code strings in from[] with the equivalent internal
5773 * vim representation. This is used for the "from" and "to" part of a 5863 * vim representation. This is used for the "from" and "to" part of a
5774 * mapping, and the "to" part of a menu command. 5864 * mapping, and the "to" part of a menu command.
5775 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains 5865 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains