1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 /* 10 * 11 * term.c: functions for controlling the terminal 12 * 13 * primitive termcap support for Amiga and Win32 included 14 * 15 * NOTE: padding and variable substitution is not performed, 16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies. 17 */ 18 19 /* 20 * Some systems have a prototype for tgetstr() with (char *) instead of 21 * (char **). This define removes that prototype. We include our own prototype 22 * below. 23 */ 24 #define tgetstr tgetstr_defined_wrong 25 26 #include "vim.h" 27 28 #ifdef HAVE_TGETENT 29 # ifdef HAVE_TERMIOS_H 30 # include <termios.h> // seems to be required for some Linux 31 # endif 32 # ifdef HAVE_TERMCAP_H 33 # include <termcap.h> 34 # endif 35 36 /* 37 * A few linux systems define outfuntype in termcap.h to be used as the third 38 * argument for tputs(). 39 */ 40 # ifdef VMS 41 # define TPUTSFUNCAST 42 # else 43 # ifdef HAVE_OUTFUNTYPE 44 # define TPUTSFUNCAST (outfuntype) 45 # else 46 # define TPUTSFUNCAST (int (*)()) 47 # endif 48 # endif 49 #endif 50 51 #undef tgetstr 52 53 /* 54 * Here are the builtin termcap entries. They are not stored as complete 55 * structures with all entries, as such a structure is too big. 56 * 57 * The entries are compact, therefore they normally are included even when 58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries 59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc. 60 * 61 * Each termcap is a list of builtin_term structures. It always starts with 62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all 63 * details. 64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code. 65 * 66 * Entries marked with "guessed" may be wrong. 67 */ 68 struct builtin_term 69 { 70 int bt_entry; 71 char *bt_string; 72 }; 73 74 // start of keys that are not directly used by Vim but can be mapped 75 #define BT_EXTRA_KEYS 0x101 76 77 static void parse_builtin_tcap(char_u *s); 78 static void gather_termleader(void); 79 #ifdef FEAT_TERMRESPONSE 80 static void req_codes_from_term(void); 81 static void req_more_codes_from_term(void); 82 static void got_code_from_term(char_u *code, int len); 83 static void check_for_codes_from_term(void); 84 #endif 85 static void del_termcode_idx(int idx); 86 static int find_term_bykeys(char_u *src); 87 static int term_is_builtin(char_u *name); 88 static int term_7to8bit(char_u *p); 89 90 #ifdef HAVE_TGETENT 91 static char *tgetent_error(char_u *, char_u *); 92 93 /* 94 * Here is our own prototype for tgetstr(), any prototypes from the include 95 * files have been disabled by the define at the start of this file. 96 */ 97 char *tgetstr(char *, char **); 98 99 # ifdef FEAT_TERMRESPONSE 100 // Change this to "if 1" to debug what happens with termresponse. 101 # if 0 102 # define DEBUG_TERMRESPONSE 103 static void log_tr(const char *fmt, ...); 104 # define LOG_TR(msg) log_tr msg 105 # else 106 # define LOG_TR(msg) do { /**/ } while (0) 107 # endif 108 109 typedef enum { 110 STATUS_GET, // send request when switching to RAW mode 111 STATUS_SENT, // did send request, checking for response 112 STATUS_GOT, // received response 113 STATUS_FAIL // timed out 114 } request_progress_T; 115 116 typedef struct { 117 request_progress_T tr_progress; 118 time_t tr_start; // when request was sent, -1 for never 119 } termrequest_T; 120 121 # define TERMREQUEST_INIT {STATUS_GET, -1} 122 123 // Request Terminal Version status: 124 static termrequest_T crv_status = TERMREQUEST_INIT; 125 126 // Request Cursor position report: 127 static termrequest_T u7_status = TERMREQUEST_INIT; 128 129 # ifdef FEAT_TERMINAL 130 // Request foreground color report: 131 static termrequest_T rfg_status = TERMREQUEST_INIT; 132 static int fg_r = 0; 133 static int fg_g = 0; 134 static int fg_b = 0; 135 static int bg_r = 255; 136 static int bg_g = 255; 137 static int bg_b = 255; 138 # endif 139 140 // Request background color report: 141 static termrequest_T rbg_status = TERMREQUEST_INIT; 142 143 // Request cursor blinking mode report: 144 static termrequest_T rbm_status = TERMREQUEST_INIT; 145 146 // Request cursor style report: 147 static termrequest_T rcs_status = TERMREQUEST_INIT; 148 149 // Request window's position report: 150 static termrequest_T winpos_status = TERMREQUEST_INIT; 151 152 static termrequest_T *all_termrequests[] = { 153 &crv_status, 154 &u7_status, 155 # ifdef FEAT_TERMINAL 156 &rfg_status, 157 # endif 158 &rbg_status, 159 &rbm_status, 160 &rcs_status, 161 &winpos_status, 162 NULL 163 }; 164 # endif 165 166 /* 167 * Don't declare these variables if termcap.h contains them. 168 * Autoconf checks if these variables should be declared extern (not all 169 * systems have them). 170 * Some versions define ospeed to be speed_t, but that is incompatible with 171 * BSD, where ospeed is short and speed_t is long. 172 */ 173 # ifndef HAVE_OSPEED 174 # ifdef OSPEED_EXTERN 175 extern short ospeed; 176 # else 177 short ospeed; 178 # endif 179 # endif 180 # ifndef HAVE_UP_BC_PC 181 # ifdef UP_BC_PC_EXTERN 182 extern char *UP, *BC, PC; 183 # else 184 char *UP, *BC, PC; 185 # endif 186 # endif 187 188 # define TGETSTR(s, p) vim_tgetstr((s), (p)) 189 # define TGETENT(b, t) tgetent((char *)(b), (char *)(t)) 190 static char_u *vim_tgetstr(char *s, char_u **pp); 191 #endif // HAVE_TGETENT 192 193 static int detected_8bit = FALSE; // detected 8-bit terminal 194 195 #ifdef FEAT_TERMRESPONSE 196 // When the cursor shape was detected these values are used: 197 // 1: block, 2: underline, 3: vertical bar 198 static int initial_cursor_shape = 0; 199 200 // The blink flag from the style response may be inverted from the actual 201 // blinking state, xterm XORs the flags. 202 static int initial_cursor_shape_blink = FALSE; 203 204 // The blink flag from the blinking-cursor mode response 205 static int initial_cursor_blink = FALSE; 206 #endif 207 208 static struct builtin_term builtin_termcaps[] = 209 { 210 211 #if defined(FEAT_GUI) 212 /* 213 * GUI pseudo term-cap. 214 */ 215 {(int)KS_NAME, "gui"}, 216 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")}, 217 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")}, 218 # ifdef TERMINFO 219 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")}, 220 # else 221 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")}, 222 # endif 223 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")}, 224 # ifdef TERMINFO 225 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")}, 226 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")}, 227 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")}, 228 # else 229 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")}, 230 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")}, 231 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")}, 232 # endif 233 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")}, 234 // attributes switched on with 'h', off with * 'H' 235 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, // HL_ALL 236 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, // HL_INVERSE 237 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, // HL_BOLD 238 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, // HL_STANDOUT 239 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, // HL_STANDOUT 240 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, // HL_UNDERLINE 241 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, // HL_UNDERLINE 242 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, // HL_UNDERCURL 243 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, // HL_UNDERCURL 244 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, // HL_STRIKETHROUGH 245 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, // HL_STRIKETHROUGH 246 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, // HL_ITALIC 247 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, // HL_ITALIC 248 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")}, 249 {(int)KS_MS, "y"}, 250 {(int)KS_UT, "y"}, 251 {(int)KS_XN, "y"}, 252 {(int)KS_LE, "\b"}, // cursor-left = BS 253 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L 254 # ifdef TERMINFO 255 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")}, 256 # else 257 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")}, 258 # endif 259 // there are no key sequences here, the GUI sequences are recognized 260 // in check_termcode() 261 #endif 262 263 #ifndef NO_BUILTIN_TCAPS 264 265 # if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS) 266 /* 267 * Amiga console window, default for Amiga 268 */ 269 {(int)KS_NAME, "amiga"}, 270 {(int)KS_CE, "\033[K"}, 271 {(int)KS_CD, "\033[J"}, 272 {(int)KS_AL, "\033[L"}, 273 # ifdef TERMINFO 274 {(int)KS_CAL, "\033[%p1%dL"}, 275 # else 276 {(int)KS_CAL, "\033[%dL"}, 277 # endif 278 {(int)KS_DL, "\033[M"}, 279 # ifdef TERMINFO 280 {(int)KS_CDL, "\033[%p1%dM"}, 281 # else 282 {(int)KS_CDL, "\033[%dM"}, 283 # endif 284 {(int)KS_CL, "\014"}, 285 {(int)KS_VI, "\033[0 p"}, 286 {(int)KS_VE, "\033[1 p"}, 287 {(int)KS_ME, "\033[0m"}, 288 {(int)KS_MR, "\033[7m"}, 289 {(int)KS_MD, "\033[1m"}, 290 {(int)KS_SE, "\033[0m"}, 291 {(int)KS_SO, "\033[33m"}, 292 {(int)KS_US, "\033[4m"}, 293 {(int)KS_UE, "\033[0m"}, 294 {(int)KS_CZH, "\033[3m"}, 295 {(int)KS_CZR, "\033[0m"}, 296 #if defined(__MORPHOS__) || defined(__AROS__) 297 {(int)KS_CCO, "8"}, // allow 8 colors 298 # ifdef TERMINFO 299 {(int)KS_CAB, "\033[4%p1%dm"},// set background color 300 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color 301 # else 302 {(int)KS_CAB, "\033[4%dm"}, // set background color 303 {(int)KS_CAF, "\033[3%dm"}, // set foreground color 304 # endif 305 {(int)KS_OP, "\033[m"}, // reset colors 306 #endif 307 {(int)KS_MS, "y"}, 308 {(int)KS_UT, "y"}, // guessed 309 {(int)KS_LE, "\b"}, 310 # ifdef TERMINFO 311 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"}, 312 # else 313 {(int)KS_CM, "\033[%i%d;%dH"}, 314 # endif 315 #if defined(__MORPHOS__) 316 {(int)KS_SR, "\033M"}, 317 #endif 318 # ifdef TERMINFO 319 {(int)KS_CRI, "\033[%p1%dC"}, 320 # else 321 {(int)KS_CRI, "\033[%dC"}, 322 # endif 323 {K_UP, "\233A"}, 324 {K_DOWN, "\233B"}, 325 {K_LEFT, "\233D"}, 326 {K_RIGHT, "\233C"}, 327 {K_S_UP, "\233T"}, 328 {K_S_DOWN, "\233S"}, 329 {K_S_LEFT, "\233 A"}, 330 {K_S_RIGHT, "\233 @"}, 331 {K_S_TAB, "\233Z"}, 332 {K_F1, "\233\060~"},// some compilers don't dig "\2330" 333 {K_F2, "\233\061~"}, 334 {K_F3, "\233\062~"}, 335 {K_F4, "\233\063~"}, 336 {K_F5, "\233\064~"}, 337 {K_F6, "\233\065~"}, 338 {K_F7, "\233\066~"}, 339 {K_F8, "\233\067~"}, 340 {K_F9, "\233\070~"}, 341 {K_F10, "\233\071~"}, 342 {K_S_F1, "\233\061\060~"}, 343 {K_S_F2, "\233\061\061~"}, 344 {K_S_F3, "\233\061\062~"}, 345 {K_S_F4, "\233\061\063~"}, 346 {K_S_F5, "\233\061\064~"}, 347 {K_S_F6, "\233\061\065~"}, 348 {K_S_F7, "\233\061\066~"}, 349 {K_S_F8, "\233\061\067~"}, 350 {K_S_F9, "\233\061\070~"}, 351 {K_S_F10, "\233\061\071~"}, 352 {K_HELP, "\233?~"}, 353 {K_INS, "\233\064\060~"}, // 101 key keyboard 354 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard 355 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard 356 {K_HOME, "\233\064\064~"}, // 101 key keyboard 357 {K_END, "\233\064\065~"}, // 101 key keyboard 358 359 {BT_EXTRA_KEYS, ""}, 360 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key 361 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key 362 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key 363 # endif 364 365 # if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS) 366 /* 367 * almost standard ANSI terminal, default for bebox 368 */ 369 {(int)KS_NAME, "beos-ansi"}, 370 {(int)KS_CE, "\033[K"}, 371 {(int)KS_CD, "\033[J"}, 372 {(int)KS_AL, "\033[L"}, 373 # ifdef TERMINFO 374 {(int)KS_CAL, "\033[%p1%dL"}, 375 # else 376 {(int)KS_CAL, "\033[%dL"}, 377 # endif 378 {(int)KS_DL, "\033[M"}, 379 # ifdef TERMINFO 380 {(int)KS_CDL, "\033[%p1%dM"}, 381 # else 382 {(int)KS_CDL, "\033[%dM"}, 383 # endif 384 #ifdef BEOS_PR_OR_BETTER 385 # ifdef TERMINFO 386 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"}, 387 # else 388 {(int)KS_CS, "\033[%i%d;%dr"}, // scroll region 389 # endif 390 #endif 391 {(int)KS_CL, "\033[H\033[2J"}, 392 #ifdef notyet 393 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l 394 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h 395 #endif 396 {(int)KS_ME, "\033[m"}, // normal mode 397 {(int)KS_MR, "\033[7m"}, // reverse 398 {(int)KS_MD, "\033[1m"}, // bold 399 {(int)KS_SO, "\033[31m"}, // standout mode: red 400 {(int)KS_SE, "\033[m"}, // standout end 401 {(int)KS_CZH, "\033[35m"}, // italic: purple 402 {(int)KS_CZR, "\033[m"}, // italic end 403 {(int)KS_US, "\033[4m"}, // underscore mode 404 {(int)KS_UE, "\033[m"}, // underscore end 405 {(int)KS_CCO, "8"}, // allow 8 colors 406 # ifdef TERMINFO 407 {(int)KS_CAB, "\033[4%p1%dm"},// set background color 408 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color 409 # else 410 {(int)KS_CAB, "\033[4%dm"}, // set background color 411 {(int)KS_CAF, "\033[3%dm"}, // set foreground color 412 # endif 413 {(int)KS_OP, "\033[m"}, // reset colors 414 {(int)KS_MS, "y"}, // safe to move cur in reverse mode 415 {(int)KS_UT, "y"}, // guessed 416 {(int)KS_LE, "\b"}, 417 # ifdef TERMINFO 418 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"}, 419 # else 420 {(int)KS_CM, "\033[%i%d;%dH"}, 421 # endif 422 {(int)KS_SR, "\033M"}, 423 # ifdef TERMINFO 424 {(int)KS_CRI, "\033[%p1%dC"}, 425 # else 426 {(int)KS_CRI, "\033[%dC"}, 427 # endif 428 # if defined(BEOS_DR8) 429 {(int)KS_DB, ""}, // hack! see screen.c 430 # endif 431 432 {K_UP, "\033[A"}, 433 {K_DOWN, "\033[B"}, 434 {K_LEFT, "\033[D"}, 435 {K_RIGHT, "\033[C"}, 436 # endif 437 438 # if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) 439 /* 440 * standard ANSI terminal, default for unix 441 */ 442 {(int)KS_NAME, "ansi"}, 443 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")}, 444 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")}, 445 # ifdef TERMINFO 446 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")}, 447 # else 448 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")}, 449 # endif 450 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")}, 451 # ifdef TERMINFO 452 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")}, 453 # else 454 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")}, 455 # endif 456 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")}, 457 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")}, 458 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")}, 459 {(int)KS_MS, "y"}, 460 {(int)KS_UT, "y"}, // guessed 461 {(int)KS_LE, "\b"}, 462 # ifdef TERMINFO 463 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")}, 464 # else 465 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")}, 466 # endif 467 # ifdef TERMINFO 468 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")}, 469 # else 470 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")}, 471 # endif 472 # endif 473 474 # if defined(ALL_BUILTIN_TCAPS) 475 /* 476 * These codes are valid when nansi.sys or equivalent has been installed. 477 * Function keys on a PC are preceded with a NUL. These are converted into 478 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes. 479 * CTRL-arrow is used instead of SHIFT-arrow. 480 */ 481 {(int)KS_NAME, "pcansi"}, 482 {(int)KS_DL, "\033[M"}, 483 {(int)KS_AL, "\033[L"}, 484 {(int)KS_CE, "\033[K"}, 485 {(int)KS_CL, "\033[2J"}, 486 {(int)KS_ME, "\033[0m"}, 487 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey 488 {(int)KS_MD, "\033[1m"}, // bold: white text 489 {(int)KS_SE, "\033[0m"}, // standout end 490 {(int)KS_SO, "\033[31m"}, // standout: white on blue 491 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow 492 {(int)KS_CZR, "\033[0m"}, // italic mode end 493 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red 494 {(int)KS_UE, "\033[0m"}, // underscore mode end 495 {(int)KS_CCO, "8"}, // allow 8 colors 496 # ifdef TERMINFO 497 {(int)KS_CAB, "\033[4%p1%dm"},// set background color 498 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color 499 # else 500 {(int)KS_CAB, "\033[4%dm"}, // set background color 501 {(int)KS_CAF, "\033[3%dm"}, // set foreground color 502 # endif 503 {(int)KS_OP, "\033[0m"}, // reset colors 504 {(int)KS_MS, "y"}, 505 {(int)KS_UT, "y"}, // guessed 506 {(int)KS_LE, "\b"}, 507 # ifdef TERMINFO 508 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"}, 509 # else 510 {(int)KS_CM, "\033[%i%d;%dH"}, 511 # endif 512 # ifdef TERMINFO 513 {(int)KS_CRI, "\033[%p1%dC"}, 514 # else 515 {(int)KS_CRI, "\033[%dC"}, 516 # endif 517 {K_UP, "\316H"}, 518 {K_DOWN, "\316P"}, 519 {K_LEFT, "\316K"}, 520 {K_RIGHT, "\316M"}, 521 {K_S_LEFT, "\316s"}, 522 {K_S_RIGHT, "\316t"}, 523 {K_F1, "\316;"}, 524 {K_F2, "\316<"}, 525 {K_F3, "\316="}, 526 {K_F4, "\316>"}, 527 {K_F5, "\316?"}, 528 {K_F6, "\316@"}, 529 {K_F7, "\316A"}, 530 {K_F8, "\316B"}, 531 {K_F9, "\316C"}, 532 {K_F10, "\316D"}, 533 {K_F11, "\316\205"}, // guessed 534 {K_F12, "\316\206"}, // guessed 535 {K_S_F1, "\316T"}, 536 {K_S_F2, "\316U"}, 537 {K_S_F3, "\316V"}, 538 {K_S_F4, "\316W"}, 539 {K_S_F5, "\316X"}, 540 {K_S_F6, "\316Y"}, 541 {K_S_F7, "\316Z"}, 542 {K_S_F8, "\316["}, 543 {K_S_F9, "\316\\"}, 544 {K_S_F10, "\316]"}, 545 {K_S_F11, "\316\207"}, // guessed 546 {K_S_F12, "\316\210"}, // guessed 547 {K_INS, "\316R"}, 548 {K_DEL, "\316S"}, 549 {K_HOME, "\316G"}, 550 {K_END, "\316O"}, 551 {K_PAGEDOWN, "\316Q"}, 552 {K_PAGEUP, "\316I"}, 553 # endif 554 555 # if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS) 556 /* 557 * These codes are valid for the Win32 Console . The entries that start with 558 * ESC | are translated into console calls in os_win32.c. The function keys 559 * are also translated in os_win32.c. 560 */ 561 {(int)KS_NAME, "win32"}, 562 {(int)KS_CE, "\033|K"}, // clear to end of line 563 {(int)KS_AL, "\033|L"}, // add new blank line 564 # ifdef TERMINFO 565 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines 566 # else 567 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines 568 # endif 569 {(int)KS_DL, "\033|M"}, // delete line 570 # ifdef TERMINFO 571 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines 572 {(int)KS_CSV, "\033|%p1%d;%p2%dV"}, 573 # else 574 {(int)KS_CDL, "\033|%dM"}, // delete number of lines 575 {(int)KS_CSV, "\033|%d;%dV"}, 576 # endif 577 {(int)KS_CL, "\033|J"}, // clear screen 578 {(int)KS_CD, "\033|j"}, // clear to end of display 579 {(int)KS_VI, "\033|v"}, // cursor invisible 580 {(int)KS_VE, "\033|V"}, // cursor visible 581 582 {(int)KS_ME, "\033|0m"}, // normal 583 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray 584 {(int)KS_MD, "\033|15m"}, // bold: white on black 585 #if 1 586 {(int)KS_SO, "\033|31m"}, // standout: white on blue 587 {(int)KS_SE, "\033|0m"}, // standout end 588 #else 589 {(int)KS_SO, "\033|F"}, // standout: high intensity 590 {(int)KS_SE, "\033|f"}, // standout end 591 #endif 592 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow 593 {(int)KS_CZR, "\033|0m"}, // italic end 594 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red 595 {(int)KS_UE, "\033|0m"}, // underscore end 596 {(int)KS_CCO, "16"}, // allow 16 colors 597 # ifdef TERMINFO 598 {(int)KS_CAB, "\033|%p1%db"}, // set background color 599 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color 600 # else 601 {(int)KS_CAB, "\033|%db"}, // set background color 602 {(int)KS_CAF, "\033|%df"}, // set foreground color 603 # endif 604 605 {(int)KS_MS, "y"}, // save to move cur in reverse mode 606 {(int)KS_UT, "y"}, 607 {(int)KS_XN, "y"}, 608 {(int)KS_LE, "\b"}, 609 # ifdef TERMINFO 610 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion 611 # else 612 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion 613 # endif 614 {(int)KS_VB, "\033|B"}, // visual bell 615 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode 616 {(int)KS_TE, "\033|E"}, // out of termcap mode 617 # ifdef TERMINFO 618 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region 619 # else 620 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region 621 # endif 622 # ifdef FEAT_TERMGUICOLORS 623 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"}, 624 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"}, 625 # endif 626 627 {K_UP, "\316H"}, 628 {K_DOWN, "\316P"}, 629 {K_LEFT, "\316K"}, 630 {K_RIGHT, "\316M"}, 631 {K_S_UP, "\316\304"}, 632 {K_S_DOWN, "\316\317"}, 633 {K_S_LEFT, "\316\311"}, 634 {K_C_LEFT, "\316s"}, 635 {K_S_RIGHT, "\316\313"}, 636 {K_C_RIGHT, "\316t"}, 637 {K_S_TAB, "\316\017"}, 638 {K_F1, "\316;"}, 639 {K_F2, "\316<"}, 640 {K_F3, "\316="}, 641 {K_F4, "\316>"}, 642 {K_F5, "\316?"}, 643 {K_F6, "\316@"}, 644 {K_F7, "\316A"}, 645 {K_F8, "\316B"}, 646 {K_F9, "\316C"}, 647 {K_F10, "\316D"}, 648 {K_F11, "\316\205"}, 649 {K_F12, "\316\206"}, 650 {K_S_F1, "\316T"}, 651 {K_S_F2, "\316U"}, 652 {K_S_F3, "\316V"}, 653 {K_S_F4, "\316W"}, 654 {K_S_F5, "\316X"}, 655 {K_S_F6, "\316Y"}, 656 {K_S_F7, "\316Z"}, 657 {K_S_F8, "\316["}, 658 {K_S_F9, "\316\\"}, 659 {K_S_F10, "\316]"}, 660 {K_S_F11, "\316\207"}, 661 {K_S_F12, "\316\210"}, 662 {K_INS, "\316R"}, 663 {K_DEL, "\316S"}, 664 {K_HOME, "\316G"}, 665 {K_S_HOME, "\316\302"}, 666 {K_C_HOME, "\316w"}, 667 {K_END, "\316O"}, 668 {K_S_END, "\316\315"}, 669 {K_C_END, "\316u"}, 670 {K_PAGEDOWN, "\316Q"}, 671 {K_PAGEUP, "\316I"}, 672 {K_KPLUS, "\316N"}, 673 {K_KMINUS, "\316J"}, 674 {K_KMULTIPLY, "\316\067"}, 675 {K_K0, "\316\332"}, 676 {K_K1, "\316\336"}, 677 {K_K2, "\316\342"}, 678 {K_K3, "\316\346"}, 679 {K_K4, "\316\352"}, 680 {K_K5, "\316\356"}, 681 {K_K6, "\316\362"}, 682 {K_K7, "\316\366"}, 683 {K_K8, "\316\372"}, 684 {K_K9, "\316\376"}, 685 {K_BS, "\316x"}, 686 # endif 687 688 # if defined(VMS) || defined(ALL_BUILTIN_TCAPS) 689 /* 690 * VT320 is working as an ANSI terminal compatible DEC terminal. 691 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well) 692 * TODO:- rewrite ESC[ codes to CSI 693 * - keyboard languages (CSI ? 26 n) 694 */ 695 {(int)KS_NAME, "vt320"}, 696 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")}, 697 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")}, 698 # ifdef TERMINFO 699 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")}, 700 # else 701 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")}, 702 # endif 703 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")}, 704 # ifdef TERMINFO 705 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")}, 706 # else 707 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")}, 708 # endif 709 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")}, 710 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")}, 711 {(int)KS_CCO, "8"}, // allow 8 colors 712 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")}, 713 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")}, 714 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, // bold mode 715 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},// normal mode 716 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},// exit underscore mode 717 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, // underscore mode 718 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, // italic mode: blue text on yellow 719 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, // italic mode end 720 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, // set background color (ANSI) 721 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, // set foreground color (ANSI) 722 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, // set screen background color 723 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, // set screen foreground color 724 {(int)KS_MS, "y"}, 725 {(int)KS_UT, "y"}, 726 {(int)KS_XN, "y"}, 727 {(int)KS_LE, "\b"}, 728 # ifdef TERMINFO 729 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", 730 ESC_STR "[%i%p1%d;%p2%dH")}, 731 # else 732 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")}, 733 # endif 734 # ifdef TERMINFO 735 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")}, 736 # else 737 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")}, 738 # endif 739 {K_UP, IF_EB("\033[A", ESC_STR "[A")}, 740 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")}, 741 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")}, 742 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")}, 743 // Note: cursor key sequences for application cursor mode are omitted, 744 // because they interfere with typed commands: <Esc>OA. 745 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")}, 746 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")}, 747 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")}, 748 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")}, 749 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")}, 750 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")}, 751 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")}, 752 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")}, 753 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")}, 754 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")}, 755 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")}, 756 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")}, 757 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")}, 758 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")}, 759 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, // Help 760 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, // Select 761 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")}, 762 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")}, 763 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")}, 764 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")}, 765 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")}, 766 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")}, 767 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")}, 768 {K_END, IF_EB("\033[4~", ESC_STR "[4~")}, 769 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")}, 770 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")}, 771 // These sequences starting with <Esc> O may interfere with what the user 772 // is typing. Remove these if that bothers you. 773 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, // keypad plus 774 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, // keypad minus 775 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, // keypad / 776 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, // keypad * 777 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, // keypad Enter 778 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, // keypad 0 779 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, // keypad 1 780 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, // keypad 2 781 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, // keypad 3 782 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, // keypad 4 783 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, // keypad 5 784 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, // keypad 6 785 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, // keypad 7 786 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, // keypad 8 787 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, // keypad 9 788 {K_BS, "\x7f"}, // for some reason 0177 doesn't work 789 # endif 790 791 # if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__) 792 /* 793 * Ordinary vt52 794 */ 795 {(int)KS_NAME, "vt52"}, 796 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")}, 797 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")}, 798 # ifdef TERMINFO 799 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c", 800 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")}, 801 # else 802 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")}, 803 # endif 804 {(int)KS_LE, "\b"}, 805 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")}, 806 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")}, 807 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")}, 808 {K_UP, IF_EB("\033A", ESC_STR "A")}, 809 {K_DOWN, IF_EB("\033B", ESC_STR "B")}, 810 {K_LEFT, IF_EB("\033D", ESC_STR "D")}, 811 {K_RIGHT, IF_EB("\033C", ESC_STR "C")}, 812 {K_F1, IF_EB("\033P", ESC_STR "P")}, 813 {K_F2, IF_EB("\033Q", ESC_STR "Q")}, 814 {K_F3, IF_EB("\033R", ESC_STR "R")}, 815 # ifdef __MINT__ 816 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")}, 817 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")}, 818 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")}, 819 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")}, 820 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")}, 821 {K_S_UP, IF_EB("\033a", ESC_STR "a")}, 822 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")}, 823 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")}, 824 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")}, 825 {K_F4, IF_EB("\033S", ESC_STR "S")}, 826 {K_F5, IF_EB("\033T", ESC_STR "T")}, 827 {K_F6, IF_EB("\033U", ESC_STR "U")}, 828 {K_F7, IF_EB("\033V", ESC_STR "V")}, 829 {K_F8, IF_EB("\033W", ESC_STR "W")}, 830 {K_F9, IF_EB("\033X", ESC_STR "X")}, 831 {K_F10, IF_EB("\033Y", ESC_STR "Y")}, 832 {K_S_F1, IF_EB("\033p", ESC_STR "p")}, 833 {K_S_F2, IF_EB("\033q", ESC_STR "q")}, 834 {K_S_F3, IF_EB("\033r", ESC_STR "r")}, 835 {K_S_F4, IF_EB("\033s", ESC_STR "s")}, 836 {K_S_F5, IF_EB("\033t", ESC_STR "t")}, 837 {K_S_F6, IF_EB("\033u", ESC_STR "u")}, 838 {K_S_F7, IF_EB("\033v", ESC_STR "v")}, 839 {K_S_F8, IF_EB("\033w", ESC_STR "w")}, 840 {K_S_F9, IF_EB("\033x", ESC_STR "x")}, 841 {K_S_F10, IF_EB("\033y", ESC_STR "y")}, 842 {K_INS, IF_EB("\033I", ESC_STR "I")}, 843 {K_HOME, IF_EB("\033E", ESC_STR "E")}, 844 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")}, 845 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")}, 846 # else 847 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")}, 848 {(int)KS_MS, "y"}, 849 # endif 850 # endif 851 852 # if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) 853 {(int)KS_NAME, "xterm"}, 854 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")}, 855 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")}, 856 # ifdef TERMINFO 857 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")}, 858 # else 859 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")}, 860 # endif 861 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")}, 862 # ifdef TERMINFO 863 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")}, 864 # else 865 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")}, 866 # endif 867 # ifdef TERMINFO 868 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr", 869 ESC_STR "[%i%p1%d;%p2%dr")}, 870 # else 871 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")}, 872 # endif 873 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")}, 874 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")}, 875 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")}, 876 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")}, 877 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, 878 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")}, 879 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, 880 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")}, 881 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")}, 882 {(int)KS_MS, "y"}, 883 {(int)KS_UT, "y"}, 884 {(int)KS_LE, "\b"}, 885 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")}, 886 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")}, 887 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")}, 888 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")}, 889 # ifdef TERMINFO 890 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")}, 891 # else 892 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")}, 893 # endif 894 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")}, 895 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")}, 896 # ifdef TERMINFO 897 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", 898 ESC_STR "[%i%p1%d;%p2%dH")}, 899 # else 900 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")}, 901 # endif 902 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")}, 903 # ifdef TERMINFO 904 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")}, 905 # else 906 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")}, 907 # endif 908 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")}, 909 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")}, 910 # ifdef FEAT_XTERM_SAVE 911 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")}, 912 {(int)KS_TE, IF_EB("\033[?47l\0338", 913 ESC_STR_nc "[?47l" ESC_STR_nc "8")}, 914 # endif 915 {(int)KS_CTI, IF_EB("\033[>4;2m", ESC_STR_nc "[>4;2m")}, 916 {(int)KS_CTE, IF_EB("\033[>4;m", ESC_STR_nc "[>4;m")}, 917 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")}, 918 {(int)KS_CIE, "\007"}, 919 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")}, 920 {(int)KS_FS, "\007"}, 921 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")}, 922 {(int)KS_CEC, "\007"}, 923 # ifdef TERMINFO 924 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt", 925 ESC_STR "[8;%p1%d;%p2%dt")}, 926 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt", 927 ESC_STR "[3;%p1%d;%p2%dt")}, 928 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")}, 929 # else 930 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")}, 931 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")}, 932 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")}, 933 # endif 934 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")}, 935 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")}, 936 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")}, 937 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")}, 938 # ifdef FEAT_TERMGUICOLORS 939 // These are printf strings, not terminal codes. 940 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")}, 941 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")}, 942 # endif 943 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")}, 944 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")}, 945 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")}, 946 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")}, 947 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")}, 948 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")}, 949 950 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")}, 951 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")}, 952 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")}, 953 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")}, 954 // An extra set of cursor keys for vt100 mode 955 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")}, 956 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")}, 957 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")}, 958 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")}, 959 // An extra set of function keys for vt100 mode 960 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")}, 961 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")}, 962 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")}, 963 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")}, 964 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")}, 965 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")}, 966 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")}, 967 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")}, 968 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")}, 969 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")}, 970 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")}, 971 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")}, 972 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")}, 973 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")}, 974 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")}, 975 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")}, 976 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")}, 977 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")}, 978 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")}, 979 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")}, 980 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")}, 981 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, 982 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, 983 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")}, 984 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home 985 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home 986 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")}, 987 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, 988 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, 989 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")}, 990 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End 991 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")}, 992 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")}, 993 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")}, 994 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus 995 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus 996 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad / 997 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad * 998 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter 999 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad . 1000 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0 1001 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1 1002 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2 1003 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3 1004 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4 1005 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5 1006 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6 1007 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7 1008 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8 1009 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9 1010 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del 1011 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start 1012 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end 1013 1014 {BT_EXTRA_KEYS, ""}, 1015 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0 1016 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13 1017 // F14 and F15 are missing, because they send the same codes as the undo 1018 // and help key, although they don't work on all keyboards. 1019 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16 1020 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17 1021 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18 1022 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19 1023 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20 1024 1025 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21 1026 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22 1027 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23 1028 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24 1029 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25 1030 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26 1031 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27 1032 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28 1033 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29 1034 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30 1035 1036 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31 1037 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32 1038 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33 1039 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34 1040 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35 1041 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36 1042 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37 1043 # endif 1044 1045 # if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) 1046 /* 1047 * iris-ansi for Silicon Graphics machines. 1048 */ 1049 {(int)KS_NAME, "iris-ansi"}, 1050 {(int)KS_CE, "\033[K"}, 1051 {(int)KS_CD, "\033[J"}, 1052 {(int)KS_AL, "\033[L"}, 1053 # ifdef TERMINFO 1054 {(int)KS_CAL, "\033[%p1%dL"}, 1055 # else 1056 {(int)KS_CAL, "\033[%dL"}, 1057 # endif 1058 {(int)KS_DL, "\033[M"}, 1059 # ifdef TERMINFO 1060 {(int)KS_CDL, "\033[%p1%dM"}, 1061 # else 1062 {(int)KS_CDL, "\033[%dM"}, 1063 # endif 1064 #if 0 // The scroll region is not working as Vim expects. 1065 # ifdef TERMINFO 1066 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"}, 1067 # else 1068 {(int)KS_CS, "\033[%i%d;%dr"}, 1069 # endif 1070 #endif 1071 {(int)KS_CL, "\033[H\033[2J"}, 1072 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented 1073 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented 1074 {(int)KS_TI, "\033[=6h"}, 1075 {(int)KS_TE, "\033[=6l"}, 1076 {(int)KS_SE, "\033[21;27m"}, 1077 {(int)KS_SO, "\033[1;7m"}, 1078 {(int)KS_ME, "\033[m"}, 1079 {(int)KS_MR, "\033[7m"}, 1080 {(int)KS_MD, "\033[1m"}, 1081 {(int)KS_CCO, "8"}, // allow 8 colors 1082 {(int)KS_CZH, "\033[3m"}, // italic mode on 1083 {(int)KS_CZR, "\033[23m"}, // italic mode off 1084 {(int)KS_US, "\033[4m"}, // underline on 1085 {(int)KS_UE, "\033[24m"}, // underline off 1086 # ifdef TERMINFO 1087 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI) 1088 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI) 1089 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color 1090 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color 1091 # else 1092 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI) 1093 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI) 1094 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color 1095 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color 1096 # endif 1097 {(int)KS_MS, "y"}, // guessed 1098 {(int)KS_UT, "y"}, // guessed 1099 {(int)KS_LE, "\b"}, 1100 # ifdef TERMINFO 1101 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"}, 1102 # else 1103 {(int)KS_CM, "\033[%i%d;%dH"}, 1104 # endif 1105 {(int)KS_SR, "\033M"}, 1106 # ifdef TERMINFO 1107 {(int)KS_CRI, "\033[%p1%dC"}, 1108 # else 1109 {(int)KS_CRI, "\033[%dC"}, 1110 # endif 1111 {(int)KS_CIS, "\033P3.y"}, 1112 {(int)KS_CIE, "\234"}, // ST "String Terminator" 1113 {(int)KS_TS, "\033P1.y"}, 1114 {(int)KS_FS, "\234"}, // ST "String Terminator" 1115 # ifdef TERMINFO 1116 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"}, 1117 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"}, 1118 # else 1119 {(int)KS_CWS, "\033[203;%d;%d/y"}, 1120 {(int)KS_CWP, "\033[205;%d;%d/y"}, 1121 # endif 1122 {K_UP, "\033[A"}, 1123 {K_DOWN, "\033[B"}, 1124 {K_LEFT, "\033[D"}, 1125 {K_RIGHT, "\033[C"}, 1126 {K_S_UP, "\033[161q"}, 1127 {K_S_DOWN, "\033[164q"}, 1128 {K_S_LEFT, "\033[158q"}, 1129 {K_S_RIGHT, "\033[167q"}, 1130 {K_F1, "\033[001q"}, 1131 {K_F2, "\033[002q"}, 1132 {K_F3, "\033[003q"}, 1133 {K_F4, "\033[004q"}, 1134 {K_F5, "\033[005q"}, 1135 {K_F6, "\033[006q"}, 1136 {K_F7, "\033[007q"}, 1137 {K_F8, "\033[008q"}, 1138 {K_F9, "\033[009q"}, 1139 {K_F10, "\033[010q"}, 1140 {K_F11, "\033[011q"}, 1141 {K_F12, "\033[012q"}, 1142 {K_S_F1, "\033[013q"}, 1143 {K_S_F2, "\033[014q"}, 1144 {K_S_F3, "\033[015q"}, 1145 {K_S_F4, "\033[016q"}, 1146 {K_S_F5, "\033[017q"}, 1147 {K_S_F6, "\033[018q"}, 1148 {K_S_F7, "\033[019q"}, 1149 {K_S_F8, "\033[020q"}, 1150 {K_S_F9, "\033[021q"}, 1151 {K_S_F10, "\033[022q"}, 1152 {K_S_F11, "\033[023q"}, 1153 {K_S_F12, "\033[024q"}, 1154 {K_INS, "\033[139q"}, 1155 {K_HOME, "\033[H"}, 1156 {K_END, "\033[146q"}, 1157 {K_PAGEUP, "\033[150q"}, 1158 {K_PAGEDOWN, "\033[154q"}, 1159 # endif 1160 1161 # if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS) 1162 /* 1163 * for debugging 1164 */ 1165 {(int)KS_NAME, "debug"}, 1166 {(int)KS_CE, "[CE]"}, 1167 {(int)KS_CD, "[CD]"}, 1168 {(int)KS_AL, "[AL]"}, 1169 # ifdef TERMINFO 1170 {(int)KS_CAL, "[CAL%p1%d]"}, 1171 # else 1172 {(int)KS_CAL, "[CAL%d]"}, 1173 # endif 1174 {(int)KS_DL, "[DL]"}, 1175 # ifdef TERMINFO 1176 {(int)KS_CDL, "[CDL%p1%d]"}, 1177 # else 1178 {(int)KS_CDL, "[CDL%d]"}, 1179 # endif 1180 # ifdef TERMINFO 1181 {(int)KS_CS, "[%p1%dCS%p2%d]"}, 1182 # else 1183 {(int)KS_CS, "[%dCS%d]"}, 1184 # endif 1185 # ifdef TERMINFO 1186 {(int)KS_CSV, "[%p1%dCSV%p2%d]"}, 1187 # else 1188 {(int)KS_CSV, "[%dCSV%d]"}, 1189 # endif 1190 # ifdef TERMINFO 1191 {(int)KS_CAB, "[CAB%p1%d]"}, 1192 {(int)KS_CAF, "[CAF%p1%d]"}, 1193 {(int)KS_CSB, "[CSB%p1%d]"}, 1194 {(int)KS_CSF, "[CSF%p1%d]"}, 1195 # else 1196 {(int)KS_CAB, "[CAB%d]"}, 1197 {(int)KS_CAF, "[CAF%d]"}, 1198 {(int)KS_CSB, "[CSB%d]"}, 1199 {(int)KS_CSF, "[CSF%d]"}, 1200 # endif 1201 {(int)KS_OP, "[OP]"}, 1202 {(int)KS_LE, "[LE]"}, 1203 {(int)KS_CL, "[CL]"}, 1204 {(int)KS_VI, "[VI]"}, 1205 {(int)KS_VE, "[VE]"}, 1206 {(int)KS_VS, "[VS]"}, 1207 {(int)KS_ME, "[ME]"}, 1208 {(int)KS_MR, "[MR]"}, 1209 {(int)KS_MB, "[MB]"}, 1210 {(int)KS_MD, "[MD]"}, 1211 {(int)KS_SE, "[SE]"}, 1212 {(int)KS_SO, "[SO]"}, 1213 {(int)KS_UE, "[UE]"}, 1214 {(int)KS_US, "[US]"}, 1215 {(int)KS_UCE, "[UCE]"}, 1216 {(int)KS_UCS, "[UCS]"}, 1217 {(int)KS_STE, "[STE]"}, 1218 {(int)KS_STS, "[STS]"}, 1219 {(int)KS_MS, "[MS]"}, 1220 {(int)KS_UT, "[UT]"}, 1221 {(int)KS_XN, "[XN]"}, 1222 # ifdef TERMINFO 1223 {(int)KS_CM, "[%p1%dCM%p2%d]"}, 1224 # else 1225 {(int)KS_CM, "[%dCM%d]"}, 1226 # endif 1227 {(int)KS_SR, "[SR]"}, 1228 # ifdef TERMINFO 1229 {(int)KS_CRI, "[CRI%p1%d]"}, 1230 # else 1231 {(int)KS_CRI, "[CRI%d]"}, 1232 # endif 1233 {(int)KS_VB, "[VB]"}, 1234 {(int)KS_KS, "[KS]"}, 1235 {(int)KS_KE, "[KE]"}, 1236 {(int)KS_TI, "[TI]"}, 1237 {(int)KS_TE, "[TE]"}, 1238 {(int)KS_CIS, "[CIS]"}, 1239 {(int)KS_CIE, "[CIE]"}, 1240 {(int)KS_CSC, "[CSC]"}, 1241 {(int)KS_CEC, "[CEC]"}, 1242 {(int)KS_TS, "[TS]"}, 1243 {(int)KS_FS, "[FS]"}, 1244 # ifdef TERMINFO 1245 {(int)KS_CWS, "[%p1%dCWS%p2%d]"}, 1246 {(int)KS_CWP, "[%p1%dCWP%p2%d]"}, 1247 # else 1248 {(int)KS_CWS, "[%dCWS%d]"}, 1249 {(int)KS_CWP, "[%dCWP%d]"}, 1250 # endif 1251 {(int)KS_CRV, "[CRV]"}, 1252 {(int)KS_U7, "[U7]"}, 1253 {(int)KS_RFG, "[RFG]"}, 1254 {(int)KS_RBG, "[RBG]"}, 1255 {K_UP, "[KU]"}, 1256 {K_DOWN, "[KD]"}, 1257 {K_LEFT, "[KL]"}, 1258 {K_RIGHT, "[KR]"}, 1259 {K_XUP, "[xKU]"}, 1260 {K_XDOWN, "[xKD]"}, 1261 {K_XLEFT, "[xKL]"}, 1262 {K_XRIGHT, "[xKR]"}, 1263 {K_S_UP, "[S-KU]"}, 1264 {K_S_DOWN, "[S-KD]"}, 1265 {K_S_LEFT, "[S-KL]"}, 1266 {K_C_LEFT, "[C-KL]"}, 1267 {K_S_RIGHT, "[S-KR]"}, 1268 {K_C_RIGHT, "[C-KR]"}, 1269 {K_F1, "[F1]"}, 1270 {K_XF1, "[xF1]"}, 1271 {K_F2, "[F2]"}, 1272 {K_XF2, "[xF2]"}, 1273 {K_F3, "[F3]"}, 1274 {K_XF3, "[xF3]"}, 1275 {K_F4, "[F4]"}, 1276 {K_XF4, "[xF4]"}, 1277 {K_F5, "[F5]"}, 1278 {K_F6, "[F6]"}, 1279 {K_F7, "[F7]"}, 1280 {K_F8, "[F8]"}, 1281 {K_F9, "[F9]"}, 1282 {K_F10, "[F10]"}, 1283 {K_F11, "[F11]"}, 1284 {K_F12, "[F12]"}, 1285 {K_S_F1, "[S-F1]"}, 1286 {K_S_XF1, "[S-xF1]"}, 1287 {K_S_F2, "[S-F2]"}, 1288 {K_S_XF2, "[S-xF2]"}, 1289 {K_S_F3, "[S-F3]"}, 1290 {K_S_XF3, "[S-xF3]"}, 1291 {K_S_F4, "[S-F4]"}, 1292 {K_S_XF4, "[S-xF4]"}, 1293 {K_S_F5, "[S-F5]"}, 1294 {K_S_F6, "[S-F6]"}, 1295 {K_S_F7, "[S-F7]"}, 1296 {K_S_F8, "[S-F8]"}, 1297 {K_S_F9, "[S-F9]"}, 1298 {K_S_F10, "[S-F10]"}, 1299 {K_S_F11, "[S-F11]"}, 1300 {K_S_F12, "[S-F12]"}, 1301 {K_HELP, "[HELP]"}, 1302 {K_UNDO, "[UNDO]"}, 1303 {K_BS, "[BS]"}, 1304 {K_INS, "[INS]"}, 1305 {K_KINS, "[KINS]"}, 1306 {K_DEL, "[DEL]"}, 1307 {K_KDEL, "[KDEL]"}, 1308 {K_HOME, "[HOME]"}, 1309 {K_S_HOME, "[C-HOME]"}, 1310 {K_C_HOME, "[C-HOME]"}, 1311 {K_KHOME, "[KHOME]"}, 1312 {K_XHOME, "[XHOME]"}, 1313 {K_ZHOME, "[ZHOME]"}, 1314 {K_END, "[END]"}, 1315 {K_S_END, "[C-END]"}, 1316 {K_C_END, "[C-END]"}, 1317 {K_KEND, "[KEND]"}, 1318 {K_XEND, "[XEND]"}, 1319 {K_ZEND, "[ZEND]"}, 1320 {K_PAGEUP, "[PAGEUP]"}, 1321 {K_PAGEDOWN, "[PAGEDOWN]"}, 1322 {K_KPAGEUP, "[KPAGEUP]"}, 1323 {K_KPAGEDOWN, "[KPAGEDOWN]"}, 1324 {K_MOUSE, "[MOUSE]"}, 1325 {K_KPLUS, "[KPLUS]"}, 1326 {K_KMINUS, "[KMINUS]"}, 1327 {K_KDIVIDE, "[KDIVIDE]"}, 1328 {K_KMULTIPLY, "[KMULTIPLY]"}, 1329 {K_KENTER, "[KENTER]"}, 1330 {K_KPOINT, "[KPOINT]"}, 1331 {K_PS, "[PASTE-START]"}, 1332 {K_PE, "[PASTE-END]"}, 1333 {K_K0, "[K0]"}, 1334 {K_K1, "[K1]"}, 1335 {K_K2, "[K2]"}, 1336 {K_K3, "[K3]"}, 1337 {K_K4, "[K4]"}, 1338 {K_K5, "[K5]"}, 1339 {K_K6, "[K6]"}, 1340 {K_K7, "[K7]"}, 1341 {K_K8, "[K8]"}, 1342 {K_K9, "[K9]"}, 1343 # endif 1344 1345 #endif // NO_BUILTIN_TCAPS 1346 1347 /* 1348 * The most minimal terminal: only clear screen and cursor positioning 1349 * Always included. 1350 */ 1351 {(int)KS_NAME, "dumb"}, 1352 {(int)KS_CL, "\014"}, 1353 #ifdef TERMINFO 1354 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", 1355 ESC_STR "[%i%p1%d;%p2%dH")}, 1356 #else 1357 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")}, 1358 #endif 1359 1360 /* 1361 * end marker 1362 */ 1363 {(int)KS_NAME, NULL} 1364 1365 }; // end of builtin_termcaps 1366 1367 #if defined(FEAT_TERMGUICOLORS) || defined(PROTO) 1368 static guicolor_T 1369 termgui_mch_get_color(char_u *name) 1370 { 1371 return gui_get_color_cmn(name); 1372 } 1373 1374 guicolor_T 1375 termgui_get_color(char_u *name) 1376 { 1377 guicolor_T t; 1378 1379 if (*name == NUL) 1380 return INVALCOLOR; 1381 t = termgui_mch_get_color(name); 1382 1383 if (t == INVALCOLOR) 1384 semsg(_("E254: Cannot allocate color %s"), name); 1385 return t; 1386 } 1387 1388 guicolor_T 1389 termgui_mch_get_rgb(guicolor_T color) 1390 { 1391 return color; 1392 } 1393 #endif 1394 1395 /* 1396 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM. 1397 */ 1398 #ifdef AMIGA 1399 # define DEFAULT_TERM (char_u *)"amiga" 1400 #endif 1401 1402 #ifdef MSWIN 1403 # define DEFAULT_TERM (char_u *)"win32" 1404 #endif 1405 1406 #if defined(UNIX) && !defined(__MINT__) 1407 # define DEFAULT_TERM (char_u *)"ansi" 1408 #endif 1409 1410 #ifdef __MINT__ 1411 # define DEFAULT_TERM (char_u *)"vt52" 1412 #endif 1413 1414 #ifdef VMS 1415 # define DEFAULT_TERM (char_u *)"vt320" 1416 #endif 1417 1418 #ifdef __BEOS__ 1419 # undef DEFAULT_TERM 1420 # define DEFAULT_TERM (char_u *)"beos-ansi" 1421 #endif 1422 1423 #ifndef DEFAULT_TERM 1424 # define DEFAULT_TERM (char_u *)"dumb" 1425 #endif 1426 1427 /* 1428 * Term_strings contains currently used terminal output strings. 1429 * It is initialized with the default values by parse_builtin_tcap(). 1430 * The values can be changed by setting the option with the same name. 1431 */ 1432 char_u *(term_strings[(int)KS_LAST + 1]); 1433 1434 static int need_gather = FALSE; // need to fill termleader[] 1435 static char_u termleader[256 + 1]; // for check_termcode() 1436 #ifdef FEAT_TERMRESPONSE 1437 static int check_for_codes = FALSE; // check for key code response 1438 static int is_not_xterm = FALSE; // recognized not-really-xterm 1439 #endif 1440 1441 static struct builtin_term * 1442 find_builtin_term(char_u *term) 1443 { 1444 struct builtin_term *p; 1445 1446 p = builtin_termcaps; 1447 while (p->bt_string != NULL) 1448 { 1449 if (p->bt_entry == (int)KS_NAME) 1450 { 1451 #ifdef UNIX 1452 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term)) 1453 return p; 1454 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term)) 1455 return p; 1456 else 1457 #endif 1458 #ifdef VMS 1459 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term)) 1460 return p; 1461 else 1462 #endif 1463 if (STRCMP(term, p->bt_string) == 0) 1464 return p; 1465 } 1466 ++p; 1467 } 1468 return p; 1469 } 1470 1471 /* 1472 * Parsing of the builtin termcap entries. 1473 * Caller should check if 'name' is a valid builtin term. 1474 * The terminal's name is not set, as this is already done in termcapinit(). 1475 */ 1476 static void 1477 parse_builtin_tcap(char_u *term) 1478 { 1479 struct builtin_term *p; 1480 char_u name[2]; 1481 int term_8bit; 1482 1483 p = find_builtin_term(term); 1484 term_8bit = term_is_8bit(term); 1485 1486 // Do not parse if builtin term not found 1487 if (p->bt_string == NULL) 1488 return; 1489 1490 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p) 1491 { 1492 if ((int)p->bt_entry >= 0) // KS_xx entry 1493 { 1494 // Only set the value if it wasn't set yet. 1495 if (term_strings[p->bt_entry] == NULL 1496 || term_strings[p->bt_entry] == empty_option) 1497 { 1498 #ifdef FEAT_EVAL 1499 int opt_idx = -1; 1500 #endif 1501 // 8bit terminal: use CSI instead of <Esc>[ 1502 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0) 1503 { 1504 char_u *s, *t; 1505 1506 s = vim_strsave((char_u *)p->bt_string); 1507 if (s != NULL) 1508 { 1509 for (t = s; *t; ++t) 1510 if (term_7to8bit(t)) 1511 { 1512 *t = term_7to8bit(t); 1513 STRMOVE(t + 1, t + 2); 1514 } 1515 term_strings[p->bt_entry] = s; 1516 #ifdef FEAT_EVAL 1517 opt_idx = 1518 #endif 1519 set_term_option_alloced( 1520 &term_strings[p->bt_entry]); 1521 } 1522 } 1523 else 1524 { 1525 term_strings[p->bt_entry] = (char_u *)p->bt_string; 1526 #ifdef FEAT_EVAL 1527 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]); 1528 #endif 1529 } 1530 #ifdef FEAT_EVAL 1531 set_term_option_sctx_idx(NULL, opt_idx); 1532 #endif 1533 } 1534 } 1535 else 1536 { 1537 name[0] = KEY2TERMCAP0((int)p->bt_entry); 1538 name[1] = KEY2TERMCAP1((int)p->bt_entry); 1539 if (find_termcode(name) == NULL) 1540 add_termcode(name, (char_u *)p->bt_string, term_8bit); 1541 } 1542 } 1543 } 1544 1545 /* 1546 * Set number of colors. 1547 * Store it as a number in t_colors. 1548 * Store it as a string in T_CCO (using nr_colors[]). 1549 */ 1550 static void 1551 set_color_count(int nr) 1552 { 1553 char_u nr_colors[20]; // string for number of colors 1554 1555 t_colors = nr; 1556 if (t_colors > 1) 1557 sprintf((char *)nr_colors, "%d", t_colors); 1558 else 1559 *nr_colors = NUL; 1560 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0); 1561 } 1562 1563 #if defined(FEAT_TERMRESPONSE) 1564 /* 1565 * Set the color count to "val" and redraw if it changed. 1566 */ 1567 static void 1568 may_adjust_color_count(int val) 1569 { 1570 if (val != t_colors) 1571 { 1572 // Nr of colors changed, initialize highlighting and 1573 // redraw everything. This causes a redraw, which usually 1574 // clears the message. Try keeping the message if it 1575 // might work. 1576 set_keep_msg_from_hist(); 1577 set_color_count(val); 1578 init_highlight(TRUE, FALSE); 1579 # ifdef DEBUG_TERMRESPONSE 1580 { 1581 int r = redraw_asap(CLEAR); 1582 1583 log_tr("Received t_Co, redraw_asap(): %d", r); 1584 } 1585 #else 1586 redraw_asap(CLEAR); 1587 #endif 1588 } 1589 } 1590 #endif 1591 1592 #ifdef HAVE_TGETENT 1593 static char *(key_names[]) = 1594 { 1595 #ifdef FEAT_TERMRESPONSE 1596 // Do this one first, it may cause a screen redraw. 1597 "Co", 1598 #endif 1599 "ku", "kd", "kr", "kl", 1600 "#2", "#4", "%i", "*7", 1601 "k1", "k2", "k3", "k4", "k5", "k6", 1602 "k7", "k8", "k9", "k;", "F1", "F2", 1603 "%1", "&8", "kb", "kI", "kD", "kh", 1604 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB", 1605 NULL 1606 }; 1607 #endif 1608 1609 #ifdef HAVE_TGETENT 1610 static void 1611 get_term_entries(int *height, int *width) 1612 { 1613 static struct { 1614 enum SpecialKey dest; // index in term_strings[] 1615 char *name; // termcap name for string 1616 } string_names[] = 1617 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"}, 1618 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"}, 1619 {KS_CL, "cl"}, {KS_CD, "cd"}, 1620 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"}, 1621 {KS_ME, "me"}, {KS_MR, "mr"}, 1622 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"}, 1623 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"}, 1624 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"}, 1625 {KS_STE,"Te"}, {KS_STS,"Ts"}, 1626 {KS_CM, "cm"}, {KS_SR, "sr"}, 1627 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"}, 1628 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"}, 1629 {KS_CTI, "TI"}, {KS_CTE, "TE"}, 1630 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"}, 1631 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"}, 1632 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"}, 1633 {KS_VS, "vs"}, {KS_CVS, "VS"}, 1634 {KS_CIS, "IS"}, {KS_CIE, "IE"}, 1635 {KS_CSC, "SC"}, {KS_CEC, "EC"}, 1636 {KS_TS, "ts"}, {KS_FS, "fs"}, 1637 {KS_CWP, "WP"}, {KS_CWS, "WS"}, 1638 {KS_CSI, "SI"}, {KS_CEI, "EI"}, 1639 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"}, 1640 {KS_8F, "8f"}, {KS_8B, "8b"}, 1641 {KS_CBE, "BE"}, {KS_CBD, "BD"}, 1642 {KS_CPS, "PS"}, {KS_CPE, "PE"}, 1643 {KS_CST, "ST"}, {KS_CRT, "RT"}, 1644 {KS_SSI, "Si"}, {KS_SRI, "Ri"}, 1645 {(enum SpecialKey)0, NULL} 1646 }; 1647 int i; 1648 char_u *p; 1649 static char_u tstrbuf[TBUFSZ]; 1650 char_u *tp = tstrbuf; 1651 1652 /* 1653 * get output strings 1654 */ 1655 for (i = 0; string_names[i].name != NULL; ++i) 1656 { 1657 if (TERM_STR(string_names[i].dest) == NULL 1658 || TERM_STR(string_names[i].dest) == empty_option) 1659 { 1660 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp); 1661 #ifdef FEAT_EVAL 1662 set_term_option_sctx_idx(string_names[i].name, -1); 1663 #endif 1664 } 1665 } 1666 1667 // tgetflag() returns 1 if the flag is present, 0 if not and 1668 // possibly -1 if the flag doesn't exist. 1669 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0) 1670 T_MS = (char_u *)"y"; 1671 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0) 1672 T_XS = (char_u *)"y"; 1673 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0) 1674 T_XN = (char_u *)"y"; 1675 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0) 1676 T_DB = (char_u *)"y"; 1677 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0) 1678 T_DA = (char_u *)"y"; 1679 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0) 1680 T_UT = (char_u *)"y"; 1681 1682 /* 1683 * get key codes 1684 */ 1685 for (i = 0; key_names[i] != NULL; ++i) 1686 if (find_termcode((char_u *)key_names[i]) == NULL) 1687 { 1688 p = TGETSTR(key_names[i], &tp); 1689 // if cursor-left == backspace, ignore it (televideo 925) 1690 if (p != NULL 1691 && (*p != Ctrl_H 1692 || key_names[i][0] != 'k' 1693 || key_names[i][1] != 'l')) 1694 add_termcode((char_u *)key_names[i], p, FALSE); 1695 } 1696 1697 if (*height == 0) 1698 *height = tgetnum("li"); 1699 if (*width == 0) 1700 *width = tgetnum("co"); 1701 1702 /* 1703 * Get number of colors (if not done already). 1704 */ 1705 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option) 1706 { 1707 set_color_count(tgetnum("Co")); 1708 #ifdef FEAT_EVAL 1709 set_term_option_sctx_idx("Co", -1); 1710 #endif 1711 } 1712 1713 # ifndef hpux 1714 BC = (char *)TGETSTR("bc", &tp); 1715 UP = (char *)TGETSTR("up", &tp); 1716 p = TGETSTR("pc", &tp); 1717 if (p) 1718 PC = *p; 1719 # endif 1720 } 1721 #endif 1722 1723 static void 1724 report_term_error(char *error_msg, char_u *term) 1725 { 1726 struct builtin_term *termp; 1727 1728 mch_errmsg("\r\n"); 1729 if (error_msg != NULL) 1730 { 1731 mch_errmsg(error_msg); 1732 mch_errmsg("\r\n"); 1733 } 1734 mch_errmsg("'"); 1735 mch_errmsg((char *)term); 1736 mch_errmsg(_("' not known. Available builtin terminals are:")); 1737 mch_errmsg("\r\n"); 1738 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp) 1739 { 1740 if (termp->bt_entry == (int)KS_NAME) 1741 { 1742 #ifdef HAVE_TGETENT 1743 mch_errmsg(" builtin_"); 1744 #else 1745 mch_errmsg(" "); 1746 #endif 1747 mch_errmsg(termp->bt_string); 1748 mch_errmsg("\r\n"); 1749 } 1750 } 1751 } 1752 1753 static void 1754 report_default_term(char_u *term) 1755 { 1756 mch_errmsg(_("defaulting to '")); 1757 mch_errmsg((char *)term); 1758 mch_errmsg("'\r\n"); 1759 if (emsg_silent == 0) 1760 { 1761 screen_start(); // don't know where cursor is now 1762 out_flush(); 1763 if (!is_not_a_term()) 1764 ui_delay(2007L, TRUE); 1765 } 1766 } 1767 1768 /* 1769 * Set terminal options for terminal "term". 1770 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise. 1771 * 1772 * While doing this, until ttest(), some options may be NULL, be careful. 1773 */ 1774 int 1775 set_termname(char_u *term) 1776 { 1777 struct builtin_term *termp; 1778 #ifdef HAVE_TGETENT 1779 int builtin_first = p_tbi; 1780 int try; 1781 int termcap_cleared = FALSE; 1782 #endif 1783 int width = 0, height = 0; 1784 char *error_msg = NULL; 1785 char_u *bs_p, *del_p; 1786 1787 // In silect mode (ex -s) we don't use the 'term' option. 1788 if (silent_mode) 1789 return OK; 1790 1791 detected_8bit = FALSE; // reset 8-bit detection 1792 1793 if (term_is_builtin(term)) 1794 { 1795 term += 8; 1796 #ifdef HAVE_TGETENT 1797 builtin_first = 1; 1798 #endif 1799 } 1800 1801 /* 1802 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise: 1803 * If builtin_first is TRUE: 1804 * 0. try builtin termcap 1805 * 1. try external termcap 1806 * 2. if both fail default to a builtin terminal 1807 * If builtin_first is FALSE: 1808 * 1. try external termcap 1809 * 2. try builtin termcap, if both fail default to a builtin terminal 1810 */ 1811 #ifdef HAVE_TGETENT 1812 for (try = builtin_first ? 0 : 1; try < 3; ++try) 1813 { 1814 /* 1815 * Use external termcap 1816 */ 1817 if (try == 1) 1818 { 1819 char_u tbuf[TBUFSZ]; 1820 1821 /* 1822 * If the external termcap does not have a matching entry, try the 1823 * builtin ones. 1824 */ 1825 if ((error_msg = tgetent_error(tbuf, term)) == NULL) 1826 { 1827 if (!termcap_cleared) 1828 { 1829 clear_termoptions(); // clear old options 1830 termcap_cleared = TRUE; 1831 } 1832 1833 get_term_entries(&height, &width); 1834 } 1835 } 1836 else // try == 0 || try == 2 1837 #endif // HAVE_TGETENT 1838 /* 1839 * Use builtin termcap 1840 */ 1841 { 1842 #ifdef HAVE_TGETENT 1843 /* 1844 * If builtin termcap was already used, there is no need to search 1845 * for the builtin termcap again, quit now. 1846 */ 1847 if (try == 2 && builtin_first && termcap_cleared) 1848 break; 1849 #endif 1850 /* 1851 * search for 'term' in builtin_termcaps[] 1852 */ 1853 termp = find_builtin_term(term); 1854 if (termp->bt_string == NULL) // did not find it 1855 { 1856 #ifdef HAVE_TGETENT 1857 /* 1858 * If try == 0, first try the external termcap. If that is not 1859 * found we'll get back here with try == 2. 1860 * If termcap_cleared is set we used the external termcap, 1861 * don't complain about not finding the term in the builtin 1862 * termcap. 1863 */ 1864 if (try == 0) // try external one 1865 continue; 1866 if (termcap_cleared) // found in external termcap 1867 break; 1868 #endif 1869 report_term_error(error_msg, term); 1870 1871 // when user typed :set term=xxx, quit here 1872 if (starting != NO_SCREEN) 1873 { 1874 screen_start(); // don't know where cursor is now 1875 wait_return(TRUE); 1876 return FAIL; 1877 } 1878 term = DEFAULT_TERM; 1879 report_default_term(term); 1880 set_string_option_direct((char_u *)"term", -1, term, 1881 OPT_FREE, 0); 1882 display_errors(); 1883 } 1884 out_flush(); 1885 #ifdef HAVE_TGETENT 1886 if (!termcap_cleared) 1887 { 1888 #endif 1889 clear_termoptions(); // clear old options 1890 #ifdef HAVE_TGETENT 1891 termcap_cleared = TRUE; 1892 } 1893 #endif 1894 parse_builtin_tcap(term); 1895 #ifdef FEAT_GUI 1896 if (term_is_gui(term)) 1897 { 1898 out_flush(); 1899 gui_init(); 1900 // If starting the GUI failed, don't do any of the other 1901 // things for this terminal 1902 if (!gui.in_use) 1903 return FAIL; 1904 #ifdef HAVE_TGETENT 1905 break; // don't try using external termcap 1906 #endif 1907 } 1908 #endif // FEAT_GUI 1909 } 1910 #ifdef HAVE_TGETENT 1911 } 1912 #endif 1913 1914 /* 1915 * special: There is no info in the termcap about whether the cursor 1916 * positioning is relative to the start of the screen or to the start of the 1917 * scrolling region. We just guess here. Only msdos pcterm is known to do it 1918 * relative. 1919 */ 1920 if (STRCMP(term, "pcterm") == 0) 1921 T_CCS = (char_u *)"yes"; 1922 else 1923 T_CCS = empty_option; 1924 1925 #ifdef UNIX 1926 /* 1927 * Any "stty" settings override the default for t_kb from the termcap. 1928 * This is in os_unix.c, because it depends a lot on the version of unix that 1929 * is being used. 1930 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly. 1931 */ 1932 # ifdef FEAT_GUI 1933 if (!gui.in_use) 1934 # endif 1935 get_stty(); 1936 #endif 1937 1938 /* 1939 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also 1940 * didn't work, use the default CTRL-H 1941 * The default for t_kD is DEL, unless t_kb is DEL. 1942 * The vim_strsave'd strings are probably lost forever, well it's only two 1943 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD" 1944 * directly. 1945 */ 1946 #ifdef FEAT_GUI 1947 if (!gui.in_use) 1948 #endif 1949 { 1950 bs_p = find_termcode((char_u *)"kb"); 1951 del_p = find_termcode((char_u *)"kD"); 1952 if (bs_p == NULL || *bs_p == NUL) 1953 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE); 1954 if ((del_p == NULL || *del_p == NUL) && 1955 (bs_p == NULL || *bs_p != DEL)) 1956 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE); 1957 } 1958 1959 #if defined(UNIX) || defined(VMS) 1960 term_is_xterm = vim_is_xterm(term); 1961 #endif 1962 #ifdef FEAT_TERMRESPONSE 1963 is_not_xterm = FALSE; 1964 is_mac_terminal = FALSE; 1965 #endif 1966 1967 #if defined(UNIX) || defined(VMS) 1968 /* 1969 * For Unix, set the 'ttymouse' option to the type of mouse to be used. 1970 * The termcode for the mouse is added as a side effect in option.c. 1971 */ 1972 { 1973 char_u *p = (char_u *)""; 1974 1975 # ifdef FEAT_MOUSE_XTERM 1976 if (use_xterm_like_mouse(term)) 1977 { 1978 if (use_xterm_mouse()) 1979 p = NULL; // keep existing value, might be "xterm2" 1980 else 1981 p = (char_u *)"xterm"; 1982 } 1983 # endif 1984 if (p != NULL) 1985 { 1986 set_option_value((char_u *)"ttym", 0L, p, 0); 1987 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or 1988 // "xterm2" in check_termcode(). 1989 reset_option_was_set((char_u *)"ttym"); 1990 } 1991 if (p == NULL 1992 # ifdef FEAT_GUI 1993 || gui.in_use 1994 # endif 1995 ) 1996 check_mouse_termcode(); // set mouse termcode anyway 1997 } 1998 #else 1999 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M"); 2000 #endif 2001 2002 #ifdef USE_TERM_CONSOLE 2003 // DEFAULT_TERM indicates that it is the machine console. 2004 if (STRCMP(term, DEFAULT_TERM) != 0) 2005 term_console = FALSE; 2006 else 2007 { 2008 term_console = TRUE; 2009 # ifdef AMIGA 2010 win_resize_on(); // enable window resizing reports 2011 # endif 2012 } 2013 #endif 2014 2015 #if defined(UNIX) || defined(VMS) 2016 /* 2017 * 'ttyfast' is default on for xterm, iris-ansi and a few others. 2018 */ 2019 if (vim_is_fastterm(term)) 2020 p_tf = TRUE; 2021 #endif 2022 #ifdef USE_TERM_CONSOLE 2023 /* 2024 * 'ttyfast' is default on consoles 2025 */ 2026 if (term_console) 2027 p_tf = TRUE; 2028 #endif 2029 2030 ttest(TRUE); // make sure we have a valid set of terminal codes 2031 2032 full_screen = TRUE; // we can use termcap codes from now on 2033 set_term_defaults(); // use current values as defaults 2034 #ifdef FEAT_TERMRESPONSE 2035 LOG_TR(("setting crv_status to STATUS_GET")); 2036 crv_status.tr_progress = STATUS_GET; // Get terminal version later 2037 #endif 2038 2039 /* 2040 * Initialize the terminal with the appropriate termcap codes. 2041 * Set the mouse and window title if possible. 2042 * Don't do this when starting, need to parse the .vimrc first, because it 2043 * may redefine t_TI etc. 2044 */ 2045 if (starting != NO_SCREEN) 2046 { 2047 starttermcap(); // may change terminal mode 2048 setmouse(); // may start using the mouse 2049 #ifdef FEAT_TITLE 2050 maketitle(); // may display window title 2051 #endif 2052 } 2053 2054 // display initial screen after ttest() checking. jw. 2055 if (width <= 0 || height <= 0) 2056 { 2057 // termcap failed to report size 2058 // set defaults, in case ui_get_shellsize() also fails 2059 width = 80; 2060 #if defined(MSWIN) 2061 height = 25; // console is often 25 lines 2062 #else 2063 height = 24; // most terminals are 24 lines 2064 #endif 2065 } 2066 set_shellsize(width, height, FALSE); // may change Rows 2067 if (starting != NO_SCREEN) 2068 { 2069 if (scroll_region) 2070 scroll_region_reset(); // In case Rows changed 2071 check_map_keycodes(); // check mappings for terminal codes used 2072 2073 { 2074 bufref_T old_curbuf; 2075 2076 /* 2077 * Execute the TermChanged autocommands for each buffer that is 2078 * loaded. 2079 */ 2080 set_bufref(&old_curbuf, curbuf); 2081 FOR_ALL_BUFFERS(curbuf) 2082 { 2083 if (curbuf->b_ml.ml_mfp != NULL) 2084 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE, 2085 curbuf); 2086 } 2087 if (bufref_valid(&old_curbuf)) 2088 curbuf = old_curbuf.br_buf; 2089 } 2090 } 2091 2092 #ifdef FEAT_TERMRESPONSE 2093 may_req_termresponse(); 2094 #endif 2095 2096 return OK; 2097 } 2098 2099 #ifdef HAVE_TGETENT 2100 /* 2101 * Call tgetent() 2102 * Return error message if it fails, NULL if it's OK. 2103 */ 2104 static char * 2105 tgetent_error(char_u *tbuf, char_u *term) 2106 { 2107 int i; 2108 2109 // Note: Valgrind may report a leak here, because the library keeps one 2110 // buffer around that we can't ever free. 2111 i = TGETENT(tbuf, term); 2112 if (i < 0 // -1 is always an error 2113 # ifdef TGETENT_ZERO_ERR 2114 || i == 0 // sometimes zero is also an error 2115 # endif 2116 ) 2117 { 2118 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call 2119 // tgetent() with the always existing "dumb" entry to avoid a crash or 2120 // hang. 2121 (void)TGETENT(tbuf, "dumb"); 2122 2123 if (i < 0) 2124 # ifdef TGETENT_ZERO_ERR 2125 return _("E557: Cannot open termcap file"); 2126 if (i == 0) 2127 # endif 2128 #ifdef TERMINFO 2129 return _("E558: Terminal entry not found in terminfo"); 2130 #else 2131 return _("E559: Terminal entry not found in termcap"); 2132 #endif 2133 } 2134 return NULL; 2135 } 2136 2137 /* 2138 * Some versions of tgetstr() have been reported to return -1 instead of NULL. 2139 * Fix that here. 2140 */ 2141 static char_u * 2142 vim_tgetstr(char *s, char_u **pp) 2143 { 2144 char *p; 2145 2146 p = tgetstr(s, (char **)pp); 2147 if (p == (char *)-1) 2148 p = NULL; 2149 return (char_u *)p; 2150 } 2151 #endif // HAVE_TGETENT 2152 2153 #if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X)) 2154 /* 2155 * Get Columns and Rows from the termcap. Used after a window signal if the 2156 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co" 2157 * and "li" entries never change. But on some systems this works. 2158 * Errors while getting the entries are ignored. 2159 */ 2160 void 2161 getlinecol( 2162 long *cp, // pointer to columns 2163 long *rp) // pointer to rows 2164 { 2165 char_u tbuf[TBUFSZ]; 2166 2167 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL) 2168 { 2169 if (*cp == 0) 2170 *cp = tgetnum("co"); 2171 if (*rp == 0) 2172 *rp = tgetnum("li"); 2173 } 2174 } 2175 #endif // defined(HAVE_TGETENT) && defined(UNIX) 2176 2177 /* 2178 * Get a string entry from the termcap and add it to the list of termcodes. 2179 * Used for <t_xx> special keys. 2180 * Give an error message for failure when not sourcing. 2181 * If force given, replace an existing entry. 2182 * Return FAIL if the entry was not found, OK if the entry was added. 2183 */ 2184 int 2185 add_termcap_entry(char_u *name, int force) 2186 { 2187 char_u *term; 2188 int key; 2189 struct builtin_term *termp; 2190 #ifdef HAVE_TGETENT 2191 char_u *string; 2192 int i; 2193 int builtin_first; 2194 char_u tbuf[TBUFSZ]; 2195 char_u tstrbuf[TBUFSZ]; 2196 char_u *tp = tstrbuf; 2197 char *error_msg = NULL; 2198 #endif 2199 2200 /* 2201 * If the GUI is running or will start in a moment, we only support the keys 2202 * that the GUI can produce. 2203 */ 2204 #ifdef FEAT_GUI 2205 if (gui.in_use || gui.starting) 2206 return gui_mch_haskey(name); 2207 #endif 2208 2209 if (!force && find_termcode(name) != NULL) // it's already there 2210 return OK; 2211 2212 term = T_NAME; 2213 if (term == NULL || *term == NUL) // 'term' not defined yet 2214 return FAIL; 2215 2216 if (term_is_builtin(term)) // name starts with "builtin_" 2217 { 2218 term += 8; 2219 #ifdef HAVE_TGETENT 2220 builtin_first = TRUE; 2221 #endif 2222 } 2223 #ifdef HAVE_TGETENT 2224 else 2225 builtin_first = p_tbi; 2226 #endif 2227 2228 #ifdef HAVE_TGETENT 2229 /* 2230 * We can get the entry from the builtin termcap and from the external one. 2231 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try 2232 * builtin termcap first. 2233 * If 'ttybuiltin' is off, try external termcap first. 2234 */ 2235 for (i = 0; i < 2; ++i) 2236 { 2237 if ((!builtin_first) == i) 2238 #endif 2239 /* 2240 * Search in builtin termcap 2241 */ 2242 { 2243 termp = find_builtin_term(term); 2244 if (termp->bt_string != NULL) // found it 2245 { 2246 key = TERMCAP2KEY(name[0], name[1]); 2247 ++termp; 2248 while (termp->bt_entry != (int)KS_NAME) 2249 { 2250 if ((int)termp->bt_entry == key) 2251 { 2252 add_termcode(name, (char_u *)termp->bt_string, 2253 term_is_8bit(term)); 2254 return OK; 2255 } 2256 ++termp; 2257 } 2258 } 2259 } 2260 #ifdef HAVE_TGETENT 2261 else 2262 /* 2263 * Search in external termcap 2264 */ 2265 { 2266 error_msg = tgetent_error(tbuf, term); 2267 if (error_msg == NULL) 2268 { 2269 string = TGETSTR((char *)name, &tp); 2270 if (string != NULL && *string != NUL) 2271 { 2272 add_termcode(name, string, FALSE); 2273 return OK; 2274 } 2275 } 2276 } 2277 } 2278 #endif 2279 2280 if (SOURCING_NAME == NULL) 2281 { 2282 #ifdef HAVE_TGETENT 2283 if (error_msg != NULL) 2284 emsg(error_msg); 2285 else 2286 #endif 2287 semsg(_("E436: No \"%s\" entry in termcap"), name); 2288 } 2289 return FAIL; 2290 } 2291 2292 static int 2293 term_is_builtin(char_u *name) 2294 { 2295 return (STRNCMP(name, "builtin_", (size_t)8) == 0); 2296 } 2297 2298 /* 2299 * Return TRUE if terminal "name" uses CSI instead of <Esc>[. 2300 * Assume that the terminal is using 8-bit controls when the name contains 2301 * "8bit", like in "xterm-8bit". 2302 */ 2303 int 2304 term_is_8bit(char_u *name) 2305 { 2306 return (detected_8bit || strstr((char *)name, "8bit") != NULL); 2307 } 2308 2309 /* 2310 * Translate terminal control chars from 7-bit to 8-bit: 2311 * <Esc>[ -> CSI <M_C_[> 2312 * <Esc>] -> OSC <M-C-]> 2313 * <Esc>O -> <M-C-O> 2314 */ 2315 static int 2316 term_7to8bit(char_u *p) 2317 { 2318 if (*p == ESC) 2319 { 2320 if (p[1] == '[') 2321 return CSI; 2322 if (p[1] == ']') 2323 return OSC; 2324 if (p[1] == 'O') 2325 return 0x8f; 2326 } 2327 return 0; 2328 } 2329 2330 #if defined(FEAT_GUI) || defined(PROTO) 2331 int 2332 term_is_gui(char_u *name) 2333 { 2334 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0); 2335 } 2336 #endif 2337 2338 #if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO) 2339 2340 char_u * 2341 tltoa(unsigned long i) 2342 { 2343 static char_u buf[16]; 2344 char_u *p; 2345 2346 p = buf + 15; 2347 *p = '\0'; 2348 do 2349 { 2350 --p; 2351 *p = (char_u) (i % 10 + '0'); 2352 i /= 10; 2353 } 2354 while (i > 0 && p > buf); 2355 return p; 2356 } 2357 #endif 2358 2359 #ifndef HAVE_TGETENT 2360 2361 /* 2362 * minimal tgoto() implementation. 2363 * no padding and we only parse for %i %d and %+char 2364 */ 2365 static char * 2366 tgoto(char *cm, int x, int y) 2367 { 2368 static char buf[30]; 2369 char *p, *s, *e; 2370 2371 if (!cm) 2372 return "OOPS"; 2373 e = buf + 29; 2374 for (s = buf; s < e && *cm; cm++) 2375 { 2376 if (*cm != '%') 2377 { 2378 *s++ = *cm; 2379 continue; 2380 } 2381 switch (*++cm) 2382 { 2383 case 'd': 2384 p = (char *)tltoa((unsigned long)y); 2385 y = x; 2386 while (*p) 2387 *s++ = *p++; 2388 break; 2389 case 'i': 2390 x++; 2391 y++; 2392 break; 2393 case '+': 2394 *s++ = (char)(*++cm + y); 2395 y = x; 2396 break; 2397 case '%': 2398 *s++ = *cm; 2399 break; 2400 default: 2401 return "OOPS"; 2402 } 2403 } 2404 *s = '\0'; 2405 return buf; 2406 } 2407 2408 #endif // HAVE_TGETENT 2409 2410 /* 2411 * Set the terminal name and initialize the terminal options. 2412 * If "name" is NULL or empty, get the terminal name from the environment. 2413 * If that fails, use the default terminal name. 2414 */ 2415 void 2416 termcapinit(char_u *name) 2417 { 2418 char_u *term; 2419 2420 if (name != NULL && *name == NUL) 2421 name = NULL; // empty name is equal to no name 2422 term = name; 2423 2424 #ifdef __BEOS__ 2425 /* 2426 * TERM environment variable is normally set to 'ansi' on the Bebox; 2427 * Since the BeBox doesn't quite support full ANSI yet, we use our 2428 * own custom 'ansi-beos' termcap instead, unless the -T option has 2429 * been given on the command line. 2430 */ 2431 if (term == NULL 2432 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0) 2433 term = DEFAULT_TERM; 2434 #endif 2435 #ifndef MSWIN 2436 if (term == NULL) 2437 term = mch_getenv((char_u *)"TERM"); 2438 #endif 2439 if (term == NULL || *term == NUL) 2440 term = DEFAULT_TERM; 2441 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0); 2442 2443 // Set the default terminal name. 2444 set_string_default("term", term); 2445 set_string_default("ttytype", term); 2446 2447 /* 2448 * Avoid using "term" here, because the next mch_getenv() may overwrite it. 2449 */ 2450 set_termname(T_NAME != NULL ? T_NAME : term); 2451 } 2452 2453 /* 2454 * The number of calls to ui_write is reduced by using "out_buf". 2455 */ 2456 #define OUT_SIZE 2047 2457 2458 // add one to allow mch_write() in os_win32.c to append a NUL 2459 static char_u out_buf[OUT_SIZE + 1]; 2460 2461 static int out_pos = 0; // number of chars in out_buf 2462 2463 // Since the maximum number of SGR parameters shown as a normal value range is 2464 // 16, the escape sequence length can be 4 * 16 + lead + tail. 2465 #define MAX_ESC_SEQ_LEN 80 2466 2467 /* 2468 * out_flush(): flush the output buffer 2469 */ 2470 void 2471 out_flush(void) 2472 { 2473 int len; 2474 2475 if (out_pos != 0) 2476 { 2477 // set out_pos to 0 before ui_write, to avoid recursiveness 2478 len = out_pos; 2479 out_pos = 0; 2480 ui_write(out_buf, len); 2481 } 2482 } 2483 2484 /* 2485 * out_flush_cursor(): flush the output buffer and redraw the cursor. 2486 * Does not flush recursively in the GUI to avoid slow drawing. 2487 */ 2488 void 2489 out_flush_cursor( 2490 int force UNUSED, // when TRUE, update cursor even when not moved 2491 int clear_selection UNUSED) // clear selection under cursor 2492 { 2493 mch_disable_flush(); 2494 out_flush(); 2495 mch_enable_flush(); 2496 #ifdef FEAT_GUI 2497 if (gui.in_use) 2498 { 2499 gui_update_cursor(force, clear_selection); 2500 gui_may_flush(); 2501 } 2502 #endif 2503 } 2504 2505 2506 /* 2507 * Sometimes a byte out of a multi-byte character is written with out_char(). 2508 * To avoid flushing half of the character, call this function first. 2509 */ 2510 void 2511 out_flush_check(void) 2512 { 2513 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES) 2514 out_flush(); 2515 } 2516 2517 #ifdef FEAT_GUI 2518 /* 2519 * out_trash(): Throw away the contents of the output buffer 2520 */ 2521 void 2522 out_trash(void) 2523 { 2524 out_pos = 0; 2525 } 2526 #endif 2527 2528 /* 2529 * out_char(c): put a byte into the output buffer. 2530 * Flush it if it becomes full. 2531 * This should not be used for outputting text on the screen (use functions 2532 * like msg_puts() and screen_putchar() for that). 2533 */ 2534 void 2535 out_char(unsigned c) 2536 { 2537 #if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X) 2538 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this) 2539 out_char('\r'); 2540 #endif 2541 2542 out_buf[out_pos++] = c; 2543 2544 // For testing we flush each time. 2545 if (out_pos >= OUT_SIZE || p_wd) 2546 out_flush(); 2547 } 2548 2549 /* 2550 * Output "c" like out_char(), but don't flush when p_wd is set. 2551 */ 2552 static void 2553 out_char_nf(unsigned c) 2554 { 2555 out_buf[out_pos++] = c; 2556 2557 if (out_pos >= OUT_SIZE) 2558 out_flush(); 2559 } 2560 2561 /* 2562 * A never-padding out_str(). 2563 * Use this whenever you don't want to run the string through tputs(). 2564 * tputs() above is harmless, but tputs() from the termcap library 2565 * is likely to strip off leading digits, that it mistakes for padding 2566 * information, and "%i", "%d", etc. 2567 * This should only be used for writing terminal codes, not for outputting 2568 * normal text (use functions like msg_puts() and screen_putchar() for that). 2569 */ 2570 void 2571 out_str_nf(char_u *s) 2572 { 2573 // avoid terminal strings being split up 2574 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN) 2575 out_flush(); 2576 2577 while (*s) 2578 out_char_nf(*s++); 2579 2580 // For testing we write one string at a time. 2581 if (p_wd) 2582 out_flush(); 2583 } 2584 2585 /* 2586 * A conditional-flushing out_str, mainly for visualbell. 2587 * Handles a delay internally, because termlib may not respect the delay or do 2588 * it at the wrong time. 2589 * Note: Only for terminal strings. 2590 */ 2591 void 2592 out_str_cf(char_u *s) 2593 { 2594 if (s != NULL && *s) 2595 { 2596 #ifdef HAVE_TGETENT 2597 char_u *p; 2598 #endif 2599 2600 #ifdef FEAT_GUI 2601 // Don't use tputs() when GUI is used, ncurses crashes. 2602 if (gui.in_use) 2603 { 2604 out_str_nf(s); 2605 return; 2606 } 2607 #endif 2608 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN) 2609 out_flush(); 2610 #ifdef HAVE_TGETENT 2611 for (p = s; *s; ++s) 2612 { 2613 // flush just before delay command 2614 if (*s == '$' && *(s + 1) == '<') 2615 { 2616 char_u save_c = *s; 2617 int duration = atoi((char *)s + 2); 2618 2619 *s = NUL; 2620 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf); 2621 *s = save_c; 2622 out_flush(); 2623 # ifdef ELAPSED_FUNC 2624 // Only sleep here if we can limit this happening in 2625 // vim_beep(). 2626 p = vim_strchr(s, '>'); 2627 if (p == NULL || duration <= 0) 2628 { 2629 // can't parse the time, don't sleep here 2630 p = s; 2631 } 2632 else 2633 { 2634 ++p; 2635 do_sleep(duration); 2636 } 2637 # else 2638 // Rely on the terminal library to sleep. 2639 p = s; 2640 # endif 2641 break; 2642 } 2643 } 2644 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf); 2645 #else 2646 while (*s) 2647 out_char_nf(*s++); 2648 #endif 2649 2650 // For testing we write one string at a time. 2651 if (p_wd) 2652 out_flush(); 2653 } 2654 } 2655 2656 /* 2657 * out_str(s): Put a character string a byte at a time into the output buffer. 2658 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw) 2659 * This should only be used for writing terminal codes, not for outputting 2660 * normal text (use functions like msg_puts() and screen_putchar() for that). 2661 */ 2662 void 2663 out_str(char_u *s) 2664 { 2665 if (s != NULL && *s) 2666 { 2667 #ifdef FEAT_GUI 2668 // Don't use tputs() when GUI is used, ncurses crashes. 2669 if (gui.in_use) 2670 { 2671 out_str_nf(s); 2672 return; 2673 } 2674 #endif 2675 // avoid terminal strings being split up 2676 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN) 2677 out_flush(); 2678 #ifdef HAVE_TGETENT 2679 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf); 2680 #else 2681 while (*s) 2682 out_char_nf(*s++); 2683 #endif 2684 2685 // For testing we write one string at a time. 2686 if (p_wd) 2687 out_flush(); 2688 } 2689 } 2690 2691 /* 2692 * cursor positioning using termcap parser. (jw) 2693 */ 2694 void 2695 term_windgoto(int row, int col) 2696 { 2697 OUT_STR(tgoto((char *)T_CM, col, row)); 2698 } 2699 2700 void 2701 term_cursor_right(int i) 2702 { 2703 OUT_STR(tgoto((char *)T_CRI, 0, i)); 2704 } 2705 2706 void 2707 term_append_lines(int line_count) 2708 { 2709 OUT_STR(tgoto((char *)T_CAL, 0, line_count)); 2710 } 2711 2712 void 2713 term_delete_lines(int line_count) 2714 { 2715 OUT_STR(tgoto((char *)T_CDL, 0, line_count)); 2716 } 2717 2718 #if defined(HAVE_TGETENT) || defined(PROTO) 2719 void 2720 term_set_winpos(int x, int y) 2721 { 2722 // Can't handle a negative value here 2723 if (x < 0) 2724 x = 0; 2725 if (y < 0) 2726 y = 0; 2727 OUT_STR(tgoto((char *)T_CWP, y, x)); 2728 } 2729 2730 # if defined(FEAT_TERMRESPONSE) || defined(PROTO) 2731 /* 2732 * Return TRUE if we can request the terminal for a response. 2733 */ 2734 static int 2735 can_get_termresponse() 2736 { 2737 return cur_tmode == TMODE_RAW 2738 && termcap_active 2739 # ifdef UNIX 2740 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd))) 2741 # endif 2742 && p_ek; 2743 } 2744 2745 /* 2746 * Set "status" to STATUS_SENT. 2747 */ 2748 static void 2749 termrequest_sent(termrequest_T *status) 2750 { 2751 status->tr_progress = STATUS_SENT; 2752 status->tr_start = time(NULL); 2753 } 2754 2755 /* 2756 * Return TRUE if any of the requests are in STATUS_SENT. 2757 */ 2758 static int 2759 termrequest_any_pending() 2760 { 2761 int i; 2762 time_t now = time(NULL); 2763 2764 for (i = 0; all_termrequests[i] != NULL; ++i) 2765 { 2766 if (all_termrequests[i]->tr_progress == STATUS_SENT) 2767 { 2768 if (all_termrequests[i]->tr_start > 0 && now > 0 2769 && all_termrequests[i]->tr_start + 2 < now) 2770 // Sent the request more than 2 seconds ago and didn't get a 2771 // response, assume it failed. 2772 all_termrequests[i]->tr_progress = STATUS_FAIL; 2773 else 2774 return TRUE; 2775 } 2776 } 2777 return FALSE; 2778 } 2779 2780 static int winpos_x = -1; 2781 static int winpos_y = -1; 2782 static int did_request_winpos = 0; 2783 2784 # if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO) 2785 /* 2786 * Try getting the Vim window position from the terminal. 2787 * Returns OK or FAIL. 2788 */ 2789 int 2790 term_get_winpos(int *x, int *y, varnumber_T timeout) 2791 { 2792 int count = 0; 2793 int prev_winpos_x = winpos_x; 2794 int prev_winpos_y = winpos_y; 2795 2796 if (*T_CGP == NUL || !can_get_termresponse()) 2797 return FAIL; 2798 winpos_x = -1; 2799 winpos_y = -1; 2800 ++did_request_winpos; 2801 termrequest_sent(&winpos_status); 2802 OUT_STR(T_CGP); 2803 out_flush(); 2804 2805 // Try reading the result for "timeout" msec. 2806 while (count++ <= timeout / 10 && !got_int) 2807 { 2808 (void)vpeekc_nomap(); 2809 if (winpos_x >= 0 && winpos_y >= 0) 2810 { 2811 *x = winpos_x; 2812 *y = winpos_y; 2813 return OK; 2814 } 2815 ui_delay(10L, FALSE); 2816 } 2817 // Do not reset "did_request_winpos", if we timed out the response might 2818 // still come later and we must consume it. 2819 2820 winpos_x = prev_winpos_x; 2821 winpos_y = prev_winpos_y; 2822 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0) 2823 { 2824 // Polling: return previous values if we have them. 2825 *x = winpos_x; 2826 *y = winpos_y; 2827 return OK; 2828 } 2829 2830 return FALSE; 2831 } 2832 # endif 2833 # endif 2834 2835 void 2836 term_set_winsize(int height, int width) 2837 { 2838 OUT_STR(tgoto((char *)T_CWS, width, height)); 2839 } 2840 #endif 2841 2842 static void 2843 term_color(char_u *s, int n) 2844 { 2845 char buf[20]; 2846 int i = *s == CSI ? 1 : 2; 2847 // index in s[] just after <Esc>[ or CSI 2848 2849 // Special handling of 16 colors, because termcap can't handle it 2850 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used 2851 // Also accept CSI instead of <Esc>[ 2852 if (n >= 8 && t_colors >= 16 2853 && ((s[0] == ESC && s[1] == '[') 2854 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS) 2855 || (s[0] == ESC && s[1] == '|') 2856 #endif 2857 || (s[0] == CSI && (i = 1) == 1)) 2858 && s[i] != NUL 2859 && (STRCMP(s + i + 1, "%p1%dm") == 0 2860 || STRCMP(s + i + 1, "%dm") == 0) 2861 && (s[i] == '3' || s[i] == '4')) 2862 { 2863 #ifdef TERMINFO 2864 char *format = "%s%s%%p1%%dm"; 2865 #else 2866 char *format = "%s%s%%dm"; 2867 #endif 2868 char *lead = i == 2 ? ( 2869 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS) 2870 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") : 2871 #endif 2872 IF_EB("\033[", ESC_STR "[")) : "\233"; 2873 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9") 2874 : (n >= 16 ? "48;5;" : "10"); 2875 2876 sprintf(buf, format, lead, tail); 2877 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8)); 2878 } 2879 else 2880 OUT_STR(tgoto((char *)s, 0, n)); 2881 } 2882 2883 void 2884 term_fg_color(int n) 2885 { 2886 // Use "AF" termcap entry if present, "Sf" entry otherwise 2887 if (*T_CAF) 2888 term_color(T_CAF, n); 2889 else if (*T_CSF) 2890 term_color(T_CSF, n); 2891 } 2892 2893 void 2894 term_bg_color(int n) 2895 { 2896 // Use "AB" termcap entry if present, "Sb" entry otherwise 2897 if (*T_CAB) 2898 term_color(T_CAB, n); 2899 else if (*T_CSB) 2900 term_color(T_CSB, n); 2901 } 2902 2903 /* 2904 * Return "dark" or "light" depending on the kind of terminal. 2905 * This is just guessing! Recognized are: 2906 * "linux" Linux console 2907 * "screen.linux" Linux console with screen 2908 * "cygwin.*" Cygwin shell 2909 * "putty.*" Putty program 2910 * We also check the COLORFGBG environment variable, which is set by 2911 * rxvt and derivatives. This variable contains either two or three 2912 * values separated by semicolons; we want the last value in either 2913 * case. If this value is 0-6 or 8, our background is dark. 2914 */ 2915 char_u * 2916 term_bg_default(void) 2917 { 2918 #if defined(MSWIN) 2919 // DOS console is nearly always black 2920 return (char_u *)"dark"; 2921 #else 2922 char_u *p; 2923 2924 if (STRCMP(T_NAME, "linux") == 0 2925 || STRCMP(T_NAME, "screen.linux") == 0 2926 || STRNCMP(T_NAME, "cygwin", 6) == 0 2927 || STRNCMP(T_NAME, "putty", 5) == 0 2928 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL 2929 && (p = vim_strrchr(p, ';')) != NULL 2930 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') 2931 && p[2] == NUL)) 2932 return (char_u *)"dark"; 2933 return (char_u *)"light"; 2934 #endif 2935 } 2936 2937 #if defined(FEAT_TERMGUICOLORS) || defined(PROTO) 2938 2939 #define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF) 2940 #define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF) 2941 #define BLUE(rgb) (((long_u)(rgb) ) & 0xFF) 2942 2943 static void 2944 term_rgb_color(char_u *s, guicolor_T rgb) 2945 { 2946 #define MAX_COLOR_STR_LEN 100 2947 char buf[MAX_COLOR_STR_LEN]; 2948 2949 vim_snprintf(buf, MAX_COLOR_STR_LEN, 2950 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb)); 2951 OUT_STR(buf); 2952 } 2953 2954 void 2955 term_fg_rgb_color(guicolor_T rgb) 2956 { 2957 term_rgb_color(T_8F, rgb); 2958 } 2959 2960 void 2961 term_bg_rgb_color(guicolor_T rgb) 2962 { 2963 term_rgb_color(T_8B, rgb); 2964 } 2965 #endif 2966 2967 #if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \ 2968 || defined(MACOS_X))) || defined(PROTO) 2969 /* 2970 * Generic function to set window title, using t_ts and t_fs. 2971 */ 2972 void 2973 term_settitle(char_u *title) 2974 { 2975 // t_ts takes one argument: column in status line 2976 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start 2977 out_str_nf(title); 2978 out_str(T_FS); // set title end 2979 out_flush(); 2980 } 2981 2982 /* 2983 * Tell the terminal to push (save) the title and/or icon, so that it can be 2984 * popped (restored) later. 2985 */ 2986 void 2987 term_push_title(int which) 2988 { 2989 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL) 2990 { 2991 OUT_STR(T_CST); 2992 out_flush(); 2993 } 2994 2995 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL) 2996 { 2997 OUT_STR(T_SSI); 2998 out_flush(); 2999 } 3000 } 3001 3002 /* 3003 * Tell the terminal to pop the title and/or icon. 3004 */ 3005 void 3006 term_pop_title(int which) 3007 { 3008 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL) 3009 { 3010 OUT_STR(T_CRT); 3011 out_flush(); 3012 } 3013 3014 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL) 3015 { 3016 OUT_STR(T_SRI); 3017 out_flush(); 3018 } 3019 } 3020 #endif 3021 3022 /* 3023 * Make sure we have a valid set or terminal options. 3024 * Replace all entries that are NULL by empty_option 3025 */ 3026 void 3027 ttest(int pairs) 3028 { 3029 char_u *env_colors; 3030 3031 check_options(); // make sure no options are NULL 3032 3033 /* 3034 * MUST have "cm": cursor motion. 3035 */ 3036 if (*T_CM == NUL) 3037 emsg(_("E437: terminal capability \"cm\" required")); 3038 3039 /* 3040 * if "cs" defined, use a scroll region, it's faster. 3041 */ 3042 if (*T_CS != NUL) 3043 scroll_region = TRUE; 3044 else 3045 scroll_region = FALSE; 3046 3047 if (pairs) 3048 { 3049 /* 3050 * optional pairs 3051 */ 3052 // TP goes to normal mode for TI (invert) and TB (bold) 3053 if (*T_ME == NUL) 3054 T_ME = T_MR = T_MD = T_MB = empty_option; 3055 if (*T_SO == NUL || *T_SE == NUL) 3056 T_SO = T_SE = empty_option; 3057 if (*T_US == NUL || *T_UE == NUL) 3058 T_US = T_UE = empty_option; 3059 if (*T_CZH == NUL || *T_CZR == NUL) 3060 T_CZH = T_CZR = empty_option; 3061 3062 // T_VE is needed even though T_VI is not defined 3063 if (*T_VE == NUL) 3064 T_VI = empty_option; 3065 3066 // if 'mr' or 'me' is not defined use 'so' and 'se' 3067 if (*T_ME == NUL) 3068 { 3069 T_ME = T_SE; 3070 T_MR = T_SO; 3071 T_MD = T_SO; 3072 } 3073 3074 // if 'so' or 'se' is not defined use 'mr' and 'me' 3075 if (*T_SO == NUL) 3076 { 3077 T_SE = T_ME; 3078 if (*T_MR == NUL) 3079 T_SO = T_MD; 3080 else 3081 T_SO = T_MR; 3082 } 3083 3084 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me' 3085 if (*T_CZH == NUL) 3086 { 3087 T_CZR = T_ME; 3088 if (*T_MR == NUL) 3089 T_CZH = T_MD; 3090 else 3091 T_CZH = T_MR; 3092 } 3093 3094 // "Sb" and "Sf" come in pairs 3095 if (*T_CSB == NUL || *T_CSF == NUL) 3096 { 3097 T_CSB = empty_option; 3098 T_CSF = empty_option; 3099 } 3100 3101 // "AB" and "AF" come in pairs 3102 if (*T_CAB == NUL || *T_CAF == NUL) 3103 { 3104 T_CAB = empty_option; 3105 T_CAF = empty_option; 3106 } 3107 3108 // if 'Sb' and 'AB' are not defined, reset "Co" 3109 if (*T_CSB == NUL && *T_CAB == NUL) 3110 free_one_termoption(T_CCO); 3111 3112 // Set 'weirdinvert' according to value of 't_xs' 3113 p_wiv = (*T_XS != NUL); 3114 } 3115 need_gather = TRUE; 3116 3117 // Set t_colors to the value of $COLORS or t_Co. 3118 t_colors = atoi((char *)T_CCO); 3119 env_colors = mch_getenv((char_u *)"COLORS"); 3120 if (env_colors != NULL && isdigit(*env_colors)) 3121 { 3122 int colors = atoi((char *)env_colors); 3123 3124 if (colors != t_colors) 3125 set_color_count(colors); 3126 } 3127 } 3128 3129 #if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \ 3130 || defined(PROTO) 3131 /* 3132 * Represent the given long_u as individual bytes, with the most significant 3133 * byte first, and store them in dst. 3134 */ 3135 void 3136 add_long_to_buf(long_u val, char_u *dst) 3137 { 3138 int i; 3139 int shift; 3140 3141 for (i = 1; i <= (int)sizeof(long_u); i++) 3142 { 3143 shift = 8 * (sizeof(long_u) - i); 3144 dst[i - 1] = (char_u) ((val >> shift) & 0xff); 3145 } 3146 } 3147 3148 /* 3149 * Interpret the next string of bytes in buf as a long integer, with the most 3150 * significant byte first. Note that it is assumed that buf has been through 3151 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each. 3152 * Puts result in val, and returns the number of bytes read from buf 3153 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes 3154 * were present. 3155 */ 3156 static int 3157 get_long_from_buf(char_u *buf, long_u *val) 3158 { 3159 int len; 3160 char_u bytes[sizeof(long_u)]; 3161 int i; 3162 int shift; 3163 3164 *val = 0; 3165 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u)); 3166 if (len != -1) 3167 { 3168 for (i = 0; i < (int)sizeof(long_u); i++) 3169 { 3170 shift = 8 * (sizeof(long_u) - 1 - i); 3171 *val += (long_u)bytes[i] << shift; 3172 } 3173 } 3174 return len; 3175 } 3176 #endif 3177 3178 /* 3179 * Read the next num_bytes bytes from buf, and store them in bytes. Assume 3180 * that buf has been through inchar(). Returns the actual number of bytes used 3181 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were 3182 * available. 3183 */ 3184 int 3185 get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes) 3186 { 3187 int len = 0; 3188 int i; 3189 char_u c; 3190 3191 for (i = 0; i < num_bytes; i++) 3192 { 3193 if ((c = buf[len++]) == NUL) 3194 return -1; 3195 if (c == K_SPECIAL) 3196 { 3197 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen? 3198 return -1; 3199 if (buf[len++] == (int)KS_ZERO) 3200 c = NUL; 3201 // else it should be KS_SPECIAL; when followed by KE_FILLER c is 3202 // K_SPECIAL, or followed by KE_CSI and c must be CSI. 3203 if (buf[len++] == (int)KE_CSI) 3204 c = CSI; 3205 } 3206 else if (c == CSI && buf[len] == KS_EXTRA 3207 && buf[len + 1] == (int)KE_CSI) 3208 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with 3209 // the start of a special key, see add_to_input_buf_csi(). 3210 len += 2; 3211 bytes[i] = c; 3212 } 3213 return len; 3214 } 3215 3216 /* 3217 * Check if the new shell size is valid, correct it if it's too small or way 3218 * too big. 3219 */ 3220 void 3221 check_shellsize(void) 3222 { 3223 if (Rows < min_rows()) // need room for one window and command line 3224 Rows = min_rows(); 3225 limit_screen_size(); 3226 } 3227 3228 /* 3229 * Limit Rows and Columns to avoid an overflow in Rows * Columns. 3230 */ 3231 void 3232 limit_screen_size(void) 3233 { 3234 if (Columns < MIN_COLUMNS) 3235 Columns = MIN_COLUMNS; 3236 else if (Columns > 10000) 3237 Columns = 10000; 3238 if (Rows > 1000) 3239 Rows = 1000; 3240 } 3241 3242 /* 3243 * Invoked just before the screen structures are going to be (re)allocated. 3244 */ 3245 void 3246 win_new_shellsize(void) 3247 { 3248 static int old_Rows = 0; 3249 static int old_Columns = 0; 3250 3251 if (old_Rows != Rows || old_Columns != Columns) 3252 ui_new_shellsize(); 3253 if (old_Rows != Rows) 3254 { 3255 // if 'window' uses the whole screen, keep it using that 3256 if (p_window == old_Rows - 1 || old_Rows == 0) 3257 p_window = Rows - 1; 3258 old_Rows = Rows; 3259 shell_new_rows(); // update window sizes 3260 } 3261 if (old_Columns != Columns) 3262 { 3263 old_Columns = Columns; 3264 shell_new_columns(); // update window sizes 3265 } 3266 } 3267 3268 /* 3269 * Call this function when the Vim shell has been resized in any way. 3270 * Will obtain the current size and redraw (also when size didn't change). 3271 */ 3272 void 3273 shell_resized(void) 3274 { 3275 set_shellsize(0, 0, FALSE); 3276 } 3277 3278 /* 3279 * Check if the shell size changed. Handle a resize. 3280 * When the size didn't change, nothing happens. 3281 */ 3282 void 3283 shell_resized_check(void) 3284 { 3285 int old_Rows = Rows; 3286 int old_Columns = Columns; 3287 3288 if (!exiting 3289 #ifdef FEAT_GUI 3290 // Do not get the size when executing a shell command during 3291 // startup. 3292 && !gui.starting 3293 #endif 3294 ) 3295 { 3296 (void)ui_get_shellsize(); 3297 check_shellsize(); 3298 if (old_Rows != Rows || old_Columns != Columns) 3299 shell_resized(); 3300 } 3301 } 3302 3303 /* 3304 * Set size of the Vim shell. 3305 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real 3306 * window size (this is used for the :win command). 3307 * If 'mustset' is FALSE, we may try to get the real window size and if 3308 * it fails use 'width' and 'height'. 3309 */ 3310 void 3311 set_shellsize(int width, int height, int mustset) 3312 { 3313 static int busy = FALSE; 3314 3315 /* 3316 * Avoid recursiveness, can happen when setting the window size causes 3317 * another window-changed signal. 3318 */ 3319 if (busy) 3320 return; 3321 3322 if (width < 0 || height < 0) // just checking... 3323 return; 3324 3325 if (State == HITRETURN || State == SETWSIZE) 3326 { 3327 // postpone the resizing 3328 State = SETWSIZE; 3329 return; 3330 } 3331 3332 if (updating_screen) 3333 // resizing while in update_screen() may cause a crash 3334 return; 3335 3336 // curwin->w_buffer can be NULL when we are closing a window and the 3337 // buffer has already been closed and removing a scrollbar causes a resize 3338 // event. Don't resize then, it will happen after entering another buffer. 3339 if (curwin->w_buffer == NULL) 3340 return; 3341 3342 ++busy; 3343 3344 #ifdef AMIGA 3345 out_flush(); // must do this before mch_get_shellsize() for 3346 // some obscure reason 3347 #endif 3348 3349 if (mustset || (ui_get_shellsize() == FAIL && height != 0)) 3350 { 3351 Rows = height; 3352 Columns = width; 3353 check_shellsize(); 3354 ui_set_shellsize(mustset); 3355 } 3356 else 3357 check_shellsize(); 3358 3359 // The window layout used to be adjusted here, but it now happens in 3360 // screenalloc() (also invoked from screenclear()). That is because the 3361 // "busy" check above may skip this, but not screenalloc(). 3362 3363 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM) 3364 screenclear(); 3365 else 3366 screen_start(); // don't know where cursor is now 3367 3368 if (starting != NO_SCREEN) 3369 { 3370 #ifdef FEAT_TITLE 3371 maketitle(); 3372 #endif 3373 changed_line_abv_curs(); 3374 invalidate_botline(); 3375 3376 /* 3377 * We only redraw when it's needed: 3378 * - While at the more prompt or executing an external command, don't 3379 * redraw, but position the cursor. 3380 * - While editing the command line, only redraw that. 3381 * - in Ex mode, don't redraw anything. 3382 * - Otherwise, redraw right now, and position the cursor. 3383 * Always need to call update_screen() or screenalloc(), to make 3384 * sure Rows/Columns and the size of ScreenLines[] is correct! 3385 */ 3386 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM 3387 || exmode_active) 3388 { 3389 screenalloc(FALSE); 3390 repeat_message(); 3391 } 3392 else 3393 { 3394 if (curwin->w_p_scb) 3395 do_check_scrollbind(TRUE); 3396 if (State & CMDLINE) 3397 { 3398 update_screen(NOT_VALID); 3399 redrawcmdline(); 3400 } 3401 else 3402 { 3403 update_topline(); 3404 if (pum_visible()) 3405 { 3406 redraw_later(NOT_VALID); 3407 ins_compl_show_pum(); 3408 } 3409 update_screen(NOT_VALID); 3410 if (redrawing()) 3411 setcursor(); 3412 } 3413 } 3414 cursor_on(); // redrawing may have switched it off 3415 } 3416 out_flush(); 3417 --busy; 3418 } 3419 3420 /* 3421 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external 3422 * commands and Ex mode). 3423 */ 3424 void 3425 settmode(int tmode) 3426 { 3427 #ifdef FEAT_GUI 3428 // don't set the term where gvim was started to any mode 3429 if (gui.in_use) 3430 return; 3431 #endif 3432 3433 if (full_screen) 3434 { 3435 /* 3436 * When returning after calling a shell we want to really set the 3437 * terminal to raw mode, even though we think it already is, because 3438 * the shell program may have reset the terminal mode. 3439 * When we think the terminal is normal, don't try to set it to 3440 * normal again, because that causes problems (logout!) on some 3441 * machines. 3442 */ 3443 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK) 3444 { 3445 #ifdef FEAT_TERMRESPONSE 3446 # ifdef FEAT_GUI 3447 if (!gui.in_use && !gui.starting) 3448 # endif 3449 { 3450 // May need to check for T_CRV response and termcodes, it 3451 // doesn't work in Cooked mode, an external program may get 3452 // them. 3453 if (tmode != TMODE_RAW && termrequest_any_pending()) 3454 (void)vpeekc_nomap(); 3455 check_for_codes_from_term(); 3456 } 3457 #endif 3458 if (tmode != TMODE_RAW) 3459 mch_setmouse(FALSE); // switch mouse off 3460 if (termcap_active) 3461 { 3462 if (tmode != TMODE_RAW) 3463 out_str(T_BD); // disable bracketed paste mode 3464 else 3465 out_str(T_BE); // enable bracketed paste mode (should 3466 // be before mch_settmode(). 3467 } 3468 out_flush(); 3469 mch_settmode(tmode); // machine specific function 3470 cur_tmode = tmode; 3471 if (tmode == TMODE_RAW) 3472 setmouse(); // may switch mouse on 3473 out_flush(); 3474 } 3475 #ifdef FEAT_TERMRESPONSE 3476 may_req_termresponse(); 3477 #endif 3478 } 3479 } 3480 3481 void 3482 starttermcap(void) 3483 { 3484 if (full_screen && !termcap_active) 3485 { 3486 out_str(T_TI); // start termcap mode 3487 out_str(T_CTI); // start "raw" mode 3488 out_str(T_KS); // start "keypad transmit" mode 3489 out_str(T_BE); // enable bracketed paste mode 3490 out_flush(); 3491 termcap_active = TRUE; 3492 screen_start(); // don't know where cursor is now 3493 #ifdef FEAT_TERMRESPONSE 3494 # ifdef FEAT_GUI 3495 if (!gui.in_use && !gui.starting) 3496 # endif 3497 { 3498 may_req_termresponse(); 3499 // Immediately check for a response. If t_Co changes, we don't 3500 // want to redraw with wrong colors first. 3501 if (crv_status.tr_progress == STATUS_SENT) 3502 check_for_codes_from_term(); 3503 } 3504 #endif 3505 } 3506 } 3507 3508 void 3509 stoptermcap(void) 3510 { 3511 screen_stop_highlight(); 3512 reset_cterm_colors(); 3513 if (termcap_active) 3514 { 3515 #ifdef FEAT_TERMRESPONSE 3516 # ifdef FEAT_GUI 3517 if (!gui.in_use && !gui.starting) 3518 # endif 3519 { 3520 // May need to discard T_CRV, T_U7 or T_RBG response. 3521 if (termrequest_any_pending()) 3522 { 3523 # ifdef UNIX 3524 // Give the terminal a chance to respond. 3525 mch_delay(100L, FALSE); 3526 # endif 3527 # ifdef TCIFLUSH 3528 // Discard data received but not read. 3529 if (exiting) 3530 tcflush(fileno(stdin), TCIFLUSH); 3531 # endif 3532 } 3533 // Check for termcodes first, otherwise an external program may 3534 // get them. 3535 check_for_codes_from_term(); 3536 } 3537 #endif 3538 out_str(T_BD); // disable bracketed paste mode 3539 out_str(T_KE); // stop "keypad transmit" mode 3540 out_flush(); 3541 termcap_active = FALSE; 3542 cursor_on(); // just in case it is still off 3543 out_str(T_CTE); // stop "raw" mode 3544 out_str(T_TE); // stop termcap mode 3545 screen_start(); // don't know where cursor is now 3546 out_flush(); 3547 } 3548 } 3549 3550 #if defined(FEAT_TERMRESPONSE) || defined(PROTO) 3551 /* 3552 * Request version string (for xterm) when needed. 3553 * Only do this after switching to raw mode, otherwise the result will be 3554 * echoed. 3555 * Only do this after startup has finished, to avoid that the response comes 3556 * while executing "-c !cmd" or even after "-c quit". 3557 * Only do this after termcap mode has been started, otherwise the codes for 3558 * the cursor keys may be wrong. 3559 * Only do this when 'esckeys' is on, otherwise the response causes trouble in 3560 * Insert mode. 3561 * On Unix only do it when both output and input are a tty (avoid writing 3562 * request to terminal while reading from a file). 3563 * The result is caught in check_termcode(). 3564 */ 3565 void 3566 may_req_termresponse(void) 3567 { 3568 if (crv_status.tr_progress == STATUS_GET 3569 && can_get_termresponse() 3570 && starting == 0 3571 && *T_CRV != NUL) 3572 { 3573 LOG_TR(("Sending CRV request")); 3574 out_str(T_CRV); 3575 termrequest_sent(&crv_status); 3576 // check for the characters now, otherwise they might be eaten by 3577 // get_keystroke() 3578 out_flush(); 3579 (void)vpeekc_nomap(); 3580 } 3581 } 3582 3583 /* 3584 * Check how the terminal treats ambiguous character width (UAX #11). 3585 * First, we move the cursor to (1, 0) and print a test ambiguous character 3586 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position. 3587 * If the terminal treats \u25bd as single width, the position is (1, 1), 3588 * or if it is treated as double width, that will be (1, 2). 3589 * This function has the side effect that changes cursor position, so 3590 * it must be called immediately after entering termcap mode. 3591 */ 3592 void 3593 may_req_ambiguous_char_width(void) 3594 { 3595 if (u7_status.tr_progress == STATUS_GET 3596 && can_get_termresponse() 3597 && starting == 0 3598 && *T_U7 != NUL 3599 && !option_was_set((char_u *)"ambiwidth")) 3600 { 3601 char_u buf[16]; 3602 3603 LOG_TR(("Sending U7 request")); 3604 // Do this in the second row. In the first row the returned sequence 3605 // may be CSI 1;2R, which is the same as <S-F3>. 3606 term_windgoto(1, 0); 3607 buf[mb_char2bytes(0x25bd, buf)] = 0; 3608 out_str(buf); 3609 out_str(T_U7); 3610 termrequest_sent(&u7_status); 3611 out_flush(); 3612 3613 // This overwrites a few characters on the screen, a redraw is needed 3614 // after this. Clear them out for now. 3615 screen_stop_highlight(); 3616 term_windgoto(1, 0); 3617 out_str((char_u *)" "); 3618 term_windgoto(0, 0); 3619 3620 // Need to reset the known cursor position. 3621 screen_start(); 3622 3623 // check for the characters now, otherwise they might be eaten by 3624 // get_keystroke() 3625 out_flush(); 3626 (void)vpeekc_nomap(); 3627 } 3628 } 3629 3630 /* 3631 * Similar to requesting the version string: Request the terminal background 3632 * color when it is the right moment. 3633 */ 3634 void 3635 may_req_bg_color(void) 3636 { 3637 if (can_get_termresponse() && starting == 0) 3638 { 3639 int didit = FALSE; 3640 3641 # ifdef FEAT_TERMINAL 3642 // Only request foreground if t_RF is set. 3643 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL) 3644 { 3645 LOG_TR(("Sending FG request")); 3646 out_str(T_RFG); 3647 termrequest_sent(&rfg_status); 3648 didit = TRUE; 3649 } 3650 # endif 3651 3652 // Only request background if t_RB is set. 3653 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL) 3654 { 3655 LOG_TR(("Sending BG request")); 3656 out_str(T_RBG); 3657 termrequest_sent(&rbg_status); 3658 didit = TRUE; 3659 } 3660 3661 if (didit) 3662 { 3663 // check for the characters now, otherwise they might be eaten by 3664 // get_keystroke() 3665 out_flush(); 3666 (void)vpeekc_nomap(); 3667 } 3668 } 3669 } 3670 3671 # ifdef DEBUG_TERMRESPONSE 3672 static void 3673 log_tr(const char *fmt, ...) 3674 { 3675 static FILE *fd_tr = NULL; 3676 static proftime_T start; 3677 proftime_T now; 3678 va_list ap; 3679 3680 if (fd_tr == NULL) 3681 { 3682 fd_tr = fopen("termresponse.log", "w"); 3683 profile_start(&start); 3684 } 3685 now = start; 3686 profile_end(&now); 3687 fprintf(fd_tr, "%s: %s ", profile_msg(&now), 3688 must_redraw == NOT_VALID ? "NV" 3689 : must_redraw == CLEAR ? "CL" : " "); 3690 va_start(ap, fmt); 3691 vfprintf(fd_tr, fmt, ap); 3692 va_end(ap); 3693 fputc('\n', fd_tr); 3694 fflush(fd_tr); 3695 } 3696 # endif 3697 #endif 3698 3699 /* 3700 * Return TRUE when saving and restoring the screen. 3701 */ 3702 int 3703 swapping_screen(void) 3704 { 3705 return (full_screen && *T_TI != NUL); 3706 } 3707 3708 /* 3709 * By outputting the 'cursor very visible' termcap code, for some windowed 3710 * terminals this makes the screen scrolled to the correct position. 3711 * Used when starting Vim or returning from a shell. 3712 */ 3713 void 3714 scroll_start(void) 3715 { 3716 if (*T_VS != NUL && *T_CVS != NUL) 3717 { 3718 out_str(T_VS); 3719 out_str(T_CVS); 3720 screen_start(); // don't know where cursor is now 3721 } 3722 } 3723 3724 static int cursor_is_off = FALSE; 3725 3726 /* 3727 * Enable the cursor without checking if it's already enabled. 3728 */ 3729 void 3730 cursor_on_force(void) 3731 { 3732 out_str(T_VE); 3733 cursor_is_off = FALSE; 3734 } 3735 3736 /* 3737 * Enable the cursor if it's currently off. 3738 */ 3739 void 3740 cursor_on(void) 3741 { 3742 if (cursor_is_off) 3743 cursor_on_force(); 3744 } 3745 3746 /* 3747 * Disable the cursor. 3748 */ 3749 void 3750 cursor_off(void) 3751 { 3752 if (full_screen && !cursor_is_off) 3753 { 3754 out_str(T_VI); // disable cursor 3755 cursor_is_off = TRUE; 3756 } 3757 } 3758 3759 #if defined(CURSOR_SHAPE) || defined(PROTO) 3760 /* 3761 * Set cursor shape to match Insert or Replace mode. 3762 */ 3763 void 3764 term_cursor_mode(int forced) 3765 { 3766 static int showing_mode = -1; 3767 char_u *p; 3768 3769 // Only do something when redrawing the screen and we can restore the 3770 // mode. 3771 if (!full_screen || *T_CEI == NUL) 3772 { 3773 # ifdef FEAT_TERMRESPONSE 3774 if (forced && initial_cursor_shape > 0) 3775 // Restore to initial values. 3776 term_cursor_shape(initial_cursor_shape, initial_cursor_blink); 3777 # endif 3778 return; 3779 } 3780 3781 if ((State & REPLACE) == REPLACE) 3782 { 3783 if (forced || showing_mode != REPLACE) 3784 { 3785 if (*T_CSR != NUL) 3786 p = T_CSR; // Replace mode cursor 3787 else 3788 p = T_CSI; // fall back to Insert mode cursor 3789 if (*p != NUL) 3790 { 3791 out_str(p); 3792 showing_mode = REPLACE; 3793 } 3794 } 3795 } 3796 else if (State & INSERT) 3797 { 3798 if ((forced || showing_mode != INSERT) && *T_CSI != NUL) 3799 { 3800 out_str(T_CSI); // Insert mode cursor 3801 showing_mode = INSERT; 3802 } 3803 } 3804 else if (forced || showing_mode != NORMAL) 3805 { 3806 out_str(T_CEI); // non-Insert mode cursor 3807 showing_mode = NORMAL; 3808 } 3809 } 3810 3811 # if defined(FEAT_TERMINAL) || defined(PROTO) 3812 void 3813 term_cursor_color(char_u *color) 3814 { 3815 if (*T_CSC != NUL) 3816 { 3817 out_str(T_CSC); // set cursor color start 3818 out_str_nf(color); 3819 out_str(T_CEC); // set cursor color end 3820 out_flush(); 3821 } 3822 } 3823 # endif 3824 3825 int 3826 blink_state_is_inverted() 3827 { 3828 #ifdef FEAT_TERMRESPONSE 3829 return rbm_status.tr_progress == STATUS_GOT 3830 && rcs_status.tr_progress == STATUS_GOT 3831 && initial_cursor_blink != initial_cursor_shape_blink; 3832 #else 3833 return FALSE; 3834 #endif 3835 } 3836 3837 /* 3838 * "shape": 1 = block, 2 = underline, 3 = vertical bar 3839 */ 3840 void 3841 term_cursor_shape(int shape, int blink) 3842 { 3843 if (*T_CSH != NUL) 3844 { 3845 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink)); 3846 out_flush(); 3847 } 3848 else 3849 { 3850 int do_blink = blink; 3851 3852 // t_SH is empty: try setting just the blink state. 3853 // The blink flags are XORed together, if the initial blinking from 3854 // style and shape differs, we need to invert the flag here. 3855 if (blink_state_is_inverted()) 3856 do_blink = !blink; 3857 3858 if (do_blink && *T_VS != NUL) 3859 { 3860 out_str(T_VS); 3861 out_flush(); 3862 } 3863 else if (!do_blink && *T_CVS != NUL) 3864 { 3865 out_str(T_CVS); 3866 out_flush(); 3867 } 3868 } 3869 } 3870 #endif 3871 3872 /* 3873 * Set scrolling region for window 'wp'. 3874 * The region starts 'off' lines from the start of the window. 3875 * Also set the vertical scroll region for a vertically split window. Always 3876 * the full width of the window, excluding the vertical separator. 3877 */ 3878 void 3879 scroll_region_set(win_T *wp, int off) 3880 { 3881 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1, 3882 W_WINROW(wp) + off)); 3883 if (*T_CSV != NUL && wp->w_width != Columns) 3884 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1, 3885 wp->w_wincol)); 3886 screen_start(); // don't know where cursor is now 3887 } 3888 3889 /* 3890 * Reset scrolling region to the whole screen. 3891 */ 3892 void 3893 scroll_region_reset(void) 3894 { 3895 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0)); 3896 if (*T_CSV != NUL) 3897 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0)); 3898 screen_start(); // don't know where cursor is now 3899 } 3900 3901 3902 /* 3903 * List of terminal codes that are currently recognized. 3904 */ 3905 3906 static struct termcode 3907 { 3908 char_u name[2]; // termcap name of entry 3909 char_u *code; // terminal code (in allocated memory) 3910 int len; // STRLEN(code) 3911 int modlen; // length of part before ";*~". 3912 } *termcodes = NULL; 3913 3914 static int tc_max_len = 0; // number of entries that termcodes[] can hold 3915 static int tc_len = 0; // current number of entries in termcodes[] 3916 3917 static int termcode_star(char_u *code, int len); 3918 3919 void 3920 clear_termcodes(void) 3921 { 3922 while (tc_len > 0) 3923 vim_free(termcodes[--tc_len].code); 3924 VIM_CLEAR(termcodes); 3925 tc_max_len = 0; 3926 3927 #ifdef HAVE_TGETENT 3928 BC = (char *)empty_option; 3929 UP = (char *)empty_option; 3930 PC = NUL; // set pad character to NUL 3931 ospeed = 0; 3932 #endif 3933 3934 need_gather = TRUE; // need to fill termleader[] 3935 } 3936 3937 #define ATC_FROM_TERM 55 3938 3939 /* 3940 * Add a new entry to the list of terminal codes. 3941 * The list is kept alphabetical for ":set termcap" 3942 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired. 3943 * "flags" can also be ATC_FROM_TERM for got_code_from_term(). 3944 */ 3945 void 3946 add_termcode(char_u *name, char_u *string, int flags) 3947 { 3948 struct termcode *new_tc; 3949 int i, j; 3950 char_u *s; 3951 int len; 3952 3953 if (string == NULL || *string == NUL) 3954 { 3955 del_termcode(name); 3956 return; 3957 } 3958 3959 #if defined(MSWIN) && !defined(FEAT_GUI) 3960 s = vim_strnsave(string, (int)STRLEN(string) + 1); 3961 #else 3962 # ifdef VIMDLL 3963 if (!gui.in_use) 3964 s = vim_strnsave(string, (int)STRLEN(string) + 1); 3965 else 3966 # endif 3967 s = vim_strsave(string); 3968 #endif 3969 if (s == NULL) 3970 return; 3971 3972 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. 3973 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0) 3974 { 3975 STRMOVE(s, s + 1); 3976 s[0] = term_7to8bit(string); 3977 } 3978 3979 #if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) 3980 # ifdef VIMDLL 3981 if (!gui.in_use) 3982 # endif 3983 { 3984 if (s[0] == K_NUL) 3985 { 3986 STRMOVE(s + 1, s); 3987 s[1] = 3; 3988 } 3989 } 3990 #endif 3991 3992 len = (int)STRLEN(s); 3993 3994 need_gather = TRUE; // need to fill termleader[] 3995 3996 /* 3997 * need to make space for more entries 3998 */ 3999 if (tc_len == tc_max_len) 4000 { 4001 tc_max_len += 20; 4002 new_tc = ALLOC_MULT(struct termcode, tc_max_len); 4003 if (new_tc == NULL) 4004 { 4005 tc_max_len -= 20; 4006 return; 4007 } 4008 for (i = 0; i < tc_len; ++i) 4009 new_tc[i] = termcodes[i]; 4010 vim_free(termcodes); 4011 termcodes = new_tc; 4012 } 4013 4014 /* 4015 * Look for existing entry with the same name, it is replaced. 4016 * Look for an existing entry that is alphabetical higher, the new entry 4017 * is inserted in front of it. 4018 */ 4019 for (i = 0; i < tc_len; ++i) 4020 { 4021 if (termcodes[i].name[0] < name[0]) 4022 continue; 4023 if (termcodes[i].name[0] == name[0]) 4024 { 4025 if (termcodes[i].name[1] < name[1]) 4026 continue; 4027 /* 4028 * Exact match: May replace old code. 4029 */ 4030 if (termcodes[i].name[1] == name[1]) 4031 { 4032 if (flags == ATC_FROM_TERM && (j = termcode_star( 4033 termcodes[i].code, termcodes[i].len)) > 0) 4034 { 4035 // Don't replace ESC[123;*X or ESC O*X with another when 4036 // invoked from got_code_from_term(). 4037 if (len == termcodes[i].len - j 4038 && STRNCMP(s, termcodes[i].code, len - 1) == 0 4039 && s[len - 1] 4040 == termcodes[i].code[termcodes[i].len - 1]) 4041 { 4042 // They are equal but for the ";*": don't add it. 4043 vim_free(s); 4044 return; 4045 } 4046 } 4047 else 4048 { 4049 // Replace old code. 4050 vim_free(termcodes[i].code); 4051 --tc_len; 4052 break; 4053 } 4054 } 4055 } 4056 /* 4057 * Found alphabetical larger entry, move rest to insert new entry 4058 */ 4059 for (j = tc_len; j > i; --j) 4060 termcodes[j] = termcodes[j - 1]; 4061 break; 4062 } 4063 4064 termcodes[i].name[0] = name[0]; 4065 termcodes[i].name[1] = name[1]; 4066 termcodes[i].code = s; 4067 termcodes[i].len = len; 4068 4069 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that 4070 // accept modifiers. 4071 termcodes[i].modlen = 0; 4072 j = termcode_star(s, len); 4073 if (j > 0) 4074 termcodes[i].modlen = len - 1 - j; 4075 ++tc_len; 4076 } 4077 4078 /* 4079 * Check termcode "code[len]" for ending in ;*X or *X. 4080 * The "X" can be any character. 4081 * Return 0 if not found, 2 for ;*X and 1 for *X. 4082 */ 4083 static int 4084 termcode_star(char_u *code, int len) 4085 { 4086 // Shortest is <M-O>*X. With ; shortest is <CSI>1;*X 4087 if (len >= 3 && code[len - 2] == '*') 4088 { 4089 if (len >= 5 && code[len - 3] == ';') 4090 return 2; 4091 else 4092 return 1; 4093 } 4094 return 0; 4095 } 4096 4097 char_u * 4098 find_termcode(char_u *name) 4099 { 4100 int i; 4101 4102 for (i = 0; i < tc_len; ++i) 4103 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1]) 4104 return termcodes[i].code; 4105 return NULL; 4106 } 4107 4108 char_u * 4109 get_termcode(int i) 4110 { 4111 if (i >= tc_len) 4112 return NULL; 4113 return &termcodes[i].name[0]; 4114 } 4115 4116 /* 4117 * Returns the length of the terminal code at index 'idx'. 4118 */ 4119 int 4120 get_termcode_len(int idx) 4121 { 4122 return termcodes[idx].len; 4123 } 4124 4125 void 4126 del_termcode(char_u *name) 4127 { 4128 int i; 4129 4130 if (termcodes == NULL) // nothing there yet 4131 return; 4132 4133 need_gather = TRUE; // need to fill termleader[] 4134 4135 for (i = 0; i < tc_len; ++i) 4136 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1]) 4137 { 4138 del_termcode_idx(i); 4139 return; 4140 } 4141 // not found. Give error message? 4142 } 4143 4144 static void 4145 del_termcode_idx(int idx) 4146 { 4147 int i; 4148 4149 vim_free(termcodes[idx].code); 4150 --tc_len; 4151 for (i = idx; i < tc_len; ++i) 4152 termcodes[i] = termcodes[i + 1]; 4153 } 4154 4155 #ifdef FEAT_TERMRESPONSE 4156 /* 4157 * Called when detected that the terminal sends 8-bit codes. 4158 * Convert all 7-bit codes to their 8-bit equivalent. 4159 */ 4160 static void 4161 switch_to_8bit(void) 4162 { 4163 int i; 4164 int c; 4165 4166 // Only need to do something when not already using 8-bit codes. 4167 if (!term_is_8bit(T_NAME)) 4168 { 4169 for (i = 0; i < tc_len; ++i) 4170 { 4171 c = term_7to8bit(termcodes[i].code); 4172 if (c != 0) 4173 { 4174 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2); 4175 termcodes[i].code[0] = c; 4176 } 4177 } 4178 need_gather = TRUE; // need to fill termleader[] 4179 } 4180 detected_8bit = TRUE; 4181 LOG_TR(("Switching to 8 bit")); 4182 } 4183 #endif 4184 4185 #ifdef CHECK_DOUBLE_CLICK 4186 static linenr_T orig_topline = 0; 4187 # ifdef FEAT_DIFF 4188 static int orig_topfill = 0; 4189 # endif 4190 #endif 4191 #if defined(CHECK_DOUBLE_CLICK) || defined(PROTO) 4192 /* 4193 * Checking for double clicks ourselves. 4194 * "orig_topline" is used to avoid detecting a double-click when the window 4195 * contents scrolled (e.g., when 'scrolloff' is non-zero). 4196 */ 4197 /* 4198 * Set orig_topline. Used when jumping to another window, so that a double 4199 * click still works. 4200 */ 4201 void 4202 set_mouse_topline(win_T *wp) 4203 { 4204 orig_topline = wp->w_topline; 4205 # ifdef FEAT_DIFF 4206 orig_topfill = wp->w_topfill; 4207 # endif 4208 } 4209 4210 /* 4211 * Returns TRUE if the top line and top fill of window 'wp' matches the saved 4212 * topline and topfill. 4213 */ 4214 int 4215 is_mouse_topline(win_T *wp) 4216 { 4217 return orig_topline == wp->w_topline 4218 #ifdef FEAT_DIFF 4219 && orig_topfill == wp->w_topfill 4220 #endif 4221 ; 4222 } 4223 #endif 4224 4225 /* 4226 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL. 4227 * Remove "slen" bytes. 4228 * Returns FAIL for error. 4229 */ 4230 static int 4231 put_string_in_typebuf( 4232 int offset, 4233 int slen, 4234 char_u *string, 4235 int new_slen, 4236 char_u *buf, 4237 int bufsize, 4238 int *buflen) 4239 { 4240 int extra = new_slen - slen; 4241 4242 string[new_slen] = NUL; 4243 if (buf == NULL) 4244 { 4245 if (extra < 0) 4246 // remove matched chars, taking care of noremap 4247 del_typebuf(-extra, offset); 4248 else if (extra > 0) 4249 // insert the extra space we need 4250 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE); 4251 4252 // Careful: del_typebuf() and ins_typebuf() may have reallocated 4253 // typebuf.tb_buf[]! 4254 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string, 4255 (size_t)new_slen); 4256 } 4257 else 4258 { 4259 if (extra < 0) 4260 // remove matched characters 4261 mch_memmove(buf + offset, buf + offset - extra, 4262 (size_t)(*buflen + offset + extra)); 4263 else if (extra > 0) 4264 { 4265 // Insert the extra space we need. If there is insufficient 4266 // space return -1. 4267 if (*buflen + extra + new_slen >= bufsize) 4268 return FAIL; 4269 mch_memmove(buf + offset + extra, buf + offset, 4270 (size_t)(*buflen - offset)); 4271 } 4272 mch_memmove(buf + offset, string, (size_t)new_slen); 4273 *buflen = *buflen + extra + new_slen; 4274 } 4275 return OK; 4276 } 4277 4278 /* 4279 * Decode a modifier number as xterm provides it into MOD_MASK bits. 4280 */ 4281 int 4282 decode_modifiers(int n) 4283 { 4284 int code = n - 1; 4285 int modifiers = 0; 4286 4287 if (code & 1) 4288 modifiers |= MOD_MASK_SHIFT; 4289 if (code & 2) 4290 modifiers |= MOD_MASK_ALT; 4291 if (code & 4) 4292 modifiers |= MOD_MASK_CTRL; 4293 if (code & 8) 4294 modifiers |= MOD_MASK_META; 4295 return modifiers; 4296 } 4297 4298 static int 4299 modifiers2keycode(int modifiers, int *key, char_u *string) 4300 { 4301 int new_slen = 0; 4302 4303 if (modifiers != 0) 4304 { 4305 // Some keys have the modifier included. Need to handle that here to 4306 // make mappings work. 4307 *key = simplify_key(*key, &modifiers); 4308 if (modifiers != 0) 4309 { 4310 string[new_slen++] = K_SPECIAL; 4311 string[new_slen++] = (int)KS_MODIFIER; 4312 string[new_slen++] = modifiers; 4313 } 4314 } 4315 return new_slen; 4316 } 4317 4318 /* 4319 * Check if typebuf.tb_buf[] contains a terminal key code. 4320 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off 4321 * + max_offset]. 4322 * Return 0 for no match, -1 for partial match, > 0 for full match. 4323 * Return KEYLEN_REMOVED when a key code was deleted. 4324 * With a match, the match is removed, the replacement code is inserted in 4325 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is 4326 * returned. 4327 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[]. 4328 * "buflen" is then the length of the string in buf[] and is updated for 4329 * inserts and deletes. 4330 */ 4331 int 4332 check_termcode( 4333 int max_offset, 4334 char_u *buf, 4335 int bufsize, 4336 int *buflen) 4337 { 4338 char_u *tp; 4339 char_u *p; 4340 int slen = 0; // init for GCC 4341 int modslen; 4342 int len; 4343 int retval = 0; 4344 int offset; 4345 char_u key_name[2]; 4346 int modifiers; 4347 char_u *modifiers_start = NULL; 4348 int key; 4349 int new_slen; // Length of what will replace the termcode 4350 char_u string[MAX_KEY_CODE_LEN + 1]; 4351 int i, j; 4352 int idx = 0; 4353 int cpo_koffset; 4354 4355 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL); 4356 4357 /* 4358 * Speed up the checks for terminal codes by gathering all first bytes 4359 * used in termleader[]. Often this is just a single <Esc>. 4360 */ 4361 if (need_gather) 4362 gather_termleader(); 4363 4364 /* 4365 * Check at several positions in typebuf.tb_buf[], to catch something like 4366 * "x<Up>" that can be mapped. Stop at max_offset, because characters 4367 * after that cannot be used for mapping, and with @r commands 4368 * typebuf.tb_buf[] can become very long. 4369 * This is used often, KEEP IT FAST! 4370 */ 4371 for (offset = 0; offset < max_offset; ++offset) 4372 { 4373 if (buf == NULL) 4374 { 4375 if (offset >= typebuf.tb_len) 4376 break; 4377 tp = typebuf.tb_buf + typebuf.tb_off + offset; 4378 len = typebuf.tb_len - offset; // length of the input 4379 } 4380 else 4381 { 4382 if (offset >= *buflen) 4383 break; 4384 tp = buf + offset; 4385 len = *buflen - offset; 4386 } 4387 4388 /* 4389 * Don't check characters after K_SPECIAL, those are already 4390 * translated terminal chars (avoid translating ~@^Hx). 4391 */ 4392 if (*tp == K_SPECIAL) 4393 { 4394 offset += 2; // there are always 2 extra characters 4395 continue; 4396 } 4397 4398 /* 4399 * Skip this position if the character does not appear as the first 4400 * character in term_strings. This speeds up a lot, since most 4401 * termcodes start with the same character (ESC or CSI). 4402 */ 4403 i = *tp; 4404 for (p = termleader; *p && *p != i; ++p) 4405 ; 4406 if (*p == NUL) 4407 continue; 4408 4409 /* 4410 * Skip this position if p_ek is not set and tp[0] is an ESC and we 4411 * are in Insert mode. 4412 */ 4413 if (*tp == ESC && !p_ek && (State & INSERT)) 4414 continue; 4415 4416 key_name[0] = NUL; // no key name found yet 4417 key_name[1] = NUL; // no key name found yet 4418 modifiers = 0; // no modifiers yet 4419 4420 #ifdef FEAT_GUI 4421 if (gui.in_use) 4422 { 4423 /* 4424 * GUI special key codes are all of the form [CSI xx]. 4425 */ 4426 if (*tp == CSI) // Special key from GUI 4427 { 4428 if (len < 3) 4429 return -1; // Shouldn't happen 4430 slen = 3; 4431 key_name[0] = tp[1]; 4432 key_name[1] = tp[2]; 4433 } 4434 } 4435 else 4436 #endif // FEAT_GUI 4437 { 4438 for (idx = 0; idx < tc_len; ++idx) 4439 { 4440 /* 4441 * Ignore the entry if we are not at the start of 4442 * typebuf.tb_buf[] 4443 * and there are not enough characters to make a match. 4444 * But only when the 'K' flag is in 'cpoptions'. 4445 */ 4446 slen = termcodes[idx].len; 4447 modifiers_start = NULL; 4448 if (cpo_koffset && offset && len < slen) 4449 continue; 4450 if (STRNCMP(termcodes[idx].code, tp, 4451 (size_t)(slen > len ? len : slen)) == 0) 4452 { 4453 if (len < slen) // got a partial sequence 4454 return -1; // need to get more chars 4455 4456 /* 4457 * When found a keypad key, check if there is another key 4458 * that matches and use that one. This makes <Home> to be 4459 * found instead of <kHome> when they produce the same 4460 * key code. 4461 */ 4462 if (termcodes[idx].name[0] == 'K' 4463 && VIM_ISDIGIT(termcodes[idx].name[1])) 4464 { 4465 for (j = idx + 1; j < tc_len; ++j) 4466 if (termcodes[j].len == slen && 4467 STRNCMP(termcodes[idx].code, 4468 termcodes[j].code, slen) == 0) 4469 { 4470 idx = j; 4471 break; 4472 } 4473 } 4474 4475 key_name[0] = termcodes[idx].name[0]; 4476 key_name[1] = termcodes[idx].name[1]; 4477 break; 4478 } 4479 4480 /* 4481 * Check for code with modifier, like xterm uses: 4482 * <Esc>[123;*X (modslen == slen - 3) 4483 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2). 4484 * When there is a modifier the * matches a number. 4485 * When there is no modifier the ;* or * is omitted. 4486 */ 4487 if (termcodes[idx].modlen > 0) 4488 { 4489 modslen = termcodes[idx].modlen; 4490 if (cpo_koffset && offset && len < modslen) 4491 continue; 4492 if (STRNCMP(termcodes[idx].code, tp, 4493 (size_t)(modslen > len ? len : modslen)) == 0) 4494 { 4495 int n; 4496 4497 if (len <= modslen) // got a partial sequence 4498 return -1; // need to get more chars 4499 4500 if (tp[modslen] == termcodes[idx].code[slen - 1]) 4501 slen = modslen + 1; // no modifiers 4502 else if (tp[modslen] != ';' && modslen == slen - 3) 4503 continue; // no match 4504 else 4505 { 4506 // Skip over the digits, the final char must 4507 // follow. URXVT can use a negative value, thus 4508 // also accept '-'. 4509 for (j = slen - 2; j < len && (isdigit(tp[j]) 4510 || tp[j] == '-' || tp[j] == ';'); ++j) 4511 ; 4512 ++j; 4513 if (len < j) // got a partial sequence 4514 return -1; // need to get more chars 4515 if (tp[j - 1] != termcodes[idx].code[slen - 1]) 4516 continue; // no match 4517 4518 modifiers_start = tp + slen - 2; 4519 4520 // Match! Convert modifier bits. 4521 n = atoi((char *)modifiers_start); 4522 modifiers |= decode_modifiers(n); 4523 4524 slen = j; 4525 } 4526 key_name[0] = termcodes[idx].name[0]; 4527 key_name[1] = termcodes[idx].name[1]; 4528 break; 4529 } 4530 } 4531 } 4532 } 4533 4534 #ifdef FEAT_TERMRESPONSE 4535 if (key_name[0] == NUL 4536 // Mouse codes of DEC and pterm start with <ESC>[. When 4537 // detecting the start of these mouse codes they might as well be 4538 // another key code or terminal response. 4539 # ifdef FEAT_MOUSE_DEC 4540 || key_name[0] == KS_DEC_MOUSE 4541 # endif 4542 # ifdef FEAT_MOUSE_PTERM 4543 || key_name[0] == KS_PTERM_MOUSE 4544 # endif 4545 ) 4546 { 4547 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1; 4548 4549 /* 4550 * Check for responses from the terminal starting with {lead}: 4551 * "<Esc>[" or CSI followed by [0-9>?] 4552 * 4553 * - Xterm version string: {lead}>{x};{vers};{y}c 4554 * Also eat other possible responses to t_RV, rxvt returns 4555 * "{lead}?1;2c". 4556 * 4557 * - Cursor position report: {lead}{row};{col}R 4558 * The final byte must be 'R'. It is used for checking the 4559 * ambiguous-width character state. 4560 * 4561 * - window position reply: {lead}3;{x};{y}t 4562 * 4563 * - key with modifiers when modifyOtherKeys is enabled: 4564 * {lead}27;{modifier};{key}~ 4565 * {lead}{key};{modifier}u 4566 */ 4567 if (((tp[0] == ESC && len >= 3 && tp[1] == '[') 4568 || (tp[0] == CSI && len >= 2)) 4569 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?')) 4570 { 4571 int first = -1; // optional char right after {lead} 4572 int trail; // char that ends CSI sequence 4573 int arg[3] = {-1, -1, -1}; // argument numbers 4574 int argc; // number of arguments 4575 char_u *ap = argp; 4576 int csi_len; 4577 4578 // Check for non-digit after CSI. 4579 if (!VIM_ISDIGIT(*ap)) 4580 first = *ap++; 4581 4582 // Find up to three argument numbers. 4583 for (argc = 0; argc < 3; ) 4584 { 4585 if (ap >= tp + len) 4586 { 4587 not_enough: 4588 LOG_TR(("Not enough characters for CSI sequence")); 4589 return -1; 4590 } 4591 if (*ap == ';') 4592 arg[argc++] = -1; // omitted number 4593 else if (VIM_ISDIGIT(*ap)) 4594 { 4595 arg[argc] = 0; 4596 for (;;) 4597 { 4598 if (ap >= tp + len) 4599 goto not_enough; 4600 if (!VIM_ISDIGIT(*ap)) 4601 break; 4602 arg[argc] = arg[argc] * 10 + (*ap - '0'); 4603 ++ap; 4604 } 4605 ++argc; 4606 } 4607 if (*ap == ';') 4608 ++ap; 4609 else 4610 break; 4611 } 4612 // mrxvt has been reported to have "+" in the version. Assume 4613 // the escape sequence ends with a letter or one of "{|}~". 4614 while (ap < tp + len 4615 && !(*ap >= '{' && *ap <= '~') 4616 && !ASCII_ISALPHA(*ap)) 4617 ++ap; 4618 if (ap >= tp + len) 4619 goto not_enough; 4620 trail = *ap; 4621 csi_len = (int)(ap - tp) + 1; 4622 4623 // Cursor position report: Eat it when there are 2 arguments 4624 // and it ends in 'R'. Also when u7_status is not "sent", it 4625 // may be from a previous Vim that just exited. But not for 4626 // <S-F3>, it sends something similar, check for row and column 4627 // to make sense. 4628 if (first == -1 && argc == 2 && trail == 'R') 4629 { 4630 if (arg[0] == 2 && arg[1] >= 2) 4631 { 4632 char *aw = NULL; 4633 4634 LOG_TR(("Received U7 status: %s", tp)); 4635 u7_status.tr_progress = STATUS_GOT; 4636 did_cursorhold = TRUE; 4637 if (arg[1] == 2) 4638 aw = "single"; 4639 else if (arg[1] == 3) 4640 aw = "double"; 4641 if (aw != NULL && STRCMP(aw, p_ambw) != 0) 4642 { 4643 // Setting the option causes a screen redraw. Do 4644 // that right away if possible, keeping any 4645 // messages. 4646 set_option_value((char_u *)"ambw", 0L, 4647 (char_u *)aw, 0); 4648 # ifdef DEBUG_TERMRESPONSE 4649 { 4650 int r = redraw_asap(CLEAR); 4651 4652 log_tr("set 'ambiwidth', redraw_asap(): %d", r); 4653 } 4654 # else 4655 redraw_asap(CLEAR); 4656 # endif 4657 } 4658 } 4659 key_name[0] = (int)KS_EXTRA; 4660 key_name[1] = (int)KE_IGNORE; 4661 slen = csi_len; 4662 # ifdef FEAT_EVAL 4663 set_vim_var_string(VV_TERMU7RESP, tp, slen); 4664 # endif 4665 } 4666 4667 // Version string: Eat it when there is at least one digit and 4668 // it ends in 'c' 4669 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c') 4670 { 4671 int version = arg[1]; 4672 4673 LOG_TR(("Received CRV response: %s", tp)); 4674 crv_status.tr_progress = STATUS_GOT; 4675 did_cursorhold = TRUE; 4676 4677 // If this code starts with CSI, you can bet that the 4678 // terminal uses 8-bit codes. 4679 if (tp[0] == CSI) 4680 switch_to_8bit(); 4681 4682 // Screen sends 40500. 4683 // rxvt sends its version number: "20703" is 2.7.3. 4684 // Ignore it for when the user has set 'term' to xterm, 4685 // even though it's an rxvt. 4686 if (version > 20000) 4687 version = 0; 4688 4689 if (first == '>' && argc == 3) 4690 { 4691 int need_flush = FALSE; 4692 int is_iterm2 = FALSE; 4693 int is_mintty = FALSE; 4694 int is_screen = FALSE; 4695 4696 // mintty 2.9.5 sends 77;20905;0c. 4697 // (77 is ASCII 'M' for mintty.) 4698 if (arg[0] == 77) 4699 is_mintty = TRUE; 4700 4701 // if xterm version >= 141 try to get termcap codes 4702 if (version >= 141) 4703 { 4704 LOG_TR(("Enable checking for XT codes")); 4705 check_for_codes = TRUE; 4706 need_gather = TRUE; 4707 req_codes_from_term(); 4708 } 4709 4710 // libvterm sends 0;100;0 4711 if (version == 100 && arg[0] == 0 && arg[2] == 0) 4712 { 4713 // If run from Vim $COLORS is set to the number of 4714 // colors the terminal supports. Otherwise assume 4715 // 256, libvterm supports even more. 4716 if (mch_getenv((char_u *)"COLORS") == NULL) 4717 may_adjust_color_count(256); 4718 // Libvterm can handle SGR mouse reporting. 4719 if (!option_was_set((char_u *)"ttym")) 4720 set_option_value((char_u *)"ttym", 0L, 4721 (char_u *)"sgr", 0); 4722 } 4723 4724 if (version == 95) 4725 { 4726 // Mac Terminal.app sends 1;95;0 4727 if (arg[0] == 1 && arg[2] == 0) 4728 { 4729 is_not_xterm = TRUE; 4730 is_mac_terminal = TRUE; 4731 } 4732 // iTerm2 sends 0;95;0 4733 else if (arg[0] == 0 && arg[2] == 0) 4734 is_iterm2 = TRUE; 4735 // old iTerm2 sends 0;95; 4736 else if (arg[0] == 0 && arg[2] == -1) 4737 is_not_xterm = TRUE; 4738 } 4739 4740 // screen sends 83;40500;0 83 is 'S' in ASCII. 4741 if (arg[0] == 83) 4742 is_screen = TRUE; 4743 4744 // Only set 'ttymouse' automatically if it was not set 4745 // by the user already. 4746 if (!option_was_set((char_u *)"ttym")) 4747 { 4748 // Xterm version 277 supports SGR. Also support 4749 // Terminal.app, iTerm2, mintty, and screen 4.7+. 4750 if ((!is_screen && version >= 277) 4751 || is_iterm2 4752 || is_mac_terminal 4753 || is_mintty 4754 || (is_screen && arg[1] >= 40700)) 4755 set_option_value((char_u *)"ttym", 0L, 4756 (char_u *)"sgr", 0); 4757 // if xterm version >= 95 use mouse dragging 4758 else if (version >= 95) 4759 set_option_value((char_u *)"ttym", 0L, 4760 (char_u *)"xterm2", 0); 4761 } 4762 4763 // Detect terminals that set $TERM to something like 4764 // "xterm-256colors" but are not fully xterm 4765 // compatible. 4766 4767 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0. 4768 // xfce4-terminal sends 1;2802;0. 4769 // screen sends 83;40500;0 4770 // Assuming any version number over 2500 is not an 4771 // xterm (without the limit for rxvt and screen). 4772 if (arg[1] >= 2500) 4773 is_not_xterm = TRUE; 4774 4775 // PuTTY sends 0;136;0 4776 // vandyke SecureCRT sends 1;136;0 4777 else if (version == 136 && arg[2] == 0) 4778 is_not_xterm = TRUE; 4779 4780 // Konsole sends 0;115;0 4781 else if (version == 115 && arg[0] == 0 && arg[2] == 0) 4782 is_not_xterm = TRUE; 4783 4784 // Xterm first responded to this request at patch level 4785 // 95, so assume anything below 95 is not xterm. 4786 if (version < 95) 4787 is_not_xterm = TRUE; 4788 4789 // Only request the cursor style if t_SH and t_RS are 4790 // set. Only supported properly by xterm since version 4791 // 279 (otherwise it returns 0x18). 4792 // Not for Terminal.app, it can't handle t_RS, it 4793 // echoes the characters to the screen. 4794 if (rcs_status.tr_progress == STATUS_GET 4795 && version >= 279 4796 && !is_not_xterm 4797 && *T_CSH != NUL 4798 && *T_CRS != NUL) 4799 { 4800 LOG_TR(("Sending cursor style request")); 4801 out_str(T_CRS); 4802 termrequest_sent(&rcs_status); 4803 need_flush = TRUE; 4804 } 4805 4806 // Only request the cursor blink mode if t_RC set. Not 4807 // for Gnome terminal, it can't handle t_RC, it 4808 // echoes the characters to the screen. 4809 if (rbm_status.tr_progress == STATUS_GET 4810 && !is_not_xterm 4811 && *T_CRC != NUL) 4812 { 4813 LOG_TR(("Sending cursor blink mode request")); 4814 out_str(T_CRC); 4815 termrequest_sent(&rbm_status); 4816 need_flush = TRUE; 4817 } 4818 4819 if (need_flush) 4820 out_flush(); 4821 } 4822 slen = csi_len; 4823 # ifdef FEAT_EVAL 4824 set_vim_var_string(VV_TERMRESPONSE, tp, slen); 4825 # endif 4826 apply_autocmds(EVENT_TERMRESPONSE, 4827 NULL, NULL, FALSE, curbuf); 4828 key_name[0] = (int)KS_EXTRA; 4829 key_name[1] = (int)KE_IGNORE; 4830 } 4831 4832 // Check blinking cursor from xterm: 4833 // {lead}?12;1$y set 4834 // {lead}?12;2$y not set 4835 // 4836 // {lead} can be <Esc>[ or CSI 4837 else if (rbm_status.tr_progress == STATUS_SENT 4838 && first == '?' 4839 && ap == argp + 6 4840 && arg[0] == 12 4841 && ap[-1] == '$' 4842 && trail == 'y') 4843 { 4844 initial_cursor_blink = (arg[1] == '1'); 4845 rbm_status.tr_progress = STATUS_GOT; 4846 LOG_TR(("Received cursor blinking mode response: %s", tp)); 4847 key_name[0] = (int)KS_EXTRA; 4848 key_name[1] = (int)KE_IGNORE; 4849 slen = csi_len; 4850 # ifdef FEAT_EVAL 4851 set_vim_var_string(VV_TERMBLINKRESP, tp, slen); 4852 # endif 4853 } 4854 4855 // Check for a window position response from the terminal: 4856 // {lead}3;{x};{y}t 4857 else if (did_request_winpos && argc == 3 && arg[0] == 3 4858 && trail == 't') 4859 { 4860 winpos_x = arg[1]; 4861 winpos_y = arg[2]; 4862 // got finished code: consume it 4863 key_name[0] = (int)KS_EXTRA; 4864 key_name[1] = (int)KE_IGNORE; 4865 slen = csi_len; 4866 4867 if (--did_request_winpos <= 0) 4868 winpos_status.tr_progress = STATUS_GOT; 4869 } 4870 4871 // Key with modifier: 4872 // {lead}27;{modifier};{key}~ 4873 // {lead}{key};{modifier}u 4874 else if ((arg[0] == 27 && argc == 3 && trail == '~') 4875 || (argc == 2 && trail == 'u')) 4876 { 4877 seenModifyOtherKeys = TRUE; 4878 if (trail == 'u') 4879 key = arg[0]; 4880 else 4881 key = arg[2]; 4882 4883 modifiers = decode_modifiers(arg[1]); 4884 4885 // Some keys already have Shift included, pass them as 4886 // normal keys. Not when Ctrl is also used, because <C-H> 4887 // and <C-S-H> are different. 4888 if (modifiers == MOD_MASK_SHIFT 4889 && ((key >= '@' && key <= 'Z') 4890 || key == '^' || key == '_' 4891 || (key >= '{' && key <= '~'))) 4892 modifiers = 0; 4893 4894 // When used with Ctrl we always make a letter upper case, 4895 // so that mapping <C-H> and <C-h> are the same. Typing 4896 // <C-S-H> also uses "H" but modifier is different. 4897 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key)) 4898 key = TOUPPER_ASC(key); 4899 4900 // insert modifiers with KS_MODIFIER 4901 new_slen = modifiers2keycode(modifiers, &key, string); 4902 slen = csi_len; 4903 4904 if (has_mbyte) 4905 new_slen += (*mb_char2bytes)(key, string + new_slen); 4906 else 4907 string[new_slen++] = key; 4908 4909 if (put_string_in_typebuf(offset, slen, string, new_slen, 4910 buf, bufsize, buflen) == FAIL) 4911 return -1; 4912 return len + new_slen - slen + offset; 4913 } 4914 4915 // else: Unknown CSI sequence. We could drop it, but then the 4916 // user can't create a map for it. 4917 } 4918 4919 // Check for fore/background color response from the terminal: 4920 // 4921 // {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail} 4922 // or {lead}{code};rgb:{rr}/{gg}/{bb}{tail} 4923 // 4924 // {code} is 10 for foreground, 11 for background 4925 // {lead} can be <Esc>] or OSC 4926 // {tail} can be '\007', <Esc>\ or STERM. 4927 // 4928 // Consume any code that starts with "{lead}11;", it's also 4929 // possible that "rgba" is following. 4930 else if ((*T_RBG != NUL || *T_RFG != NUL) 4931 && ((tp[0] == ESC && len >= 2 && tp[1] == ']') 4932 || tp[0] == OSC)) 4933 { 4934 j = 1 + (tp[0] == ESC); 4935 if (len >= j + 3 && (argp[0] != '1' 4936 || (argp[1] != '1' && argp[1] != '0') 4937 || argp[2] != ';')) 4938 i = 0; // no match 4939 else 4940 for (i = j; i < len; ++i) 4941 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM 4942 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\'))) 4943 { 4944 int is_bg = argp[1] == '1'; 4945 int is_4digit = i - j >= 21 && tp[j + 11] == '/' 4946 && tp[j + 16] == '/'; 4947 4948 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0 4949 && (is_4digit 4950 || (tp[j + 9] == '/' && tp[i + 12 == '/']))) 4951 { 4952 char_u *tp_r = tp + j + 7; 4953 char_u *tp_g = tp + j + (is_4digit ? 12 : 10); 4954 char_u *tp_b = tp + j + (is_4digit ? 17 : 13); 4955 # ifdef FEAT_TERMINAL 4956 int rval, gval, bval; 4957 4958 rval = hexhex2nr(tp_r); 4959 gval = hexhex2nr(tp_b); 4960 bval = hexhex2nr(tp_g); 4961 # endif 4962 if (is_bg) 4963 { 4964 char *new_bg_val = (3 * '6' < *tp_r + *tp_g + 4965 *tp_b) ? "light" : "dark"; 4966 4967 LOG_TR(("Received RBG response: %s", tp)); 4968 rbg_status.tr_progress = STATUS_GOT; 4969 # ifdef FEAT_TERMINAL 4970 bg_r = rval; 4971 bg_g = gval; 4972 bg_b = bval; 4973 # endif 4974 if (!option_was_set((char_u *)"bg") 4975 && STRCMP(p_bg, new_bg_val) != 0) 4976 { 4977 // value differs, apply it 4978 set_option_value((char_u *)"bg", 0L, 4979 (char_u *)new_bg_val, 0); 4980 reset_option_was_set((char_u *)"bg"); 4981 redraw_asap(CLEAR); 4982 } 4983 } 4984 # ifdef FEAT_TERMINAL 4985 else 4986 { 4987 LOG_TR(("Received RFG response: %s", tp)); 4988 rfg_status.tr_progress = STATUS_GOT; 4989 fg_r = rval; 4990 fg_g = gval; 4991 fg_b = bval; 4992 } 4993 # endif 4994 } 4995 4996 // got finished code: consume it 4997 key_name[0] = (int)KS_EXTRA; 4998 key_name[1] = (int)KE_IGNORE; 4999 slen = i + 1 + (tp[i] == ESC); 5000 # ifdef FEAT_EVAL 5001 set_vim_var_string(is_bg ? VV_TERMRBGRESP 5002 : VV_TERMRFGRESP, tp, slen); 5003 # endif 5004 break; 5005 } 5006 if (i == len) 5007 { 5008 LOG_TR(("not enough characters for RB")); 5009 return -1; 5010 } 5011 } 5012 5013 // Check for key code response from xterm: 5014 // {lead}{flag}+r<hex bytes><{tail} 5015 // 5016 // {lead} can be <Esc>P or DCS 5017 // {flag} can be '0' or '1' 5018 // {tail} can be Esc>\ or STERM 5019 // 5020 // Check for cursor shape response from xterm: 5021 // {lead}1$r<digit> q{tail} 5022 // 5023 // {lead} can be <Esc>P or DCS 5024 // {tail} can be <Esc>\ or STERM 5025 // 5026 // Consume any code that starts with "{lead}.+r" or "{lead}.$r". 5027 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT) 5028 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P') 5029 || tp[0] == DCS)) 5030 { 5031 j = 1 + (tp[0] == ESC); 5032 if (len < j + 3) 5033 i = len; // need more chars 5034 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r') 5035 i = 0; // no match 5036 else if (argp[1] == '+') 5037 // key code response 5038 for (i = j; i < len; ++i) 5039 { 5040 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\') 5041 || tp[i] == STERM) 5042 { 5043 if (i - j >= 3) 5044 got_code_from_term(tp + j, i); 5045 key_name[0] = (int)KS_EXTRA; 5046 key_name[1] = (int)KE_IGNORE; 5047 slen = i + 1 + (tp[i] == ESC); 5048 break; 5049 } 5050 } 5051 else 5052 { 5053 // Probably the cursor shape response. Make sure that "i" 5054 // is equal to "len" when there are not sufficient 5055 // characters. 5056 for (i = j + 3; i < len; ++i) 5057 { 5058 if (i - j == 3 && !isdigit(tp[i])) 5059 break; 5060 if (i - j == 4 && tp[i] != ' ') 5061 break; 5062 if (i - j == 5 && tp[i] != 'q') 5063 break; 5064 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM) 5065 break; 5066 if ((i - j == 6 && tp[i] == STERM) 5067 || (i - j == 7 && tp[i] == '\\')) 5068 { 5069 int number = argp[3] - '0'; 5070 5071 // 0, 1 = block blink, 2 = block 5072 // 3 = underline blink, 4 = underline 5073 // 5 = vertical bar blink, 6 = vertical bar 5074 number = number == 0 ? 1 : number; 5075 initial_cursor_shape = (number + 1) / 2; 5076 // The blink flag is actually inverted, compared to 5077 // the value set with T_SH. 5078 initial_cursor_shape_blink = 5079 (number & 1) ? FALSE : TRUE; 5080 rcs_status.tr_progress = STATUS_GOT; 5081 LOG_TR(("Received cursor shape response: %s", tp)); 5082 5083 key_name[0] = (int)KS_EXTRA; 5084 key_name[1] = (int)KE_IGNORE; 5085 slen = i + 1; 5086 # ifdef FEAT_EVAL 5087 set_vim_var_string(VV_TERMSTYLERESP, tp, slen); 5088 # endif 5089 break; 5090 } 5091 } 5092 } 5093 5094 if (i == len) 5095 { 5096 // These codes arrive many together, each code can be 5097 // truncated at any point. 5098 LOG_TR(("not enough characters for XT")); 5099 return -1; 5100 } 5101 } 5102 } 5103 #endif 5104 5105 if (key_name[0] == NUL) 5106 continue; // No match at this position, try next one 5107 5108 // We only get here when we have a complete termcode match 5109 5110 #ifdef FEAT_GUI 5111 /* 5112 * Only in the GUI: Fetch the pointer coordinates of the scroll event 5113 * so that we know which window to scroll later. 5114 */ 5115 if (gui.in_use 5116 && key_name[0] == (int)KS_EXTRA 5117 && (key_name[1] == (int)KE_X1MOUSE 5118 || key_name[1] == (int)KE_X2MOUSE 5119 || key_name[1] == (int)KE_MOUSELEFT 5120 || key_name[1] == (int)KE_MOUSERIGHT 5121 || key_name[1] == (int)KE_MOUSEDOWN 5122 || key_name[1] == (int)KE_MOUSEUP)) 5123 { 5124 char_u bytes[6]; 5125 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4); 5126 5127 if (num_bytes == -1) // not enough coordinates 5128 return -1; 5129 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1; 5130 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1; 5131 slen += num_bytes; 5132 } 5133 else 5134 #endif 5135 /* 5136 * If it is a mouse click, get the coordinates. 5137 */ 5138 if (key_name[0] == KS_MOUSE 5139 #ifdef FEAT_MOUSE_GPM 5140 || key_name[0] == KS_GPM_MOUSE 5141 #endif 5142 #ifdef FEAT_MOUSE_JSB 5143 || key_name[0] == KS_JSBTERM_MOUSE 5144 #endif 5145 #ifdef FEAT_MOUSE_NET 5146 || key_name[0] == KS_NETTERM_MOUSE 5147 #endif 5148 #ifdef FEAT_MOUSE_DEC 5149 || key_name[0] == KS_DEC_MOUSE 5150 #endif 5151 #ifdef FEAT_MOUSE_PTERM 5152 || key_name[0] == KS_PTERM_MOUSE 5153 #endif 5154 #ifdef FEAT_MOUSE_URXVT 5155 || key_name[0] == KS_URXVT_MOUSE 5156 #endif 5157 || key_name[0] == KS_SGR_MOUSE 5158 || key_name[0] == KS_SGR_MOUSE_RELEASE) 5159 { 5160 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx, 5161 &modifiers) == -1) 5162 return -1; 5163 } 5164 5165 #ifdef FEAT_GUI 5166 /* 5167 * If using the GUI, then we get menu and scrollbar events. 5168 * 5169 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by 5170 * four bytes which are to be taken as a pointer to the vimmenu_T 5171 * structure. 5172 * 5173 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr" 5174 * is one byte with the tab index. 5175 * 5176 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed 5177 * by one byte representing the scrollbar number, and then four bytes 5178 * representing a long_u which is the new value of the scrollbar. 5179 * 5180 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR, 5181 * KE_FILLER followed by four bytes representing a long_u which is the 5182 * new value of the scrollbar. 5183 */ 5184 # ifdef FEAT_MENU 5185 else if (key_name[0] == (int)KS_MENU) 5186 { 5187 long_u val; 5188 int num_bytes = get_long_from_buf(tp + slen, &val); 5189 5190 if (num_bytes == -1) 5191 return -1; 5192 current_menu = (vimmenu_T *)val; 5193 slen += num_bytes; 5194 5195 // The menu may have been deleted right after it was used, check 5196 // for that. 5197 if (check_menu_pointer(root_menu, current_menu) == FAIL) 5198 { 5199 key_name[0] = KS_EXTRA; 5200 key_name[1] = (int)KE_IGNORE; 5201 } 5202 } 5203 # endif 5204 # ifdef FEAT_GUI_TABLINE 5205 else if (key_name[0] == (int)KS_TABLINE) 5206 { 5207 // Selecting tabline tab or using its menu. 5208 char_u bytes[6]; 5209 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1); 5210 5211 if (num_bytes == -1) 5212 return -1; 5213 current_tab = (int)bytes[0]; 5214 if (current_tab == 255) // -1 in a byte gives 255 5215 current_tab = -1; 5216 slen += num_bytes; 5217 } 5218 else if (key_name[0] == (int)KS_TABMENU) 5219 { 5220 // Selecting tabline tab or using its menu. 5221 char_u bytes[6]; 5222 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2); 5223 5224 if (num_bytes == -1) 5225 return -1; 5226 current_tab = (int)bytes[0]; 5227 current_tabmenu = (int)bytes[1]; 5228 slen += num_bytes; 5229 } 5230 # endif 5231 # ifndef USE_ON_FLY_SCROLL 5232 else if (key_name[0] == (int)KS_VER_SCROLLBAR) 5233 { 5234 long_u val; 5235 char_u bytes[6]; 5236 int num_bytes; 5237 5238 // Get the last scrollbar event in the queue of the same type 5239 j = 0; 5240 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR 5241 && tp[j + 2] != NUL; ++i) 5242 { 5243 j += 3; 5244 num_bytes = get_bytes_from_buf(tp + j, bytes, 1); 5245 if (num_bytes == -1) 5246 break; 5247 if (i == 0) 5248 current_scrollbar = (int)bytes[0]; 5249 else if (current_scrollbar != (int)bytes[0]) 5250 break; 5251 j += num_bytes; 5252 num_bytes = get_long_from_buf(tp + j, &val); 5253 if (num_bytes == -1) 5254 break; 5255 scrollbar_value = val; 5256 j += num_bytes; 5257 slen = j; 5258 } 5259 if (i == 0) // not enough characters to make one 5260 return -1; 5261 } 5262 else if (key_name[0] == (int)KS_HOR_SCROLLBAR) 5263 { 5264 long_u val; 5265 int num_bytes; 5266 5267 // Get the last horiz. scrollbar event in the queue 5268 j = 0; 5269 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR 5270 && tp[j + 2] != NUL; ++i) 5271 { 5272 j += 3; 5273 num_bytes = get_long_from_buf(tp + j, &val); 5274 if (num_bytes == -1) 5275 break; 5276 scrollbar_value = val; 5277 j += num_bytes; 5278 slen = j; 5279 } 5280 if (i == 0) // not enough characters to make one 5281 return -1; 5282 } 5283 # endif // !USE_ON_FLY_SCROLL 5284 #endif // FEAT_GUI 5285 5286 /* 5287 * Change <xHome> to <Home>, <xUp> to <Up>, etc. 5288 */ 5289 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1])); 5290 5291 /* 5292 * Add any modifier codes to our string. 5293 */ 5294 new_slen = modifiers2keycode(modifiers, &key, string); 5295 5296 // Finally, add the special key code to our string 5297 key_name[0] = KEY2TERMCAP0(key); 5298 key_name[1] = KEY2TERMCAP1(key); 5299 if (key_name[0] == KS_KEY) 5300 { 5301 // from ":set <M-b>=xx" 5302 if (has_mbyte) 5303 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen); 5304 else 5305 string[new_slen++] = key_name[1]; 5306 } 5307 else if (new_slen == 0 && key_name[0] == KS_EXTRA 5308 && key_name[1] == KE_IGNORE) 5309 { 5310 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED 5311 // to indicate what happened. 5312 retval = KEYLEN_REMOVED; 5313 } 5314 else 5315 { 5316 string[new_slen++] = K_SPECIAL; 5317 string[new_slen++] = key_name[0]; 5318 string[new_slen++] = key_name[1]; 5319 } 5320 if (put_string_in_typebuf(offset, slen, string, new_slen, 5321 buf, bufsize, buflen) == FAIL) 5322 return -1; 5323 return retval == 0 ? (len + new_slen - slen + offset) : retval; 5324 } 5325 5326 #ifdef FEAT_TERMRESPONSE 5327 LOG_TR(("normal character")); 5328 #endif 5329 5330 return 0; // no match found 5331 } 5332 5333 #if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO) 5334 /* 5335 * Get the text foreground color, if known. 5336 */ 5337 void 5338 term_get_fg_color(char_u *r, char_u *g, char_u *b) 5339 { 5340 if (rfg_status.tr_progress == STATUS_GOT) 5341 { 5342 *r = fg_r; 5343 *g = fg_g; 5344 *b = fg_b; 5345 } 5346 } 5347 5348 /* 5349 * Get the text background color, if known. 5350 */ 5351 void 5352 term_get_bg_color(char_u *r, char_u *g, char_u *b) 5353 { 5354 if (rbg_status.tr_progress == STATUS_GOT) 5355 { 5356 *r = bg_r; 5357 *g = bg_g; 5358 *b = bg_b; 5359 } 5360 } 5361 #endif 5362 5363 /* 5364 * Replace any terminal code strings in from[] with the equivalent internal 5365 * vim representation. This is used for the "from" and "to" part of a 5366 * mapping, and the "to" part of a menu command. 5367 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains 5368 * '<'. 5369 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER. 5370 * 5371 * The replacement is done in result[] and finally copied into allocated 5372 * memory. If this all works well *bufp is set to the allocated memory and a 5373 * pointer to it is returned. If something fails *bufp is set to NULL and from 5374 * is returned. 5375 * 5376 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a 5377 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps 5378 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be 5379 * used instead of a CTRL-V. 5380 * 5381 * Flags: 5382 * REPTERM_FROM_PART see above 5383 * REPTERM_DO_LT also translate <lt> 5384 * REPTERM_SPECIAL always accept <key> notation 5385 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x> 5386 * 5387 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless 5388 * it is NULL. 5389 */ 5390 char_u * 5391 replace_termcodes( 5392 char_u *from, 5393 char_u **bufp, 5394 int flags, 5395 int *did_simplify) 5396 { 5397 int i; 5398 int slen; 5399 int key; 5400 int dlen = 0; 5401 char_u *src; 5402 int do_backslash; // backslash is a special character 5403 int do_special; // recognize <> key codes 5404 int do_key_code; // recognize raw key codes 5405 char_u *result; // buffer for resulting string 5406 5407 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL); 5408 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) 5409 || (flags & REPTERM_SPECIAL); 5410 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL); 5411 5412 /* 5413 * Allocate space for the translation. Worst case a single character is 5414 * replaced by 6 bytes (shifted special key), plus a NUL at the end. 5415 */ 5416 result = alloc(STRLEN(from) * 6 + 1); 5417 if (result == NULL) // out of memory 5418 { 5419 *bufp = NULL; 5420 return from; 5421 } 5422 5423 src = from; 5424 5425 /* 5426 * Check for #n at start only: function key n 5427 */ 5428 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1])) 5429 { 5430 result[dlen++] = K_SPECIAL; 5431 result[dlen++] = 'k'; 5432 if (src[1] == '0') 5433 result[dlen++] = ';'; // #0 is F10 is "k;" 5434 else 5435 result[dlen++] = src[1]; // #3 is F3 is "k3" 5436 src += 2; 5437 } 5438 5439 /* 5440 * Copy each byte from *from to result[dlen] 5441 */ 5442 while (*src != NUL) 5443 { 5444 /* 5445 * If 'cpoptions' does not contain '<', check for special key codes, 5446 * like "<C-S-LeftMouse>" 5447 */ 5448 if (do_special && ((flags & REPTERM_DO_LT) 5449 || STRNCMP(src, "<lt>", 4) != 0)) 5450 { 5451 #ifdef FEAT_EVAL 5452 /* 5453 * Replace <SID> by K_SNR <script-nr> _. 5454 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14) 5455 */ 5456 if (STRNICMP(src, "<SID>", 5) == 0) 5457 { 5458 if (current_sctx.sc_sid <= 0) 5459 emsg(_(e_usingsid)); 5460 else 5461 { 5462 src += 5; 5463 result[dlen++] = K_SPECIAL; 5464 result[dlen++] = (int)KS_EXTRA; 5465 result[dlen++] = (int)KE_SNR; 5466 sprintf((char *)result + dlen, "%ld", 5467 (long)current_sctx.sc_sid); 5468 dlen += (int)STRLEN(result + dlen); 5469 result[dlen++] = '_'; 5470 continue; 5471 } 5472 } 5473 #endif 5474 5475 slen = trans_special(&src, result + dlen, TRUE, FALSE, 5476 (flags & REPTERM_NO_SIMPLIFY) == 0, did_simplify); 5477 if (slen) 5478 { 5479 dlen += slen; 5480 continue; 5481 } 5482 } 5483 5484 /* 5485 * If 'cpoptions' does not contain 'k', see if it's an actual key-code. 5486 * Note that this is also checked after replacing the <> form. 5487 * Single character codes are NOT replaced (e.g. ^H or DEL), because 5488 * it could be a character in the file. 5489 */ 5490 if (do_key_code) 5491 { 5492 i = find_term_bykeys(src); 5493 if (i >= 0) 5494 { 5495 result[dlen++] = K_SPECIAL; 5496 result[dlen++] = termcodes[i].name[0]; 5497 result[dlen++] = termcodes[i].name[1]; 5498 src += termcodes[i].len; 5499 // If terminal code matched, continue after it. 5500 continue; 5501 } 5502 } 5503 5504 #ifdef FEAT_EVAL 5505 if (do_special) 5506 { 5507 char_u *p, *s, len; 5508 5509 /* 5510 * Replace <Leader> by the value of "mapleader". 5511 * Replace <LocalLeader> by the value of "maplocalleader". 5512 * If "mapleader" or "maplocalleader" isn't set use a backslash. 5513 */ 5514 if (STRNICMP(src, "<Leader>", 8) == 0) 5515 { 5516 len = 8; 5517 p = get_var_value((char_u *)"g:mapleader"); 5518 } 5519 else if (STRNICMP(src, "<LocalLeader>", 13) == 0) 5520 { 5521 len = 13; 5522 p = get_var_value((char_u *)"g:maplocalleader"); 5523 } 5524 else 5525 { 5526 len = 0; 5527 p = NULL; 5528 } 5529 if (len != 0) 5530 { 5531 // Allow up to 8 * 6 characters for "mapleader". 5532 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6) 5533 s = (char_u *)"\\"; 5534 else 5535 s = p; 5536 while (*s != NUL) 5537 result[dlen++] = *s++; 5538 src += len; 5539 continue; 5540 } 5541 } 5542 #endif 5543 5544 /* 5545 * Remove CTRL-V and ignore the next character. 5546 * For "from" side the CTRL-V at the end is included, for the "to" 5547 * part it is removed. 5548 * If 'cpoptions' does not contain 'B', also accept a backslash. 5549 */ 5550 key = *src; 5551 if (key == Ctrl_V || (do_backslash && key == '\\')) 5552 { 5553 ++src; // skip CTRL-V or backslash 5554 if (*src == NUL) 5555 { 5556 if (flags & REPTERM_FROM_PART) 5557 result[dlen++] = key; 5558 break; 5559 } 5560 } 5561 5562 // skip multibyte char correctly 5563 for (i = (*mb_ptr2len)(src); i > 0; --i) 5564 { 5565 /* 5566 * If the character is K_SPECIAL, replace it with K_SPECIAL 5567 * KS_SPECIAL KE_FILLER. 5568 * If compiled with the GUI replace CSI with K_CSI. 5569 */ 5570 if (*src == K_SPECIAL) 5571 { 5572 result[dlen++] = K_SPECIAL; 5573 result[dlen++] = KS_SPECIAL; 5574 result[dlen++] = KE_FILLER; 5575 } 5576 # ifdef FEAT_GUI 5577 else if (*src == CSI) 5578 { 5579 result[dlen++] = K_SPECIAL; 5580 result[dlen++] = KS_EXTRA; 5581 result[dlen++] = (int)KE_CSI; 5582 } 5583 # endif 5584 else 5585 result[dlen++] = *src; 5586 ++src; 5587 } 5588 } 5589 result[dlen] = NUL; 5590 5591 /* 5592 * Copy the new string to allocated memory. 5593 * If this fails, just return from. 5594 */ 5595 if ((*bufp = vim_strsave(result)) != NULL) 5596 from = *bufp; 5597 vim_free(result); 5598 return from; 5599 } 5600 5601 /* 5602 * Find a termcode with keys 'src' (must be NUL terminated). 5603 * Return the index in termcodes[], or -1 if not found. 5604 */ 5605 static int 5606 find_term_bykeys(char_u *src) 5607 { 5608 int i; 5609 int slen = (int)STRLEN(src); 5610 5611 for (i = 0; i < tc_len; ++i) 5612 { 5613 if (slen == termcodes[i].len 5614 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0) 5615 return i; 5616 } 5617 return -1; 5618 } 5619 5620 /* 5621 * Gather the first characters in the terminal key codes into a string. 5622 * Used to speed up check_termcode(). 5623 */ 5624 static void 5625 gather_termleader(void) 5626 { 5627 int i; 5628 int len = 0; 5629 5630 #ifdef FEAT_GUI 5631 if (gui.in_use) 5632 termleader[len++] = CSI; // the GUI codes are not in termcodes[] 5633 #endif 5634 #ifdef FEAT_TERMRESPONSE 5635 if (check_for_codes || *T_CRS != NUL) 5636 termleader[len++] = DCS; // the termcode response starts with DCS 5637 // in 8-bit mode 5638 #endif 5639 termleader[len] = NUL; 5640 5641 for (i = 0; i < tc_len; ++i) 5642 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL) 5643 { 5644 termleader[len++] = termcodes[i].code[0]; 5645 termleader[len] = NUL; 5646 } 5647 5648 need_gather = FALSE; 5649 } 5650 5651 /* 5652 * Show all termcodes (for ":set termcap") 5653 * This code looks a lot like showoptions(), but is different. 5654 */ 5655 void 5656 show_termcodes(void) 5657 { 5658 int col; 5659 int *items; 5660 int item_count; 5661 int run; 5662 int row, rows; 5663 int cols; 5664 int i; 5665 int len; 5666 5667 #define INC3 27 // try to make three columns 5668 #define INC2 40 // try to make two columns 5669 #define GAP 2 // spaces between columns 5670 5671 if (tc_len == 0) // no terminal codes (must be GUI) 5672 return; 5673 items = ALLOC_MULT(int, tc_len); 5674 if (items == NULL) 5675 return; 5676 5677 // Highlight title 5678 msg_puts_title(_("\n--- Terminal keys ---")); 5679 5680 /* 5681 * do the loop two times: 5682 * 1. display the short items (non-strings and short strings) 5683 * 2. display the medium items (medium length strings) 5684 * 3. display the long items (remaining strings) 5685 */ 5686 for (run = 1; run <= 3 && !got_int; ++run) 5687 { 5688 /* 5689 * collect the items in items[] 5690 */ 5691 item_count = 0; 5692 for (i = 0; i < tc_len; i++) 5693 { 5694 len = show_one_termcode(termcodes[i].name, 5695 termcodes[i].code, FALSE); 5696 if (len <= INC3 - GAP ? run == 1 5697 : len <= INC2 - GAP ? run == 2 5698 : run == 3) 5699 items[item_count++] = i; 5700 } 5701 5702 /* 5703 * display the items 5704 */ 5705 if (run <= 2) 5706 { 5707 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2); 5708 if (cols == 0) 5709 cols = 1; 5710 rows = (item_count + cols - 1) / cols; 5711 } 5712 else // run == 3 5713 rows = item_count; 5714 for (row = 0; row < rows && !got_int; ++row) 5715 { 5716 msg_putchar('\n'); // go to next line 5717 if (got_int) // 'q' typed in more 5718 break; 5719 col = 0; 5720 for (i = row; i < item_count; i += rows) 5721 { 5722 msg_col = col; // make columns 5723 show_one_termcode(termcodes[items[i]].name, 5724 termcodes[items[i]].code, TRUE); 5725 if (run == 2) 5726 col += INC2; 5727 else 5728 col += INC3; 5729 } 5730 out_flush(); 5731 ui_breakcheck(); 5732 } 5733 } 5734 vim_free(items); 5735 } 5736 5737 /* 5738 * Show one termcode entry. 5739 * Output goes into IObuff[] 5740 */ 5741 int 5742 show_one_termcode(char_u *name, char_u *code, int printit) 5743 { 5744 char_u *p; 5745 int len; 5746 5747 if (name[0] > '~') 5748 { 5749 IObuff[0] = ' '; 5750 IObuff[1] = ' '; 5751 IObuff[2] = ' '; 5752 IObuff[3] = ' '; 5753 } 5754 else 5755 { 5756 IObuff[0] = 't'; 5757 IObuff[1] = '_'; 5758 IObuff[2] = name[0]; 5759 IObuff[3] = name[1]; 5760 } 5761 IObuff[4] = ' '; 5762 5763 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0); 5764 if (p[1] != 't') 5765 STRCPY(IObuff + 5, p); 5766 else 5767 IObuff[5] = NUL; 5768 len = (int)STRLEN(IObuff); 5769 do 5770 IObuff[len++] = ' '; 5771 while (len < 17); 5772 IObuff[len] = NUL; 5773 if (code == NULL) 5774 len += 4; 5775 else 5776 len += vim_strsize(code); 5777 5778 if (printit) 5779 { 5780 msg_puts((char *)IObuff); 5781 if (code == NULL) 5782 msg_puts("NULL"); 5783 else 5784 msg_outtrans(code); 5785 } 5786 return len; 5787 } 5788 5789 #if defined(FEAT_TERMRESPONSE) || defined(PROTO) 5790 /* 5791 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used 5792 * termcap codes from the terminal itself. 5793 * We get them one by one to avoid a very long response string. 5794 */ 5795 static int xt_index_in = 0; 5796 static int xt_index_out = 0; 5797 5798 static void 5799 req_codes_from_term(void) 5800 { 5801 xt_index_out = 0; 5802 xt_index_in = 0; 5803 req_more_codes_from_term(); 5804 } 5805 5806 static void 5807 req_more_codes_from_term(void) 5808 { 5809 char buf[11]; 5810 int old_idx = xt_index_out; 5811 5812 // Don't do anything when going to exit. 5813 if (exiting) 5814 return; 5815 5816 // Send up to 10 more requests out than we received. Avoid sending too 5817 // many, there can be a buffer overflow somewhere. 5818 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL) 5819 { 5820 char *key_name = key_names[xt_index_out]; 5821 5822 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name)); 5823 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]); 5824 out_str_nf((char_u *)buf); 5825 ++xt_index_out; 5826 } 5827 5828 // Send the codes out right away. 5829 if (xt_index_out != old_idx) 5830 out_flush(); 5831 } 5832 5833 /* 5834 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'. 5835 * A "0" instead of the "1" indicates a code that isn't supported. 5836 * Both <name> and <string> are encoded in hex. 5837 * "code" points to the "0" or "1". 5838 */ 5839 static void 5840 got_code_from_term(char_u *code, int len) 5841 { 5842 #define XT_LEN 100 5843 char_u name[3]; 5844 char_u str[XT_LEN]; 5845 int i; 5846 int j = 0; 5847 int c; 5848 5849 // A '1' means the code is supported, a '0' means it isn't. 5850 // When half the length is > XT_LEN we can't use it. 5851 // Our names are currently all 2 characters. 5852 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN) 5853 { 5854 // Get the name from the response and find it in the table. 5855 name[0] = hexhex2nr(code + 3); 5856 name[1] = hexhex2nr(code + 5); 5857 name[2] = NUL; 5858 for (i = 0; key_names[i] != NULL; ++i) 5859 { 5860 if (STRCMP(key_names[i], name) == 0) 5861 { 5862 xt_index_in = i; 5863 break; 5864 } 5865 } 5866 5867 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name)); 5868 5869 if (key_names[i] != NULL) 5870 { 5871 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2) 5872 str[j++] = c; 5873 str[j] = NUL; 5874 if (name[0] == 'C' && name[1] == 'o') 5875 { 5876 // Color count is not a key code. 5877 i = atoi((char *)str); 5878 may_adjust_color_count(i); 5879 } 5880 else 5881 { 5882 // First delete any existing entry with the same code. 5883 i = find_term_bykeys(str); 5884 if (i >= 0) 5885 del_termcode_idx(i); 5886 add_termcode(name, str, ATC_FROM_TERM); 5887 } 5888 } 5889 } 5890 5891 // May request more codes now that we received one. 5892 ++xt_index_in; 5893 req_more_codes_from_term(); 5894 } 5895 5896 /* 5897 * Check if there are any unanswered requests and deal with them. 5898 * This is called before starting an external program or getting direct 5899 * keyboard input. We don't want responses to be send to that program or 5900 * handled as typed text. 5901 */ 5902 static void 5903 check_for_codes_from_term(void) 5904 { 5905 int c; 5906 5907 // If no codes requested or all are answered, no need to wait. 5908 if (xt_index_out == 0 || xt_index_out == xt_index_in) 5909 return; 5910 5911 // Vgetc() will check for and handle any response. 5912 // Keep calling vpeekc() until we don't get any responses. 5913 ++no_mapping; 5914 ++allow_keys; 5915 for (;;) 5916 { 5917 c = vpeekc(); 5918 if (c == NUL) // nothing available 5919 break; 5920 5921 // If a response is recognized it's replaced with K_IGNORE, must read 5922 // it from the input stream. If there is no K_IGNORE we can't do 5923 // anything, break here (there might be some responses further on, but 5924 // we don't want to throw away any typed chars). 5925 if (c != K_SPECIAL && c != K_IGNORE) 5926 break; 5927 c = vgetc(); 5928 if (c != K_IGNORE) 5929 { 5930 vungetc(c); 5931 break; 5932 } 5933 } 5934 --no_mapping; 5935 --allow_keys; 5936 } 5937 #endif 5938 5939 #if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO) 5940 static char ksme_str[20]; 5941 static char ksmr_str[20]; 5942 static char ksmd_str[20]; 5943 5944 /* 5945 * For Win32 console: update termcap codes for existing console attributes. 5946 */ 5947 void 5948 update_tcap(int attr) 5949 { 5950 struct builtin_term *p; 5951 5952 p = find_builtin_term(DEFAULT_TERM); 5953 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr); 5954 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"), 5955 attr | 0x08); // FOREGROUND_INTENSITY 5956 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"), 5957 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4)); 5958 5959 while (p->bt_string != NULL) 5960 { 5961 if (p->bt_entry == (int)KS_ME) 5962 p->bt_string = &ksme_str[0]; 5963 else if (p->bt_entry == (int)KS_MR) 5964 p->bt_string = &ksmr_str[0]; 5965 else if (p->bt_entry == (int)KS_MD) 5966 p->bt_string = &ksmd_str[0]; 5967 ++p; 5968 } 5969 } 5970 5971 # ifdef FEAT_TERMGUICOLORS 5972 # define KSSIZE 20 5973 struct ks_tbl_s 5974 { 5975 int code; // value of KS_ 5976 char *vtp; // code in vtp mode 5977 char *vtp2; // code in vtp2 mode 5978 char buf[KSSIZE]; // save buffer in non-vtp mode 5979 char vbuf[KSSIZE]; // save buffer in vtp mode 5980 char v2buf[KSSIZE]; // save buffer in vtp2 mode 5981 char arr[KSSIZE]; // real buffer 5982 }; 5983 5984 static struct ks_tbl_s ks_tbl[] = 5985 { 5986 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal 5987 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse 5988 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold 5989 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text 5990 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color 5991 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text 5992 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end 5993 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore 5994 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end 5995 # ifdef TERMINFO 5996 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color 5997 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color 5998 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"}, 5999 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"}, 6000 # else 6001 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color 6002 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color 6003 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"}, 6004 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"}, 6005 # endif 6006 {(int)KS_CCO, "256", "256"}, // colors 6007 {(int)KS_NAME} // terminator 6008 }; 6009 6010 static struct builtin_term * 6011 find_first_tcap( 6012 char_u *name, 6013 int code) 6014 { 6015 struct builtin_term *p; 6016 6017 for (p = find_builtin_term(name); p->bt_string != NULL; ++p) 6018 if (p->bt_entry == code) 6019 return p; 6020 return NULL; 6021 } 6022 # endif 6023 6024 /* 6025 * For Win32 console: replace the sequence immediately after termguicolors. 6026 */ 6027 void 6028 swap_tcap(void) 6029 { 6030 # ifdef FEAT_TERMGUICOLORS 6031 static int init_done = FALSE; 6032 static int curr_mode; 6033 struct ks_tbl_s *ks; 6034 struct builtin_term *bt; 6035 int mode; 6036 enum 6037 { 6038 CMODEINDEX, 6039 CMODE24, 6040 CMODE256 6041 }; 6042 6043 // buffer initialization 6044 if (!init_done) 6045 { 6046 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++) 6047 { 6048 bt = find_first_tcap(DEFAULT_TERM, ks->code); 6049 if (bt != NULL) 6050 { 6051 STRNCPY(ks->buf, bt->bt_string, KSSIZE); 6052 STRNCPY(ks->vbuf, ks->vtp, KSSIZE); 6053 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE); 6054 6055 STRNCPY(ks->arr, bt->bt_string, KSSIZE); 6056 bt->bt_string = &ks->arr[0]; 6057 } 6058 } 6059 init_done = TRUE; 6060 curr_mode = CMODEINDEX; 6061 } 6062 6063 if (p_tgc) 6064 mode = CMODE24; 6065 else if (t_colors >= 256) 6066 mode = CMODE256; 6067 else 6068 mode = CMODEINDEX; 6069 6070 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++) 6071 { 6072 bt = find_first_tcap(DEFAULT_TERM, ks->code); 6073 if (bt != NULL) 6074 { 6075 switch (curr_mode) 6076 { 6077 case CMODEINDEX: 6078 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE); 6079 break; 6080 case CMODE24: 6081 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE); 6082 break; 6083 default: 6084 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE); 6085 } 6086 } 6087 } 6088 6089 if (mode != curr_mode) 6090 { 6091 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++) 6092 { 6093 bt = find_first_tcap(DEFAULT_TERM, ks->code); 6094 if (bt != NULL) 6095 { 6096 switch (mode) 6097 { 6098 case CMODEINDEX: 6099 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE); 6100 break; 6101 case CMODE24: 6102 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE); 6103 break; 6104 default: 6105 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE); 6106 } 6107 } 6108 } 6109 6110 curr_mode = mode; 6111 } 6112 # endif 6113 } 6114 6115 #endif 6116 6117 #if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO) 6118 static int 6119 hex_digit(int c) 6120 { 6121 if (isdigit(c)) 6122 return c - '0'; 6123 c = TOLOWER_ASC(c); 6124 if (c >= 'a' && c <= 'f') 6125 return c - 'a' + 10; 6126 return 0x1ffffff; 6127 } 6128 6129 # ifdef VIMDLL 6130 static guicolor_T 6131 gui_adjust_rgb(guicolor_T c) 6132 { 6133 if (gui.in_use) 6134 return c; 6135 else 6136 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff); 6137 } 6138 # else 6139 # define gui_adjust_rgb(c) (c) 6140 # endif 6141 6142 guicolor_T 6143 gui_get_color_cmn(char_u *name) 6144 { 6145 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color 6146 // values as used by the MS-Windows GDI api. It should be used only for 6147 // MS-Windows GDI builds. 6148 # if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI) 6149 # undef RGB 6150 # endif 6151 # ifndef RGB 6152 # define RGB(r, g, b) ((r<<16) | (g<<8) | (b)) 6153 # endif 6154 # define LINE_LEN 100 6155 FILE *fd; 6156 char line[LINE_LEN]; 6157 char_u *fname; 6158 int r, g, b, i; 6159 guicolor_T color; 6160 6161 struct rgbcolor_table_S { 6162 char_u *color_name; 6163 guicolor_T color; 6164 }; 6165 6166 // Only non X11 colors (not present in rgb.txt) and colors in 6167 // color_names[], useful when $VIMRUNTIME is not found,. 6168 static struct rgbcolor_table_S rgb_table[] = { 6169 {(char_u *)"black", RGB(0x00, 0x00, 0x00)}, 6170 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)}, 6171 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)}, 6172 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)}, 6173 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)}, 6174 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)}, 6175 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)}, 6176 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)}, 6177 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)}, 6178 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)}, 6179 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)}, 6180 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11 6181 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)}, 6182 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)}, 6183 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)}, 6184 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)}, 6185 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)}, 6186 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)}, 6187 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)}, 6188 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)}, 6189 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)}, 6190 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)}, 6191 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)}, 6192 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11 6193 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11 6194 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)}, 6195 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)}, 6196 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)}, 6197 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)}, 6198 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)}, 6199 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)}, 6200 }; 6201 6202 static struct rgbcolor_table_S *colornames_table; 6203 static int size = 0; 6204 6205 if (name[0] == '#' && STRLEN(name) == 7) 6206 { 6207 // Name is in "#rrggbb" format 6208 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])), 6209 ((hex_digit(name[3]) << 4) + hex_digit(name[4])), 6210 ((hex_digit(name[5]) << 4) + hex_digit(name[6]))); 6211 if (color > 0xffffff) 6212 return INVALCOLOR; 6213 return gui_adjust_rgb(color); 6214 } 6215 6216 // Check if the name is one of the colors we know 6217 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++) 6218 if (STRICMP(name, rgb_table[i].color_name) == 0) 6219 return gui_adjust_rgb(rgb_table[i].color); 6220 6221 /* 6222 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt". 6223 */ 6224 if (size == 0) 6225 { 6226 int counting; 6227 6228 // colornames_table not yet initialized 6229 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt"); 6230 if (fname == NULL) 6231 return INVALCOLOR; 6232 6233 fd = fopen((char *)fname, "rt"); 6234 vim_free(fname); 6235 if (fd == NULL) 6236 { 6237 if (p_verbose > 1) 6238 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt")); 6239 size = -1; // don't try again 6240 return INVALCOLOR; 6241 } 6242 6243 for (counting = 1; counting >= 0; --counting) 6244 { 6245 if (!counting) 6246 { 6247 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size); 6248 if (colornames_table == NULL) 6249 { 6250 fclose(fd); 6251 return INVALCOLOR; 6252 } 6253 rewind(fd); 6254 } 6255 size = 0; 6256 6257 while (!feof(fd)) 6258 { 6259 size_t len; 6260 int pos; 6261 6262 vim_ignoredp = fgets(line, LINE_LEN, fd); 6263 len = strlen(line); 6264 6265 if (len <= 1 || line[len - 1] != '\n') 6266 continue; 6267 6268 line[len - 1] = '\0'; 6269 6270 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos); 6271 if (i != 3) 6272 continue; 6273 6274 if (!counting) 6275 { 6276 char_u *s = vim_strsave((char_u *)line + pos); 6277 6278 if (s == NULL) 6279 { 6280 fclose(fd); 6281 return INVALCOLOR; 6282 } 6283 colornames_table[size].color_name = s; 6284 colornames_table[size].color = (guicolor_T)RGB(r, g, b); 6285 } 6286 size++; 6287 6288 // The distributed rgb.txt has less than 1000 entries. Limit to 6289 // 10000, just in case the file was messed up. 6290 if (size == 10000) 6291 break; 6292 } 6293 } 6294 fclose(fd); 6295 } 6296 6297 for (i = 0; i < size; i++) 6298 if (STRICMP(name, colornames_table[i].color_name) == 0) 6299 return gui_adjust_rgb(colornames_table[i].color); 6300 6301 return INVALCOLOR; 6302 } 6303 6304 guicolor_T 6305 gui_get_rgb_color_cmn(int r, int g, int b) 6306 { 6307 guicolor_T color = RGB(r, g, b); 6308 6309 if (color > 0xffffff) 6310 return INVALCOLOR; 6311 return gui_adjust_rgb(color); 6312 } 6313 #endif 6314 6315 #if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \ 6316 || defined(PROTO) 6317 static int cube_value[] = { 6318 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF 6319 }; 6320 6321 static int grey_ramp[] = { 6322 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76, 6323 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE 6324 }; 6325 6326 # ifdef FEAT_TERMINAL 6327 # include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE 6328 # else 6329 # define VTERM_ANSI_INDEX_NONE 0 6330 # endif 6331 6332 static char_u ansi_table[16][4] = { 6333 // R G B idx 6334 { 0, 0, 0, 1}, // black 6335 {224, 0, 0, 2}, // dark red 6336 { 0, 224, 0, 3}, // dark green 6337 {224, 224, 0, 4}, // dark yellow / brown 6338 { 0, 0, 224, 5}, // dark blue 6339 {224, 0, 224, 6}, // dark magenta 6340 { 0, 224, 224, 7}, // dark cyan 6341 {224, 224, 224, 8}, // light grey 6342 6343 {128, 128, 128, 9}, // dark grey 6344 {255, 64, 64, 10}, // light red 6345 { 64, 255, 64, 11}, // light green 6346 {255, 255, 64, 12}, // yellow 6347 { 64, 64, 255, 13}, // light blue 6348 {255, 64, 255, 14}, // light magenta 6349 { 64, 255, 255, 15}, // light cyan 6350 {255, 255, 255, 16}, // white 6351 }; 6352 6353 void 6354 cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx) 6355 { 6356 int idx; 6357 6358 if (nr < 16) 6359 { 6360 *r = ansi_table[nr][0]; 6361 *g = ansi_table[nr][1]; 6362 *b = ansi_table[nr][2]; 6363 *ansi_idx = ansi_table[nr][3]; 6364 } 6365 else if (nr < 232) 6366 { 6367 // 216 color cube 6368 idx = nr - 16; 6369 *r = cube_value[idx / 36 % 6]; 6370 *g = cube_value[idx / 6 % 6]; 6371 *b = cube_value[idx % 6]; 6372 *ansi_idx = VTERM_ANSI_INDEX_NONE; 6373 } 6374 else if (nr < 256) 6375 { 6376 // 24 grey scale ramp 6377 idx = nr - 232; 6378 *r = grey_ramp[idx]; 6379 *g = grey_ramp[idx]; 6380 *b = grey_ramp[idx]; 6381 *ansi_idx = VTERM_ANSI_INDEX_NONE; 6382 } 6383 else 6384 { 6385 *r = 0; 6386 *g = 0; 6387 *b = 0; 6388 *ansi_idx = 0; 6389 } 6390 } 6391 #endif 6392 6393