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