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