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