blob: 99b33d4399c0442ce502b704d626150409cde184 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# 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
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
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 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010074// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void req_codes_from_term(void);
81static void req_more_codes_from_term(void);
82static void got_code_from_term(char_u *code, int len);
83static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020086static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010087static int term_is_builtin(char_u *name);
88static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010091static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93/*
94 * Here is our own prototype for tgetstr(), any prototypes from the include
95 * files have been disabled by the define at the start of this file.
96 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100100 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +0200101# if 0
102# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200103static void log_tr(const char *fmt, ...);
104# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200105# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200106# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200107# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaarafd78262019-05-10 23:10:31 +0200109typedef enum {
110 STATUS_GET, // send request when switching to RAW mode
111 STATUS_SENT, // did send request, checking for response
112 STATUS_GOT, // received response
113 STATUS_FAIL // timed out
114} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200115
Bram Moolenaarafd78262019-05-10 23:10:31 +0200116typedef struct {
117 request_progress_T tr_progress;
118 time_t tr_start; // when request was sent, -1 for never
119} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200120
Bram Moolenaarafd78262019-05-10 23:10:31 +0200121# define TERMREQUEST_INIT {STATUS_GET, -1}
122
123// Request Terminal Version status:
124static termrequest_T crv_status = TERMREQUEST_INIT;
125
126// Request Cursor position report:
127static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200128
Bram Moolenaara45551a2020-06-09 15:57:37 +0200129// Request xterm compatibility check:
130static termrequest_T xcc_status = TERMREQUEST_INIT;
131
Bram Moolenaar9377df32017-10-15 13:22:01 +0200132# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200133// Request foreground color report:
134static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200135static int fg_r = 0;
136static int fg_g = 0;
137static int fg_b = 0;
138static int bg_r = 255;
139static int bg_g = 255;
140static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200141# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100143// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100149// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100151
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100152// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200153static termrequest_T winpos_status = TERMREQUEST_INIT;
154
155static termrequest_T *all_termrequests[] = {
156 &crv_status,
157 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200158 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200159# ifdef FEAT_TERMINAL
160 &rfg_status,
161# endif
162 &rbg_status,
163 &rbm_status,
164 &rcs_status,
165 &winpos_status,
166 NULL
167};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# 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
179extern short ospeed;
180# else
181short ospeed;
182# endif
183# endif
184# ifndef HAVE_UP_BC_PC
185# ifdef UP_BC_PC_EXTERN
186extern char *UP, *BC, PC;
187# else
188char *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))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100195#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100199#if (defined(UNIX) || defined(VMS))
200static int focus_mode = FALSE; // xterm's "focus reporting" availability
201static int focus_state = FALSE; // TRUE if the terminal window gains focus
202#endif
203
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200204#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100205// When the cursor shape was detected these values are used:
206// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200207static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100209// The blink flag from the style response may be inverted from the actual
210// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200211static int initial_cursor_shape_blink = FALSE;
212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100213// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200214static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200215#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200216
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000217static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219
220#if defined(FEAT_GUI)
221/*
222 * GUI pseudo term-cap.
223 */
224 {(int)KS_NAME, "gui"},
225 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
226 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
227# ifdef TERMINFO
228 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
229# else
230 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
231# endif
232 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
233# ifdef TERMINFO
234 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
235 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# else
238 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
239 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# endif
242 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100243 // attributes switched on with 'h', off with * 'H'
244 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, // HL_ALL
245 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, // HL_INVERSE
246 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, // HL_BOLD
247 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, // HL_STANDOUT
248 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, // HL_STANDOUT
249 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, // HL_UNDERLINE
250 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, // HL_UNDERLINE
251 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, // HL_UNDERCURL
252 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, // HL_UNDERCURL
253 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, // HL_STRIKETHROUGH
254 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, // HL_STRIKETHROUGH
255 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, // HL_ITALIC
256 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, // HL_ITALIC
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
258 {(int)KS_MS, "y"},
259 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100260 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100261 {(int)KS_LE, "\b"}, // cursor-left = BS
262 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263# ifdef TERMINFO
264 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
265# else
266 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
267# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100268 // there are no key sequences here, the GUI sequences are recognized
269 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
272#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
275/*
276 * Amiga console window, default for Amiga
277 */
278 {(int)KS_NAME, "amiga"},
279 {(int)KS_CE, "\033[K"},
280 {(int)KS_CD, "\033[J"},
281 {(int)KS_AL, "\033[L"},
282# ifdef TERMINFO
283 {(int)KS_CAL, "\033[%p1%dL"},
284# else
285 {(int)KS_CAL, "\033[%dL"},
286# endif
287 {(int)KS_DL, "\033[M"},
288# ifdef TERMINFO
289 {(int)KS_CDL, "\033[%p1%dM"},
290# else
291 {(int)KS_CDL, "\033[%dM"},
292# endif
293 {(int)KS_CL, "\014"},
294 {(int)KS_VI, "\033[0 p"},
295 {(int)KS_VE, "\033[1 p"},
296 {(int)KS_ME, "\033[0m"},
297 {(int)KS_MR, "\033[7m"},
298 {(int)KS_MD, "\033[1m"},
299 {(int)KS_SE, "\033[0m"},
300 {(int)KS_SO, "\033[33m"},
301 {(int)KS_US, "\033[4m"},
302 {(int)KS_UE, "\033[0m"},
303 {(int)KS_CZH, "\033[3m"},
304 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100305#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100306 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100308 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
309 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100311 {(int)KS_CAB, "\033[4%dm"}, // set background color
312 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100314 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
316 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100317 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 {(int)KS_LE, "\b"},
319# ifdef TERMINFO
320 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
321# else
322 {(int)KS_CM, "\033[%i%d;%dH"},
323# endif
324#if defined(__MORPHOS__)
325 {(int)KS_SR, "\033M"},
326#endif
327# ifdef TERMINFO
328 {(int)KS_CRI, "\033[%p1%dC"},
329# else
330 {(int)KS_CRI, "\033[%dC"},
331# endif
332 {K_UP, "\233A"},
333 {K_DOWN, "\233B"},
334 {K_LEFT, "\233D"},
335 {K_RIGHT, "\233C"},
336 {K_S_UP, "\233T"},
337 {K_S_DOWN, "\233S"},
338 {K_S_LEFT, "\233 A"},
339 {K_S_RIGHT, "\233 @"},
340 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100341 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {K_F2, "\233\061~"},
343 {K_F3, "\233\062~"},
344 {K_F4, "\233\063~"},
345 {K_F5, "\233\064~"},
346 {K_F6, "\233\065~"},
347 {K_F7, "\233\066~"},
348 {K_F8, "\233\067~"},
349 {K_F9, "\233\070~"},
350 {K_F10, "\233\071~"},
351 {K_S_F1, "\233\061\060~"},
352 {K_S_F2, "\233\061\061~"},
353 {K_S_F3, "\233\061\062~"},
354 {K_S_F4, "\233\061\063~"},
355 {K_S_F5, "\233\061\064~"},
356 {K_S_F6, "\233\061\065~"},
357 {K_S_F7, "\233\061\066~"},
358 {K_S_F8, "\233\061\067~"},
359 {K_S_F9, "\233\061\070~"},
360 {K_S_F10, "\233\061\071~"},
361 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100362 {K_INS, "\233\064\060~"}, // 101 key keyboard
363 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
364 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
365 {K_HOME, "\233\064\064~"}, // 101 key keyboard
366 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
368 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
370 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
371 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# endif
373
Bram Moolenaar041c7102020-05-30 18:14:57 +0200374# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200376 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {(int)KS_CE, "\033[K"},
379 {(int)KS_CD, "\033[J"},
380 {(int)KS_AL, "\033[L"},
381# ifdef TERMINFO
382 {(int)KS_CAL, "\033[%p1%dL"},
383# else
384 {(int)KS_CAL, "\033[%dL"},
385# endif
386 {(int)KS_DL, "\033[M"},
387# ifdef TERMINFO
388 {(int)KS_CDL, "\033[%p1%dM"},
389# else
390 {(int)KS_CDL, "\033[%dM"},
391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 {(int)KS_CL, "\033[H\033[2J"},
393#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100394 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
395 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100397 {(int)KS_ME, "\033[m"}, // normal mode
398 {(int)KS_MR, "\033[7m"}, // reverse
399 {(int)KS_MD, "\033[1m"}, // bold
400 {(int)KS_SO, "\033[31m"}, // standout mode: red
401 {(int)KS_SE, "\033[m"}, // standout end
402 {(int)KS_CZH, "\033[35m"}, // italic: purple
403 {(int)KS_CZR, "\033[m"}, // italic end
404 {(int)KS_US, "\033[4m"}, // underscore mode
405 {(int)KS_UE, "\033[m"}, // underscore end
406 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100408 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
409 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100411 {(int)KS_CAB, "\033[4%dm"}, // set background color
412 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100414 {(int)KS_OP, "\033[m"}, // reset colors
415 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
416 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {(int)KS_LE, "\b"},
418# ifdef TERMINFO
419 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
420# else
421 {(int)KS_CM, "\033[%i%d;%dH"},
422# endif
423 {(int)KS_SR, "\033M"},
424# ifdef TERMINFO
425 {(int)KS_CRI, "\033[%p1%dC"},
426# else
427 {(int)KS_CRI, "\033[%dC"},
428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
430 {K_UP, "\033[A"},
431 {K_DOWN, "\033[B"},
432 {K_LEFT, "\033[D"},
433 {K_RIGHT, "\033[C"},
434# endif
435
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200436# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437/*
438 * standard ANSI terminal, default for unix
439 */
440 {(int)KS_NAME, "ansi"},
441 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
442 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
443# ifdef TERMINFO
444 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
445# else
446 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
447# endif
448 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
449# ifdef TERMINFO
450 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
451# else
452 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
453# endif
454 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
455 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
456 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
457 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100458 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {(int)KS_LE, "\b"},
460# ifdef TERMINFO
461 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
462# else
463 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
464# endif
465# ifdef TERMINFO
466 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
467# else
468 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
469# endif
470# endif
471
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200472# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473/*
474 * These codes are valid when nansi.sys or equivalent has been installed.
475 * Function keys on a PC are preceded with a NUL. These are converted into
476 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
477 * CTRL-arrow is used instead of SHIFT-arrow.
478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {(int)KS_NAME, "pcansi"},
480 {(int)KS_DL, "\033[M"},
481 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {(int)KS_CE, "\033[K"},
483 {(int)KS_CL, "\033[2J"},
484 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100485 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
486 {(int)KS_MD, "\033[1m"}, // bold: white text
487 {(int)KS_SE, "\033[0m"}, // standout end
488 {(int)KS_SO, "\033[31m"}, // standout: white on blue
489 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
490 {(int)KS_CZR, "\033[0m"}, // italic mode end
491 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
492 {(int)KS_UE, "\033[0m"}, // underscore mode end
493 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100495 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
496 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100498 {(int)KS_CAB, "\033[4%dm"}, // set background color
499 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100501 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 {(int)KS_LE, "\b"},
505# ifdef TERMINFO
506 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
507# else
508 {(int)KS_CM, "\033[%i%d;%dH"},
509# endif
510# ifdef TERMINFO
511 {(int)KS_CRI, "\033[%p1%dC"},
512# else
513 {(int)KS_CRI, "\033[%dC"},
514# endif
515 {K_UP, "\316H"},
516 {K_DOWN, "\316P"},
517 {K_LEFT, "\316K"},
518 {K_RIGHT, "\316M"},
519 {K_S_LEFT, "\316s"},
520 {K_S_RIGHT, "\316t"},
521 {K_F1, "\316;"},
522 {K_F2, "\316<"},
523 {K_F3, "\316="},
524 {K_F4, "\316>"},
525 {K_F5, "\316?"},
526 {K_F6, "\316@"},
527 {K_F7, "\316A"},
528 {K_F8, "\316B"},
529 {K_F9, "\316C"},
530 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100531 {K_F11, "\316\205"}, // guessed
532 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {K_S_F1, "\316T"},
534 {K_S_F2, "\316U"},
535 {K_S_F3, "\316V"},
536 {K_S_F4, "\316W"},
537 {K_S_F5, "\316X"},
538 {K_S_F6, "\316Y"},
539 {K_S_F7, "\316Z"},
540 {K_S_F8, "\316["},
541 {K_S_F9, "\316\\"},
542 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100543 {K_S_F11, "\316\207"}, // guessed
544 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {K_INS, "\316R"},
546 {K_DEL, "\316S"},
547 {K_HOME, "\316G"},
548 {K_END, "\316O"},
549 {K_PAGEDOWN, "\316Q"},
550 {K_PAGEUP, "\316I"},
551# endif
552
Bram Moolenaar4f974752019-02-17 17:44:42 +0100553# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554/*
555 * These codes are valid for the Win32 Console . The entries that start with
556 * ESC | are translated into console calls in os_win32.c. The function keys
557 * are also translated in os_win32.c.
558 */
559 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100560 {(int)KS_CE, "\033|K"}, // clear to end of line
561 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100563 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100565 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100569 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
570 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100572 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
573 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_CL, "\033|J"}, // clear screen
576 {(int)KS_CD, "\033|j"}, // clear to end of display
577 {(int)KS_VI, "\033|v"}, // cursor invisible
578 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaar6982f422019-02-16 16:48:01 +0100580 {(int)KS_ME, "\033|0m"}, // normal
581 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
582 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100584 {(int)KS_SO, "\033|31m"}, // standout: white on blue
585 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_SO, "\033|F"}, // standout: high intensity
588 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100590 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
591 {(int)KS_CZR, "\033|0m"}, // italic end
592 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
593 {(int)KS_UE, "\033|0m"}, // underscore end
594 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100596 {(int)KS_CAB, "\033|%p1%db"}, // set background color
597 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100599 {(int)KS_CAB, "\033|%db"}, // set background color
600 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# endif
602
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100605 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {(int)KS_LE, "\b"},
607# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100608 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100612 {(int)KS_VB, "\033|B"}, // visual bell
613 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
614 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100616 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100620# ifdef FEAT_TERMGUICOLORS
621 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
622 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 {K_UP, "\316H"},
626 {K_DOWN, "\316P"},
627 {K_LEFT, "\316K"},
628 {K_RIGHT, "\316M"},
629 {K_S_UP, "\316\304"},
630 {K_S_DOWN, "\316\317"},
631 {K_S_LEFT, "\316\311"},
632 {K_C_LEFT, "\316s"},
633 {K_S_RIGHT, "\316\313"},
634 {K_C_RIGHT, "\316t"},
635 {K_S_TAB, "\316\017"},
636 {K_F1, "\316;"},
637 {K_F2, "\316<"},
638 {K_F3, "\316="},
639 {K_F4, "\316>"},
640 {K_F5, "\316?"},
641 {K_F6, "\316@"},
642 {K_F7, "\316A"},
643 {K_F8, "\316B"},
644 {K_F9, "\316C"},
645 {K_F10, "\316D"},
646 {K_F11, "\316\205"},
647 {K_F12, "\316\206"},
648 {K_S_F1, "\316T"},
649 {K_S_F2, "\316U"},
650 {K_S_F3, "\316V"},
651 {K_S_F4, "\316W"},
652 {K_S_F5, "\316X"},
653 {K_S_F6, "\316Y"},
654 {K_S_F7, "\316Z"},
655 {K_S_F8, "\316["},
656 {K_S_F9, "\316\\"},
657 {K_S_F10, "\316]"},
658 {K_S_F11, "\316\207"},
659 {K_S_F12, "\316\210"},
660 {K_INS, "\316R"},
661 {K_DEL, "\316S"},
662 {K_HOME, "\316G"},
663 {K_S_HOME, "\316\302"},
664 {K_C_HOME, "\316w"},
665 {K_END, "\316O"},
666 {K_S_END, "\316\315"},
667 {K_C_END, "\316u"},
668 {K_PAGEDOWN, "\316Q"},
669 {K_PAGEUP, "\316I"},
670 {K_KPLUS, "\316N"},
671 {K_KMINUS, "\316J"},
672 {K_KMULTIPLY, "\316\067"},
673 {K_K0, "\316\332"},
674 {K_K1, "\316\336"},
675 {K_K2, "\316\342"},
676 {K_K3, "\316\346"},
677 {K_K4, "\316\352"},
678 {K_K5, "\316\356"},
679 {K_K6, "\316\362"},
680 {K_K7, "\316\366"},
681 {K_K8, "\316\372"},
682 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100683 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684# endif
685
686# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
687/*
688 * VT320 is working as an ANSI terminal compatible DEC terminal.
689 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * TODO:- rewrite ESC[ codes to CSI
691 * - keyboard languages (CSI ? 26 n)
692 */
693 {(int)KS_NAME, "vt320"},
694 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
695 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
696# ifdef TERMINFO
697 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
698# else
699 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
700# endif
701 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
702# ifdef TERMINFO
703 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
704# else
705 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
706# endif
707 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000708 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100709 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
711 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100712 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, // bold mode
713 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},// normal mode
714 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},// exit underscore mode
715 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, // underscore mode
716 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, // italic mode: blue text on yellow
717 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, // italic mode end
718 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, // set background color (ANSI)
719 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, // set foreground color (ANSI)
720 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, // set screen background color
721 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {(int)KS_MS, "y"},
723 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100724 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {(int)KS_LE, "\b"},
726# ifdef TERMINFO
727 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
728 ESC_STR "[%i%p1%d;%p2%dH")},
729# else
730 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
731# endif
732# ifdef TERMINFO
733 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
734# else
735 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
736# endif
737 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
738 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
739 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
740 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200741 // Note: cursor key sequences for application cursor mode are omitted,
742 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
744 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
745 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
746 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
747 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
749 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
750 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
751 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
752 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000753 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
755 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
756 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100757 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, // Help
758 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, // Select
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
760 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
761 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
762 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
763 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
764 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
765 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
766 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
767 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
768 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200769 // These sequences starting with <Esc> O may interfere with what the user
770 // is typing. Remove these if that bothers you.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100771 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, // keypad plus
772 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, // keypad minus
773 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, // keypad /
774 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, // keypad *
775 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, // keypad Enter
776 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, // keypad 0
777 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, // keypad 1
778 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, // keypad 2
779 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, // keypad 3
780 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, // keypad 4
781 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, // keypad 5
782 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, // keypad 6
783 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, // keypad 7
784 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, // keypad 8
785 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, // keypad 9
786 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787# endif
788
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200789# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790/*
791 * Ordinary vt52
792 */
793 {(int)KS_NAME, "vt52"},
794 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
795 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100796# ifdef TERMINFO
797 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
798 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
799# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
805 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {K_UP, IF_EB("\033A", ESC_STR "A")},
807 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
808 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
809 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100810 {K_F1, IF_EB("\033P", ESC_STR "P")},
811 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
812 {K_F3, IF_EB("\033R", ESC_STR "R")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815# endif
816
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200817# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200818 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
820 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
821# ifdef TERMINFO
822 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
823# else
824 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
825# endif
826 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
827# ifdef TERMINFO
828 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
829# else
830 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
831# endif
832# ifdef TERMINFO
833 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
834 ESC_STR "[%i%p1%d;%p2%dr")},
835# else
836 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
837# endif
838 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
839 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
840 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
841 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
842 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
843 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
844 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200845 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
846 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {(int)KS_MS, "y"},
848 {(int)KS_UT, "y"},
849 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200850 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
851 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200852 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200853 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200854# ifdef TERMINFO
855 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
856# else
857 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
858# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200859 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200860 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861# ifdef TERMINFO
862 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
863 ESC_STR "[%i%p1%d;%p2%dH")},
864# else
865 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
866# endif
867 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
868# ifdef TERMINFO
869 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
870# else
871 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
872# endif
873 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
874 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
875# ifdef FEAT_XTERM_SAVE
876 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
Bram Moolenaard02e5082020-02-08 14:22:53 +0100877 {(int)KS_TE, IF_EB("\033[?47l\0338",
878 ESC_STR_nc "[?47l" ESC_STR_nc "8")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# endif
Bram Moolenaar4b570182019-10-20 19:53:22 +0200880 {(int)KS_CTI, IF_EB("\033[>4;2m", ESC_STR_nc "[>4;2m")},
881 {(int)KS_CTE, IF_EB("\033[>4;m", ESC_STR_nc "[>4;m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
883 {(int)KS_CIE, "\007"},
884 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
885 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200886 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
887 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888# ifdef TERMINFO
889 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
890 ESC_STR "[8;%p1%d;%p2%dt")},
891 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
892 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200893 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894# else
895 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
896 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200897 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898# endif
899 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200900 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200901 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100902 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200903# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100904 // These are printf strings, not terminal codes.
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200905 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
906 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
Bram Moolenaare023e882020-05-31 16:42:30 +0200907 {(int)KS_8U, IF_EB("\033[58;2;%lu;%lu;%lum", ESC_STR "[58;2;%lu;%lu;%lum")},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200908# endif
Bram Moolenaare023e882020-05-31 16:42:30 +0200909 {(int)KS_CAU, IF_EB("\033[58;5;%dm", ESC_STR "[58;5;%dm")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100910 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
911 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200912 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
913 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
914 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
915 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100916# if (defined(UNIX) || defined(VMS))
917 {(int)KS_FD, IF_EB("\033[?1004l", ESC_STR "[?1004l")},
918 {(int)KS_FE, IF_EB("\033[?1004h", ESC_STR "[?1004h")},
919# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000920
921 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
922 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
923 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
924 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100925 // An extra set of cursor keys for vt100 mode
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100926 {K_XUP, IF_EB("\033[@;*A", ESC_STR "[@;*A")},
927 {K_XDOWN, IF_EB("\033[@;*B", ESC_STR "[@;*B")},
928 {K_XRIGHT, IF_EB("\033[@;*C", ESC_STR "[@;*C")},
929 {K_XLEFT, IF_EB("\033[@;*D", ESC_STR "[@;*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100930 // An extra set of function keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000931 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
932 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
933 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
934 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000935 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
936 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
937 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
938 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
939 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
940 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
941 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
942 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
943 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
944 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
945 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
946 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000948 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
949 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
950 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
951 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100952 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")},
953 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000954 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100955 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home
956 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000957 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100958 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")},
959 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000960 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100961 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000962 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000963 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
964 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100965 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus
966 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus
967 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad /
968 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad *
969 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter
970 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad .
971 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0
972 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1
973 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2
974 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3
975 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4
976 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5
977 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6
978 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7
979 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8
980 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9
981 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del
982 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start
983 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0
987 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13
988 // F14 and F15 are missing, because they send the same codes as the undo
989 // and help key, although they don't work on all keyboards.
990 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16
991 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17
992 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18
993 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19
994 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100996 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21
997 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22
998 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23
999 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24
1000 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25
1001 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26
1002 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27
1003 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28
1004 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29
1005 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001007 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31
1008 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32
1009 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33
1010 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34
1011 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35
1012 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36
1013 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014# endif
1015
1016# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1017/*
1018 * iris-ansi for Silicon Graphics machines.
1019 */
1020 {(int)KS_NAME, "iris-ansi"},
1021 {(int)KS_CE, "\033[K"},
1022 {(int)KS_CD, "\033[J"},
1023 {(int)KS_AL, "\033[L"},
1024# ifdef TERMINFO
1025 {(int)KS_CAL, "\033[%p1%dL"},
1026# else
1027 {(int)KS_CAL, "\033[%dL"},
1028# endif
1029 {(int)KS_DL, "\033[M"},
1030# ifdef TERMINFO
1031 {(int)KS_CDL, "\033[%p1%dM"},
1032# else
1033 {(int)KS_CDL, "\033[%dM"},
1034# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036# ifdef TERMINFO
1037 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1038# else
1039 {(int)KS_CS, "\033[%i%d;%dr"},
1040# endif
1041#endif
1042 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001043 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1044 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {(int)KS_TI, "\033[=6h"},
1046 {(int)KS_TE, "\033[=6l"},
1047 {(int)KS_SE, "\033[21;27m"},
1048 {(int)KS_SO, "\033[1;7m"},
1049 {(int)KS_ME, "\033[m"},
1050 {(int)KS_MR, "\033[7m"},
1051 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 {(int)KS_CCO, "8"}, // allow 8 colors
1053 {(int)KS_CZH, "\033[3m"}, // italic mode on
1054 {(int)KS_CZR, "\033[23m"}, // italic mode off
1055 {(int)KS_US, "\033[4m"}, // underline on
1056 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001058 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1059 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1060 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1061 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001063 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1064 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1065 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1066 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001068 {(int)KS_MS, "y"}, // guessed
1069 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {(int)KS_LE, "\b"},
1071# ifdef TERMINFO
1072 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1073# else
1074 {(int)KS_CM, "\033[%i%d;%dH"},
1075# endif
1076 {(int)KS_SR, "\033M"},
1077# ifdef TERMINFO
1078 {(int)KS_CRI, "\033[%p1%dC"},
1079# else
1080 {(int)KS_CRI, "\033[%dC"},
1081# endif
1082 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001083 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001085 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086# ifdef TERMINFO
1087 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1088 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1089# else
1090 {(int)KS_CWS, "\033[203;%d;%d/y"},
1091 {(int)KS_CWP, "\033[205;%d;%d/y"},
1092# endif
1093 {K_UP, "\033[A"},
1094 {K_DOWN, "\033[B"},
1095 {K_LEFT, "\033[D"},
1096 {K_RIGHT, "\033[C"},
1097 {K_S_UP, "\033[161q"},
1098 {K_S_DOWN, "\033[164q"},
1099 {K_S_LEFT, "\033[158q"},
1100 {K_S_RIGHT, "\033[167q"},
1101 {K_F1, "\033[001q"},
1102 {K_F2, "\033[002q"},
1103 {K_F3, "\033[003q"},
1104 {K_F4, "\033[004q"},
1105 {K_F5, "\033[005q"},
1106 {K_F6, "\033[006q"},
1107 {K_F7, "\033[007q"},
1108 {K_F8, "\033[008q"},
1109 {K_F9, "\033[009q"},
1110 {K_F10, "\033[010q"},
1111 {K_F11, "\033[011q"},
1112 {K_F12, "\033[012q"},
1113 {K_S_F1, "\033[013q"},
1114 {K_S_F2, "\033[014q"},
1115 {K_S_F3, "\033[015q"},
1116 {K_S_F4, "\033[016q"},
1117 {K_S_F5, "\033[017q"},
1118 {K_S_F6, "\033[018q"},
1119 {K_S_F7, "\033[019q"},
1120 {K_S_F8, "\033[020q"},
1121 {K_S_F9, "\033[021q"},
1122 {K_S_F10, "\033[022q"},
1123 {K_S_F11, "\033[023q"},
1124 {K_S_F12, "\033[024q"},
1125 {K_INS, "\033[139q"},
1126 {K_HOME, "\033[H"},
1127 {K_END, "\033[146q"},
1128 {K_PAGEUP, "\033[150q"},
1129 {K_PAGEDOWN, "\033[154q"},
1130# endif
1131
1132# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1133/*
1134 * for debugging
1135 */
1136 {(int)KS_NAME, "debug"},
1137 {(int)KS_CE, "[CE]"},
1138 {(int)KS_CD, "[CD]"},
1139 {(int)KS_AL, "[AL]"},
1140# ifdef TERMINFO
1141 {(int)KS_CAL, "[CAL%p1%d]"},
1142# else
1143 {(int)KS_CAL, "[CAL%d]"},
1144# endif
1145 {(int)KS_DL, "[DL]"},
1146# ifdef TERMINFO
1147 {(int)KS_CDL, "[CDL%p1%d]"},
1148# else
1149 {(int)KS_CDL, "[CDL%d]"},
1150# endif
1151# ifdef TERMINFO
1152 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1153# else
1154 {(int)KS_CS, "[%dCS%d]"},
1155# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001158# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160# endif
1161# ifdef TERMINFO
1162 {(int)KS_CAB, "[CAB%p1%d]"},
1163 {(int)KS_CAF, "[CAF%p1%d]"},
1164 {(int)KS_CSB, "[CSB%p1%d]"},
1165 {(int)KS_CSF, "[CSF%p1%d]"},
1166# else
1167 {(int)KS_CAB, "[CAB%d]"},
1168 {(int)KS_CAF, "[CAF%d]"},
1169 {(int)KS_CSB, "[CSB%d]"},
1170 {(int)KS_CSF, "[CSF%d]"},
1171# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001172 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {(int)KS_OP, "[OP]"},
1174 {(int)KS_LE, "[LE]"},
1175 {(int)KS_CL, "[CL]"},
1176 {(int)KS_VI, "[VI]"},
1177 {(int)KS_VE, "[VE]"},
1178 {(int)KS_VS, "[VS]"},
1179 {(int)KS_ME, "[ME]"},
1180 {(int)KS_MR, "[MR]"},
1181 {(int)KS_MB, "[MB]"},
1182 {(int)KS_MD, "[MD]"},
1183 {(int)KS_SE, "[SE]"},
1184 {(int)KS_SO, "[SO]"},
1185 {(int)KS_UE, "[UE]"},
1186 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001187 {(int)KS_UCE, "[UCE]"},
1188 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001189 {(int)KS_STE, "[STE]"},
1190 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {(int)KS_MS, "[MS]"},
1192 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001193 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194# ifdef TERMINFO
1195 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1196# else
1197 {(int)KS_CM, "[%dCM%d]"},
1198# endif
1199 {(int)KS_SR, "[SR]"},
1200# ifdef TERMINFO
1201 {(int)KS_CRI, "[CRI%p1%d]"},
1202# else
1203 {(int)KS_CRI, "[CRI%d]"},
1204# endif
1205 {(int)KS_VB, "[VB]"},
1206 {(int)KS_KS, "[KS]"},
1207 {(int)KS_KE, "[KE]"},
1208 {(int)KS_TI, "[TI]"},
1209 {(int)KS_TE, "[TE]"},
1210 {(int)KS_CIS, "[CIS]"},
1211 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001212 {(int)KS_CSC, "[CSC]"},
1213 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {(int)KS_TS, "[TS]"},
1215 {(int)KS_FS, "[FS]"},
1216# ifdef TERMINFO
1217 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1218 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1219# else
1220 {(int)KS_CWS, "[%dCWS%d]"},
1221 {(int)KS_CWP, "[%dCWP%d]"},
1222# endif
1223 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001224 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001225 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001226 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_UP, "[KU]"},
1228 {K_DOWN, "[KD]"},
1229 {K_LEFT, "[KL]"},
1230 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001231 {K_XUP, "[xKU]"},
1232 {K_XDOWN, "[xKD]"},
1233 {K_XLEFT, "[xKL]"},
1234 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_S_UP, "[S-KU]"},
1236 {K_S_DOWN, "[S-KD]"},
1237 {K_S_LEFT, "[S-KL]"},
1238 {K_C_LEFT, "[C-KL]"},
1239 {K_S_RIGHT, "[S-KR]"},
1240 {K_C_RIGHT, "[C-KR]"},
1241 {K_F1, "[F1]"},
1242 {K_XF1, "[xF1]"},
1243 {K_F2, "[F2]"},
1244 {K_XF2, "[xF2]"},
1245 {K_F3, "[F3]"},
1246 {K_XF3, "[xF3]"},
1247 {K_F4, "[F4]"},
1248 {K_XF4, "[xF4]"},
1249 {K_F5, "[F5]"},
1250 {K_F6, "[F6]"},
1251 {K_F7, "[F7]"},
1252 {K_F8, "[F8]"},
1253 {K_F9, "[F9]"},
1254 {K_F10, "[F10]"},
1255 {K_F11, "[F11]"},
1256 {K_F12, "[F12]"},
1257 {K_S_F1, "[S-F1]"},
1258 {K_S_XF1, "[S-xF1]"},
1259 {K_S_F2, "[S-F2]"},
1260 {K_S_XF2, "[S-xF2]"},
1261 {K_S_F3, "[S-F3]"},
1262 {K_S_XF3, "[S-xF3]"},
1263 {K_S_F4, "[S-F4]"},
1264 {K_S_XF4, "[S-xF4]"},
1265 {K_S_F5, "[S-F5]"},
1266 {K_S_F6, "[S-F6]"},
1267 {K_S_F7, "[S-F7]"},
1268 {K_S_F8, "[S-F8]"},
1269 {K_S_F9, "[S-F9]"},
1270 {K_S_F10, "[S-F10]"},
1271 {K_S_F11, "[S-F11]"},
1272 {K_S_F12, "[S-F12]"},
1273 {K_HELP, "[HELP]"},
1274 {K_UNDO, "[UNDO]"},
1275 {K_BS, "[BS]"},
1276 {K_INS, "[INS]"},
1277 {K_KINS, "[KINS]"},
1278 {K_DEL, "[DEL]"},
1279 {K_KDEL, "[KDEL]"},
1280 {K_HOME, "[HOME]"},
1281 {K_S_HOME, "[C-HOME]"},
1282 {K_C_HOME, "[C-HOME]"},
1283 {K_KHOME, "[KHOME]"},
1284 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001285 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {K_END, "[END]"},
1287 {K_S_END, "[C-END]"},
1288 {K_C_END, "[C-END]"},
1289 {K_KEND, "[KEND]"},
1290 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001291 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {K_PAGEUP, "[PAGEUP]"},
1293 {K_PAGEDOWN, "[PAGEDOWN]"},
1294 {K_KPAGEUP, "[KPAGEUP]"},
1295 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1296 {K_MOUSE, "[MOUSE]"},
1297 {K_KPLUS, "[KPLUS]"},
1298 {K_KMINUS, "[KMINUS]"},
1299 {K_KDIVIDE, "[KDIVIDE]"},
1300 {K_KMULTIPLY, "[KMULTIPLY]"},
1301 {K_KENTER, "[KENTER]"},
1302 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001303 {K_PS, "[PASTE-START]"},
1304 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {K_K0, "[K0]"},
1306 {K_K1, "[K1]"},
1307 {K_K2, "[K2]"},
1308 {K_K3, "[K3]"},
1309 {K_K4, "[K4]"},
1310 {K_K5, "[K5]"},
1311 {K_K6, "[K6]"},
1312 {K_K7, "[K7]"},
1313 {K_K8, "[K8]"},
1314 {K_K9, "[K9]"},
1315# endif
1316
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319/*
1320 * The most minimal terminal: only clear screen and cursor positioning
1321 * Always included.
1322 */
1323 {(int)KS_NAME, "dumb"},
1324 {(int)KS_CL, "\014"},
1325#ifdef TERMINFO
1326 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1327 ESC_STR "[%i%p1%d;%p2%dH")},
1328#else
1329 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1330#endif
1331
1332/*
1333 * end marker
1334 */
1335 {(int)KS_NAME, NULL}
1336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001339#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001340 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001341termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001342{
Bram Moolenaarab302212016-04-26 20:59:29 +02001343 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344}
1345
1346 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348{
1349 guicolor_T t;
1350
1351 if (*name == NUL)
1352 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001353 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001354
1355 if (t == INVALCOLOR)
Bram Moolenaar87202262020-05-24 17:23:45 +02001356 semsg(_(e_alloc_color), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357 return t;
1358}
1359
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001360 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef AMIGA
1371# define DEFAULT_TERM (char_u *)"amiga"
1372#endif
1373
1374#ifdef MSWIN
1375# define DEFAULT_TERM (char_u *)"win32"
1376#endif
1377
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001378#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379# define DEFAULT_TERM (char_u *)"ansi"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#ifdef VMS
1383# define DEFAULT_TERM (char_u *)"vt320"
1384#endif
1385
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001386#ifdef __HAIKU__
1387# undef DEFAULT_TERM
1388# define DEFAULT_TERM (char_u *)"xterm"
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifndef DEFAULT_TERM
1392# define DEFAULT_TERM (char_u *)"dumb"
1393#endif
1394
1395/*
1396 * Term_strings contains currently used terminal output strings.
1397 * It is initialized with the default values by parse_builtin_tcap().
1398 * The values can be changed by setting the option with the same name.
1399 */
1400char_u *(term_strings[(int)KS_LAST + 1]);
1401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402static int need_gather = FALSE; // need to fill termleader[]
1403static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001406
1407/*
1408 * Structure and table to store terminal features that can be detected by
1409 * querying the terminal. Either by inspecting the termresponse or a more
1410 * specific request. Besides this there are:
1411 * t_colors - number of colors supported
1412 */
1413typedef struct {
1414 char *tpr_name;
1415 int tpr_set_by_termresponse;
1416 int tpr_status;
1417} termprop_T;
1418
1419// Values for tpr_status.
1420#define TPR_UNKNOWN 'u'
1421#define TPR_YES 'y'
1422#define TPR_NO 'n'
1423#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1424#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1425#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1426
1427// can request the cursor style without messing up the display
1428#define TPR_CURSOR_STYLE 0
1429// can request the cursor blink mode without messing up the display
1430#define TPR_CURSOR_BLINK 1
1431// can set the underline color with t_8u without resetting other colors
1432#define TPR_UNDERLINE_RGB 2
1433// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1434#define TPR_MOUSE 3
1435// table size
1436#define TPR_COUNT 4
1437
1438static termprop_T term_props[TPR_COUNT];
1439
1440/*
1441 * Initialize the term_props table.
1442 * When "all" is FALSE only set those that are detected from the version
1443 * response.
1444 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001445 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001446init_term_props(int all)
1447{
1448 int i;
1449
1450 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1451 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1452 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1453 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1454 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1455 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1456 term_props[TPR_MOUSE].tpr_name = "mouse";
1457 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1458
1459 for (i = 0; i < TPR_COUNT; ++i)
1460 if (all || term_props[i].tpr_set_by_termresponse)
1461 term_props[i].tpr_status = TPR_UNKNOWN;
1462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
1464
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001465#if defined(FEAT_EVAL) || defined(PROTO)
1466 void
1467f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1468{
1469# ifdef FEAT_TERMRESPONSE
1470 int i;
1471# endif
1472
1473 if (rettv_dict_alloc(rettv) != OK)
1474 return;
1475# ifdef FEAT_TERMRESPONSE
1476 for (i = 0; i < TPR_COUNT; ++i)
1477 {
1478 char_u value[2];
1479
1480 value[0] = term_props[i].tpr_status;
1481 value[1] = NUL;
1482 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1483 }
1484# endif
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 struct builtin_term *p;
1492
1493 p = builtin_termcaps;
1494 while (p->bt_string != NULL)
1495 {
1496 if (p->bt_entry == (int)KS_NAME)
1497 {
1498#ifdef UNIX
1499 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1500 return p;
1501 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1502 return p;
1503 else
1504#endif
1505#ifdef VMS
1506 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1507 return p;
1508 else
1509#endif
1510 if (STRCMP(term, p->bt_string) == 0)
1511 return p;
1512 }
1513 ++p;
1514 }
1515 return p;
1516}
1517
1518/*
1519 * Parsing of the builtin termcap entries.
1520 * Caller should check if 'name' is a valid builtin term.
1521 * The terminal's name is not set, as this is already done in termcapinit().
1522 */
1523 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001524parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 struct builtin_term *p;
1527 char_u name[2];
1528 int term_8bit;
1529
1530 p = find_builtin_term(term);
1531 term_8bit = term_is_8bit(term);
1532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001533 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (p->bt_string == NULL)
1535 return;
1536
1537 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1538 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (term_strings[p->bt_entry] == NULL
1543 || term_strings[p->bt_entry] == empty_option)
1544 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001545#ifdef FEAT_EVAL
1546 int opt_idx = -1;
1547#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1550 {
1551 char_u *s, *t;
1552
1553 s = vim_strsave((char_u *)p->bt_string);
1554 if (s != NULL)
1555 {
1556 for (t = s; *t; ++t)
1557 if (term_7to8bit(t))
1558 {
1559 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001560 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001563#ifdef FEAT_EVAL
1564 opt_idx =
1565#endif
1566 set_term_option_alloced(
1567 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 }
1570 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1575#endif
1576 }
1577#ifdef FEAT_EVAL
1578 set_term_option_sctx_idx(NULL, opt_idx);
1579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
1582 else
1583 {
1584 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1585 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1586 if (find_termcode(name) == NULL)
1587 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1588 }
1589 }
1590}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
1593 * Set number of colors.
1594 * Store it as a number in t_colors.
1595 * Store it as a string in T_CCO (using nr_colors[]).
1596 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001598set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001600 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 t_colors = nr;
1603 if (t_colors > 1)
1604 sprintf((char *)nr_colors, "%d", t_colors);
1605 else
1606 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001607 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001609
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001610#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001611/*
1612 * Set the color count to "val" and redraw if it changed.
1613 */
1614 static void
1615may_adjust_color_count(int val)
1616{
1617 if (val != t_colors)
1618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001619 // Nr of colors changed, initialize highlighting and
1620 // redraw everything. This causes a redraw, which usually
1621 // clears the message. Try keeping the message if it
1622 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001623 set_keep_msg_from_hist();
1624 set_color_count(val);
1625 init_highlight(TRUE, FALSE);
1626# ifdef DEBUG_TERMRESPONSE
1627 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001628 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001629
Bram Moolenaarb255b902018-04-24 21:40:10 +02001630 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001631 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001632# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001633 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001634# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001635 }
1636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638
1639#ifdef HAVE_TGETENT
1640static char *(key_names[]) =
1641{
Bram Moolenaar87202262020-05-24 17:23:45 +02001642# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001643 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 "#2", "#4", "%i", "*7",
1648 "k1", "k2", "k3", "k4", "k5", "k6",
1649 "k7", "k8", "k9", "k;", "F1", "F2",
1650 "%1", "&8", "kb", "kI", "kD", "kh",
1651 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1652 NULL
1653};
1654#endif
1655
Bram Moolenaar9289df52018-05-10 14:40:57 +02001656#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001657 static void
1658get_term_entries(int *height, int *width)
1659{
1660 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001661 enum SpecialKey dest; // index in term_strings[]
1662 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 } string_names[] =
1664 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1665 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1666 {KS_CL, "cl"}, {KS_CD, "cd"},
1667 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1668 {KS_ME, "me"}, {KS_MR, "mr"},
1669 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1670 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1671 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1672 {KS_STE,"Te"}, {KS_STS,"Ts"},
1673 {KS_CM, "cm"}, {KS_SR, "sr"},
1674 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1675 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001676 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001677 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001678 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1679 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001680 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1681 {KS_VS, "vs"}, {KS_CVS, "VS"},
1682 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1683 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1684 {KS_TS, "ts"}, {KS_FS, "fs"},
1685 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1686 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1687 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001688 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1690 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001691 {KS_CST, "ST"}, {KS_CRT, "RT"},
1692 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693 {(enum SpecialKey)0, NULL}
1694 };
1695 int i;
1696 char_u *p;
1697 static char_u tstrbuf[TBUFSZ];
1698 char_u *tp = tstrbuf;
1699
1700 /*
1701 * get output strings
1702 */
1703 for (i = 0; string_names[i].name != NULL; ++i)
1704 {
1705 if (TERM_STR(string_names[i].dest) == NULL
1706 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001707 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709#ifdef FEAT_EVAL
1710 set_term_option_sctx_idx(string_names[i].name, -1);
1711#endif
1712 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 }
1714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // tgetflag() returns 1 if the flag is present, 0 if not and
1716 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001717 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1718 T_MS = (char_u *)"y";
1719 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1720 T_XS = (char_u *)"y";
1721 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1722 T_XN = (char_u *)"y";
1723 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1724 T_DB = (char_u *)"y";
1725 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1726 T_DA = (char_u *)"y";
1727 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1728 T_UT = (char_u *)"y";
1729
1730 /*
1731 * get key codes
1732 */
1733 for (i = 0; key_names[i] != NULL; ++i)
1734 if (find_termcode((char_u *)key_names[i]) == NULL)
1735 {
1736 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001737 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001738 if (p != NULL
1739 && (*p != Ctrl_H
1740 || key_names[i][0] != 'k'
1741 || key_names[i][1] != 'l'))
1742 add_termcode((char_u *)key_names[i], p, FALSE);
1743 }
1744
1745 if (*height == 0)
1746 *height = tgetnum("li");
1747 if (*width == 0)
1748 *width = tgetnum("co");
1749
1750 /*
1751 * Get number of colors (if not done already).
1752 */
1753 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001754 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001756#ifdef FEAT_EVAL
1757 set_term_option_sctx_idx("Co", -1);
1758#endif
1759 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760
1761# ifndef hpux
1762 BC = (char *)TGETSTR("bc", &tp);
1763 UP = (char *)TGETSTR("up", &tp);
1764 p = TGETSTR("pc", &tp);
1765 if (p)
1766 PC = *p;
1767# endif
1768}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001769#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770
1771 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001772report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773{
1774 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001775 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001776
1777 mch_errmsg("\r\n");
1778 if (error_msg != NULL)
1779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001780 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001781 mch_errmsg("\r\n");
1782 }
1783 mch_errmsg("'");
1784 mch_errmsg((char *)term);
1785 mch_errmsg(_("' not known. Available builtin terminals are:"));
1786 mch_errmsg("\r\n");
1787 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1788 {
1789 if (termp->bt_entry == (int)KS_NAME)
1790 {
1791#ifdef HAVE_TGETENT
1792 mch_errmsg(" builtin_");
1793#else
1794 mch_errmsg(" ");
1795#endif
1796 mch_errmsg(termp->bt_string);
1797 mch_errmsg("\r\n");
1798 }
1799 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001800 // Output extra 'cmdheight' line breaks to avoid that the following error
1801 // message overwrites the last terminal name.
1802 for (i = 1; i < p_ch; ++i)
1803 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001804}
1805
1806 static void
1807report_default_term(char_u *term)
1808{
1809 mch_errmsg(_("defaulting to '"));
1810 mch_errmsg((char *)term);
1811 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001812 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001813 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001814 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001815 out_flush();
1816 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001817 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001818 }
1819}
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * Set terminal options for terminal "term".
1823 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1824 *
1825 * While doing this, until ttest(), some options may be NULL, be careful.
1826 */
1827 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001828set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 struct builtin_term *termp;
1831#ifdef HAVE_TGETENT
1832 int builtin_first = p_tbi;
1833 int try;
1834 int termcap_cleared = FALSE;
1835#endif
1836 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 char_u *bs_p, *del_p;
1839
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001840 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001841 if (silent_mode)
1842 return OK;
1843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001844 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 if (term_is_builtin(term))
1847 {
1848 term += 8;
1849#ifdef HAVE_TGETENT
1850 builtin_first = 1;
1851#endif
1852 }
1853
1854/*
1855 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1856 * If builtin_first is TRUE:
1857 * 0. try builtin termcap
1858 * 1. try external termcap
1859 * 2. if both fail default to a builtin terminal
1860 * If builtin_first is FALSE:
1861 * 1. try external termcap
1862 * 2. try builtin termcap, if both fail default to a builtin terminal
1863 */
1864#ifdef HAVE_TGETENT
1865 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1866 {
1867 /*
1868 * Use external termcap
1869 */
1870 if (try == 1)
1871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 /*
1875 * If the external termcap does not have a matching entry, try the
1876 * builtin ones.
1877 */
1878 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (!termcap_cleared)
1881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 termcap_cleared = TRUE;
1884 }
1885
Bram Moolenaar69e05692018-05-10 14:11:52 +02001886 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001889 else // try == 0 || try == 2
1890#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 /*
1892 * Use builtin termcap
1893 */
1894 {
1895#ifdef HAVE_TGETENT
1896 /*
1897 * If builtin termcap was already used, there is no need to search
1898 * for the builtin termcap again, quit now.
1899 */
1900 if (try == 2 && builtin_first && termcap_cleared)
1901 break;
1902#endif
1903 /*
1904 * search for 'term' in builtin_termcaps[]
1905 */
1906 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001907 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909#ifdef HAVE_TGETENT
1910 /*
1911 * If try == 0, first try the external termcap. If that is not
1912 * found we'll get back here with try == 2.
1913 * If termcap_cleared is set we used the external termcap,
1914 * don't complain about not finding the term in the builtin
1915 * termcap.
1916 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 break;
1921#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001922 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (starting != NO_SCREEN)
1926 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 wait_return(TRUE);
1929 return FAIL;
1930 }
1931 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001932 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001933 set_string_option_direct((char_u *)"term", -1, term,
1934 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 display_errors();
1936 }
1937 out_flush();
1938#ifdef HAVE_TGETENT
1939 if (!termcap_cleared)
1940 {
1941#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001942 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943#ifdef HAVE_TGETENT
1944 termcap_cleared = TRUE;
1945 }
1946#endif
1947 parse_builtin_tcap(term);
1948#ifdef FEAT_GUI
1949 if (term_is_gui(term))
1950 {
1951 out_flush();
1952 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001953 // If starting the GUI failed, don't do any of the other
1954 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (!gui.in_use)
1956 return FAIL;
1957#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001958 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#endif
1960 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001961#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963#ifdef HAVE_TGETENT
1964 }
1965#endif
1966
1967/*
1968 * special: There is no info in the termcap about whether the cursor
1969 * positioning is relative to the start of the screen or to the start of the
1970 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1971 * relative.
1972 */
1973 if (STRCMP(term, "pcterm") == 0)
1974 T_CCS = (char_u *)"yes";
1975 else
1976 T_CCS = empty_option;
1977
1978#ifdef UNIX
1979/*
1980 * Any "stty" settings override the default for t_kb from the termcap.
1981 * This is in os_unix.c, because it depends a lot on the version of unix that
1982 * is being used.
1983 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1984 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001985# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 get_stty();
1989#endif
1990
1991/*
1992 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1993 * didn't work, use the default CTRL-H
1994 * The default for t_kD is DEL, unless t_kb is DEL.
1995 * The vim_strsave'd strings are probably lost forever, well it's only two
1996 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1997 * directly.
1998 */
1999#ifdef FEAT_GUI
2000 if (!gui.in_use)
2001#endif
2002 {
2003 bs_p = find_termcode((char_u *)"kb");
2004 del_p = find_termcode((char_u *)"kD");
2005 if (bs_p == NULL || *bs_p == NUL)
2006 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2007 if ((del_p == NULL || *del_p == NUL) &&
2008 (bs_p == NULL || *bs_p != DEL))
2009 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2010 }
2011
2012#if defined(UNIX) || defined(VMS)
2013 term_is_xterm = vim_is_xterm(term);
2014#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002015#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002016 // Reset terminal properties that are set based on the termresponse, which
2017 // will be sent out soon.
2018 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002021#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /*
2023 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2024 * The termcode for the mouse is added as a side effect in option.c.
2025 */
2026 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002027 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002029# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002030 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 else
2035 p = (char_u *)"xterm";
2036 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2042 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002043 reset_option_was_set((char_u *)"ttym");
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002046# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002052#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
Bram Moolenaarbf789742021-01-16 11:21:40 +01002056#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002057 // focus reporting is supported by xterm compatible terminals and tmux.
2058 if (use_xterm_like_mouse(term))
2059 {
2060 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002061
2062 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002063 name[0] = KS_EXTRA;
2064 name[1] = KE_FOCUSGAINED;
2065 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002066 add_termcode(name, (char_u *)"\033[I", FALSE);
2067
2068 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002069 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002070 add_termcode(name, (char_u *)"\033[O", FALSE);
2071
2072 focus_mode = TRUE;
2073 focus_state = TRUE;
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002074 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002075 }
2076#endif
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002079 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (STRCMP(term, DEFAULT_TERM) != 0)
2081 term_console = FALSE;
2082 else
2083 {
2084 term_console = TRUE;
2085# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002086 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087# endif
2088 }
2089#endif
2090
2091#if defined(UNIX) || defined(VMS)
2092 /*
2093 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2094 */
2095 if (vim_is_fastterm(term))
2096 p_tf = TRUE;
2097#endif
2098#ifdef USE_TERM_CONSOLE
2099 /*
2100 * 'ttyfast' is default on consoles
2101 */
2102 if (term_console)
2103 p_tf = TRUE;
2104#endif
2105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002106 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 full_screen = TRUE; // we can use termcap codes from now on
2109 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002111 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002112 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#endif
2114
2115 /*
2116 * Initialize the terminal with the appropriate termcap codes.
2117 * Set the mouse and window title if possible.
2118 * Don't do this when starting, need to parse the .vimrc first, because it
2119 * may redefine t_TI etc.
2120 */
2121 if (starting != NO_SCREEN)
2122 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002123 starttermcap(); // may change terminal mode
2124 setmouse(); // may start using the mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#ifdef FEAT_TITLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002126 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127#endif
2128 }
2129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002130 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (width <= 0 || height <= 0)
2132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002133 // termcap failed to report size
2134 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002136#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#endif
2141 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002142 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (starting != NO_SCREEN)
2144 {
2145 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002146 scroll_region_reset(); // In case Rows changed
2147 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002150 buf_T *buf;
2151 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 /*
2154 * Execute the TermChanged autocommands for each buffer that is
2155 * loaded.
2156 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002157 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002160 {
2161 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2163 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002164 // restore curwin/curbuf and a few other things
2165 aucmd_restbuf(&aco);
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170
2171#ifdef FEAT_TERMRESPONSE
2172 may_req_termresponse();
2173#endif
2174
2175 return OK;
2176}
2177
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef HAVE_TGETENT
2179/*
2180 * Call tgetent()
2181 * Return error message if it fails, NULL if it's OK.
2182 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002184tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 int i;
2187
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002188 // Note: Valgrind may report a leak here, because the library keeps one
2189 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002191 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002193 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194# endif
2195 )
2196 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002197 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2198 // tgetent() with the always existing "dumb" entry to avoid a crash or
2199 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 (void)TGETENT(tbuf, "dumb");
2201
2202 if (i < 0)
2203# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (i == 0)
2206# endif
2207#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#endif
2212 }
2213 return NULL;
2214}
2215
2216/*
2217 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2218 * Fix that here.
2219 */
2220 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002221vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 char *p;
2224
2225 p = tgetstr(s, (char **)pp);
2226 if (p == (char *)-1)
2227 p = NULL;
2228 return (char_u *)p;
2229}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002230#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002232#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233/*
2234 * Get Columns and Rows from the termcap. Used after a window signal if the
2235 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2236 * and "li" entries never change. But on some systems this works.
2237 * Errors while getting the entries are ignored.
2238 */
2239 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002240getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002241 long *cp, // pointer to columns
2242 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 char_u tbuf[TBUFSZ];
2245
2246 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2247 {
2248 if (*cp == 0)
2249 *cp = tgetnum("co");
2250 if (*rp == 0)
2251 *rp = tgetnum("li");
2252 }
2253}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256/*
2257 * Get a string entry from the termcap and add it to the list of termcodes.
2258 * Used for <t_xx> special keys.
2259 * Give an error message for failure when not sourcing.
2260 * If force given, replace an existing entry.
2261 * Return FAIL if the entry was not found, OK if the entry was added.
2262 */
2263 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002264add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 char_u *term;
2267 int key;
2268 struct builtin_term *termp;
2269#ifdef HAVE_TGETENT
2270 char_u *string;
2271 int i;
2272 int builtin_first;
2273 char_u tbuf[TBUFSZ];
2274 char_u tstrbuf[TBUFSZ];
2275 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002276 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
2279/*
2280 * If the GUI is running or will start in a moment, we only support the keys
2281 * that the GUI can produce.
2282 */
2283#ifdef FEAT_GUI
2284 if (gui.in_use || gui.starting)
2285 return gui_mch_haskey(name);
2286#endif
2287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return OK;
2290
2291 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002292 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 return FAIL;
2294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 term += 8;
2298#ifdef HAVE_TGETENT
2299 builtin_first = TRUE;
2300#endif
2301 }
2302#ifdef HAVE_TGETENT
2303 else
2304 builtin_first = p_tbi;
2305#endif
2306
2307#ifdef HAVE_TGETENT
2308/*
2309 * We can get the entry from the builtin termcap and from the external one.
2310 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2311 * builtin termcap first.
2312 * If 'ttybuiltin' is off, try external termcap first.
2313 */
2314 for (i = 0; i < 2; ++i)
2315 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002316 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
2318 /*
2319 * Search in builtin termcap
2320 */
2321 {
2322 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002326 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 while (termp->bt_entry != (int)KS_NAME)
2328 {
2329 if ((int)termp->bt_entry == key)
2330 {
2331 add_termcode(name, (char_u *)termp->bt_string,
2332 term_is_8bit(term));
2333 return OK;
2334 }
2335 ++termp;
2336 }
2337 }
2338 }
2339#ifdef HAVE_TGETENT
2340 else
2341 /*
2342 * Search in external termcap
2343 */
2344 {
2345 error_msg = tgetent_error(tbuf, term);
2346 if (error_msg == NULL)
2347 {
2348 string = TGETSTR((char *)name, &tp);
2349 if (string != NULL && *string != NUL)
2350 {
2351 add_termcode(name, string, FALSE);
2352 return OK;
2353 }
2354 }
2355 }
2356 }
2357#endif
2358
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002359 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361#ifdef HAVE_TGETENT
2362 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002363 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 else
2365#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002366 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 return FAIL;
2369}
2370
2371 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002372term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2375}
2376
2377/*
2378 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2379 * Assume that the terminal is using 8-bit controls when the name contains
2380 * "8bit", like in "xterm-8bit".
2381 */
2382 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002383term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2386}
2387
2388/*
2389 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002390 * <Esc>[ -> CSI <M_C_[>
2391 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * <Esc>O -> <M-C-O>
2393 */
2394 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002395term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 if (*p == ESC)
2398 {
2399 if (p[1] == '[')
2400 return CSI;
2401 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002402 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (p[1] == 'O')
2404 return 0x8f;
2405 }
2406 return 0;
2407}
2408
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002409#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002411term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2414}
2415#endif
2416
2417#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2418
2419 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002420tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 static char_u buf[16];
2423 char_u *p;
2424
2425 p = buf + 15;
2426 *p = '\0';
2427 do
2428 {
2429 --p;
2430 *p = (char_u) (i % 10 + '0');
2431 i /= 10;
2432 }
2433 while (i > 0 && p > buf);
2434 return p;
2435}
2436#endif
2437
2438#ifndef HAVE_TGETENT
2439
2440/*
2441 * minimal tgoto() implementation.
2442 * no padding and we only parse for %i %d and %+char
2443 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002444 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002445tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 static char buf[30];
2448 char *p, *s, *e;
2449
2450 if (!cm)
2451 return "OOPS";
2452 e = buf + 29;
2453 for (s = buf; s < e && *cm; cm++)
2454 {
2455 if (*cm != '%')
2456 {
2457 *s++ = *cm;
2458 continue;
2459 }
2460 switch (*++cm)
2461 {
2462 case 'd':
2463 p = (char *)tltoa((unsigned long)y);
2464 y = x;
2465 while (*p)
2466 *s++ = *p++;
2467 break;
2468 case 'i':
2469 x++;
2470 y++;
2471 break;
2472 case '+':
2473 *s++ = (char)(*++cm + y);
2474 y = x;
2475 break;
2476 case '%':
2477 *s++ = *cm;
2478 break;
2479 default:
2480 return "OOPS";
2481 }
2482 }
2483 *s = '\0';
2484 return buf;
2485}
2486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002487#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489/*
2490 * Set the terminal name and initialize the terminal options.
2491 * If "name" is NULL or empty, get the terminal name from the environment.
2492 * If that fails, use the default terminal name.
2493 */
2494 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002495termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 char_u *term;
2498
2499 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002500 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 term = name;
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#ifndef MSWIN
2504 if (term == NULL)
2505 term = mch_getenv((char_u *)"TERM");
2506#endif
2507 if (term == NULL || *term == NUL)
2508 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002509 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 set_string_default("term", term);
2513 set_string_default("ttytype", term);
2514
2515 /*
2516 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2517 */
2518 set_termname(T_NAME != NULL ? T_NAME : term);
2519}
2520
2521/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002522 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002524#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002525
2526// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002528
2529static int out_pos = 0; // number of chars in out_buf
2530
2531// Since the maximum number of SGR parameters shown as a normal value range is
2532// 16, the escape sequence length can be 4 * 16 + lead + tail.
2533#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535/*
2536 * out_flush(): flush the output buffer
2537 */
2538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int len;
2542
2543 if (out_pos != 0)
2544 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002545 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 len = out_pos;
2547 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002548 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002549#ifdef FEAT_JOB_CHANNEL
2550 if (ch_log_output)
2551 {
2552 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002553 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002554# ifdef FEAT_GUI
2555 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2556# endif
2557 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002558 out_buf);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002559 ch_log_output = FALSE;
2560 }
2561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563}
2564
Bram Moolenaara338adc2018-01-31 20:51:47 +01002565/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002566 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2567 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002568 */
2569 void
2570out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002571 int force UNUSED, // when TRUE, update cursor even when not moved
2572 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002573{
2574 mch_disable_flush();
2575 out_flush();
2576 mch_enable_flush();
2577#ifdef FEAT_GUI
2578 if (gui.in_use)
2579 {
2580 gui_update_cursor(force, clear_selection);
2581 gui_may_flush();
2582 }
2583#endif
2584}
2585
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587/*
2588 * Sometimes a byte out of a multi-byte character is written with out_char().
2589 * To avoid flushing half of the character, call this function first.
2590 */
2591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2595 out_flush();
2596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598#ifdef FEAT_GUI
2599/*
2600 * out_trash(): Throw away the contents of the output buffer
2601 */
2602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002603out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 out_pos = 0;
2606}
2607#endif
2608
2609/*
2610 * out_char(c): put a byte into the output buffer.
2611 * Flush it if it becomes full.
2612 * This should not be used for outputting text on the screen (use functions
2613 * like msg_puts() and screen_putchar() for that).
2614 */
2615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002616out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaard0573012017-10-28 21:11:06 +02002618#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 out_char('\r');
2621#endif
2622
2623 out_buf[out_pos++] = c;
2624
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002625 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (out_pos >= OUT_SIZE || p_wd)
2627 out_flush();
2628}
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002631 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002633 static int
2634out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002636 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 if (out_pos >= OUT_SIZE)
2639 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002640 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641}
2642
2643/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002644 * A never-padding out_str().
2645 * Use this whenever you don't want to run the string through tputs().
2646 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * is likely to strip off leading digits, that it mistakes for padding
2648 * information, and "%i", "%d", etc.
2649 * This should only be used for writing terminal codes, not for outputting
2650 * normal text (use functions like msg_puts() and screen_putchar() for that).
2651 */
2652 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002653out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002655 // avoid terminal strings being split up
2656 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 while (*s)
2660 out_char_nf(*s++);
2661
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002662 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (p_wd)
2664 out_flush();
2665}
2666
2667/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002668 * A conditional-flushing out_str, mainly for visualbell.
2669 * Handles a delay internally, because termlib may not respect the delay or do
2670 * it at the wrong time.
2671 * Note: Only for terminal strings.
2672 */
2673 void
2674out_str_cf(char_u *s)
2675{
2676 if (s != NULL && *s)
2677 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002678#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002679 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002680#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002681
2682#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002683 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002684 if (gui.in_use)
2685 {
2686 out_str_nf(s);
2687 return;
2688 }
2689#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002690 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002691 out_flush();
2692#ifdef HAVE_TGETENT
2693 for (p = s; *s; ++s)
2694 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002695 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002696 if (*s == '$' && *(s + 1) == '<')
2697 {
2698 char_u save_c = *s;
2699 int duration = atoi((char *)s + 2);
2700
2701 *s = NUL;
2702 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2703 *s = save_c;
2704 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002705# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002706 // Only sleep here if we can limit this happening in
2707 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002708 p = vim_strchr(s, '>');
2709 if (p == NULL || duration <= 0)
2710 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002711 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002712 p = s;
2713 }
2714 else
2715 {
2716 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002717 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002718 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002719# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002720 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002721 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002722# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002723 break;
2724 }
2725 }
2726 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2727#else
2728 while (*s)
2729 out_char_nf(*s++);
2730#endif
2731
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002732 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002733 if (p_wd)
2734 out_flush();
2735 }
2736}
2737
2738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002740 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 * This should only be used for writing terminal codes, not for outputting
2742 * normal text (use functions like msg_puts() and screen_putchar() for that).
2743 */
2744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002745out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 if (s != NULL && *s)
2748 {
2749#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002750 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (gui.in_use)
2752 {
2753 out_str_nf(s);
2754 return;
2755 }
2756#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002757 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002758 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 out_flush();
2760#ifdef HAVE_TGETENT
2761 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2762#else
2763 while (*s)
2764 out_char_nf(*s++);
2765#endif
2766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002767 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (p_wd)
2769 out_flush();
2770 }
2771}
2772
2773/*
2774 * cursor positioning using termcap parser. (jw)
2775 */
2776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002777term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 OUT_STR(tgoto((char *)T_CM, col, row));
2780}
2781
2782 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002783term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 OUT_STR(tgoto((char *)T_CRI, 0, i));
2786}
2787
2788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002789term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2792}
2793
2794 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002795term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
2797 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2798}
2799
2800#if defined(HAVE_TGETENT) || defined(PROTO)
2801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002802term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002804 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (x < 0)
2806 x = 0;
2807 if (y < 0)
2808 y = 0;
2809 OUT_STR(tgoto((char *)T_CWP, y, x));
2810}
2811
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2813/*
2814 * Return TRUE if we can request the terminal for a response.
2815 */
2816 static int
2817can_get_termresponse()
2818{
2819 return cur_tmode == TMODE_RAW
2820 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002821# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002822 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002823# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002824 && p_ek;
2825}
2826
Bram Moolenaarafd78262019-05-10 23:10:31 +02002827/*
2828 * Set "status" to STATUS_SENT.
2829 */
2830 static void
2831termrequest_sent(termrequest_T *status)
2832{
2833 status->tr_progress = STATUS_SENT;
2834 status->tr_start = time(NULL);
2835}
2836
2837/*
2838 * Return TRUE if any of the requests are in STATUS_SENT.
2839 */
2840 static int
2841termrequest_any_pending()
2842{
2843 int i;
2844 time_t now = time(NULL);
2845
2846 for (i = 0; all_termrequests[i] != NULL; ++i)
2847 {
2848 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2849 {
2850 if (all_termrequests[i]->tr_start > 0 && now > 0
2851 && all_termrequests[i]->tr_start + 2 < now)
2852 // Sent the request more than 2 seconds ago and didn't get a
2853 // response, assume it failed.
2854 all_termrequests[i]->tr_progress = STATUS_FAIL;
2855 else
2856 return TRUE;
2857 }
2858 }
2859 return FALSE;
2860}
2861
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002862static int winpos_x = -1;
2863static int winpos_y = -1;
2864static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002865
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002866# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002867/*
2868 * Try getting the Vim window position from the terminal.
2869 * Returns OK or FAIL.
2870 */
2871 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002872term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002873{
2874 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002875 int prev_winpos_x = winpos_x;
2876 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002877
2878 if (*T_CGP == NUL || !can_get_termresponse())
2879 return FAIL;
2880 winpos_x = -1;
2881 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002882 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002883 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002884 OUT_STR(T_CGP);
2885 out_flush();
2886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002887 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002888 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002889 {
2890 (void)vpeekc_nomap();
2891 if (winpos_x >= 0 && winpos_y >= 0)
2892 {
2893 *x = winpos_x;
2894 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002895 return OK;
2896 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002897 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002898 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002899 // Do not reset "did_request_winpos", if we timed out the response might
2900 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002901
2902 winpos_x = prev_winpos_x;
2903 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002904 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002905 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002906 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002907 *x = winpos_x;
2908 *y = winpos_y;
2909 return OK;
2910 }
2911
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002912 return FALSE;
2913}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002914# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002915# endif
2916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002918term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002920 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922#endif
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002925term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002928 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002929 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002931 // Special handling of 16 colors, because termcap can't handle it
2932 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2933 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002935 && ((s[0] == ESC && s[1] == '[')
2936#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2937 || (s[0] == ESC && s[1] == '|')
2938#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002939 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 && s[i] != NUL
2941 && (STRCMP(s + i + 1, "%p1%dm") == 0
2942 || STRCMP(s + i + 1, "%dm") == 0)
2943 && (s[i] == '3' || s[i] == '4'))
2944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002946 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002948 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002950 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002951#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002952 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002953#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002954 IF_EB("\033[", ESC_STR "[")) : "\233";
2955 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2956 : (n >= 16 ? "48;5;" : "10");
2957
2958 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2960 }
2961 else
2962 OUT_STR(tgoto((char *)s, 0, n));
2963}
2964
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002965 void
2966term_fg_color(int n)
2967{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002968 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002969 if (*T_CAF)
2970 term_color(T_CAF, n);
2971 else if (*T_CSF)
2972 term_color(T_CSF, n);
2973}
2974
2975 void
2976term_bg_color(int n)
2977{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002978 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002979 if (*T_CAB)
2980 term_color(T_CAB, n);
2981 else if (*T_CSB)
2982 term_color(T_CSB, n);
2983}
2984
Bram Moolenaare023e882020-05-31 16:42:30 +02002985 void
2986term_ul_color(int n)
2987{
2988 if (*T_CAU)
2989 term_color(T_CAU, n);
2990}
2991
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002992/*
2993 * Return "dark" or "light" depending on the kind of terminal.
2994 * This is just guessing! Recognized are:
2995 * "linux" Linux console
2996 * "screen.linux" Linux console with screen
2997 * "cygwin.*" Cygwin shell
2998 * "putty.*" Putty program
2999 * We also check the COLORFGBG environment variable, which is set by
3000 * rxvt and derivatives. This variable contains either two or three
3001 * values separated by semicolons; we want the last value in either
3002 * case. If this value is 0-6 or 8, our background is dark.
3003 */
3004 char_u *
3005term_bg_default(void)
3006{
3007#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003008 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003009 return (char_u *)"dark";
3010#else
3011 char_u *p;
3012
3013 if (STRCMP(T_NAME, "linux") == 0
3014 || STRCMP(T_NAME, "screen.linux") == 0
3015 || STRNCMP(T_NAME, "cygwin", 6) == 0
3016 || STRNCMP(T_NAME, "putty", 5) == 0
3017 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3018 && (p = vim_strrchr(p, ';')) != NULL
3019 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3020 && p[2] == NUL))
3021 return (char_u *)"dark";
3022 return (char_u *)"light";
3023#endif
3024}
3025
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003026#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003027
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003028#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3029#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3030#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003031
3032 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003033term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003034{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003035#define MAX_COLOR_STR_LEN 100
3036 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003037
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003038 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003039 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003040#ifdef FEAT_VTP
3041 if (use_wt())
3042 {
3043 out_flush();
3044 buf[1] = '[';
3045 vtp_printf(buf);
3046 }
3047 else
3048#endif
3049 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003050}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003051
3052 void
3053term_fg_rgb_color(guicolor_T rgb)
3054{
3055 term_rgb_color(T_8F, rgb);
3056}
3057
3058 void
3059term_bg_rgb_color(guicolor_T rgb)
3060{
3061 term_rgb_color(T_8B, rgb);
3062}
Bram Moolenaare023e882020-05-31 16:42:30 +02003063
3064 void
3065term_ul_rgb_color(guicolor_T rgb)
3066{
3067 term_rgb_color(T_8U, rgb);
3068}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003069#endif
3070
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003071#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3072 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073/*
3074 * Generic function to set window title, using t_ts and t_fs.
3075 */
3076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003079#ifdef FEAT_JOB_CHANNEL
3080 ch_log_output = TRUE;
3081#endif
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003082 // t_ts takes one argument: column in status line
3083 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003085 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 out_flush();
3087}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003088
3089/*
3090 * Tell the terminal to push (save) the title and/or icon, so that it can be
3091 * popped (restored) later.
3092 */
3093 void
3094term_push_title(int which)
3095{
Bram Moolenaar27821262019-05-08 16:41:09 +02003096 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003097 {
3098 OUT_STR(T_CST);
3099 out_flush();
3100 }
3101
Bram Moolenaar27821262019-05-08 16:41:09 +02003102 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003103 {
3104 OUT_STR(T_SSI);
3105 out_flush();
3106 }
3107}
3108
3109/*
3110 * Tell the terminal to pop the title and/or icon.
3111 */
3112 void
3113term_pop_title(int which)
3114{
Bram Moolenaar27821262019-05-08 16:41:09 +02003115 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003116 {
3117 OUT_STR(T_CRT);
3118 out_flush();
3119 }
3120
Bram Moolenaar27821262019-05-08 16:41:09 +02003121 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003122 {
3123 OUT_STR(T_SRI);
3124 out_flush();
3125 }
3126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128
3129/*
3130 * Make sure we have a valid set or terminal options.
3131 * Replace all entries that are NULL by empty_option
3132 */
3133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003134ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003136 char_u *env_colors;
3137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003138 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /*
3141 * MUST have "cm": cursor motion.
3142 */
3143 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003144 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 /*
3147 * if "cs" defined, use a scroll region, it's faster.
3148 */
3149 if (*T_CS != NUL)
3150 scroll_region = TRUE;
3151 else
3152 scroll_region = FALSE;
3153
3154 if (pairs)
3155 {
3156 /*
3157 * optional pairs
3158 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003159 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (*T_ME == NUL)
3161 T_ME = T_MR = T_MD = T_MB = empty_option;
3162 if (*T_SO == NUL || *T_SE == NUL)
3163 T_SO = T_SE = empty_option;
3164 if (*T_US == NUL || *T_UE == NUL)
3165 T_US = T_UE = empty_option;
3166 if (*T_CZH == NUL || *T_CZR == NUL)
3167 T_CZH = T_CZR = empty_option;
3168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003169 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (*T_VE == NUL)
3171 T_VI = empty_option;
3172
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003173 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (*T_ME == NUL)
3175 {
3176 T_ME = T_SE;
3177 T_MR = T_SO;
3178 T_MD = T_SO;
3179 }
3180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003181 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (*T_SO == NUL)
3183 {
3184 T_SE = T_ME;
3185 if (*T_MR == NUL)
3186 T_SO = T_MD;
3187 else
3188 T_SO = T_MR;
3189 }
3190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003191 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (*T_CZH == NUL)
3193 {
3194 T_CZR = T_ME;
3195 if (*T_MR == NUL)
3196 T_CZH = T_MD;
3197 else
3198 T_CZH = T_MR;
3199 }
3200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003201 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (*T_CSB == NUL || *T_CSF == NUL)
3203 {
3204 T_CSB = empty_option;
3205 T_CSF = empty_option;
3206 }
3207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003208 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (*T_CAB == NUL || *T_CAF == NUL)
3210 {
3211 T_CAB = empty_option;
3212 T_CAF = empty_option;
3213 }
3214
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003215 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003217 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003219 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 p_wiv = (*T_XS != NUL);
3221 }
3222 need_gather = TRUE;
3223
Bram Moolenaar759d8152020-04-26 16:52:49 +02003224 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3225 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003227#ifdef FEAT_GUI
3228 if (!gui.in_use)
3229#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003230 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003231 env_colors = mch_getenv((char_u *)"COLORS");
3232 if (env_colors != NULL && isdigit(*env_colors))
3233 {
3234 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003235
Bram Moolenaar759d8152020-04-26 16:52:49 +02003236 if (colors != t_colors)
3237 set_color_count(colors);
3238 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240}
3241
3242#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3243 || defined(PROTO)
3244/*
3245 * Represent the given long_u as individual bytes, with the most significant
3246 * byte first, and store them in dst.
3247 */
3248 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003249add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int i;
3252 int shift;
3253
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003254 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 shift = 8 * (sizeof(long_u) - i);
3257 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3258 }
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261/*
3262 * Interpret the next string of bytes in buf as a long integer, with the most
3263 * significant byte first. Note that it is assumed that buf has been through
3264 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3265 * Puts result in val, and returns the number of bytes read from buf
3266 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3267 * were present.
3268 */
3269 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003270get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 int len;
3273 char_u bytes[sizeof(long_u)];
3274 int i;
3275 int shift;
3276
3277 *val = 0;
3278 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3279 if (len != -1)
3280 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003281 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
3283 shift = 8 * (sizeof(long_u) - 1 - i);
3284 *val += (long_u)bytes[i] << shift;
3285 }
3286 }
3287 return len;
3288}
3289#endif
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291/*
3292 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3293 * that buf has been through inchar(). Returns the actual number of bytes used
3294 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3295 * available.
3296 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003297 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003298get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 int len = 0;
3301 int i;
3302 char_u c;
3303
3304 for (i = 0; i < num_bytes; i++)
3305 {
3306 if ((c = buf[len++]) == NUL)
3307 return -1;
3308 if (c == K_SPECIAL)
3309 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003310 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return -1;
3312 if (buf[len++] == (int)KS_ZERO)
3313 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003314 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3315 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003316 if (buf[len++] == (int)KE_CSI)
3317 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003319 else if (c == CSI && buf[len] == KS_EXTRA
3320 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003321 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3322 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 bytes[i] = c;
3325 }
3326 return len;
3327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003330 * Check if the new shell size is valid, correct it if it's too small or way
3331 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 */
3333 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003334check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003336 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003338 limit_screen_size();
3339}
3340
3341/*
3342 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3343 */
3344 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003345limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003346{
3347 if (Columns < MIN_COLUMNS)
3348 Columns = MIN_COLUMNS;
3349 else if (Columns > 10000)
3350 Columns = 10000;
3351 if (Rows > 1000)
3352 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
Bram Moolenaar86b68352004-12-27 21:59:20 +00003355/*
3356 * Invoked just before the screen structures are going to be (re)allocated.
3357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003359win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 static int old_Rows = 0;
3362 static int old_Columns = 0;
3363
3364 if (old_Rows != Rows || old_Columns != Columns)
3365 ui_new_shellsize();
3366 if (old_Rows != Rows)
3367 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003368 // If 'window' uses the whole screen, keep it using that.
3369 // Don't change it when set with "-w size" on the command line.
3370 if (p_window == old_Rows - 1 || (old_Rows == 0 && p_window == 0))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003371 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003373 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 if (old_Columns != Columns)
3376 {
3377 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003378 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380}
3381
3382/*
3383 * Call this function when the Vim shell has been resized in any way.
3384 * Will obtain the current size and redraw (also when size didn't change).
3385 */
3386 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003387shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 set_shellsize(0, 0, FALSE);
3390}
3391
3392/*
3393 * Check if the shell size changed. Handle a resize.
3394 * When the size didn't change, nothing happens.
3395 */
3396 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003397shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 int old_Rows = Rows;
3400 int old_Columns = Columns;
3401
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003402 if (!exiting
3403#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003404 // Do not get the size when executing a shell command during
3405 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003406 && !gui.starting
3407#endif
3408 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003409 {
3410 (void)ui_get_shellsize();
3411 check_shellsize();
3412 if (old_Rows != Rows || old_Columns != Columns)
3413 shell_resized();
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415}
3416
3417/*
3418 * Set size of the Vim shell.
3419 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3420 * window size (this is used for the :win command).
3421 * If 'mustset' is FALSE, we may try to get the real window size and if
3422 * it fails use 'width' and 'height'.
3423 */
3424 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003425set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 static int busy = FALSE;
3428
3429 /*
3430 * Avoid recursiveness, can happen when setting the window size causes
3431 * another window-changed signal.
3432 */
3433 if (busy)
3434 return;
3435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003436 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return;
3438
Bram Moolenaara971b822011-09-14 14:43:25 +02003439 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003441 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 State = SETWSIZE;
3443 return;
3444 }
3445
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003446 if (updating_screen)
3447 // resizing while in update_screen() may cause a crash
3448 return;
3449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003451 // buffer (or window) has already been closed and removing a scrollbar
3452 // causes a resize event. Don't resize then, it will happen after entering
3453 // another buffer.
3454 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003455 return;
3456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 ++busy;
3458
3459#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003460 out_flush(); // must do this before mch_get_shellsize() for
3461 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#endif
3463
3464 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3465 {
3466 Rows = height;
3467 Columns = width;
3468 check_shellsize();
3469 ui_set_shellsize(mustset);
3470 }
3471 else
3472 check_shellsize();
3473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003474 // The window layout used to be adjusted here, but it now happens in
3475 // screenalloc() (also invoked from screenclear()). That is because the
3476 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3479 screenclear();
3480 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003481 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 if (starting != NO_SCREEN)
3484 {
3485#ifdef FEAT_TITLE
3486 maketitle();
3487#endif
3488 changed_line_abv_curs();
3489 invalidate_botline();
3490
3491 /*
3492 * We only redraw when it's needed:
3493 * - While at the more prompt or executing an external command, don't
3494 * redraw, but position the cursor.
3495 * - While editing the command line, only redraw that.
3496 * - in Ex mode, don't redraw anything.
3497 * - Otherwise, redraw right now, and position the cursor.
3498 * Always need to call update_screen() or screenalloc(), to make
3499 * sure Rows/Columns and the size of ScreenLines[] is correct!
3500 */
3501 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3502 || exmode_active)
3503 {
3504 screenalloc(FALSE);
3505 repeat_message();
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 else
3508 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003509 if (curwin->w_p_scb)
3510 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003511 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003512 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003513 update_screen(NOT_VALID);
3514 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003515 }
3516 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003517 {
3518 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003519 if (pum_visible())
3520 {
3521 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003522 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003523 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003524 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003525 if (redrawing())
3526 setcursor();
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003529 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 out_flush();
3532 --busy;
3533}
3534
3535/*
3536 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3537 * commands and Ex mode).
3538 */
3539 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003540settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003543 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (gui.in_use)
3545 return;
3546#endif
3547
3548 if (full_screen)
3549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003551 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3552 * set the terminal to raw mode, even though we think it already is,
3553 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * When we think the terminal is normal, don't try to set it to
3555 * normal again, because that causes problems (logout!) on some
3556 * machines.
3557 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003558 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003561# ifdef FEAT_GUI
3562 if (!gui.in_use && !gui.starting)
3563# endif
3564 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003565 // May need to check for T_CRV response and termcodes, it
3566 // doesn't work in Cooked mode, an external program may get
3567 // them.
3568 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003569 (void)vpeekc_nomap();
3570 check_for_codes_from_term();
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003574 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003575
3576 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3577 // Avoid doing this too often, on some terminals the codes are not
3578 // handled properly.
3579 if (termcap_active && tmode != TMODE_SLEEP
3580 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003581 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003582#ifdef FEAT_JOB_CHANNEL
3583 ch_log_output = TRUE;
3584#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003585 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003586 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003587 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003588 out_str(T_CTE); // possibly disables modifyOtherKeys
3589 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003590 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003591 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003592 out_str(T_BE); // enable bracketed paste mode (should
3593 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003594 out_str(T_CTI); // possibly enables modifyOtherKeys
3595 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003598 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003601 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 out_flush();
3603 }
3604#ifdef FEAT_TERMRESPONSE
3605 may_req_termresponse();
3606#endif
3607 }
3608}
3609
3610 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003611starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
3613 if (full_screen && !termcap_active)
3614 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003615#ifdef FEAT_JOB_CHANNEL
3616 ch_log_output = TRUE;
3617#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003618 out_str(T_TI); // start termcap mode
3619 out_str(T_CTI); // start "raw" mode
3620 out_str(T_KS); // start "keypad transmit" mode
3621 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003622
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003623#if defined(UNIX) || defined(VMS)
3624 // Enable xterm's focus reporting mode when 'esckeys' is set.
3625 if (focus_mode && p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003626 out_str(T_FE);
3627#endif
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 out_flush();
3630 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003631 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003633# ifdef FEAT_GUI
3634 if (!gui.in_use && !gui.starting)
3635# endif
3636 {
3637 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003638 // Immediately check for a response. If t_Co changes, we don't
3639 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003640 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003641 check_for_codes_from_term();
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643#endif
3644 }
3645}
3646
3647 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003648stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
3650 screen_stop_highlight();
3651 reset_cterm_colors();
3652 if (termcap_active)
3653 {
3654#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003655# ifdef FEAT_GUI
3656 if (!gui.in_use && !gui.starting)
3657# endif
3658 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003659 // May need to discard T_CRV, T_U7 or T_RBG response.
3660 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003661 {
3662# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003663 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003664 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003665# endif
3666# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003667 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003668 if (exiting)
3669 tcflush(fileno(stdin), TCIFLUSH);
3670# endif
3671 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003672 // Check for termcodes first, otherwise an external program may
3673 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003674 check_for_codes_from_term();
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676#endif
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003677#ifdef FEAT_JOB_CHANNEL
3678 ch_log_output = TRUE;
3679#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003680
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003681#if defined(UNIX) || defined(VMS)
3682 // Disable xterm's focus reporting mode if 'esckeys' is set.
3683 if (focus_mode && p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003684 out_str(T_FD);
3685#endif
3686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003687 out_str(T_BD); // disable bracketed paste mode
3688 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 out_flush();
3690 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003691 cursor_on(); // just in case it is still off
3692 out_str(T_CTE); // stop "raw" mode
3693 out_str(T_TE); // stop termcap mode
3694 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 out_flush();
3696 }
3697}
3698
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003699#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * Request version string (for xterm) when needed.
3702 * Only do this after switching to raw mode, otherwise the result will be
3703 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003704 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003705 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 * Only do this after termcap mode has been started, otherwise the codes for
3707 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003708 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3709 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003710 * On Unix only do it when both output and input are a tty (avoid writing
3711 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * The result is caught in check_termcode().
3713 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003715may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003717 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003718 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003719 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 && *T_CRV != NUL)
3721 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003722#ifdef FEAT_JOB_CHANNEL
3723 ch_log_output = TRUE;
3724#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003725 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003727 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 // check for the characters now, otherwise they might be eaten by
3729 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 out_flush();
3731 (void)vpeekc_nomap();
3732 }
3733}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003734
Bram Moolenaar9584b312013-03-13 19:29:28 +01003735/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003736 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3737 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003738 * Note that this goes out before T_CRV, so that the result can be used when
3739 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003740 */
3741 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003742check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003743{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003744 int did_send = FALSE;
3745
3746 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3747 return;
3748
Bram Moolenaarafd78262019-05-10 23:10:31 +02003749 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003750 && !option_was_set((char_u *)"ambiwidth"))
3751 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003752 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003753
Bram Moolenaara45551a2020-06-09 15:57:37 +02003754 // Ambiguous width check.
3755 // Check how the terminal treats ambiguous character width (UAX #11).
3756 // First, we move the cursor to (1, 0) and print a test ambiguous
3757 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3758 // the current cursor position. If the terminal treats \u25bd as
3759 // single width, the position is (1, 1), or if it is treated as double
3760 // width, that will be (1, 2). This function has the side effect that
3761 // changes cursor position, so it must be called immediately after
3762 // entering termcap mode.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003763#ifdef FEAT_JOB_CHANNEL
3764 ch_log_output = TRUE;
3765#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003766 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003767 // Do this in the second row. In the first row the returned sequence
3768 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003769 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003770 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003771 out_str(buf);
3772 out_str(T_U7);
3773 termrequest_sent(&u7_status);
3774 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003775 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003777 // This overwrites a few characters on the screen, a redraw is needed
3778 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003779 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003780 term_windgoto(1, 0);
3781 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003782 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003783 }
3784
3785 if (xcc_status.tr_progress == STATUS_GET)
3786 {
3787 // 2. Check compatibility with xterm.
3788 // We move the cursor to (2, 0), print a test sequence and then query
3789 // the current cursor position. If the terminal properly handles
3790 // unknown DCS string and CSI sequence with intermediate byte, the test
3791 // sequence is ignored and the cursor does not move. If the terminal
3792 // handles test sequence incorrectly, a garbage string is displayed and
3793 // the cursor does move.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003794#ifdef FEAT_JOB_CHANNEL
3795 ch_log_output = TRUE;
3796#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003797 LOG_TR(("Sending xterm compatibility test sequence."));
3798 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003799 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003800 term_windgoto(2, 0);
3801 // send the test DCS string.
3802 out_str((char_u *)"\033Pzz\033\\");
3803 // send the test CSI sequence with intermediate byte.
3804 out_str((char_u *)"\033[0%m");
3805 out_str(T_U7);
3806 termrequest_sent(&xcc_status);
3807 out_flush();
3808 did_send = TRUE;
3809
3810 // If the terminal handles test sequence incorrectly, garbage text is
3811 // displayed. Clear them out for now.
3812 screen_stop_highlight();
3813 term_windgoto(2, 0);
3814 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003815 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003816 }
3817
3818 if (did_send)
3819 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003820 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003822 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003823 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003824
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003825 // check for the characters now, otherwise they might be eaten by
3826 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003827 out_flush();
3828 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003829 }
3830}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003831
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003832/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003833 * Similar to requesting the version string: Request the terminal background
3834 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003835 */
3836 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003837may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003838{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003839 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003840 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003841 int didit = FALSE;
3842
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003843# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003844 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003845 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003846 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003847#ifdef FEAT_JOB_CHANNEL
3848 ch_log_output = TRUE;
3849#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003850 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003851 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003852 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003853 didit = TRUE;
3854 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003855# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003857 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003858 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003859 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003860#ifdef FEAT_JOB_CHANNEL
3861 ch_log_output = TRUE;
3862#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003863 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003864 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003865 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003866 didit = TRUE;
3867 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003868
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003869 if (didit)
3870 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003871 // check for the characters now, otherwise they might be eaten by
3872 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003873 out_flush();
3874 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003875 }
3876 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003877}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003878
Bram Moolenaar2951b772013-07-03 12:45:31 +02003879# ifdef DEBUG_TERMRESPONSE
3880 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003881log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003882{
3883 static FILE *fd_tr = NULL;
3884 static proftime_T start;
3885 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003886 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003887
3888 if (fd_tr == NULL)
3889 {
3890 fd_tr = fopen("termresponse.log", "w");
3891 profile_start(&start);
3892 }
3893 now = start;
3894 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003895 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3896 must_redraw == NOT_VALID ? "NV"
3897 : must_redraw == CLEAR ? "CL" : " ");
3898 va_start(ap, fmt);
3899 vfprintf(fd_tr, fmt, ap);
3900 va_end(ap);
3901 fputc('\n', fd_tr);
3902 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003903}
3904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905#endif
3906
3907/*
3908 * Return TRUE when saving and restoring the screen.
3909 */
3910 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003911swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913 return (full_screen && *T_TI != NUL);
3914}
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916/*
3917 * By outputting the 'cursor very visible' termcap code, for some windowed
3918 * terminals this makes the screen scrolled to the correct position.
3919 * Used when starting Vim or returning from a shell.
3920 */
3921 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003922scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003924 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003926#ifdef FEAT_JOB_CHANNEL
3927 ch_log_output = TRUE;
3928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003930 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933}
3934
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003935// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936static int cursor_is_off = FALSE;
3937
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003938// True if cursor is not visible due to an ongoing cursor-less sleep
3939static int cursor_is_asleep = FALSE;
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003942 * Enable the cursor without checking if it's already enabled.
3943 */
3944 void
3945cursor_on_force(void)
3946{
3947 out_str(T_VE);
3948 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003949 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003950}
3951
3952/*
3953 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 */
3955 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003956cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003958 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003959 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960}
3961
3962/*
3963 * Disable the cursor.
3964 */
3965 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003966cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003968 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003970 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 cursor_is_off = TRUE;
3972 }
3973}
3974
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003975/*
3976 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3977 */
3978 int
3979cursor_is_sleeping(void)
3980{
3981 return cursor_is_asleep;
3982}
3983
3984/*
3985 * Disable the cursor and mark it disabled by cursor-less sleep
3986 */
3987 void
3988cursor_sleep(void)
3989{
3990 cursor_is_asleep = TRUE;
3991 cursor_off();
3992}
3993
3994/*
3995 * Enable the cursor and mark it not disabled by cursor-less sleep
3996 */
3997 void
3998cursor_unsleep(void)
3999{
4000 cursor_is_asleep = FALSE;
4001 cursor_on();
4002}
4003
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004004#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004006 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004007 */
4008 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004009term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004010{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004011 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004012 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004014 // Only do something when redrawing the screen and we can restore the
4015 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004016 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004017 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004018# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004019 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004020 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004021 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004022# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004024 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004025
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004026 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004027 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004028 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004029 {
4030 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004031 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004032 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004033 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004034 if (*p != NUL)
4035 {
4036 out_str(p);
4037 showing_mode = REPLACE;
4038 }
4039 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004040 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004041 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004042 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004043 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004044 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004045 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004046 showing_mode = INSERT;
4047 }
4048 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004049 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004050 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004051 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004052 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004053 }
4054}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004055
4056# if defined(FEAT_TERMINAL) || defined(PROTO)
4057 void
4058term_cursor_color(char_u *color)
4059{
4060 if (*T_CSC != NUL)
4061 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004062 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004063 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004064 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004065 out_flush();
4066 }
4067}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004068# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004069
Bram Moolenaar4db25542017-08-28 22:43:05 +02004070 int
4071blink_state_is_inverted()
4072{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004073#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004074 return rbm_status.tr_progress == STATUS_GOT
4075 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004076 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004077#else
4078 return FALSE;
4079#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004080}
4081
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004082/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004083 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004084 */
4085 void
4086term_cursor_shape(int shape, int blink)
4087{
4088 if (*T_CSH != NUL)
4089 {
4090 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4091 out_flush();
4092 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004093 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004094 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004095 int do_blink = blink;
4096
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004097 // t_SH is empty: try setting just the blink state.
4098 // The blink flags are XORed together, if the initial blinking from
4099 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004100 if (blink_state_is_inverted())
4101 do_blink = !blink;
4102
4103 if (do_blink && *T_VS != NUL)
4104 {
4105 out_str(T_VS);
4106 out_flush();
4107 }
4108 else if (!do_blink && *T_CVS != NUL)
4109 {
4110 out_str(T_CVS);
4111 out_flush();
4112 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004113 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004114}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004115#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004116
4117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Set scrolling region for window 'wp'.
4119 * The region starts 'off' lines from the start of the window.
4120 * Also set the vertical scroll region for a vertically split window. Always
4121 * the full width of the window, excluding the vertical separator.
4122 */
4123 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004124scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4127 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004129 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4130 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004131 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
4133
4134/*
4135 * Reset scrolling region to the whole screen.
4136 */
4137 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004138scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
4140 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (*T_CSV != NUL)
4142 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004143 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144}
4145
4146
4147/*
4148 * List of terminal codes that are currently recognized.
4149 */
4150
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004151static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004153 char_u name[2]; // termcap name of entry
4154 char_u *code; // terminal code (in allocated memory)
4155 int len; // STRLEN(code)
4156 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157} *termcodes = NULL;
4158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004159static int tc_max_len = 0; // number of entries that termcodes[] can hold
4160static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004162static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004165clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 while (tc_len > 0)
4168 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004169 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 tc_max_len = 0;
4171
4172#ifdef HAVE_TGETENT
4173 BC = (char *)empty_option;
4174 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 ospeed = 0;
4177#endif
4178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004179 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180}
4181
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182#define ATC_FROM_TERM 55
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184/*
4185 * Add a new entry to the list of terminal codes.
4186 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004187 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4188 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
4190 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004191add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 struct termcode *new_tc;
4194 int i, j;
4195 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004196 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 if (string == NULL || *string == NUL)
4199 {
4200 del_termcode(name);
4201 return;
4202 }
4203
Bram Moolenaar4f974752019-02-17 17:44:42 +01004204#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004205 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004206#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004207# ifdef VIMDLL
4208 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004209 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004210 else
4211# endif
4212 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (s == NULL)
4215 return;
4216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004217 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004220 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 s[0] = term_7to8bit(string);
4222 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004223
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004224#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4225# ifdef VIMDLL
4226 if (!gui.in_use)
4227# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004228 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004229 if (s[0] == K_NUL)
4230 {
4231 STRMOVE(s + 1, s);
4232 s[1] = 3;
4233 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004234 }
4235#endif
4236
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004239 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * need to make space for more entries
4243 */
4244 if (tc_len == tc_max_len)
4245 {
4246 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004247 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (new_tc == NULL)
4249 {
4250 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004251 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return;
4253 }
4254 for (i = 0; i < tc_len; ++i)
4255 new_tc[i] = termcodes[i];
4256 vim_free(termcodes);
4257 termcodes = new_tc;
4258 }
4259
4260 /*
4261 * Look for existing entry with the same name, it is replaced.
4262 * Look for an existing entry that is alphabetical higher, the new entry
4263 * is inserted in front of it.
4264 */
4265 for (i = 0; i < tc_len; ++i)
4266 {
4267 if (termcodes[i].name[0] < name[0])
4268 continue;
4269 if (termcodes[i].name[0] == name[0])
4270 {
4271 if (termcodes[i].name[1] < name[1])
4272 continue;
4273 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 */
4276 if (termcodes[i].name[1] == name[1])
4277 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 if (flags == ATC_FROM_TERM && (j = termcode_star(
4279 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004280 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004281 // Don't replace ESC[123;*X or ESC O*X with another when
4282 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004283 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004284 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004285 && s[len - 1]
4286 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004288 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004289 vim_free(s);
4290 return;
4291 }
4292 }
4293 else
4294 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004295 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004296 vim_free(termcodes[i].code);
4297 --tc_len;
4298 break;
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 }
4302 /*
4303 * Found alphabetical larger entry, move rest to insert new entry
4304 */
4305 for (j = tc_len; j > i; --j)
4306 termcodes[j] = termcodes[j - 1];
4307 break;
4308 }
4309
4310 termcodes[i].name[0] = name[0];
4311 termcodes[i].name[1] = name[1];
4312 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004313 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004315 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4316 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004317 termcodes[i].modlen = 0;
4318 j = termcode_star(s, len);
4319 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004320 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004321 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004322 // For "CSI[@;X" the "@" is not included in "modlen".
4323 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4324 --termcodes[i].modlen;
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 ++tc_len;
4327}
4328
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004329/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004330 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004332 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004333 */
4334 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004335termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004336{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004337 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004338 if (len >= 3 && code[len - 2] == '*')
4339 {
4340 if (len >= 5 && code[len - 3] == ';')
4341 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004342 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004343 return 1;
4344 }
4345 return 0;
4346}
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004349find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 int i;
4352
4353 for (i = 0; i < tc_len; ++i)
4354 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4355 return termcodes[i].code;
4356 return NULL;
4357}
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004360get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
4362 if (i >= tc_len)
4363 return NULL;
4364 return &termcodes[i].name[0];
4365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004367/*
4368 * Returns the length of the terminal code at index 'idx'.
4369 */
4370 int
4371get_termcode_len(int idx)
4372{
4373 return termcodes[idx].len;
4374}
4375
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004376 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004377del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
4379 int i;
4380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004381 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 return;
4383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004384 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 for (i = 0; i < tc_len; ++i)
4387 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4388 {
4389 del_termcode_idx(i);
4390 return;
4391 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004392 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
4395 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004396del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 int i;
4399
4400 vim_free(termcodes[idx].code);
4401 --tc_len;
4402 for (i = idx; i < tc_len; ++i)
4403 termcodes[i] = termcodes[i + 1];
4404}
4405
4406#ifdef FEAT_TERMRESPONSE
4407/*
4408 * Called when detected that the terminal sends 8-bit codes.
4409 * Convert all 7-bit codes to their 8-bit equivalent.
4410 */
4411 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004412switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 int i;
4415 int c;
4416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004417 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (!term_is_8bit(T_NAME))
4419 {
4420 for (i = 0; i < tc_len; ++i)
4421 {
4422 c = term_7to8bit(termcodes[i].code);
4423 if (c != 0)
4424 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004425 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 termcodes[i].code[0] = c;
4427 }
4428 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004429 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004432 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
4434#endif
4435
4436#ifdef CHECK_DOUBLE_CLICK
4437static linenr_T orig_topline = 0;
4438# ifdef FEAT_DIFF
4439static int orig_topfill = 0;
4440# endif
4441#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004442#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443/*
4444 * Checking for double clicks ourselves.
4445 * "orig_topline" is used to avoid detecting a double-click when the window
4446 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4447 */
4448/*
4449 * Set orig_topline. Used when jumping to another window, so that a double
4450 * click still works.
4451 */
4452 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004453set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
4455 orig_topline = wp->w_topline;
4456# ifdef FEAT_DIFF
4457 orig_topfill = wp->w_topfill;
4458# endif
4459}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004460
4461/*
4462 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4463 * topline and topfill.
4464 */
4465 int
4466is_mouse_topline(win_T *wp)
4467{
4468 return orig_topline == wp->w_topline
4469#ifdef FEAT_DIFF
4470 && orig_topfill == wp->w_topfill
4471#endif
4472 ;
4473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#endif
4475
4476/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004477 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4478 * Remove "slen" bytes.
4479 * Returns FAIL for error.
4480 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004481 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004482put_string_in_typebuf(
4483 int offset,
4484 int slen,
4485 char_u *string,
4486 int new_slen,
4487 char_u *buf,
4488 int bufsize,
4489 int *buflen)
4490{
4491 int extra = new_slen - slen;
4492
4493 string[new_slen] = NUL;
4494 if (buf == NULL)
4495 {
4496 if (extra < 0)
4497 // remove matched chars, taking care of noremap
4498 del_typebuf(-extra, offset);
4499 else if (extra > 0)
4500 // insert the extra space we need
4501 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4502
4503 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4504 // typebuf.tb_buf[]!
4505 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4506 (size_t)new_slen);
4507 }
4508 else
4509 {
4510 if (extra < 0)
4511 // remove matched characters
4512 mch_memmove(buf + offset, buf + offset - extra,
4513 (size_t)(*buflen + offset + extra));
4514 else if (extra > 0)
4515 {
4516 // Insert the extra space we need. If there is insufficient
4517 // space return -1.
4518 if (*buflen + extra + new_slen >= bufsize)
4519 return FAIL;
4520 mch_memmove(buf + offset + extra, buf + offset,
4521 (size_t)(*buflen - offset));
4522 }
4523 mch_memmove(buf + offset, string, (size_t)new_slen);
4524 *buflen = *buflen + extra + new_slen;
4525 }
4526 return OK;
4527}
4528
4529/*
4530 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4531 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004532 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004533decode_modifiers(int n)
4534{
4535 int code = n - 1;
4536 int modifiers = 0;
4537
4538 if (code & 1)
4539 modifiers |= MOD_MASK_SHIFT;
4540 if (code & 2)
4541 modifiers |= MOD_MASK_ALT;
4542 if (code & 4)
4543 modifiers |= MOD_MASK_CTRL;
4544 if (code & 8)
4545 modifiers |= MOD_MASK_META;
4546 return modifiers;
4547}
4548
4549 static int
4550modifiers2keycode(int modifiers, int *key, char_u *string)
4551{
4552 int new_slen = 0;
4553
4554 if (modifiers != 0)
4555 {
4556 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004557 // make mappings work. This may result in a special key, such as
4558 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004559 *key = simplify_key(*key, &modifiers);
4560 if (modifiers != 0)
4561 {
4562 string[new_slen++] = K_SPECIAL;
4563 string[new_slen++] = (int)KS_MODIFIER;
4564 string[new_slen++] = modifiers;
4565 }
4566 }
4567 return new_slen;
4568}
4569
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004570#ifdef FEAT_TERMRESPONSE
4571/*
4572 * Handle a cursor position report.
4573 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004574 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004575handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004576{
4577 if (arg[0] == 2 && arg[1] >= 2)
4578 {
4579 char *aw = NULL;
4580
4581 LOG_TR(("Received U7 status: %s", tp));
4582 u7_status.tr_progress = STATUS_GOT;
4583 did_cursorhold = TRUE;
4584 if (arg[1] == 2)
4585 aw = "single";
4586 else if (arg[1] == 3)
4587 aw = "double";
4588 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4589 {
4590 // Setting the option causes a screen redraw. Do
4591 // that right away if possible, keeping any
4592 // messages.
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004593 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004594# ifdef DEBUG_TERMRESPONSE
4595 {
4596 int r = redraw_asap(CLEAR);
4597
4598 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4599 }
4600# else
4601 redraw_asap(CLEAR);
4602# endif
4603# ifdef FEAT_EVAL
4604 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4605# endif
4606 }
4607 }
4608 else if (arg[0] == 3)
4609 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004610 int value;
4611
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004612 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004613 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004614
4615 // Third row: xterm compatibility test.
4616 // If the cursor is on the first column then the terminal can handle
4617 // the request for cursor style and blinking.
4618 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4619 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4620 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004621 }
4622}
4623
4624/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004625 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004626 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004627 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004628 */
4629 static void
4630handle_version_response(int first, int *arg, int argc, char_u *tp)
4631{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004632 // The xterm version. It is set to zero when it can't be an actual xterm
4633 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004634 int version = arg[1];
4635
4636 LOG_TR(("Received CRV response: %s", tp));
4637 crv_status.tr_progress = STATUS_GOT;
4638 did_cursorhold = TRUE;
4639
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004640 // Reset terminal properties that are set based on the termresponse.
4641 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004642 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004643 init_term_props(
4644#ifdef FEAT_EVAL
4645 reset_term_props_on_termresponse
4646#else
4647 FALSE
4648#endif
4649 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004650
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004651 // If this code starts with CSI, you can bet that the
4652 // terminal uses 8-bit codes.
4653 if (tp[0] == CSI)
4654 switch_to_8bit();
4655
4656 // Screen sends 40500.
4657 // rxvt sends its version number: "20703" is 2.7.3.
4658 // Ignore it for when the user has set 'term' to xterm,
4659 // even though it's an rxvt.
4660 if (version > 20000)
4661 version = 0;
4662
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004663 // Figure out more if the reeponse is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004664 if (first == '>' && argc == 3)
4665 {
4666 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004667
4668 // mintty 2.9.5 sends 77;20905;0c.
4669 // (77 is ASCII 'M' for mintty.)
4670 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004671 {
4672 // mintty can do SGR mouse reporting
4673 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4674 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004675
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004676 // If xterm version >= 141 try to get termcap codes. For other
4677 // terminals the request should be ignored.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004678 if (version >= 141)
4679 {
4680 LOG_TR(("Enable checking for XT codes"));
4681 check_for_codes = TRUE;
4682 need_gather = TRUE;
4683 req_codes_from_term();
4684 }
4685
4686 // libvterm sends 0;100;0
4687 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4688 {
4689 // If run from Vim $COLORS is set to the number of
4690 // colors the terminal supports. Otherwise assume
4691 // 256, libvterm supports even more.
4692 if (mch_getenv((char_u *)"COLORS") == NULL)
4693 may_adjust_color_count(256);
4694 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004695 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004696 }
4697
4698 if (version == 95)
4699 {
4700 // Mac Terminal.app sends 1;95;0
4701 if (arg[0] == 1 && arg[2] == 0)
4702 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004703 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4704 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004705 }
4706 // iTerm2 sends 0;95;0
4707 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004708 {
4709 // iTerm2 can do SGR mouse reporting
4710 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4711 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004712 // old iTerm2 sends 0;95;
4713 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004714 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004715 }
4716
4717 // screen sends 83;40500;0 83 is 'S' in ASCII.
4718 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004719 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004720 // screen supports SGR mouse codes since 4.7.0
4721 if (arg[1] >= 40700)
4722 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4723 else
4724 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4725 }
4726
4727 // If no recognized terminal has set mouse behavior, assume xterm.
4728 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4729 {
4730 // Xterm version 277 supports SGR.
4731 // Xterm version >= 95 supports mouse dragging.
4732 if (version >= 277)
4733 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004734 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004735 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004736 }
4737
4738 // Detect terminals that set $TERM to something like
4739 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004740 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004741 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004742 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004743 // xfce4-terminal sends 1;2802;0.
4744 // screen sends 83;40500;0
4745 // Assuming any version number over 2500 is not an
4746 // xterm (without the limit for rxvt and screen).
4747 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004748 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004749
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004750 else if (version == 136 && arg[2] == 0)
4751 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004752 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004753
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004754 // PuTTY sends 0;136;0
4755 if (arg[0] == 0)
4756 {
4757 // supports sgr-like mouse reporting.
4758 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4759 }
4760 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004761 }
4762
4763 // Konsole sends 0;115;0
4764 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004765 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004766
4767 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004768 // 30600/40500 is a version number of GNU screen. DA2 support is added
4769 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4770 // compatibility checking does not detect GNU screen.
4771 if (arg[0] == 83 && arg[1] >= 30600)
4772 {
4773 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4774 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4775 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776
4777 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004778 // 95, so assume anything below 95 is not xterm and hopefully supports
4779 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004780 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004781 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004782
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004783 // Getting the cursor style is only supported properly by xterm since
4784 // version 279 (otherwise it returns 0x18).
4785 if (version < 279)
4786 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4787
4788 /*
4789 * Take action on the detected properties.
4790 */
4791
4792 // Unless the underline RGB color is expected to work, disable "t_8u".
4793 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004794 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES && *T_8U != NUL)
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004795 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4796 OPT_FREE, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004797
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004798 // Only set 'ttymouse' automatically if it was not set
4799 // by the user already.
4800 if (!option_was_set((char_u *)"ttym")
4801 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4802 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4803 {
4804 set_option_value((char_u *)"ttym", 0L,
4805 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4806 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4807 }
4808
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004809 // Only request the cursor style if t_SH and t_RS are
4810 // set. Only supported properly by xterm since version
4811 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004812 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004813 // Not for Terminal.app, it can't handle t_RS, it
4814 // echoes the characters to the screen.
4815 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004816 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004817 && *T_CSH != NUL
4818 && *T_CRS != NUL)
4819 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004820#ifdef FEAT_JOB_CHANNEL
4821 ch_log_output = TRUE;
4822#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004823 LOG_TR(("Sending cursor style request"));
4824 out_str(T_CRS);
4825 termrequest_sent(&rcs_status);
4826 need_flush = TRUE;
4827 }
4828
4829 // Only request the cursor blink mode if t_RC set. Not
4830 // for Gnome terminal, it can't handle t_RC, it
4831 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004832 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004833 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004834 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004835 && *T_CRC != NUL)
4836 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004837#ifdef FEAT_JOB_CHANNEL
4838 ch_log_output = TRUE;
4839#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004840 LOG_TR(("Sending cursor blink mode request"));
4841 out_str(T_CRC);
4842 termrequest_sent(&rbm_status);
4843 need_flush = TRUE;
4844 }
4845
4846 if (need_flush)
4847 out_flush();
4848 }
4849}
4850
4851/*
4852 * Handle a sequence with key and modifier, one of:
4853 * {lead}27;{modifier};{key}~
4854 * {lead}{key};{modifier}u
4855 * Returns the difference in length.
4856 */
4857 static int
4858handle_key_with_modifier(
4859 int *arg,
4860 int trail,
4861 int csi_len,
4862 int offset,
4863 char_u *buf,
4864 int bufsize,
4865 int *buflen)
4866{
4867 int key;
4868 int modifiers;
4869 int new_slen;
4870 char_u string[MAX_KEY_CODE_LEN + 1];
4871
4872 seenModifyOtherKeys = TRUE;
4873 if (trail == 'u')
4874 key = arg[0];
4875 else
4876 key = arg[2];
4877
4878 modifiers = decode_modifiers(arg[1]);
4879
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004880 // Some keys need adjustment when the Ctrl modifier is used.
4881 key = may_adjust_key_for_ctrl(modifiers, key);
4882
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004883 // May remove the shift modifier if it's already included in the key.
4884 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004885
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886 // insert modifiers with KS_MODIFIER
4887 new_slen = modifiers2keycode(modifiers, &key, string);
4888
Bram Moolenaar749bc952020-10-31 16:33:47 +01004889 if (IS_SPECIAL(key))
4890 {
4891 string[new_slen++] = K_SPECIAL;
4892 string[new_slen++] = KEY2TERMCAP0(key);
4893 string[new_slen++] = KEY2TERMCAP1(key);
4894 }
4895 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004896 new_slen += (*mb_char2bytes)(key, string + new_slen);
4897 else
4898 string[new_slen++] = key;
4899
4900 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4901 buf, bufsize, buflen) == FAIL)
4902 return -1;
4903 return new_slen - csi_len + offset;
4904}
4905
4906/*
4907 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004908 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004909 *
4910 * - Cursor position report: {lead}{row};{col}R
4911 * The final byte must be 'R'. It is used for checking the
4912 * ambiguous-width character state.
4913 *
4914 * - window position reply: {lead}3;{x};{y}t
4915 *
4916 * - key with modifiers when modifyOtherKeys is enabled:
4917 * {lead}27;{modifier};{key}~
4918 * {lead}{key};{modifier}u
4919 * Return 0 for no match, -1 for partial match, > 0 for full match.
4920 */
4921 static int
4922handle_csi(
4923 char_u *tp,
4924 int len,
4925 char_u *argp,
4926 int offset,
4927 char_u *buf,
4928 int bufsize,
4929 int *buflen,
4930 char_u *key_name,
4931 int *slen)
4932{
4933 int first = -1; // optional char right after {lead}
4934 int trail; // char that ends CSI sequence
4935 int arg[3] = {-1, -1, -1}; // argument numbers
4936 int argc; // number of arguments
4937 char_u *ap = argp;
4938 int csi_len;
4939
4940 // Check for non-digit after CSI.
4941 if (!VIM_ISDIGIT(*ap))
4942 first = *ap++;
4943
4944 // Find up to three argument numbers.
4945 for (argc = 0; argc < 3; )
4946 {
4947 if (ap >= tp + len)
4948 return -1;
4949 if (*ap == ';')
4950 arg[argc++] = -1; // omitted number
4951 else if (VIM_ISDIGIT(*ap))
4952 {
4953 arg[argc] = 0;
4954 for (;;)
4955 {
4956 if (ap >= tp + len)
4957 return -1;
4958 if (!VIM_ISDIGIT(*ap))
4959 break;
4960 arg[argc] = arg[argc] * 10 + (*ap - '0');
4961 ++ap;
4962 }
4963 ++argc;
4964 }
4965 if (*ap == ';')
4966 ++ap;
4967 else
4968 break;
4969 }
4970
4971 // mrxvt has been reported to have "+" in the version. Assume
4972 // the escape sequence ends with a letter or one of "{|}~".
4973 while (ap < tp + len
4974 && !(*ap >= '{' && *ap <= '~')
4975 && !ASCII_ISALPHA(*ap))
4976 ++ap;
4977 if (ap >= tp + len)
4978 return -1;
4979 trail = *ap;
4980 csi_len = (int)(ap - tp) + 1;
4981
4982 // Cursor position report: Eat it when there are 2 arguments
4983 // and it ends in 'R'. Also when u7_status is not "sent", it
4984 // may be from a previous Vim that just exited. But not for
4985 // <S-F3>, it sends something similar, check for row and column
4986 // to make sense.
4987 if (first == -1 && argc == 2 && trail == 'R')
4988 {
4989 handle_u7_response(arg, tp, csi_len);
4990
4991 key_name[0] = (int)KS_EXTRA;
4992 key_name[1] = (int)KE_IGNORE;
4993 *slen = csi_len;
4994 }
4995
4996 // Version string: Eat it when there is at least one digit and
4997 // it ends in 'c'
4998 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
4999 {
5000 handle_version_response(first, arg, argc, tp);
5001
5002 *slen = csi_len;
5003# ifdef FEAT_EVAL
5004 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5005# endif
5006 apply_autocmds(EVENT_TERMRESPONSE,
5007 NULL, NULL, FALSE, curbuf);
5008 key_name[0] = (int)KS_EXTRA;
5009 key_name[1] = (int)KE_IGNORE;
5010 }
5011
5012 // Check blinking cursor from xterm:
5013 // {lead}?12;1$y set
5014 // {lead}?12;2$y not set
5015 //
5016 // {lead} can be <Esc>[ or CSI
5017 else if (rbm_status.tr_progress == STATUS_SENT
5018 && first == '?'
5019 && ap == argp + 6
5020 && arg[0] == 12
5021 && ap[-1] == '$'
5022 && trail == 'y')
5023 {
5024 initial_cursor_blink = (arg[1] == '1');
5025 rbm_status.tr_progress = STATUS_GOT;
5026 LOG_TR(("Received cursor blinking mode response: %s", tp));
5027 key_name[0] = (int)KS_EXTRA;
5028 key_name[1] = (int)KE_IGNORE;
5029 *slen = csi_len;
5030# ifdef FEAT_EVAL
5031 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5032# endif
5033 }
5034
5035 // Check for a window position response from the terminal:
5036 // {lead}3;{x};{y}t
5037 else if (did_request_winpos && argc == 3 && arg[0] == 3
5038 && trail == 't')
5039 {
5040 winpos_x = arg[1];
5041 winpos_y = arg[2];
5042 // got finished code: consume it
5043 key_name[0] = (int)KS_EXTRA;
5044 key_name[1] = (int)KE_IGNORE;
5045 *slen = csi_len;
5046
5047 if (--did_request_winpos <= 0)
5048 winpos_status.tr_progress = STATUS_GOT;
5049 }
5050
5051 // Key with modifier:
5052 // {lead}27;{modifier};{key}~
5053 // {lead}{key};{modifier}u
5054 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5055 || (argc == 2 && trail == 'u'))
5056 {
5057 return len + handle_key_with_modifier(arg, trail,
5058 csi_len, offset, buf, bufsize, buflen);
5059 }
5060
5061 // else: Unknown CSI sequence. We could drop it, but then the
5062 // user can't create a map for it.
5063 return 0;
5064}
5065
5066/*
5067 * Handle an OSC sequence, fore/background color response from the terminal:
5068 *
5069 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5070 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5071 *
5072 * {code} is 10 for foreground, 11 for background
5073 * {lead} can be <Esc>] or OSC
5074 * {tail} can be '\007', <Esc>\ or STERM.
5075 *
5076 * Consume any code that starts with "{lead}11;", it's also
5077 * possible that "rgba" is following.
5078 */
5079 static int
5080handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5081{
5082 int i, j;
5083
5084 j = 1 + (tp[0] == ESC);
5085 if (len >= j + 3 && (argp[0] != '1'
5086 || (argp[1] != '1' && argp[1] != '0')
5087 || argp[2] != ';'))
5088 i = 0; // no match
5089 else
5090 for (i = j; i < len; ++i)
5091 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5092 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5093 {
5094 int is_bg = argp[1] == '1';
5095 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5096 && tp[j + 16] == '/';
5097
5098 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5099 && (is_4digit
5100 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5101 {
5102 char_u *tp_r = tp + j + 7;
5103 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5104 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5105# ifdef FEAT_TERMINAL
5106 int rval, gval, bval;
5107
5108 rval = hexhex2nr(tp_r);
5109 gval = hexhex2nr(tp_b);
5110 bval = hexhex2nr(tp_g);
5111# endif
5112 if (is_bg)
5113 {
5114 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5115 *tp_b) ? "light" : "dark";
5116
5117 LOG_TR(("Received RBG response: %s", tp));
5118 rbg_status.tr_progress = STATUS_GOT;
5119# ifdef FEAT_TERMINAL
5120 bg_r = rval;
5121 bg_g = gval;
5122 bg_b = bval;
5123# endif
5124 if (!option_was_set((char_u *)"bg")
5125 && STRCMP(p_bg, new_bg_val) != 0)
5126 {
5127 // value differs, apply it
5128 set_option_value((char_u *)"bg", 0L,
5129 (char_u *)new_bg_val, 0);
5130 reset_option_was_set((char_u *)"bg");
5131 redraw_asap(CLEAR);
5132 }
5133 }
5134# ifdef FEAT_TERMINAL
5135 else
5136 {
5137 LOG_TR(("Received RFG response: %s", tp));
5138 rfg_status.tr_progress = STATUS_GOT;
5139 fg_r = rval;
5140 fg_g = gval;
5141 fg_b = bval;
5142 }
5143# endif
5144 }
5145
5146 // got finished code: consume it
5147 key_name[0] = (int)KS_EXTRA;
5148 key_name[1] = (int)KE_IGNORE;
5149 *slen = i + 1 + (tp[i] == ESC);
5150# ifdef FEAT_EVAL
5151 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5152 : VV_TERMRFGRESP, tp, *slen);
5153# endif
5154 break;
5155 }
5156 if (i == len)
5157 {
5158 LOG_TR(("not enough characters for RB"));
5159 return FAIL;
5160 }
5161 return OK;
5162}
5163
5164/*
5165 * Check for key code response from xterm:
5166 * {lead}{flag}+r<hex bytes><{tail}
5167 *
5168 * {lead} can be <Esc>P or DCS
5169 * {flag} can be '0' or '1'
5170 * {tail} can be Esc>\ or STERM
5171 *
5172 * Check for cursor shape response from xterm:
5173 * {lead}1$r<digit> q{tail}
5174 *
5175 * {lead} can be <Esc>P or DCS
5176 * {tail} can be <Esc>\ or STERM
5177 *
5178 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5179 */
5180 static int
5181handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5182{
5183 int i, j;
5184
5185 j = 1 + (tp[0] == ESC);
5186 if (len < j + 3)
5187 i = len; // need more chars
5188 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5189 i = 0; // no match
5190 else if (argp[1] == '+')
5191 // key code response
5192 for (i = j; i < len; ++i)
5193 {
5194 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5195 || tp[i] == STERM)
5196 {
5197 if (i - j >= 3)
5198 got_code_from_term(tp + j, i);
5199 key_name[0] = (int)KS_EXTRA;
5200 key_name[1] = (int)KE_IGNORE;
5201 *slen = i + 1 + (tp[i] == ESC);
5202 break;
5203 }
5204 }
5205 else
5206 {
5207 // Probably the cursor shape response. Make sure that "i"
5208 // is equal to "len" when there are not sufficient
5209 // characters.
5210 for (i = j + 3; i < len; ++i)
5211 {
5212 if (i - j == 3 && !isdigit(tp[i]))
5213 break;
5214 if (i - j == 4 && tp[i] != ' ')
5215 break;
5216 if (i - j == 5 && tp[i] != 'q')
5217 break;
5218 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5219 break;
5220 if ((i - j == 6 && tp[i] == STERM)
5221 || (i - j == 7 && tp[i] == '\\'))
5222 {
5223 int number = argp[3] - '0';
5224
5225 // 0, 1 = block blink, 2 = block
5226 // 3 = underline blink, 4 = underline
5227 // 5 = vertical bar blink, 6 = vertical bar
5228 number = number == 0 ? 1 : number;
5229 initial_cursor_shape = (number + 1) / 2;
5230 // The blink flag is actually inverted, compared to
5231 // the value set with T_SH.
5232 initial_cursor_shape_blink =
5233 (number & 1) ? FALSE : TRUE;
5234 rcs_status.tr_progress = STATUS_GOT;
5235 LOG_TR(("Received cursor shape response: %s", tp));
5236
5237 key_name[0] = (int)KS_EXTRA;
5238 key_name[1] = (int)KE_IGNORE;
5239 *slen = i + 1;
5240# ifdef FEAT_EVAL
5241 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5242# endif
5243 break;
5244 }
5245 }
5246 }
5247
5248 if (i == len)
5249 {
5250 // These codes arrive many together, each code can be
5251 // truncated at any point.
5252 LOG_TR(("not enough characters for XT"));
5253 return FAIL;
5254 }
5255 return OK;
5256}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005257#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005258
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 * Check if typebuf.tb_buf[] contains a terminal key code.
5261 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005262 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005264 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * With a match, the match is removed, the replacement code is inserted in
5266 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5267 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005268 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5269 * "buflen" is then the length of the string in buf[] and is updated for
5270 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
5272 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005273check_termcode(
5274 int max_offset,
5275 char_u *buf,
5276 int bufsize,
5277 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 char_u *tp;
5280 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005281 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005282 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005284 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 int offset;
5286 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005287 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005288 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005290 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 char_u string[MAX_KEY_CODE_LEN + 1];
5292 int i, j;
5293 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5297
5298 /*
5299 * Speed up the checks for terminal codes by gathering all first bytes
5300 * used in termleader[]. Often this is just a single <Esc>.
5301 */
5302 if (need_gather)
5303 gather_termleader();
5304
5305 /*
5306 * Check at several positions in typebuf.tb_buf[], to catch something like
5307 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5308 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005309 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * This is used often, KEEP IT FAST!
5311 */
5312 for (offset = 0; offset < max_offset; ++offset)
5313 {
5314 if (buf == NULL)
5315 {
5316 if (offset >= typebuf.tb_len)
5317 break;
5318 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005319 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 else
5322 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005323 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 break;
5325 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005326 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
5328
5329 /*
5330 * Don't check characters after K_SPECIAL, those are already
5331 * translated terminal chars (avoid translating ~@^Hx).
5332 */
5333 if (*tp == K_SPECIAL)
5334 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005335 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 continue;
5337 }
5338
5339 /*
5340 * Skip this position if the character does not appear as the first
5341 * character in term_strings. This speeds up a lot, since most
5342 * termcodes start with the same character (ESC or CSI).
5343 */
5344 i = *tp;
5345 for (p = termleader; *p && *p != i; ++p)
5346 ;
5347 if (*p == NUL)
5348 continue;
5349
5350 /*
5351 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5352 * are in Insert mode.
5353 */
5354 if (*tp == ESC && !p_ek && (State & INSERT))
5355 continue;
5356
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005357 key_name[0] = NUL; // no key name found yet
5358 key_name[1] = NUL; // no key name found yet
5359 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361#ifdef FEAT_GUI
5362 if (gui.in_use)
5363 {
5364 /*
5365 * GUI special key codes are all of the form [CSI xx].
5366 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005367 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005370 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 slen = 3;
5372 key_name[0] = tp[1];
5373 key_name[1] = tp[2];
5374 }
5375 }
5376 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005379 int mouse_index_found = -1;
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 for (idx = 0; idx < tc_len; ++idx)
5382 {
5383 /*
5384 * Ignore the entry if we are not at the start of
5385 * typebuf.tb_buf[]
5386 * and there are not enough characters to make a match.
5387 * But only when the 'K' flag is in 'cpoptions'.
5388 */
5389 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005390 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (cpo_koffset && offset && len < slen)
5392 continue;
5393 if (STRNCMP(termcodes[idx].code, tp,
5394 (size_t)(slen > len ? len : slen)) == 0)
5395 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005396 if (len < slen) // got a partial sequence
5397 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
5399 /*
5400 * When found a keypad key, check if there is another key
5401 * that matches and use that one. This makes <Home> to be
5402 * found instead of <kHome> when they produce the same
5403 * key code.
5404 */
5405 if (termcodes[idx].name[0] == 'K'
5406 && VIM_ISDIGIT(termcodes[idx].name[1]))
5407 {
5408 for (j = idx + 1; j < tc_len; ++j)
5409 if (termcodes[j].len == slen &&
5410 STRNCMP(termcodes[idx].code,
5411 termcodes[j].code, slen) == 0)
5412 {
5413 idx = j;
5414 break;
5415 }
5416 }
5417
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005418 // The mouse termcode "ESC [" is also the prefix of
5419 // "ESC [ I" (focus gained). Only use it when there is
5420 // no other match. Do use it when a digit is following to
5421 // avoid waiting for more bytes.
5422 if (slen == 2 && len > 2
5423 && termcodes[idx].code[0] == ESC
5424 && termcodes[idx].code[1] == '['
5425 && !isdigit(tp[2]))
5426 {
5427 if (mouse_index_found < 0)
5428 mouse_index_found = idx;
5429 }
5430 else
5431 {
5432 key_name[0] = termcodes[idx].name[0];
5433 key_name[1] = termcodes[idx].name[1];
5434 break;
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005437
5438 /*
5439 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005440 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005441 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005442 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5443 * When there is a modifier the * matches a number.
5444 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005445 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005446 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005447 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005448 int at_code;
5449
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005450 modslen = termcodes[idx].modlen;
5451 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005452 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005453 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005454 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005455 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005456 {
5457 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005459 if (len <= modslen) // got a partial sequence
5460 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005461
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005462 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005463 // no modifiers
5464 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005465 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005466 // no match for "code;*X" with "code;"
5467 continue;
5468 else if (at_code && tp[modslen] != '1')
5469 // no match for "<Esc>[@" with "<Esc>[1"
5470 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005471 else
5472 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005473 // Skip over the digits, the final char must
5474 // follow. URXVT can use a negative value, thus
5475 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005476 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005477 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005478 ;
5479 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 if (len < j) // got a partial sequence
5481 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005482 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005483 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005484
Bram Moolenaara529ce02017-06-22 22:37:57 +02005485 modifiers_start = tp + slen - 2;
5486
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005487 // Match! Convert modifier bits.
5488 n = atoi((char *)modifiers_start);
5489 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005490
5491 slen = j;
5492 }
5493 key_name[0] = termcodes[idx].name[0];
5494 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005495 break;
5496 }
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005499 if (idx == tc_len && mouse_index_found >= 0)
5500 {
5501 key_name[0] = termcodes[mouse_index_found].name[0];
5502 key_name[1] = termcodes[mouse_index_found].name[1];
5503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
5505
5506#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005507 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005508 // Mouse codes of DEC and pterm start with <ESC>[. When
5509 // detecting the start of these mouse codes they might as well be
5510 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005511# ifdef FEAT_MOUSE_DEC
5512 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005513# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005514# ifdef FEAT_MOUSE_PTERM
5515 || key_name[0] == KS_PTERM_MOUSE
5516# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005517 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005519 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5520
5521 /*
5522 * Check for responses from the terminal starting with {lead}:
5523 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005524 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005525 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005526 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005527 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005528 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005529 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005530 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005531 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005532 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005533 * - window position reply: {lead}3;{x};{y}t
5534 *
5535 * - key with modifiers when modifyOtherKeys is enabled:
5536 * {lead}27;{modifier};{key}~
5537 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005538 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005539 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005540 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005541 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005543 int resp = handle_csi(tp, len, argp, offset, buf,
5544 bufsize, buflen, key_name, &slen);
5545 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005546 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005547# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005548 if (resp == -1)
5549 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005550# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005551 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005552 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005553 }
5554
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005555 // Check for fore/background color response from the terminal,
5556 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005557 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005558 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5559 || tp[0] == OSC))
5560 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005561 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005562 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005565 // Check for key code response from xterm,
5566 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005567 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005568 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 || tp[0] == DCS))
5570 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005571 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005572 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 }
5575#endif
5576
5577 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005578 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005580 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005582#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /*
5584 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5585 * so that we know which window to scroll later.
5586 */
5587 if (gui.in_use
5588 && key_name[0] == (int)KS_EXTRA
5589 && (key_name[1] == (int)KE_X1MOUSE
5590 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005591 || key_name[1] == (int)KE_MOUSELEFT
5592 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 || key_name[1] == (int)KE_MOUSEDOWN
5594 || key_name[1] == (int)KE_MOUSEUP))
5595 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005596 char_u bytes[6];
5597 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5598
5599 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return -1;
5601 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5602 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5603 slen += num_bytes;
5604 }
5605 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 /*
5608 * If it is a mouse click, get the coordinates.
5609 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005610 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005611#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005612 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005613#endif
5614#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005615 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005616#endif
5617#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005618 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005619#endif
5620#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005621 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005622#endif
5623#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005624 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005625#endif
5626#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005627 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005628#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005629 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005630 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005632 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5633 &modifiers) == -1)
5634 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637#ifdef FEAT_GUI
5638 /*
5639 * If using the GUI, then we get menu and scrollbar events.
5640 *
5641 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5642 * four bytes which are to be taken as a pointer to the vimmenu_T
5643 * structure.
5644 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005645 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005646 * is one byte with the tab index.
5647 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5649 * by one byte representing the scrollbar number, and then four bytes
5650 * representing a long_u which is the new value of the scrollbar.
5651 *
5652 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5653 * KE_FILLER followed by four bytes representing a long_u which is the
5654 * new value of the scrollbar.
5655 */
5656# ifdef FEAT_MENU
5657 else if (key_name[0] == (int)KS_MENU)
5658 {
5659 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005660 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (num_bytes == -1)
5663 return -1;
5664 current_menu = (vimmenu_T *)val;
5665 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005666
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005667 // The menu may have been deleted right after it was used, check
5668 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005669 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5670 {
5671 key_name[0] = KS_EXTRA;
5672 key_name[1] = (int)KE_IGNORE;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005676# ifdef FEAT_GUI_TABLINE
5677 else if (key_name[0] == (int)KS_TABLINE)
5678 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005679 // Selecting tabline tab or using its menu.
5680 char_u bytes[6];
5681 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5682
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005683 if (num_bytes == -1)
5684 return -1;
5685 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005686 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005687 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005688 slen += num_bytes;
5689 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005690 else if (key_name[0] == (int)KS_TABMENU)
5691 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005692 // Selecting tabline tab or using its menu.
5693 char_u bytes[6];
5694 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5695
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005696 if (num_bytes == -1)
5697 return -1;
5698 current_tab = (int)bytes[0];
5699 current_tabmenu = (int)bytes[1];
5700 slen += num_bytes;
5701 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703# ifndef USE_ON_FLY_SCROLL
5704 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5705 {
5706 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005707 char_u bytes[6];
5708 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005710 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 j = 0;
5712 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5713 && tp[j + 2] != NUL; ++i)
5714 {
5715 j += 3;
5716 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5717 if (num_bytes == -1)
5718 break;
5719 if (i == 0)
5720 current_scrollbar = (int)bytes[0];
5721 else if (current_scrollbar != (int)bytes[0])
5722 break;
5723 j += num_bytes;
5724 num_bytes = get_long_from_buf(tp + j, &val);
5725 if (num_bytes == -1)
5726 break;
5727 scrollbar_value = val;
5728 j += num_bytes;
5729 slen = j;
5730 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005731 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 return -1;
5733 }
5734 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5735 {
5736 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005737 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005739 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 j = 0;
5741 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5742 && tp[j + 2] != NUL; ++i)
5743 {
5744 j += 3;
5745 num_bytes = get_long_from_buf(tp + j, &val);
5746 if (num_bytes == -1)
5747 break;
5748 scrollbar_value = val;
5749 j += num_bytes;
5750 slen = j;
5751 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005752 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 return -1;
5754 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005755# endif // !USE_ON_FLY_SCROLL
5756#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005758#if (defined(UNIX) || defined(VMS))
5759 /*
5760 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5761 * (CSI I/CSI O)
5762 */
5763 if (focus_mode
5764# ifdef FEAT_GUI
5765 && !gui.in_use
5766# endif
5767 && key_name[0] == KS_EXTRA
5768 )
5769 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005770 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005771 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005772 if (!focus_state)
5773 {
5774 ui_focus_change(TRUE);
5775 did_cursorhold = TRUE;
5776 focus_state = TRUE;
5777 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005778 key_name[1] = (int)KE_IGNORE;
5779 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005780 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005781 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005782 if (focus_state)
5783 {
5784 ui_focus_change(FALSE);
5785 did_cursorhold = TRUE;
5786 focus_state = FALSE;
5787 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005788 key_name[1] = (int)KE_IGNORE;
5789 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005790 }
5791#endif
5792
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005793 /*
5794 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5795 */
5796 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5797
5798 /*
5799 * Add any modifier codes to our string.
5800 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005801 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005803 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005804 key_name[0] = KEY2TERMCAP0(key);
5805 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005808 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005809 if (has_mbyte)
5810 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5811 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005812 string[new_slen++] = key_name[1];
5813 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005814 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5815 && key_name[1] == KE_IGNORE)
5816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005817 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5818 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005819 retval = KEYLEN_REMOVED;
5820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 else
5822 {
5823 string[new_slen++] = K_SPECIAL;
5824 string[new_slen++] = key_name[0];
5825 string[new_slen++] = key_name[1];
5826 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005827 if (put_string_in_typebuf(offset, slen, string, new_slen,
5828 buf, bufsize, buflen) == FAIL)
5829 return -1;
5830 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
5832
Bram Moolenaar2951b772013-07-03 12:45:31 +02005833#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005834 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005835#endif
5836
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005837 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
5839
Bram Moolenaar9377df32017-10-15 13:22:01 +02005840#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005841/*
5842 * Get the text foreground color, if known.
5843 */
5844 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005845term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005846{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005847 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005848 {
5849 *r = fg_r;
5850 *g = fg_g;
5851 *b = fg_b;
5852 }
5853}
5854
5855/*
5856 * Get the text background color, if known.
5857 */
5858 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005859term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005860{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005861 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005862 {
5863 *r = bg_r;
5864 *g = bg_g;
5865 *b = bg_b;
5866 }
5867}
5868#endif
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
5871 * Replace any terminal code strings in from[] with the equivalent internal
5872 * vim representation. This is used for the "from" and "to" part of a
5873 * mapping, and the "to" part of a menu command.
5874 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5875 * '<'.
5876 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5877 *
5878 * The replacement is done in result[] and finally copied into allocated
5879 * memory. If this all works well *bufp is set to the allocated memory and a
5880 * pointer to it is returned. If something fails *bufp is set to NULL and from
5881 * is returned.
5882 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005883 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5884 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5885 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5886 * used instead of a CTRL-V.
5887 *
5888 * Flags:
5889 * REPTERM_FROM_PART see above
5890 * REPTERM_DO_LT also translate <lt>
5891 * REPTERM_SPECIAL always accept <key> notation
5892 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5893 *
5894 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5895 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005897 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005898replace_termcodes(
5899 char_u *from,
5900 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005901 int flags,
5902 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 int i;
5905 int slen;
5906 int key;
5907 int dlen = 0;
5908 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005909 int do_backslash; // backslash is a special character
5910 int do_special; // recognize <> key codes
5911 int do_key_code; // recognize raw key codes
5912 char_u *result; // buffer for resulting string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
5914 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005915 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5916 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5918
5919 /*
5920 * Allocate space for the translation. Worst case a single character is
5921 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5922 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005923 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005924 if (result == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 *bufp = NULL;
5927 return from;
5928 }
5929
5930 src = from;
5931
5932 /*
5933 * Check for #n at start only: function key n
5934 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005935 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937 result[dlen++] = K_SPECIAL;
5938 result[dlen++] = 'k';
5939 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005940 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005942 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 src += 2;
5944 }
5945
5946 /*
5947 * Copy each byte from *from to result[dlen]
5948 */
5949 while (*src != NUL)
5950 {
5951 /*
5952 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005953 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005955 if (do_special && ((flags & REPTERM_DO_LT)
5956 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
5958#ifdef FEAT_EVAL
5959 /*
5960 * Replace <SID> by K_SNR <script-nr> _.
5961 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5962 */
5963 if (STRNICMP(src, "<SID>", 5) == 0)
5964 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005965 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005966 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 else
5968 {
5969 src += 5;
5970 result[dlen++] = K_SPECIAL;
5971 result[dlen++] = (int)KS_EXTRA;
5972 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005973 sprintf((char *)result + dlen, "%ld",
5974 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 dlen += (int)STRLEN(result + dlen);
5976 result[dlen++] = '_';
5977 continue;
5978 }
5979 }
5980#endif
5981
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005982 slen = trans_special(&src, result + dlen, FSK_KEYCODE
5983 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
5984 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if (slen)
5986 {
5987 dlen += slen;
5988 continue;
5989 }
5990 }
5991
5992 /*
5993 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5994 * Note that this is also checked after replacing the <> form.
5995 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5996 * it could be a character in the file.
5997 */
5998 if (do_key_code)
5999 {
6000 i = find_term_bykeys(src);
6001 if (i >= 0)
6002 {
6003 result[dlen++] = K_SPECIAL;
6004 result[dlen++] = termcodes[i].name[0];
6005 result[dlen++] = termcodes[i].name[1];
6006 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006007 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 continue;
6009 }
6010 }
6011
6012#ifdef FEAT_EVAL
6013 if (do_special)
6014 {
6015 char_u *p, *s, len;
6016
6017 /*
6018 * Replace <Leader> by the value of "mapleader".
6019 * Replace <LocalLeader> by the value of "maplocalleader".
6020 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6021 */
6022 if (STRNICMP(src, "<Leader>", 8) == 0)
6023 {
6024 len = 8;
6025 p = get_var_value((char_u *)"g:mapleader");
6026 }
6027 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6028 {
6029 len = 13;
6030 p = get_var_value((char_u *)"g:maplocalleader");
6031 }
6032 else
6033 {
6034 len = 0;
6035 p = NULL;
6036 }
6037 if (len != 0)
6038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006039 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6041 s = (char_u *)"\\";
6042 else
6043 s = p;
6044 while (*s != NUL)
6045 result[dlen++] = *s++;
6046 src += len;
6047 continue;
6048 }
6049 }
6050#endif
6051
6052 /*
6053 * Remove CTRL-V and ignore the next character.
6054 * For "from" side the CTRL-V at the end is included, for the "to"
6055 * part it is removed.
6056 * If 'cpoptions' does not contain 'B', also accept a backslash.
6057 */
6058 key = *src;
6059 if (key == Ctrl_V || (do_backslash && key == '\\'))
6060 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006061 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (*src == NUL)
6063 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006064 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 result[dlen++] = key;
6066 break;
6067 }
6068 }
6069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006070 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006071 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 /*
6074 * If the character is K_SPECIAL, replace it with K_SPECIAL
6075 * KS_SPECIAL KE_FILLER.
6076 * If compiled with the GUI replace CSI with K_CSI.
6077 */
6078 if (*src == K_SPECIAL)
6079 {
6080 result[dlen++] = K_SPECIAL;
6081 result[dlen++] = KS_SPECIAL;
6082 result[dlen++] = KE_FILLER;
6083 }
6084# ifdef FEAT_GUI
6085 else if (*src == CSI)
6086 {
6087 result[dlen++] = K_SPECIAL;
6088 result[dlen++] = KS_EXTRA;
6089 result[dlen++] = (int)KE_CSI;
6090 }
6091# endif
6092 else
6093 result[dlen++] = *src;
6094 ++src;
6095 }
6096 }
6097 result[dlen] = NUL;
6098
6099 /*
6100 * Copy the new string to allocated memory.
6101 * If this fails, just return from.
6102 */
6103 if ((*bufp = vim_strsave(result)) != NULL)
6104 from = *bufp;
6105 vim_free(result);
6106 return from;
6107}
6108
6109/*
6110 * Find a termcode with keys 'src' (must be NUL terminated).
6111 * Return the index in termcodes[], or -1 if not found.
6112 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006113 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006114find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006117 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119 for (i = 0; i < tc_len; ++i)
6120 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006121 if (slen == termcodes[i].len
6122 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 return i;
6124 }
6125 return -1;
6126}
6127
6128/*
6129 * Gather the first characters in the terminal key codes into a string.
6130 * Used to speed up check_termcode().
6131 */
6132 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006133gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135 int i;
6136 int len = 0;
6137
6138#ifdef FEAT_GUI
6139 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006140 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141#endif
6142#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006143 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006144 termleader[len++] = DCS; // the termcode response starts with DCS
6145 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#endif
6147 termleader[len] = NUL;
6148
6149 for (i = 0; i < tc_len; ++i)
6150 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6151 {
6152 termleader[len++] = termcodes[i].code[0];
6153 termleader[len] = NUL;
6154 }
6155
6156 need_gather = FALSE;
6157}
6158
6159/*
6160 * Show all termcodes (for ":set termcap")
6161 * This code looks a lot like showoptions(), but is different.
6162 */
6163 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006164show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 int col;
6167 int *items;
6168 int item_count;
6169 int run;
6170 int row, rows;
6171 int cols;
6172 int i;
6173 int len;
6174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006175#define INC3 27 // try to make three columns
6176#define INC2 40 // try to make two columns
6177#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006179 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006181 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (items == NULL)
6183 return;
6184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006185 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006186 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 /*
6189 * do the loop two times:
6190 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006191 * 2. display the medium items (medium length strings)
6192 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006194 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
6196 /*
6197 * collect the items in items[]
6198 */
6199 item_count = 0;
6200 for (i = 0; i < tc_len; i++)
6201 {
6202 len = show_one_termcode(termcodes[i].name,
6203 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006204 if (len <= INC3 - GAP ? run == 1
6205 : len <= INC2 - GAP ? run == 2
6206 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 items[item_count++] = i;
6208 }
6209
6210 /*
6211 * display the items
6212 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006213 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006215 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (cols == 0)
6217 cols = 1;
6218 rows = (item_count + cols - 1) / cols;
6219 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006220 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 rows = item_count;
6222 for (row = 0; row < rows && !got_int; ++row)
6223 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006224 msg_putchar('\n'); // go to next line
6225 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 break;
6227 col = 0;
6228 for (i = row; i < item_count; i += rows)
6229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006230 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 show_one_termcode(termcodes[items[i]].name,
6232 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006233 if (run == 2)
6234 col += INC2;
6235 else
6236 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238 out_flush();
6239 ui_breakcheck();
6240 }
6241 }
6242 vim_free(items);
6243}
6244
6245/*
6246 * Show one termcode entry.
6247 * Output goes into IObuff[]
6248 */
6249 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006250show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 char_u *p;
6253 int len;
6254
6255 if (name[0] > '~')
6256 {
6257 IObuff[0] = ' ';
6258 IObuff[1] = ' ';
6259 IObuff[2] = ' ';
6260 IObuff[3] = ' ';
6261 }
6262 else
6263 {
6264 IObuff[0] = 't';
6265 IObuff[1] = '_';
6266 IObuff[2] = name[0];
6267 IObuff[3] = name[1];
6268 }
6269 IObuff[4] = ' ';
6270
6271 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6272 if (p[1] != 't')
6273 STRCPY(IObuff + 5, p);
6274 else
6275 IObuff[5] = NUL;
6276 len = (int)STRLEN(IObuff);
6277 do
6278 IObuff[len++] = ' ';
6279 while (len < 17);
6280 IObuff[len] = NUL;
6281 if (code == NULL)
6282 len += 4;
6283 else
6284 len += vim_strsize(code);
6285
6286 if (printit)
6287 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006288 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006290 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 else
6292 msg_outtrans(code);
6293 }
6294 return len;
6295}
6296
6297#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6298/*
6299 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6300 * termcap codes from the terminal itself.
6301 * We get them one by one to avoid a very long response string.
6302 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006303static int xt_index_in = 0;
6304static int xt_index_out = 0;
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006307req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 xt_index_out = 0;
6310 xt_index_in = 0;
6311 req_more_codes_from_term();
6312}
6313
6314 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006315req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 char buf[11];
6318 int old_idx = xt_index_out;
6319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006320 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (exiting)
6322 return;
6323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006324 // Send up to 10 more requests out than we received. Avoid sending too
6325 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6327 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006328 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006329
Bram Moolenaar86394aa2020-09-05 14:27:24 +02006330#ifdef FEAT_JOB_CHANNEL
6331 ch_log_output = TRUE;
6332#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02006333 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6334 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 out_str_nf((char_u *)buf);
6336 ++xt_index_out;
6337 }
6338
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006339 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if (xt_index_out != old_idx)
6341 out_flush();
6342}
6343
6344/*
6345 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6346 * A "0" instead of the "1" indicates a code that isn't supported.
6347 * Both <name> and <string> are encoded in hex.
6348 * "code" points to the "0" or "1".
6349 */
6350 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006351got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353#define XT_LEN 100
6354 char_u name[3];
6355 char_u str[XT_LEN];
6356 int i;
6357 int j = 0;
6358 int c;
6359
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // A '1' means the code is supported, a '0' means it isn't.
6361 // When half the length is > XT_LEN we can't use it.
6362 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6364 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006365 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 name[0] = hexhex2nr(code + 3);
6367 name[1] = hexhex2nr(code + 5);
6368 name[2] = NUL;
6369 for (i = 0; key_names[i] != NULL; ++i)
6370 {
6371 if (STRCMP(key_names[i], name) == 0)
6372 {
6373 xt_index_in = i;
6374 break;
6375 }
6376 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006377
Bram Moolenaarb255b902018-04-24 21:40:10 +02006378 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (key_names[i] != NULL)
6381 {
6382 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6383 str[j++] = c;
6384 str[j] = NUL;
6385 if (name[0] == 'C' && name[1] == 'o')
6386 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006387 // Color count is not a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006389 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391 else
6392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006393 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 i = find_term_bykeys(str);
6395 if (i >= 0)
6396 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006397 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 }
6400 }
6401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 ++xt_index_in;
6404 req_more_codes_from_term();
6405}
6406
6407/*
6408 * Check if there are any unanswered requests and deal with them.
6409 * This is called before starting an external program or getting direct
6410 * keyboard input. We don't want responses to be send to that program or
6411 * handled as typed text.
6412 */
6413 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006414check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int c;
6417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006418 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6420 return;
6421
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006422 // Vgetc() will check for and handle any response.
6423 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 ++no_mapping;
6425 ++allow_keys;
6426 for (;;)
6427 {
6428 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006429 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 break;
6431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006432 // If a response is recognized it's replaced with K_IGNORE, must read
6433 // it from the input stream. If there is no K_IGNORE we can't do
6434 // anything, break here (there might be some responses further on, but
6435 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (c != K_SPECIAL && c != K_IGNORE)
6437 break;
6438 c = vgetc();
6439 if (c != K_IGNORE)
6440 {
6441 vungetc(c);
6442 break;
6443 }
6444 }
6445 --no_mapping;
6446 --allow_keys;
6447}
6448#endif
6449
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006450#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451static char ksme_str[20];
6452static char ksmr_str[20];
6453static char ksmd_str[20];
6454
6455/*
6456 * For Win32 console: update termcap codes for existing console attributes.
6457 */
6458 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006459update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 struct builtin_term *p;
6462
6463 p = find_builtin_term(DEFAULT_TERM);
6464 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6465 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006466 attr | 0x08); // FOREGROUND_INTENSITY
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6468 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6469
6470 while (p->bt_string != NULL)
6471 {
6472 if (p->bt_entry == (int)KS_ME)
6473 p->bt_string = &ksme_str[0];
6474 else if (p->bt_entry == (int)KS_MR)
6475 p->bt_string = &ksmr_str[0];
6476 else if (p->bt_entry == (int)KS_MD)
6477 p->bt_string = &ksmd_str[0];
6478 ++p;
6479 }
6480}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006481
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006482# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006483# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006484struct ks_tbl_s
6485{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006486 int code; // value of KS_
6487 char *vtp; // code in vtp mode
6488 char *vtp2; // code in vtp2 mode
6489 char buf[KSSIZE]; // save buffer in non-vtp mode
6490 char vbuf[KSSIZE]; // save buffer in vtp mode
6491 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6492 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006493};
6494
6495static struct ks_tbl_s ks_tbl[] =
6496{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006497 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6498 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6499 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6500 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6501 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6502 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6503 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6504 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6505 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006506# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006507 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6508 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006509 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6510 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006511# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006512 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6513 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006514 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6515 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006516# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006517 {(int)KS_CCO, "256", "256"}, // colors
6518 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006519};
6520
6521 static struct builtin_term *
6522find_first_tcap(
6523 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006524 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006525{
6526 struct builtin_term *p;
6527
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006528 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006529 if (p->bt_entry == code)
6530 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006531 return NULL;
6532}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006533# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006534
6535/*
6536 * For Win32 console: replace the sequence immediately after termguicolors.
6537 */
6538 void
6539swap_tcap(void)
6540{
6541# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006542 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006543 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006544 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006545 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006546 int mode;
6547 enum
6548 {
6549 CMODEINDEX,
6550 CMODE24,
6551 CMODE256
6552 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006554 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006555 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006556 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006557 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006558 {
6559 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006560 if (bt != NULL)
6561 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006562 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6563 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6564 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6565
6566 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6567 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006568 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006569 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006570 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006571 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006572 }
6573
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006574 if (p_tgc)
6575 mode = CMODE24;
6576 else if (t_colors >= 256)
6577 mode = CMODE256;
6578 else
6579 mode = CMODEINDEX;
6580
6581 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006582 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006583 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6584 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006585 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006586 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006587 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006588 case CMODEINDEX:
6589 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6590 break;
6591 case CMODE24:
6592 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6593 break;
6594 default:
6595 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006596 }
6597 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006598 }
6599
6600 if (mode != curr_mode)
6601 {
6602 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006603 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006604 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6605 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006606 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006607 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006608 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006609 case CMODEINDEX:
6610 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6611 break;
6612 case CMODE24:
6613 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6614 break;
6615 default:
6616 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006617 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006618 }
6619 }
6620
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006621 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006622 }
6623# endif
6624}
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006627
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006628#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006629 static int
6630hex_digit(int c)
6631{
6632 if (isdigit(c))
6633 return c - '0';
6634 c = TOLOWER_ASC(c);
6635 if (c >= 'a' && c <= 'f')
6636 return c - 'a' + 10;
6637 return 0x1ffffff;
6638}
6639
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006640# ifdef VIMDLL
6641 static guicolor_T
6642gui_adjust_rgb(guicolor_T c)
6643{
6644 if (gui.in_use)
6645 return c;
6646 else
6647 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6648}
6649# else
6650# define gui_adjust_rgb(c) (c)
6651# endif
6652
Bram Moolenaarab302212016-04-26 20:59:29 +02006653 guicolor_T
6654gui_get_color_cmn(char_u *name)
6655{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006656 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6657 // values as used by the MS-Windows GDI api. It should be used only for
6658 // MS-Windows GDI builds.
Bram Moolenaar4f974752019-02-17 17:44:42 +01006659# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006660# undef RGB
6661# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006662# ifndef RGB
6663# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6664# endif
6665# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006666 FILE *fd;
6667 char line[LINE_LEN];
6668 char_u *fname;
6669 int r, g, b, i;
6670 guicolor_T color;
6671
6672 struct rgbcolor_table_S {
6673 char_u *color_name;
6674 guicolor_T color;
6675 };
6676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006677 // Only non X11 colors (not present in rgb.txt) and colors in
6678 // color_names[], useful when $VIMRUNTIME is not found,.
Bram Moolenaarab302212016-04-26 20:59:29 +02006679 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006680 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6681 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6682 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6683 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6684 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6685 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6686 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6687 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6688 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6689 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6690 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006691 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006692 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006693 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6694 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006695 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006696 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006697 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006698 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6699 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6700 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6701 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6702 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006703 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
6704 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006705 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6706 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006707 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006708 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006709 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6710 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006711 };
6712
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006713 static struct rgbcolor_table_S *colornames_table;
6714 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006715
6716 if (name[0] == '#' && STRLEN(name) == 7)
6717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006718 // Name is in "#rrggbb" format
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006719 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006720 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6721 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6722 if (color > 0xffffff)
6723 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006724 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006725 }
6726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006727 // Check if the name is one of the colors we know
K.Takataeeec2542021-06-02 13:28:16 +02006728 for (i = 0; i < (int)ARRAY_LENGTH(rgb_table); i++)
Bram Moolenaarab302212016-04-26 20:59:29 +02006729 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006730 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006731
6732 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006733 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006734 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006735 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006736 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006737 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006738
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006739 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006740 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6741 if (fname == NULL)
6742 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006743
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006744 fd = fopen((char *)fname, "rt");
6745 vim_free(fname);
6746 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006747 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006748 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006749 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006750 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006751 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006752 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006753
6754 for (counting = 1; counting >= 0; --counting)
6755 {
6756 if (!counting)
6757 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006758 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006759 if (colornames_table == NULL)
6760 {
6761 fclose(fd);
6762 return INVALCOLOR;
6763 }
6764 rewind(fd);
6765 }
6766 size = 0;
6767
6768 while (!feof(fd))
6769 {
6770 size_t len;
6771 int pos;
6772
Bram Moolenaar42335f52018-09-13 15:33:43 +02006773 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006774 len = strlen(line);
6775
6776 if (len <= 1 || line[len - 1] != '\n')
6777 continue;
6778
6779 line[len - 1] = '\0';
6780
6781 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6782 if (i != 3)
6783 continue;
6784
6785 if (!counting)
6786 {
6787 char_u *s = vim_strsave((char_u *)line + pos);
6788
6789 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006790 {
6791 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006792 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006793 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006794 colornames_table[size].color_name = s;
6795 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6796 }
6797 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006798
6799 // The distributed rgb.txt has less than 1000 entries. Limit to
6800 // 10000, just in case the file was messed up.
6801 if (size == 10000)
6802 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006803 }
6804 }
6805 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006806 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006807
6808 for (i = 0; i < size; i++)
6809 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006810 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006811
Bram Moolenaarab302212016-04-26 20:59:29 +02006812 return INVALCOLOR;
6813}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006814
6815 guicolor_T
6816gui_get_rgb_color_cmn(int r, int g, int b)
6817{
6818 guicolor_T color = RGB(r, g, b);
6819
6820 if (color > 0xffffff)
6821 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006822 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006823}
Bram Moolenaarab302212016-04-26 20:59:29 +02006824#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006825
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006826#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006827 || defined(PROTO)
6828static int cube_value[] = {
6829 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6830};
6831
6832static int grey_ramp[] = {
6833 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6834 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6835};
6836
Bram Moolenaar9894e392018-05-05 14:29:06 +02006837static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006838// R G B idx
6839 { 0, 0, 0, 1}, // black
6840 {224, 0, 0, 2}, // dark red
6841 { 0, 224, 0, 3}, // dark green
6842 {224, 224, 0, 4}, // dark yellow / brown
6843 { 0, 0, 224, 5}, // dark blue
6844 {224, 0, 224, 6}, // dark magenta
6845 { 0, 224, 224, 7}, // dark cyan
6846 {224, 224, 224, 8}, // light grey
6847
6848 {128, 128, 128, 9}, // dark grey
6849 {255, 64, 64, 10}, // light red
6850 { 64, 255, 64, 11}, // light green
6851 {255, 255, 64, 12}, // yellow
6852 { 64, 64, 255, 13}, // light blue
6853 {255, 64, 255, 14}, // light magenta
6854 { 64, 255, 255, 15}, // light cyan
6855 {255, 255, 255, 16}, // white
6856};
6857
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006858#define ANSI_INDEX_NONE 0
6859
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006860 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006861cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006862{
6863 int idx;
6864
6865 if (nr < 16)
6866 {
6867 *r = ansi_table[nr][0];
6868 *g = ansi_table[nr][1];
6869 *b = ansi_table[nr][2];
6870 *ansi_idx = ansi_table[nr][3];
6871 }
6872 else if (nr < 232)
6873 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006874 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006875 idx = nr - 16;
6876 *r = cube_value[idx / 36 % 6];
6877 *g = cube_value[idx / 6 % 6];
6878 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006879 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006880 }
6881 else if (nr < 256)
6882 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006883 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006884 idx = nr - 232;
6885 *r = grey_ramp[idx];
6886 *g = grey_ramp[idx];
6887 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006888 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006889 }
6890 else
6891 {
6892 *r = 0;
6893 *g = 0;
6894 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006895 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006896 }
6897}
6898#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006899
Bram Moolenaarf4140482020-02-15 23:06:45 +01006900/*
6901 * Replace K_BS by <BS> and K_DEL by <DEL>
6902 */
6903 void
6904term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6905{
6906 int i;
6907 int c;
6908
6909 for (i = ta_len; i < ta_len + len; ++i)
6910 {
6911 if (ta_buf[i] == CSI && len - i > 2)
6912 {
6913 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6914 if (c == K_DEL || c == K_KDEL || c == K_BS)
6915 {
6916 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6917 (size_t)(len - i - 2));
6918 if (c == K_DEL || c == K_KDEL)
6919 ta_buf[i] = DEL;
6920 else
6921 ta_buf[i] = Ctrl_H;
6922 len -= 2;
6923 }
6924 }
6925 else if (ta_buf[i] == '\r')
6926 ta_buf[i] = '\n';
6927 if (has_mbyte)
6928 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6929 }
6930}