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