blob: bcd2184f4fb58880c5ad74b1640cec5e5525edb4 [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
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
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
74/* start of keys that are not directly used by Vim but can be mapped */
75#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
85#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000086 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
87 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010088static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static void del_termcode_idx(int idx);
91static int term_is_builtin(char_u *name);
92static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010095static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
97/*
98 * Here is our own prototype for tgetstr(), any prototypes from the include
99 * files have been disabled by the define at the start of this file.
100 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
103# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200104 /* Change this to "if 1" to debug what happens with termresponse. */
105# if 0
106# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200107static void log_tr(const char *fmt, ...);
108# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200110# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200111# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200112
Bram Moolenaarafd78262019-05-10 23:10:31 +0200113typedef enum {
114 STATUS_GET, // send request when switching to RAW mode
115 STATUS_SENT, // did send request, checking for response
116 STATUS_GOT, // received response
117 STATUS_FAIL // timed out
118} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200119
Bram Moolenaarafd78262019-05-10 23:10:31 +0200120typedef struct {
121 request_progress_T tr_progress;
122 time_t tr_start; // when request was sent, -1 for never
123} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200124
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125# define TERMREQUEST_INIT {STATUS_GET, -1}
126
127// Request Terminal Version status:
128static termrequest_T crv_status = TERMREQUEST_INIT;
129
130// Request Cursor position report:
131static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200132
Bram Moolenaar9377df32017-10-15 13:22:01 +0200133# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134// Request foreground color report:
135static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200136static int fg_r = 0;
137static int fg_g = 0;
138static int fg_b = 0;
139static int bg_r = 255;
140static int bg_g = 255;
141static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200142# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200143
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200144/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200145static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200146
Bram Moolenaar4db25542017-08-28 22:43:05 +0200147/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200148static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200149
150/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200151static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100152
153/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200154static termrequest_T winpos_status = TERMREQUEST_INIT;
155
156static termrequest_T *all_termrequests[] = {
157 &crv_status,
158 &u7_status,
159# ifdef FEAT_TERMINAL
160 &rfg_status,
161# endif
162 &rbg_status,
163 &rbm_status,
164 &rcs_status,
165 &winpos_status,
166 NULL
167};
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 Moolenaar071d4272004-06-13 20:20:40 +0000195#endif /* HAVE_TGETENT */
196
197static int detected_8bit = FALSE; /* detected 8-bit terminal */
198
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200199#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200200/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200201 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203
204/* The blink flag from the style response may be inverted from the actual
205 * blinking state, xterm XORs the flags. */
206static int initial_cursor_shape_blink = FALSE;
207
208/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200209static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200210#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200211
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000212static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214
215#if defined(FEAT_GUI)
216/*
217 * GUI pseudo term-cap.
218 */
219 {(int)KS_NAME, "gui"},
220 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
221 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
222# ifdef TERMINFO
223 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
224# else
225 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
226# endif
227 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
228# ifdef TERMINFO
229 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
230 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# else
233 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
234 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
237 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
238 /* attributes switched on with 'h', off with * 'H' */
239 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
240 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
241 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
242 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
243 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
244 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
245 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000246 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
247 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200248 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
249 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
251 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
252 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
253 {(int)KS_MS, "y"},
254 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100255 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {(int)KS_LE, "\b"}, /* cursor-left = BS */
257 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
258# ifdef TERMINFO
259 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
260# else
261 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
262# endif
263 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000264 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
266
267#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
270/*
271 * Amiga console window, default for Amiga
272 */
273 {(int)KS_NAME, "amiga"},
274 {(int)KS_CE, "\033[K"},
275 {(int)KS_CD, "\033[J"},
276 {(int)KS_AL, "\033[L"},
277# ifdef TERMINFO
278 {(int)KS_CAL, "\033[%p1%dL"},
279# else
280 {(int)KS_CAL, "\033[%dL"},
281# endif
282 {(int)KS_DL, "\033[M"},
283# ifdef TERMINFO
284 {(int)KS_CDL, "\033[%p1%dM"},
285# else
286 {(int)KS_CDL, "\033[%dM"},
287# endif
288 {(int)KS_CL, "\014"},
289 {(int)KS_VI, "\033[0 p"},
290 {(int)KS_VE, "\033[1 p"},
291 {(int)KS_ME, "\033[0m"},
292 {(int)KS_MR, "\033[7m"},
293 {(int)KS_MD, "\033[1m"},
294 {(int)KS_SE, "\033[0m"},
295 {(int)KS_SO, "\033[33m"},
296 {(int)KS_US, "\033[4m"},
297 {(int)KS_UE, "\033[0m"},
298 {(int)KS_CZH, "\033[3m"},
299 {(int)KS_CZR, "\033[0m"},
300#if defined(__MORPHOS__) || defined(__AROS__)
301 {(int)KS_CCO, "8"}, /* allow 8 colors */
302# ifdef TERMINFO
303 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
304 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
305# else
306 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
307 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
308# endif
309 {(int)KS_OP, "\033[m"}, /* reset colors */
310#endif
311 {(int)KS_MS, "y"},
312 {(int)KS_UT, "y"}, /* guessed */
313 {(int)KS_LE, "\b"},
314# ifdef TERMINFO
315 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
316# else
317 {(int)KS_CM, "\033[%i%d;%dH"},
318# endif
319#if defined(__MORPHOS__)
320 {(int)KS_SR, "\033M"},
321#endif
322# ifdef TERMINFO
323 {(int)KS_CRI, "\033[%p1%dC"},
324# else
325 {(int)KS_CRI, "\033[%dC"},
326# endif
327 {K_UP, "\233A"},
328 {K_DOWN, "\233B"},
329 {K_LEFT, "\233D"},
330 {K_RIGHT, "\233C"},
331 {K_S_UP, "\233T"},
332 {K_S_DOWN, "\233S"},
333 {K_S_LEFT, "\233 A"},
334 {K_S_RIGHT, "\233 @"},
335 {K_S_TAB, "\233Z"},
336 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
337 {K_F2, "\233\061~"},
338 {K_F3, "\233\062~"},
339 {K_F4, "\233\063~"},
340 {K_F5, "\233\064~"},
341 {K_F6, "\233\065~"},
342 {K_F7, "\233\066~"},
343 {K_F8, "\233\067~"},
344 {K_F9, "\233\070~"},
345 {K_F10, "\233\071~"},
346 {K_S_F1, "\233\061\060~"},
347 {K_S_F2, "\233\061\061~"},
348 {K_S_F3, "\233\061\062~"},
349 {K_S_F4, "\233\061\063~"},
350 {K_S_F5, "\233\061\064~"},
351 {K_S_F6, "\233\061\065~"},
352 {K_S_F7, "\233\061\066~"},
353 {K_S_F8, "\233\061\067~"},
354 {K_S_F9, "\233\061\070~"},
355 {K_S_F10, "\233\061\071~"},
356 {K_HELP, "\233?~"},
357 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
358 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
359 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
360 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
361 {K_END, "\233\064\065~"}, /* 101 key keyboard */
362
363 {BT_EXTRA_KEYS, ""},
364 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
365 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
366 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
367# endif
368
369# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
370/*
371 * almost standard ANSI terminal, default for bebox
372 */
373 {(int)KS_NAME, "beos-ansi"},
374 {(int)KS_CE, "\033[K"},
375 {(int)KS_CD, "\033[J"},
376 {(int)KS_AL, "\033[L"},
377# ifdef TERMINFO
378 {(int)KS_CAL, "\033[%p1%dL"},
379# else
380 {(int)KS_CAL, "\033[%dL"},
381# endif
382 {(int)KS_DL, "\033[M"},
383# ifdef TERMINFO
384 {(int)KS_CDL, "\033[%p1%dM"},
385# else
386 {(int)KS_CDL, "\033[%dM"},
387# endif
388#ifdef BEOS_PR_OR_BETTER
389# ifdef TERMINFO
390 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
391# else
392 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
393# endif
394#endif
395 {(int)KS_CL, "\033[H\033[2J"},
396#ifdef notyet
397 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
398 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
399#endif
400 {(int)KS_ME, "\033[m"}, /* normal mode */
401 {(int)KS_MR, "\033[7m"}, /* reverse */
402 {(int)KS_MD, "\033[1m"}, /* bold */
403 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
404 {(int)KS_SE, "\033[m"}, /* standout end */
405 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
406 {(int)KS_CZR, "\033[m"}, /* italic end */
407 {(int)KS_US, "\033[4m"}, /* underscore mode */
408 {(int)KS_UE, "\033[m"}, /* underscore end */
409 {(int)KS_CCO, "8"}, /* allow 8 colors */
410# ifdef TERMINFO
411 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
412 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
413# else
414 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
415 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
416# endif
417 {(int)KS_OP, "\033[m"}, /* reset colors */
418 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
419 {(int)KS_UT, "y"}, /* guessed */
420 {(int)KS_LE, "\b"},
421# ifdef TERMINFO
422 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
423# else
424 {(int)KS_CM, "\033[%i%d;%dH"},
425# endif
426 {(int)KS_SR, "\033M"},
427# ifdef TERMINFO
428 {(int)KS_CRI, "\033[%p1%dC"},
429# else
430 {(int)KS_CRI, "\033[%dC"},
431# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200432# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
436 {K_UP, "\033[A"},
437 {K_DOWN, "\033[B"},
438 {K_LEFT, "\033[D"},
439 {K_RIGHT, "\033[C"},
440# endif
441
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200442# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443/*
444 * standard ANSI terminal, default for unix
445 */
446 {(int)KS_NAME, "ansi"},
447 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
448 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
449# ifdef TERMINFO
450 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
451# else
452 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
453# endif
454 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
455# ifdef TERMINFO
456 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
457# else
458 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
459# endif
460 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
461 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
462 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
463 {(int)KS_MS, "y"},
464 {(int)KS_UT, "y"}, /* guessed */
465 {(int)KS_LE, "\b"},
466# ifdef TERMINFO
467 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
468# else
469 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
470# endif
471# ifdef TERMINFO
472 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
473# else
474 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
475# endif
476# endif
477
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200478# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479/*
480 * These codes are valid when nansi.sys or equivalent has been installed.
481 * Function keys on a PC are preceded with a NUL. These are converted into
482 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
483 * CTRL-arrow is used instead of SHIFT-arrow.
484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {(int)KS_NAME, "pcansi"},
486 {(int)KS_DL, "\033[M"},
487 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {(int)KS_CE, "\033[K"},
489 {(int)KS_CL, "\033[2J"},
490 {(int)KS_ME, "\033[0m"},
491 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
492 {(int)KS_MD, "\033[1m"}, /* bold: white text */
493 {(int)KS_SE, "\033[0m"}, /* standout end */
494 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
495 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
496 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
497 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
498 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
499 {(int)KS_CCO, "8"}, /* allow 8 colors */
500# ifdef TERMINFO
501 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
502 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
503# else
504 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
505 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
506# endif
507 {(int)KS_OP, "\033[0m"}, /* reset colors */
508 {(int)KS_MS, "y"},
509 {(int)KS_UT, "y"}, /* guessed */
510 {(int)KS_LE, "\b"},
511# ifdef TERMINFO
512 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
513# else
514 {(int)KS_CM, "\033[%i%d;%dH"},
515# endif
516# ifdef TERMINFO
517 {(int)KS_CRI, "\033[%p1%dC"},
518# else
519 {(int)KS_CRI, "\033[%dC"},
520# endif
521 {K_UP, "\316H"},
522 {K_DOWN, "\316P"},
523 {K_LEFT, "\316K"},
524 {K_RIGHT, "\316M"},
525 {K_S_LEFT, "\316s"},
526 {K_S_RIGHT, "\316t"},
527 {K_F1, "\316;"},
528 {K_F2, "\316<"},
529 {K_F3, "\316="},
530 {K_F4, "\316>"},
531 {K_F5, "\316?"},
532 {K_F6, "\316@"},
533 {K_F7, "\316A"},
534 {K_F8, "\316B"},
535 {K_F9, "\316C"},
536 {K_F10, "\316D"},
537 {K_F11, "\316\205"}, /* guessed */
538 {K_F12, "\316\206"}, /* guessed */
539 {K_S_F1, "\316T"},
540 {K_S_F2, "\316U"},
541 {K_S_F3, "\316V"},
542 {K_S_F4, "\316W"},
543 {K_S_F5, "\316X"},
544 {K_S_F6, "\316Y"},
545 {K_S_F7, "\316Z"},
546 {K_S_F8, "\316["},
547 {K_S_F9, "\316\\"},
548 {K_S_F10, "\316]"},
549 {K_S_F11, "\316\207"}, /* guessed */
550 {K_S_F12, "\316\210"}, /* guessed */
551 {K_INS, "\316R"},
552 {K_DEL, "\316S"},
553 {K_HOME, "\316G"},
554 {K_END, "\316O"},
555 {K_PAGEDOWN, "\316Q"},
556 {K_PAGEUP, "\316I"},
557# endif
558
Bram Moolenaar4f974752019-02-17 17:44:42 +0100559# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560/*
561 * These codes are valid for the Win32 Console . The entries that start with
562 * ESC | are translated into console calls in os_win32.c. The function keys
563 * are also translated in os_win32.c.
564 */
565 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100566 {(int)KS_CE, "\033|K"}, // clear to end of line
567 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100569 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100573 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
576 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100578 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
579 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100581 {(int)KS_CL, "\033|J"}, // clear screen
582 {(int)KS_CD, "\033|j"}, // clear to end of display
583 {(int)KS_VI, "\033|v"}, // cursor invisible
584 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_ME, "\033|0m"}, // normal
587 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
588 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100590 {(int)KS_SO, "\033|31m"}, // standout: white on blue
591 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100593 {(int)KS_SO, "\033|F"}, // standout: high intensity
594 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100596 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
597 {(int)KS_CZR, "\033|0m"}, // italic end
598 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
599 {(int)KS_UE, "\033|0m"}, // underscore end
600 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100602 {(int)KS_CAB, "\033|%p1%db"}, // set background color
603 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100605 {(int)KS_CAB, "\033|%db"}, // set background color
606 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607# endif
608
Bram Moolenaar6982f422019-02-16 16:48:01 +0100609 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100611 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {(int)KS_LE, "\b"},
613# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100614 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100616 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_VB, "\033|B"}, // visual bell
619 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
620 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100622 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100624 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100626# ifdef FEAT_TERMGUICOLORS
627 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
628 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 {K_UP, "\316H"},
632 {K_DOWN, "\316P"},
633 {K_LEFT, "\316K"},
634 {K_RIGHT, "\316M"},
635 {K_S_UP, "\316\304"},
636 {K_S_DOWN, "\316\317"},
637 {K_S_LEFT, "\316\311"},
638 {K_C_LEFT, "\316s"},
639 {K_S_RIGHT, "\316\313"},
640 {K_C_RIGHT, "\316t"},
641 {K_S_TAB, "\316\017"},
642 {K_F1, "\316;"},
643 {K_F2, "\316<"},
644 {K_F3, "\316="},
645 {K_F4, "\316>"},
646 {K_F5, "\316?"},
647 {K_F6, "\316@"},
648 {K_F7, "\316A"},
649 {K_F8, "\316B"},
650 {K_F9, "\316C"},
651 {K_F10, "\316D"},
652 {K_F11, "\316\205"},
653 {K_F12, "\316\206"},
654 {K_S_F1, "\316T"},
655 {K_S_F2, "\316U"},
656 {K_S_F3, "\316V"},
657 {K_S_F4, "\316W"},
658 {K_S_F5, "\316X"},
659 {K_S_F6, "\316Y"},
660 {K_S_F7, "\316Z"},
661 {K_S_F8, "\316["},
662 {K_S_F9, "\316\\"},
663 {K_S_F10, "\316]"},
664 {K_S_F11, "\316\207"},
665 {K_S_F12, "\316\210"},
666 {K_INS, "\316R"},
667 {K_DEL, "\316S"},
668 {K_HOME, "\316G"},
669 {K_S_HOME, "\316\302"},
670 {K_C_HOME, "\316w"},
671 {K_END, "\316O"},
672 {K_S_END, "\316\315"},
673 {K_C_END, "\316u"},
674 {K_PAGEDOWN, "\316Q"},
675 {K_PAGEUP, "\316I"},
676 {K_KPLUS, "\316N"},
677 {K_KMINUS, "\316J"},
678 {K_KMULTIPLY, "\316\067"},
679 {K_K0, "\316\332"},
680 {K_K1, "\316\336"},
681 {K_K2, "\316\342"},
682 {K_K3, "\316\346"},
683 {K_K4, "\316\352"},
684 {K_K5, "\316\356"},
685 {K_K6, "\316\362"},
686 {K_K7, "\316\366"},
687 {K_K8, "\316\372"},
688 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100689 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
691
692# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
693/*
694 * VT320 is working as an ANSI terminal compatible DEC terminal.
695 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * TODO:- rewrite ESC[ codes to CSI
697 * - keyboard languages (CSI ? 26 n)
698 */
699 {(int)KS_NAME, "vt320"},
700 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
701 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
702# ifdef TERMINFO
703 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
704# else
705 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
706# endif
707 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
708# ifdef TERMINFO
709 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
710# else
711 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
712# endif
713 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000714 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
715 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
717 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000718 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
719 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
720 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
721 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
722 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
723 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
724 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
725 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
726 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
727 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {(int)KS_MS, "y"},
729 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100730 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {(int)KS_LE, "\b"},
732# ifdef TERMINFO
733 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
734 ESC_STR "[%i%p1%d;%p2%dH")},
735# else
736 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
737# endif
738# ifdef TERMINFO
739 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
740# else
741 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
742# endif
743 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
744 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
745 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
746 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200747 // Note: cursor key sequences for application cursor mode are omitted,
748 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000749 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
750 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
751 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
752 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
753 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
755 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
756 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
757 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
758 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
761 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
762 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
763 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
764 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
765 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
766 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
767 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
768 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
769 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
770 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
771 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
772 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
773 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
774 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200775 // These sequences starting with <Esc> O may interfere with what the user
776 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
778 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
779 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
780 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
781 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200782 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
783 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
784 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
785 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
786 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
787 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
788 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
789 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
790 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
791 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
793# endif
794
795# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
796/*
797 * Ordinary vt52
798 */
799 {(int)KS_NAME, "vt52"},
800 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
801 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100802# ifdef TERMINFO
803 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
804 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
805# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100809 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
811 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {K_UP, IF_EB("\033A", ESC_STR "A")},
813 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
814 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
815 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100816 {K_F1, IF_EB("\033P", ESC_STR "P")},
817 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
818 {K_F3, IF_EB("\033R", ESC_STR "R")},
819# ifdef __MINT__
820 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
821 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
822 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
823 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
824 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
826 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
827 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
828 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {K_F4, IF_EB("\033S", ESC_STR "S")},
830 {K_F5, IF_EB("\033T", ESC_STR "T")},
831 {K_F6, IF_EB("\033U", ESC_STR "U")},
832 {K_F7, IF_EB("\033V", ESC_STR "V")},
833 {K_F8, IF_EB("\033W", ESC_STR "W")},
834 {K_F9, IF_EB("\033X", ESC_STR "X")},
835 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
836 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
837 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
838 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
839 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
840 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
841 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
842 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
843 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
844 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
845 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
846 {K_INS, IF_EB("\033I", ESC_STR "I")},
847 {K_HOME, IF_EB("\033E", ESC_STR "E")},
848 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
849 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
850# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_MS, "y"},
853# endif
854# endif
855
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200856# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200857 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
859 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
860# ifdef TERMINFO
861 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
862# else
863 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
864# endif
865 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
866# ifdef TERMINFO
867 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
868# else
869 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
870# endif
871# ifdef TERMINFO
872 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
873 ESC_STR "[%i%p1%d;%p2%dr")},
874# else
875 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
876# endif
877 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
878 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
879 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
880 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
881 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
882 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
883 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200884 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
885 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_MS, "y"},
887 {(int)KS_UT, "y"},
888 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200889 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
890 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200891 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200892 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200893# ifdef TERMINFO
894 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
895# else
896 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
897# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200898 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200899 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900# ifdef TERMINFO
901 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
902 ESC_STR "[%i%p1%d;%p2%dH")},
903# else
904 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
905# endif
906 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
907# ifdef TERMINFO
908 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
909# else
910 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
911# endif
912 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
913 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
914# ifdef FEAT_XTERM_SAVE
915 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
916 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
917 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
918# endif
919 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
920 {(int)KS_CIE, "\007"},
921 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
922 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200923 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
924 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925# ifdef TERMINFO
926 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
927 ESC_STR "[8;%p1%d;%p2%dt")},
928 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
929 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200930 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931# else
932 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
933 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200934 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# endif
936 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200937 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200938 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100939 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200940# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200941 /* These are printf strings, not terminal codes. */
942 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
943 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
944# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100945 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
946 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200947 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
948 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
949 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
950 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000951
952 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
953 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
954 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
955 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
956 /* An extra set of cursor keys for vt100 mode */
957 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
958 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
959 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
960 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000962 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
963 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
964 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
965 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000966 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
967 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
968 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
969 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
970 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
971 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
972 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
973 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
974 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
975 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
976 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
977 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000979 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
980 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
981 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
982 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000983 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
984 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000986 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000988 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000989 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
990 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000991 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000992 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000994 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
995 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100996 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
997 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
998 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
999 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1000 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1001 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +02001002 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
1003 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1004 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1005 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1006 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1007 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1008 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1009 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1010 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1011 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001012 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1013 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1014 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
1016 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001017 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1018 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1019 /* F14 and F15 are missing, because they send the same codes as the undo
1020 * and help key, although they don't work on all keyboards. */
1021 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1022 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1023 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1024 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1025 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1026
1027 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1028 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1029 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1030 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1031 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1032 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1033 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1034 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1035 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1036 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1037
1038 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1039 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1040 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1041 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1042 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1043 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1044 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045# endif
1046
1047# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1048/*
1049 * iris-ansi for Silicon Graphics machines.
1050 */
1051 {(int)KS_NAME, "iris-ansi"},
1052 {(int)KS_CE, "\033[K"},
1053 {(int)KS_CD, "\033[J"},
1054 {(int)KS_AL, "\033[L"},
1055# ifdef TERMINFO
1056 {(int)KS_CAL, "\033[%p1%dL"},
1057# else
1058 {(int)KS_CAL, "\033[%dL"},
1059# endif
1060 {(int)KS_DL, "\033[M"},
1061# ifdef TERMINFO
1062 {(int)KS_CDL, "\033[%p1%dM"},
1063# else
1064 {(int)KS_CDL, "\033[%dM"},
1065# endif
1066#if 0 /* The scroll region is not working as Vim expects. */
1067# ifdef TERMINFO
1068 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1069# else
1070 {(int)KS_CS, "\033[%i%d;%dr"},
1071# endif
1072#endif
1073 {(int)KS_CL, "\033[H\033[2J"},
1074 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1075 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1076 {(int)KS_TI, "\033[=6h"},
1077 {(int)KS_TE, "\033[=6l"},
1078 {(int)KS_SE, "\033[21;27m"},
1079 {(int)KS_SO, "\033[1;7m"},
1080 {(int)KS_ME, "\033[m"},
1081 {(int)KS_MR, "\033[7m"},
1082 {(int)KS_MD, "\033[1m"},
1083 {(int)KS_CCO, "8"}, /* allow 8 colors */
1084 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1085 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1086 {(int)KS_US, "\033[4m"}, /* underline on */
1087 {(int)KS_UE, "\033[24m"}, /* underline off */
1088# ifdef TERMINFO
1089 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1090 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1091 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1092 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1093# else
1094 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1095 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1096 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1097 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1098# endif
1099 {(int)KS_MS, "y"}, /* guessed */
1100 {(int)KS_UT, "y"}, /* guessed */
1101 {(int)KS_LE, "\b"},
1102# ifdef TERMINFO
1103 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1104# else
1105 {(int)KS_CM, "\033[%i%d;%dH"},
1106# endif
1107 {(int)KS_SR, "\033M"},
1108# ifdef TERMINFO
1109 {(int)KS_CRI, "\033[%p1%dC"},
1110# else
1111 {(int)KS_CRI, "\033[%dC"},
1112# endif
1113 {(int)KS_CIS, "\033P3.y"},
1114 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1115 {(int)KS_TS, "\033P1.y"},
1116 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1117# ifdef TERMINFO
1118 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1119 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1120# else
1121 {(int)KS_CWS, "\033[203;%d;%d/y"},
1122 {(int)KS_CWP, "\033[205;%d;%d/y"},
1123# endif
1124 {K_UP, "\033[A"},
1125 {K_DOWN, "\033[B"},
1126 {K_LEFT, "\033[D"},
1127 {K_RIGHT, "\033[C"},
1128 {K_S_UP, "\033[161q"},
1129 {K_S_DOWN, "\033[164q"},
1130 {K_S_LEFT, "\033[158q"},
1131 {K_S_RIGHT, "\033[167q"},
1132 {K_F1, "\033[001q"},
1133 {K_F2, "\033[002q"},
1134 {K_F3, "\033[003q"},
1135 {K_F4, "\033[004q"},
1136 {K_F5, "\033[005q"},
1137 {K_F6, "\033[006q"},
1138 {K_F7, "\033[007q"},
1139 {K_F8, "\033[008q"},
1140 {K_F9, "\033[009q"},
1141 {K_F10, "\033[010q"},
1142 {K_F11, "\033[011q"},
1143 {K_F12, "\033[012q"},
1144 {K_S_F1, "\033[013q"},
1145 {K_S_F2, "\033[014q"},
1146 {K_S_F3, "\033[015q"},
1147 {K_S_F4, "\033[016q"},
1148 {K_S_F5, "\033[017q"},
1149 {K_S_F6, "\033[018q"},
1150 {K_S_F7, "\033[019q"},
1151 {K_S_F8, "\033[020q"},
1152 {K_S_F9, "\033[021q"},
1153 {K_S_F10, "\033[022q"},
1154 {K_S_F11, "\033[023q"},
1155 {K_S_F12, "\033[024q"},
1156 {K_INS, "\033[139q"},
1157 {K_HOME, "\033[H"},
1158 {K_END, "\033[146q"},
1159 {K_PAGEUP, "\033[150q"},
1160 {K_PAGEDOWN, "\033[154q"},
1161# endif
1162
1163# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1164/*
1165 * for debugging
1166 */
1167 {(int)KS_NAME, "debug"},
1168 {(int)KS_CE, "[CE]"},
1169 {(int)KS_CD, "[CD]"},
1170 {(int)KS_AL, "[AL]"},
1171# ifdef TERMINFO
1172 {(int)KS_CAL, "[CAL%p1%d]"},
1173# else
1174 {(int)KS_CAL, "[CAL%d]"},
1175# endif
1176 {(int)KS_DL, "[DL]"},
1177# ifdef TERMINFO
1178 {(int)KS_CDL, "[CDL%p1%d]"},
1179# else
1180 {(int)KS_CDL, "[CDL%d]"},
1181# endif
1182# ifdef TERMINFO
1183 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1184# else
1185 {(int)KS_CS, "[%dCS%d]"},
1186# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001187# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001189# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191# endif
1192# ifdef TERMINFO
1193 {(int)KS_CAB, "[CAB%p1%d]"},
1194 {(int)KS_CAF, "[CAF%p1%d]"},
1195 {(int)KS_CSB, "[CSB%p1%d]"},
1196 {(int)KS_CSF, "[CSF%p1%d]"},
1197# else
1198 {(int)KS_CAB, "[CAB%d]"},
1199 {(int)KS_CAF, "[CAF%d]"},
1200 {(int)KS_CSB, "[CSB%d]"},
1201 {(int)KS_CSF, "[CSF%d]"},
1202# endif
1203 {(int)KS_OP, "[OP]"},
1204 {(int)KS_LE, "[LE]"},
1205 {(int)KS_CL, "[CL]"},
1206 {(int)KS_VI, "[VI]"},
1207 {(int)KS_VE, "[VE]"},
1208 {(int)KS_VS, "[VS]"},
1209 {(int)KS_ME, "[ME]"},
1210 {(int)KS_MR, "[MR]"},
1211 {(int)KS_MB, "[MB]"},
1212 {(int)KS_MD, "[MD]"},
1213 {(int)KS_SE, "[SE]"},
1214 {(int)KS_SO, "[SO]"},
1215 {(int)KS_UE, "[UE]"},
1216 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001217 {(int)KS_UCE, "[UCE]"},
1218 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001219 {(int)KS_STE, "[STE]"},
1220 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {(int)KS_MS, "[MS]"},
1222 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001223 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224# ifdef TERMINFO
1225 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1226# else
1227 {(int)KS_CM, "[%dCM%d]"},
1228# endif
1229 {(int)KS_SR, "[SR]"},
1230# ifdef TERMINFO
1231 {(int)KS_CRI, "[CRI%p1%d]"},
1232# else
1233 {(int)KS_CRI, "[CRI%d]"},
1234# endif
1235 {(int)KS_VB, "[VB]"},
1236 {(int)KS_KS, "[KS]"},
1237 {(int)KS_KE, "[KE]"},
1238 {(int)KS_TI, "[TI]"},
1239 {(int)KS_TE, "[TE]"},
1240 {(int)KS_CIS, "[CIS]"},
1241 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001242 {(int)KS_CSC, "[CSC]"},
1243 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {(int)KS_TS, "[TS]"},
1245 {(int)KS_FS, "[FS]"},
1246# ifdef TERMINFO
1247 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1248 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1249# else
1250 {(int)KS_CWS, "[%dCWS%d]"},
1251 {(int)KS_CWP, "[%dCWP%d]"},
1252# endif
1253 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001254 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001255 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001256 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {K_UP, "[KU]"},
1258 {K_DOWN, "[KD]"},
1259 {K_LEFT, "[KL]"},
1260 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001261 {K_XUP, "[xKU]"},
1262 {K_XDOWN, "[xKD]"},
1263 {K_XLEFT, "[xKL]"},
1264 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {K_S_UP, "[S-KU]"},
1266 {K_S_DOWN, "[S-KD]"},
1267 {K_S_LEFT, "[S-KL]"},
1268 {K_C_LEFT, "[C-KL]"},
1269 {K_S_RIGHT, "[S-KR]"},
1270 {K_C_RIGHT, "[C-KR]"},
1271 {K_F1, "[F1]"},
1272 {K_XF1, "[xF1]"},
1273 {K_F2, "[F2]"},
1274 {K_XF2, "[xF2]"},
1275 {K_F3, "[F3]"},
1276 {K_XF3, "[xF3]"},
1277 {K_F4, "[F4]"},
1278 {K_XF4, "[xF4]"},
1279 {K_F5, "[F5]"},
1280 {K_F6, "[F6]"},
1281 {K_F7, "[F7]"},
1282 {K_F8, "[F8]"},
1283 {K_F9, "[F9]"},
1284 {K_F10, "[F10]"},
1285 {K_F11, "[F11]"},
1286 {K_F12, "[F12]"},
1287 {K_S_F1, "[S-F1]"},
1288 {K_S_XF1, "[S-xF1]"},
1289 {K_S_F2, "[S-F2]"},
1290 {K_S_XF2, "[S-xF2]"},
1291 {K_S_F3, "[S-F3]"},
1292 {K_S_XF3, "[S-xF3]"},
1293 {K_S_F4, "[S-F4]"},
1294 {K_S_XF4, "[S-xF4]"},
1295 {K_S_F5, "[S-F5]"},
1296 {K_S_F6, "[S-F6]"},
1297 {K_S_F7, "[S-F7]"},
1298 {K_S_F8, "[S-F8]"},
1299 {K_S_F9, "[S-F9]"},
1300 {K_S_F10, "[S-F10]"},
1301 {K_S_F11, "[S-F11]"},
1302 {K_S_F12, "[S-F12]"},
1303 {K_HELP, "[HELP]"},
1304 {K_UNDO, "[UNDO]"},
1305 {K_BS, "[BS]"},
1306 {K_INS, "[INS]"},
1307 {K_KINS, "[KINS]"},
1308 {K_DEL, "[DEL]"},
1309 {K_KDEL, "[KDEL]"},
1310 {K_HOME, "[HOME]"},
1311 {K_S_HOME, "[C-HOME]"},
1312 {K_C_HOME, "[C-HOME]"},
1313 {K_KHOME, "[KHOME]"},
1314 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_END, "[END]"},
1317 {K_S_END, "[C-END]"},
1318 {K_C_END, "[C-END]"},
1319 {K_KEND, "[KEND]"},
1320 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001321 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {K_PAGEUP, "[PAGEUP]"},
1323 {K_PAGEDOWN, "[PAGEDOWN]"},
1324 {K_KPAGEUP, "[KPAGEUP]"},
1325 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1326 {K_MOUSE, "[MOUSE]"},
1327 {K_KPLUS, "[KPLUS]"},
1328 {K_KMINUS, "[KMINUS]"},
1329 {K_KDIVIDE, "[KDIVIDE]"},
1330 {K_KMULTIPLY, "[KMULTIPLY]"},
1331 {K_KENTER, "[KENTER]"},
1332 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001333 {K_PS, "[PASTE-START]"},
1334 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {K_K0, "[K0]"},
1336 {K_K1, "[K1]"},
1337 {K_K2, "[K2]"},
1338 {K_K3, "[K3]"},
1339 {K_K4, "[K4]"},
1340 {K_K5, "[K5]"},
1341 {K_K6, "[K6]"},
1342 {K_K7, "[K7]"},
1343 {K_K8, "[K8]"},
1344 {K_K9, "[K9]"},
1345# endif
1346
1347#endif /* NO_BUILTIN_TCAPS */
1348
1349/*
1350 * The most minimal terminal: only clear screen and cursor positioning
1351 * Always included.
1352 */
1353 {(int)KS_NAME, "dumb"},
1354 {(int)KS_CL, "\014"},
1355#ifdef TERMINFO
1356 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1357 ESC_STR "[%i%p1%d;%p2%dH")},
1358#else
1359 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1360#endif
1361
1362/*
1363 * end marker
1364 */
1365 {(int)KS_NAME, NULL}
1366
1367}; /* end of builtin_termcaps */
1368
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001369#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001371termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001372{
Bram Moolenaarab302212016-04-26 20:59:29 +02001373 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374}
1375
1376 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001377termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001378{
1379 guicolor_T t;
1380
1381 if (*name == NUL)
1382 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001383 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001384
1385 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001386 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001387 return t;
1388}
1389
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001390 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001391termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001392{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001393 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001394}
1395#endif
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/*
1398 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef AMIGA
1401# define DEFAULT_TERM (char_u *)"amiga"
1402#endif
1403
1404#ifdef MSWIN
1405# define DEFAULT_TERM (char_u *)"win32"
1406#endif
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#if defined(UNIX) && !defined(__MINT__)
1409# define DEFAULT_TERM (char_u *)"ansi"
1410#endif
1411
1412#ifdef __MINT__
1413# define DEFAULT_TERM (char_u *)"vt52"
1414#endif
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#ifdef VMS
1417# define DEFAULT_TERM (char_u *)"vt320"
1418#endif
1419
1420#ifdef __BEOS__
1421# undef DEFAULT_TERM
1422# define DEFAULT_TERM (char_u *)"beos-ansi"
1423#endif
1424
1425#ifndef DEFAULT_TERM
1426# define DEFAULT_TERM (char_u *)"dumb"
1427#endif
1428
1429/*
1430 * Term_strings contains currently used terminal output strings.
1431 * It is initialized with the default values by parse_builtin_tcap().
1432 * The values can be changed by setting the option with the same name.
1433 */
1434char_u *(term_strings[(int)KS_LAST + 1]);
1435
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001436static int need_gather = FALSE; /* need to fill termleader[] */
1437static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001439static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001440static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#endif
1442
1443 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001444find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 struct builtin_term *p;
1447
1448 p = builtin_termcaps;
1449 while (p->bt_string != NULL)
1450 {
1451 if (p->bt_entry == (int)KS_NAME)
1452 {
1453#ifdef UNIX
1454 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1455 return p;
1456 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1457 return p;
1458 else
1459#endif
1460#ifdef VMS
1461 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1462 return p;
1463 else
1464#endif
1465 if (STRCMP(term, p->bt_string) == 0)
1466 return p;
1467 }
1468 ++p;
1469 }
1470 return p;
1471}
1472
1473/*
1474 * Parsing of the builtin termcap entries.
1475 * Caller should check if 'name' is a valid builtin term.
1476 * The terminal's name is not set, as this is already done in termcapinit().
1477 */
1478 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001479parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 struct builtin_term *p;
1482 char_u name[2];
1483 int term_8bit;
1484
1485 p = find_builtin_term(term);
1486 term_8bit = term_is_8bit(term);
1487
1488 /* Do not parse if builtin term not found */
1489 if (p->bt_string == NULL)
1490 return;
1491
1492 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1493 {
1494 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1495 {
1496 /* Only set the value if it wasn't set yet. */
1497 if (term_strings[p->bt_entry] == NULL
1498 || term_strings[p->bt_entry] == empty_option)
1499 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001500#ifdef FEAT_EVAL
1501 int opt_idx = -1;
1502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 /* 8bit terminal: use CSI instead of <Esc>[ */
1504 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1505 {
1506 char_u *s, *t;
1507
1508 s = vim_strsave((char_u *)p->bt_string);
1509 if (s != NULL)
1510 {
1511 for (t = s; *t; ++t)
1512 if (term_7to8bit(t))
1513 {
1514 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001515 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001518#ifdef FEAT_EVAL
1519 opt_idx =
1520#endif
1521 set_term_option_alloced(
1522 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 }
1525 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001528#ifdef FEAT_EVAL
1529 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1530#endif
1531 }
1532#ifdef FEAT_EVAL
1533 set_term_option_sctx_idx(NULL, opt_idx);
1534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 }
1537 else
1538 {
1539 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1540 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1541 if (find_termcode(name) == NULL)
1542 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1543 }
1544 }
1545}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547/*
1548 * Set number of colors.
1549 * Store it as a number in t_colors.
1550 * Store it as a string in T_CCO (using nr_colors[]).
1551 */
1552 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001553set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 char_u nr_colors[20]; /* string for number of colors */
1556
1557 t_colors = nr;
1558 if (t_colors > 1)
1559 sprintf((char *)nr_colors, "%d", t_colors);
1560 else
1561 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001562 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001564
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001565#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001566/*
1567 * Set the color count to "val" and redraw if it changed.
1568 */
1569 static void
1570may_adjust_color_count(int val)
1571{
1572 if (val != t_colors)
1573 {
1574 /* Nr of colors changed, initialize highlighting and
1575 * redraw everything. This causes a redraw, which usually
1576 * clears the message. Try keeping the message if it
1577 * might work. */
1578 set_keep_msg_from_hist();
1579 set_color_count(val);
1580 init_highlight(TRUE, FALSE);
1581# ifdef DEBUG_TERMRESPONSE
1582 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001583 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001584
Bram Moolenaarb255b902018-04-24 21:40:10 +02001585 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001586 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001587#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001588 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001589#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001590 }
1591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#endif
1593
1594#ifdef HAVE_TGETENT
1595static char *(key_names[]) =
1596{
1597#ifdef FEAT_TERMRESPONSE
1598 /* Do this one first, it may cause a screen redraw. */
1599 "Co",
1600#endif
1601 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 "#2", "#4", "%i", "*7",
1603 "k1", "k2", "k3", "k4", "k5", "k6",
1604 "k7", "k8", "k9", "k;", "F1", "F2",
1605 "%1", "&8", "kb", "kI", "kD", "kh",
1606 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1607 NULL
1608};
1609#endif
1610
Bram Moolenaar9289df52018-05-10 14:40:57 +02001611#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001612 static void
1613get_term_entries(int *height, int *width)
1614{
1615 static struct {
1616 enum SpecialKey dest; /* index in term_strings[] */
1617 char *name; /* termcap name for string */
1618 } string_names[] =
1619 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1620 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1621 {KS_CL, "cl"}, {KS_CD, "cd"},
1622 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1623 {KS_ME, "me"}, {KS_MR, "mr"},
1624 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1625 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1626 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1627 {KS_STE,"Te"}, {KS_STS,"Ts"},
1628 {KS_CM, "cm"}, {KS_SR, "sr"},
1629 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1630 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1631 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1632 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1633 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1634 {KS_VS, "vs"}, {KS_CVS, "VS"},
1635 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1636 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1637 {KS_TS, "ts"}, {KS_FS, "fs"},
1638 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1639 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1640 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1641 {KS_8F, "8f"}, {KS_8B, "8b"},
1642 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1643 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001644 {KS_CST, "ST"}, {KS_CRT, "RT"},
1645 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001646 {(enum SpecialKey)0, NULL}
1647 };
1648 int i;
1649 char_u *p;
1650 static char_u tstrbuf[TBUFSZ];
1651 char_u *tp = tstrbuf;
1652
1653 /*
1654 * get output strings
1655 */
1656 for (i = 0; string_names[i].name != NULL; ++i)
1657 {
1658 if (TERM_STR(string_names[i].dest) == NULL
1659 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001660 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001661 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001662#ifdef FEAT_EVAL
1663 set_term_option_sctx_idx(string_names[i].name, -1);
1664#endif
1665 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001666 }
1667
1668 /* tgetflag() returns 1 if the flag is present, 0 if not and
1669 * possibly -1 if the flag doesn't exist. */
1670 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1671 T_MS = (char_u *)"y";
1672 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1673 T_XS = (char_u *)"y";
1674 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1675 T_XN = (char_u *)"y";
1676 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1677 T_DB = (char_u *)"y";
1678 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1679 T_DA = (char_u *)"y";
1680 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1681 T_UT = (char_u *)"y";
1682
1683 /*
1684 * get key codes
1685 */
1686 for (i = 0; key_names[i] != NULL; ++i)
1687 if (find_termcode((char_u *)key_names[i]) == NULL)
1688 {
1689 p = TGETSTR(key_names[i], &tp);
1690 /* if cursor-left == backspace, ignore it (televideo 925) */
1691 if (p != NULL
1692 && (*p != Ctrl_H
1693 || key_names[i][0] != 'k'
1694 || key_names[i][1] != 'l'))
1695 add_termcode((char_u *)key_names[i], p, FALSE);
1696 }
1697
1698 if (*height == 0)
1699 *height = tgetnum("li");
1700 if (*width == 0)
1701 *width = tgetnum("co");
1702
1703 /*
1704 * Get number of colors (if not done already).
1705 */
1706 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001707 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709#ifdef FEAT_EVAL
1710 set_term_option_sctx_idx("Co", -1);
1711#endif
1712 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713
1714# ifndef hpux
1715 BC = (char *)TGETSTR("bc", &tp);
1716 UP = (char *)TGETSTR("up", &tp);
1717 p = TGETSTR("pc", &tp);
1718 if (p)
1719 PC = *p;
1720# endif
1721}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001722#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723
1724 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001725report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001726{
1727 struct builtin_term *termp;
1728
1729 mch_errmsg("\r\n");
1730 if (error_msg != NULL)
1731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001732 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 mch_errmsg("\r\n");
1734 }
1735 mch_errmsg("'");
1736 mch_errmsg((char *)term);
1737 mch_errmsg(_("' not known. Available builtin terminals are:"));
1738 mch_errmsg("\r\n");
1739 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1740 {
1741 if (termp->bt_entry == (int)KS_NAME)
1742 {
1743#ifdef HAVE_TGETENT
1744 mch_errmsg(" builtin_");
1745#else
1746 mch_errmsg(" ");
1747#endif
1748 mch_errmsg(termp->bt_string);
1749 mch_errmsg("\r\n");
1750 }
1751 }
1752}
1753
1754 static void
1755report_default_term(char_u *term)
1756{
1757 mch_errmsg(_("defaulting to '"));
1758 mch_errmsg((char *)term);
1759 mch_errmsg("'\r\n");
1760 if (emsg_silent == 0)
1761 {
1762 screen_start(); /* don't know where cursor is now */
1763 out_flush();
1764 if (!is_not_a_term())
1765 ui_delay(2000L, TRUE);
1766 }
1767}
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
1770 * Set terminal options for terminal "term".
1771 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1772 *
1773 * While doing this, until ttest(), some options may be NULL, be careful.
1774 */
1775 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001776set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 struct builtin_term *termp;
1779#ifdef HAVE_TGETENT
1780 int builtin_first = p_tbi;
1781 int try;
1782 int termcap_cleared = FALSE;
1783#endif
1784 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 char_u *bs_p, *del_p;
1787
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001788 /* In silect mode (ex -s) we don't use the 'term' option. */
1789 if (silent_mode)
1790 return OK;
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 detected_8bit = FALSE; /* reset 8-bit detection */
1793
1794 if (term_is_builtin(term))
1795 {
1796 term += 8;
1797#ifdef HAVE_TGETENT
1798 builtin_first = 1;
1799#endif
1800 }
1801
1802/*
1803 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1804 * If builtin_first is TRUE:
1805 * 0. try builtin termcap
1806 * 1. try external termcap
1807 * 2. if both fail default to a builtin terminal
1808 * If builtin_first is FALSE:
1809 * 1. try external termcap
1810 * 2. try builtin termcap, if both fail default to a builtin terminal
1811 */
1812#ifdef HAVE_TGETENT
1813 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1814 {
1815 /*
1816 * Use external termcap
1817 */
1818 if (try == 1)
1819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 /*
1823 * If the external termcap does not have a matching entry, try the
1824 * builtin ones.
1825 */
1826 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!termcap_cleared)
1829 {
1830 clear_termoptions(); /* clear old options */
1831 termcap_cleared = TRUE;
1832 }
1833
Bram Moolenaar69e05692018-05-10 14:11:52 +02001834 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836 }
1837 else /* try == 0 || try == 2 */
1838#endif /* HAVE_TGETENT */
1839 /*
1840 * Use builtin termcap
1841 */
1842 {
1843#ifdef HAVE_TGETENT
1844 /*
1845 * If builtin termcap was already used, there is no need to search
1846 * for the builtin termcap again, quit now.
1847 */
1848 if (try == 2 && builtin_first && termcap_cleared)
1849 break;
1850#endif
1851 /*
1852 * search for 'term' in builtin_termcaps[]
1853 */
1854 termp = find_builtin_term(term);
1855 if (termp->bt_string == NULL) /* did not find it */
1856 {
1857#ifdef HAVE_TGETENT
1858 /*
1859 * If try == 0, first try the external termcap. If that is not
1860 * found we'll get back here with try == 2.
1861 * If termcap_cleared is set we used the external termcap,
1862 * don't complain about not finding the term in the builtin
1863 * termcap.
1864 */
1865 if (try == 0) /* try external one */
1866 continue;
1867 if (termcap_cleared) /* found in external termcap */
1868 break;
1869#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001870 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 /* when user typed :set term=xxx, quit here */
1873 if (starting != NO_SCREEN)
1874 {
1875 screen_start(); /* don't know where cursor is now */
1876 wait_return(TRUE);
1877 return FAIL;
1878 }
1879 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001880 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001881 set_string_option_direct((char_u *)"term", -1, term,
1882 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 display_errors();
1884 }
1885 out_flush();
1886#ifdef HAVE_TGETENT
1887 if (!termcap_cleared)
1888 {
1889#endif
1890 clear_termoptions(); /* clear old options */
1891#ifdef HAVE_TGETENT
1892 termcap_cleared = TRUE;
1893 }
1894#endif
1895 parse_builtin_tcap(term);
1896#ifdef FEAT_GUI
1897 if (term_is_gui(term))
1898 {
1899 out_flush();
1900 gui_init();
1901 /* If starting the GUI failed, don't do any of the other
1902 * things for this terminal */
1903 if (!gui.in_use)
1904 return FAIL;
1905#ifdef HAVE_TGETENT
1906 break; /* don't try using external termcap */
1907#endif
1908 }
1909#endif /* FEAT_GUI */
1910 }
1911#ifdef HAVE_TGETENT
1912 }
1913#endif
1914
1915/*
1916 * special: There is no info in the termcap about whether the cursor
1917 * positioning is relative to the start of the screen or to the start of the
1918 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1919 * relative.
1920 */
1921 if (STRCMP(term, "pcterm") == 0)
1922 T_CCS = (char_u *)"yes";
1923 else
1924 T_CCS = empty_option;
1925
1926#ifdef UNIX
1927/*
1928 * Any "stty" settings override the default for t_kb from the termcap.
1929 * This is in os_unix.c, because it depends a lot on the version of unix that
1930 * is being used.
1931 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1932 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001933# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 get_stty();
1937#endif
1938
1939/*
1940 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1941 * didn't work, use the default CTRL-H
1942 * The default for t_kD is DEL, unless t_kb is DEL.
1943 * The vim_strsave'd strings are probably lost forever, well it's only two
1944 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1945 * directly.
1946 */
1947#ifdef FEAT_GUI
1948 if (!gui.in_use)
1949#endif
1950 {
1951 bs_p = find_termcode((char_u *)"kb");
1952 del_p = find_termcode((char_u *)"kD");
1953 if (bs_p == NULL || *bs_p == NUL)
1954 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1955 if ((del_p == NULL || *del_p == NUL) &&
1956 (bs_p == NULL || *bs_p != DEL))
1957 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1958 }
1959
1960#if defined(UNIX) || defined(VMS)
1961 term_is_xterm = vim_is_xterm(term);
1962#endif
1963
1964#ifdef FEAT_MOUSE
1965# if defined(UNIX) || defined(VMS)
1966# ifdef FEAT_MOUSE_TTY
1967 /*
1968 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1969 * The termcode for the mouse is added as a side effect in option.c.
1970 */
1971 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001972 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001975 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 if (use_xterm_mouse())
1978 p = NULL; /* keep existing value, might be "xterm2" */
1979 else
1980 p = (char_u *)"xterm";
1981 }
1982# endif
1983 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001986 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1987 * "xterm2" in check_termcode(). */
1988 reset_option_was_set((char_u *)"ttym");
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (p == NULL
1991# ifdef FEAT_GUI
1992 || gui.in_use
1993# endif
1994 )
1995 check_mouse_termcode(); /* set mouse termcode anyway */
1996 }
1997# endif
1998# else
1999 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
2000# endif
2001#endif /* FEAT_MOUSE */
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#ifdef USE_TERM_CONSOLE
2004 /* DEFAULT_TERM indicates that it is the machine console. */
2005 if (STRCMP(term, DEFAULT_TERM) != 0)
2006 term_console = FALSE;
2007 else
2008 {
2009 term_console = TRUE;
2010# ifdef AMIGA
2011 win_resize_on(); /* enable window resizing reports */
2012# endif
2013 }
2014#endif
2015
2016#if defined(UNIX) || defined(VMS)
2017 /*
2018 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2019 */
2020 if (vim_is_fastterm(term))
2021 p_tf = TRUE;
2022#endif
2023#ifdef USE_TERM_CONSOLE
2024 /*
2025 * 'ttyfast' is default on consoles
2026 */
2027 if (term_console)
2028 p_tf = TRUE;
2029#endif
2030
2031 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2032
2033 full_screen = TRUE; /* we can use termcap codes from now on */
2034 set_term_defaults(); /* use current values as defaults */
2035#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002036 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002037 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#endif
2039
2040 /*
2041 * Initialize the terminal with the appropriate termcap codes.
2042 * Set the mouse and window title if possible.
2043 * Don't do this when starting, need to parse the .vimrc first, because it
2044 * may redefine t_TI etc.
2045 */
2046 if (starting != NO_SCREEN)
2047 {
2048 starttermcap(); /* may change terminal mode */
2049#ifdef FEAT_MOUSE
2050 setmouse(); /* may start using the mouse */
2051#endif
2052#ifdef FEAT_TITLE
2053 maketitle(); /* may display window title */
2054#endif
2055 }
2056
2057 /* display initial screen after ttest() checking. jw. */
2058 if (width <= 0 || height <= 0)
2059 {
2060 /* termcap failed to report size */
2061 /* set defaults, in case ui_get_shellsize() also fails */
2062 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002063#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 height = 25; /* console is often 25 lines */
2065#else
2066 height = 24; /* most terminals are 24 lines */
2067#endif
2068 }
2069 set_shellsize(width, height, FALSE); /* may change Rows */
2070 if (starting != NO_SCREEN)
2071 {
2072 if (scroll_region)
2073 scroll_region_reset(); /* In case Rows changed */
2074 check_map_keycodes(); /* check mappings for terminal codes used */
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002077 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 /*
2080 * Execute the TermChanged autocommands for each buffer that is
2081 * loaded.
2082 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002083 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002084 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 if (curbuf->b_ml.ml_mfp != NULL)
2087 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2088 curbuf);
2089 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002090 if (bufref_valid(&old_curbuf))
2091 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094
2095#ifdef FEAT_TERMRESPONSE
2096 may_req_termresponse();
2097#endif
2098
2099 return OK;
2100}
2101
2102#if defined(FEAT_MOUSE) || defined(PROTO)
2103
2104# ifdef FEAT_MOUSE_TTY
2105# define HMT_NORMAL 1
2106# define HMT_NETTERM 2
2107# define HMT_DEC 4
2108# define HMT_JSBTERM 8
2109# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002110# define HMT_URXVT 32
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002111# define HMT_GPM 64
2112# define HMT_SGR 128
2113# define HMT_SGR_REL 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114static int has_mouse_termcode = 0;
2115# endif
2116
Bram Moolenaar864207d2008-06-24 22:14:38 +00002117# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002119set_mouse_termcode(
2120 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2121 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 char_u name[2];
2124
2125 name[0] = n;
2126 name[1] = KE_FILLER;
2127 add_termcode(name, s, FALSE);
2128# ifdef FEAT_MOUSE_TTY
2129# ifdef FEAT_MOUSE_JSB
2130 if (n == KS_JSBTERM_MOUSE)
2131 has_mouse_termcode |= HMT_JSBTERM;
2132 else
2133# endif
2134# ifdef FEAT_MOUSE_NET
2135 if (n == KS_NETTERM_MOUSE)
2136 has_mouse_termcode |= HMT_NETTERM;
2137 else
2138# endif
2139# ifdef FEAT_MOUSE_DEC
2140 if (n == KS_DEC_MOUSE)
2141 has_mouse_termcode |= HMT_DEC;
2142 else
2143# endif
2144# ifdef FEAT_MOUSE_PTERM
2145 if (n == KS_PTERM_MOUSE)
2146 has_mouse_termcode |= HMT_PTERM;
2147 else
2148# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002149# ifdef FEAT_MOUSE_URXVT
2150 if (n == KS_URXVT_MOUSE)
2151 has_mouse_termcode |= HMT_URXVT;
2152 else
2153# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002154# ifdef FEAT_MOUSE_GPM
2155 if (n == KS_GPM_MOUSE)
2156 has_mouse_termcode |= HMT_GPM;
2157 else
2158# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002159 if (n == KS_SGR_MOUSE)
2160 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002161 else if (n == KS_SGR_MOUSE_RELEASE)
2162 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 has_mouse_termcode |= HMT_NORMAL;
2165# endif
2166}
2167# endif
2168
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002169# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002170 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002172del_mouse_termcode(
2173 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 char_u name[2];
2176
2177 name[0] = n;
2178 name[1] = KE_FILLER;
2179 del_termcode(name);
2180# ifdef FEAT_MOUSE_TTY
2181# ifdef FEAT_MOUSE_JSB
2182 if (n == KS_JSBTERM_MOUSE)
2183 has_mouse_termcode &= ~HMT_JSBTERM;
2184 else
2185# endif
2186# ifdef FEAT_MOUSE_NET
2187 if (n == KS_NETTERM_MOUSE)
2188 has_mouse_termcode &= ~HMT_NETTERM;
2189 else
2190# endif
2191# ifdef FEAT_MOUSE_DEC
2192 if (n == KS_DEC_MOUSE)
2193 has_mouse_termcode &= ~HMT_DEC;
2194 else
2195# endif
2196# ifdef FEAT_MOUSE_PTERM
2197 if (n == KS_PTERM_MOUSE)
2198 has_mouse_termcode &= ~HMT_PTERM;
2199 else
2200# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002201# ifdef FEAT_MOUSE_URXVT
2202 if (n == KS_URXVT_MOUSE)
2203 has_mouse_termcode &= ~HMT_URXVT;
2204 else
2205# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002206# ifdef FEAT_MOUSE_GPM
2207 if (n == KS_GPM_MOUSE)
2208 has_mouse_termcode &= ~HMT_GPM;
2209 else
2210# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002211 if (n == KS_SGR_MOUSE)
2212 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002213 else if (n == KS_SGR_MOUSE_RELEASE)
2214 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 has_mouse_termcode &= ~HMT_NORMAL;
2217# endif
2218}
2219# endif
2220#endif
2221
2222#ifdef HAVE_TGETENT
2223/*
2224 * Call tgetent()
2225 * Return error message if it fails, NULL if it's OK.
2226 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002227 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002228tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 int i;
2231
2232 i = TGETENT(tbuf, term);
2233 if (i < 0 /* -1 is always an error */
2234# ifdef TGETENT_ZERO_ERR
2235 || i == 0 /* sometimes zero is also an error */
2236# endif
2237 )
2238 {
2239 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2240 * tgetent() with the always existing "dumb" entry to avoid a crash or
2241 * hang. */
2242 (void)TGETENT(tbuf, "dumb");
2243
2244 if (i < 0)
2245# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002246 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (i == 0)
2248# endif
2249#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002252 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#endif
2254 }
2255 return NULL;
2256}
2257
2258/*
2259 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2260 * Fix that here.
2261 */
2262 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 char *p;
2266
2267 p = tgetstr(s, (char **)pp);
2268 if (p == (char *)-1)
2269 p = NULL;
2270 return (char_u *)p;
2271}
2272#endif /* HAVE_TGETENT */
2273
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002274#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275/*
2276 * Get Columns and Rows from the termcap. Used after a window signal if the
2277 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2278 * and "li" entries never change. But on some systems this works.
2279 * Errors while getting the entries are ignored.
2280 */
2281 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002282getlinecol(
2283 long *cp, /* pointer to columns */
2284 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 char_u tbuf[TBUFSZ];
2287
2288 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2289 {
2290 if (*cp == 0)
2291 *cp = tgetnum("co");
2292 if (*rp == 0)
2293 *rp = tgetnum("li");
2294 }
2295}
2296#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2297
2298/*
2299 * Get a string entry from the termcap and add it to the list of termcodes.
2300 * Used for <t_xx> special keys.
2301 * Give an error message for failure when not sourcing.
2302 * If force given, replace an existing entry.
2303 * Return FAIL if the entry was not found, OK if the entry was added.
2304 */
2305 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002306add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 char_u *term;
2309 int key;
2310 struct builtin_term *termp;
2311#ifdef HAVE_TGETENT
2312 char_u *string;
2313 int i;
2314 int builtin_first;
2315 char_u tbuf[TBUFSZ];
2316 char_u tstrbuf[TBUFSZ];
2317 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002318 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
2320
2321/*
2322 * If the GUI is running or will start in a moment, we only support the keys
2323 * that the GUI can produce.
2324 */
2325#ifdef FEAT_GUI
2326 if (gui.in_use || gui.starting)
2327 return gui_mch_haskey(name);
2328#endif
2329
2330 if (!force && find_termcode(name) != NULL) /* it's already there */
2331 return OK;
2332
2333 term = T_NAME;
2334 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2335 return FAIL;
2336
2337 if (term_is_builtin(term)) /* name starts with "builtin_" */
2338 {
2339 term += 8;
2340#ifdef HAVE_TGETENT
2341 builtin_first = TRUE;
2342#endif
2343 }
2344#ifdef HAVE_TGETENT
2345 else
2346 builtin_first = p_tbi;
2347#endif
2348
2349#ifdef HAVE_TGETENT
2350/*
2351 * We can get the entry from the builtin termcap and from the external one.
2352 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2353 * builtin termcap first.
2354 * If 'ttybuiltin' is off, try external termcap first.
2355 */
2356 for (i = 0; i < 2; ++i)
2357 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002358 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360 /*
2361 * Search in builtin termcap
2362 */
2363 {
2364 termp = find_builtin_term(term);
2365 if (termp->bt_string != NULL) /* found it */
2366 {
2367 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002368 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 while (termp->bt_entry != (int)KS_NAME)
2370 {
2371 if ((int)termp->bt_entry == key)
2372 {
2373 add_termcode(name, (char_u *)termp->bt_string,
2374 term_is_8bit(term));
2375 return OK;
2376 }
2377 ++termp;
2378 }
2379 }
2380 }
2381#ifdef HAVE_TGETENT
2382 else
2383 /*
2384 * Search in external termcap
2385 */
2386 {
2387 error_msg = tgetent_error(tbuf, term);
2388 if (error_msg == NULL)
2389 {
2390 string = TGETSTR((char *)name, &tp);
2391 if (string != NULL && *string != NUL)
2392 {
2393 add_termcode(name, string, FALSE);
2394 return OK;
2395 }
2396 }
2397 }
2398 }
2399#endif
2400
2401 if (sourcing_name == NULL)
2402 {
2403#ifdef HAVE_TGETENT
2404 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
2407#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 return FAIL;
2411}
2412
2413 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002414term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2417}
2418
2419/*
2420 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2421 * Assume that the terminal is using 8-bit controls when the name contains
2422 * "8bit", like in "xterm-8bit".
2423 */
2424 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002425term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2428}
2429
2430/*
2431 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002432 * <Esc>[ -> CSI <M_C_[>
2433 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 * <Esc>O -> <M-C-O>
2435 */
2436 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002437term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (*p == ESC)
2440 {
2441 if (p[1] == '[')
2442 return CSI;
2443 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002444 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (p[1] == 'O')
2446 return 0x8f;
2447 }
2448 return 0;
2449}
2450
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002451#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002453term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2456}
2457#endif
2458
2459#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2460
2461 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002462tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 static char_u buf[16];
2465 char_u *p;
2466
2467 p = buf + 15;
2468 *p = '\0';
2469 do
2470 {
2471 --p;
2472 *p = (char_u) (i % 10 + '0');
2473 i /= 10;
2474 }
2475 while (i > 0 && p > buf);
2476 return p;
2477}
2478#endif
2479
2480#ifndef HAVE_TGETENT
2481
2482/*
2483 * minimal tgoto() implementation.
2484 * no padding and we only parse for %i %d and %+char
2485 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002486 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002487tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 static char buf[30];
2490 char *p, *s, *e;
2491
2492 if (!cm)
2493 return "OOPS";
2494 e = buf + 29;
2495 for (s = buf; s < e && *cm; cm++)
2496 {
2497 if (*cm != '%')
2498 {
2499 *s++ = *cm;
2500 continue;
2501 }
2502 switch (*++cm)
2503 {
2504 case 'd':
2505 p = (char *)tltoa((unsigned long)y);
2506 y = x;
2507 while (*p)
2508 *s++ = *p++;
2509 break;
2510 case 'i':
2511 x++;
2512 y++;
2513 break;
2514 case '+':
2515 *s++ = (char)(*++cm + y);
2516 y = x;
2517 break;
2518 case '%':
2519 *s++ = *cm;
2520 break;
2521 default:
2522 return "OOPS";
2523 }
2524 }
2525 *s = '\0';
2526 return buf;
2527}
2528
2529#endif /* HAVE_TGETENT */
2530
2531/*
2532 * Set the terminal name and initialize the terminal options.
2533 * If "name" is NULL or empty, get the terminal name from the environment.
2534 * If that fails, use the default terminal name.
2535 */
2536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002537termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 char_u *term;
2540
2541 if (name != NULL && *name == NUL)
2542 name = NULL; /* empty name is equal to no name */
2543 term = name;
2544
2545#ifdef __BEOS__
2546 /*
2547 * TERM environment variable is normally set to 'ansi' on the Bebox;
2548 * Since the BeBox doesn't quite support full ANSI yet, we use our
2549 * own custom 'ansi-beos' termcap instead, unless the -T option has
2550 * been given on the command line.
2551 */
2552 if (term == NULL
2553 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2554 term = DEFAULT_TERM;
2555#endif
2556#ifndef MSWIN
2557 if (term == NULL)
2558 term = mch_getenv((char_u *)"TERM");
2559#endif
2560 if (term == NULL || *term == NUL)
2561 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002562 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 /* Set the default terminal name. */
2565 set_string_default("term", term);
2566 set_string_default("ttytype", term);
2567
2568 /*
2569 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2570 */
2571 set_termname(T_NAME != NULL ? T_NAME : term);
2572}
2573
2574/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002575 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002577#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002578
2579// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002581
2582static int out_pos = 0; // number of chars in out_buf
2583
2584// Since the maximum number of SGR parameters shown as a normal value range is
2585// 16, the escape sequence length can be 4 * 16 + lead + tail.
2586#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588/*
2589 * out_flush(): flush the output buffer
2590 */
2591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 int len;
2595
2596 if (out_pos != 0)
2597 {
2598 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2599 len = out_pos;
2600 out_pos = 0;
2601 ui_write(out_buf, len);
2602 }
2603}
2604
Bram Moolenaara338adc2018-01-31 20:51:47 +01002605/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002606 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2607 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002608 */
2609 void
2610out_flush_cursor(
2611 int force UNUSED, /* when TRUE, update cursor even when not moved */
2612 int clear_selection UNUSED) /* clear selection under cursor */
2613{
2614 mch_disable_flush();
2615 out_flush();
2616 mch_enable_flush();
2617#ifdef FEAT_GUI
2618 if (gui.in_use)
2619 {
2620 gui_update_cursor(force, clear_selection);
2621 gui_may_flush();
2622 }
2623#endif
2624}
2625
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627/*
2628 * Sometimes a byte out of a multi-byte character is written with out_char().
2629 * To avoid flushing half of the character, call this function first.
2630 */
2631 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002632out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2635 out_flush();
2636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638#ifdef FEAT_GUI
2639/*
2640 * out_trash(): Throw away the contents of the output buffer
2641 */
2642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002643out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 out_pos = 0;
2646}
2647#endif
2648
2649/*
2650 * out_char(c): put a byte into the output buffer.
2651 * Flush it if it becomes full.
2652 * This should not be used for outputting text on the screen (use functions
2653 * like msg_puts() and screen_putchar() for that).
2654 */
2655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002656out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
Bram Moolenaard0573012017-10-28 21:11:06 +02002658#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2660 out_char('\r');
2661#endif
2662
2663 out_buf[out_pos++] = c;
2664
2665 /* For testing we flush each time. */
2666 if (out_pos >= OUT_SIZE || p_wd)
2667 out_flush();
2668}
2669
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002670static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672/*
2673 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2674 */
2675 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaard0573012017-10-28 21:11:06 +02002678#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2680 out_char_nf('\r');
2681#endif
2682
2683 out_buf[out_pos++] = c;
2684
2685 if (out_pos >= OUT_SIZE)
2686 out_flush();
2687}
2688
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002689#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2690 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691/*
2692 * A never-padding out_str.
2693 * use this whenever you don't want to run the string through tputs.
2694 * tputs above is harmless, but tputs from the termcap library
2695 * is likely to strip off leading digits, that it mistakes for padding
2696 * information, and "%i", "%d", etc.
2697 * This should only be used for writing terminal codes, not for outputting
2698 * normal text (use functions like msg_puts() and screen_putchar() for that).
2699 */
2700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002703 // avoid terminal strings being split up
2704 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 while (*s)
2708 out_char_nf(*s++);
2709
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002710 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (p_wd)
2712 out_flush();
2713}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002717 * A conditional-flushing out_str, mainly for visualbell.
2718 * Handles a delay internally, because termlib may not respect the delay or do
2719 * it at the wrong time.
2720 * Note: Only for terminal strings.
2721 */
2722 void
2723out_str_cf(char_u *s)
2724{
2725 if (s != NULL && *s)
2726 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002727#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002728 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002730
2731#ifdef FEAT_GUI
2732 /* Don't use tputs() when GUI is used, ncurses crashes. */
2733 if (gui.in_use)
2734 {
2735 out_str_nf(s);
2736 return;
2737 }
2738#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002739 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002740 out_flush();
2741#ifdef HAVE_TGETENT
2742 for (p = s; *s; ++s)
2743 {
2744 /* flush just before delay command */
2745 if (*s == '$' && *(s + 1) == '<')
2746 {
2747 char_u save_c = *s;
2748 int duration = atoi((char *)s + 2);
2749
2750 *s = NUL;
2751 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2752 *s = save_c;
2753 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002754# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002755 /* Only sleep here if we can limit this happening in
2756 * vim_beep(). */
2757 p = vim_strchr(s, '>');
2758 if (p == NULL || duration <= 0)
2759 {
2760 /* can't parse the time, don't sleep here */
2761 p = s;
2762 }
2763 else
2764 {
2765 ++p;
2766 do_sleep(duration);
2767 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002768# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002769 /* Rely on the terminal library to sleep. */
2770 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002771# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002772 break;
2773 }
2774 }
2775 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2776#else
2777 while (*s)
2778 out_char_nf(*s++);
2779#endif
2780
2781 /* For testing we write one string at a time. */
2782 if (p_wd)
2783 out_flush();
2784 }
2785}
2786
2787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * out_str(s): Put a character string a byte at a time into the output buffer.
2789 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2790 * This should only be used for writing terminal codes, not for outputting
2791 * normal text (use functions like msg_puts() and screen_putchar() for that).
2792 */
2793 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002794out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 if (s != NULL && *s)
2797 {
2798#ifdef FEAT_GUI
2799 /* Don't use tputs() when GUI is used, ncurses crashes. */
2800 if (gui.in_use)
2801 {
2802 out_str_nf(s);
2803 return;
2804 }
2805#endif
2806 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002807 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 out_flush();
2809#ifdef HAVE_TGETENT
2810 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2811#else
2812 while (*s)
2813 out_char_nf(*s++);
2814#endif
2815
2816 /* For testing we write one string at a time. */
2817 if (p_wd)
2818 out_flush();
2819 }
2820}
2821
2822/*
2823 * cursor positioning using termcap parser. (jw)
2824 */
2825 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002826term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 OUT_STR(tgoto((char *)T_CM, col, row));
2829}
2830
2831 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002832term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
2834 OUT_STR(tgoto((char *)T_CRI, 0, i));
2835}
2836
2837 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002838term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
2840 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2841}
2842
2843 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002844term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2847}
2848
2849#if defined(HAVE_TGETENT) || defined(PROTO)
2850 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002851term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 /* Can't handle a negative value here */
2854 if (x < 0)
2855 x = 0;
2856 if (y < 0)
2857 y = 0;
2858 OUT_STR(tgoto((char *)T_CWP, y, x));
2859}
2860
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002861# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2862/*
2863 * Return TRUE if we can request the terminal for a response.
2864 */
2865 static int
2866can_get_termresponse()
2867{
2868 return cur_tmode == TMODE_RAW
2869 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002870# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002871 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002872# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002873 && p_ek;
2874}
2875
Bram Moolenaarafd78262019-05-10 23:10:31 +02002876/*
2877 * Set "status" to STATUS_SENT.
2878 */
2879 static void
2880termrequest_sent(termrequest_T *status)
2881{
2882 status->tr_progress = STATUS_SENT;
2883 status->tr_start = time(NULL);
2884}
2885
2886/*
2887 * Return TRUE if any of the requests are in STATUS_SENT.
2888 */
2889 static int
2890termrequest_any_pending()
2891{
2892 int i;
2893 time_t now = time(NULL);
2894
2895 for (i = 0; all_termrequests[i] != NULL; ++i)
2896 {
2897 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2898 {
2899 if (all_termrequests[i]->tr_start > 0 && now > 0
2900 && all_termrequests[i]->tr_start + 2 < now)
2901 // Sent the request more than 2 seconds ago and didn't get a
2902 // response, assume it failed.
2903 all_termrequests[i]->tr_progress = STATUS_FAIL;
2904 else
2905 return TRUE;
2906 }
2907 }
2908 return FALSE;
2909}
2910
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002911static int winpos_x = -1;
2912static int winpos_y = -1;
2913static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002914
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002915# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002916/*
2917 * Try getting the Vim window position from the terminal.
2918 * Returns OK or FAIL.
2919 */
2920 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002921term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002922{
2923 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002924 int prev_winpos_x = winpos_x;
2925 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002926
2927 if (*T_CGP == NUL || !can_get_termresponse())
2928 return FAIL;
2929 winpos_x = -1;
2930 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002931 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002932 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002933 OUT_STR(T_CGP);
2934 out_flush();
2935
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002936 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002937 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002938 {
2939 (void)vpeekc_nomap();
2940 if (winpos_x >= 0 && winpos_y >= 0)
2941 {
2942 *x = winpos_x;
2943 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002944 return OK;
2945 }
2946 ui_delay(10, FALSE);
2947 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002948 /* Do not reset "did_request_winpos", if we timed out the response might
2949 * still come later and we must consume it. */
2950
2951 winpos_x = prev_winpos_x;
2952 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002953 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002954 {
2955 /* Polling: return previous values if we have them. */
2956 *x = winpos_x;
2957 *y = winpos_y;
2958 return OK;
2959 }
2960
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002961 return FALSE;
2962}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002963# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002964# endif
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002967term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002969 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970}
2971#endif
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002974term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002977 int i = *s == CSI ? 1 : 2;
2978 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /* Special handling of 16 colors, because termcap can't handle it */
2981 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2982 /* Also accept CSI instead of <Esc>[ */
2983 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002984 && ((s[0] == ESC && s[1] == '[')
2985#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2986 || (s[0] == ESC && s[1] == '|')
2987#endif
2988 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 && s[i] != NUL
2990 && (STRCMP(s + i + 1, "%p1%dm") == 0
2991 || STRCMP(s + i + 1, "%dm") == 0)
2992 && (s[i] == '3' || s[i] == '4'))
2993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002995 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002997 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002999 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003000#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02003001 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003002#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003003 IF_EB("\033[", ESC_STR "[")) : "\233";
3004 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3005 : (n >= 16 ? "48;5;" : "10");
3006
3007 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3009 }
3010 else
3011 OUT_STR(tgoto((char *)s, 0, n));
3012}
3013
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003014 void
3015term_fg_color(int n)
3016{
3017 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
3018 if (*T_CAF)
3019 term_color(T_CAF, n);
3020 else if (*T_CSF)
3021 term_color(T_CSF, n);
3022}
3023
3024 void
3025term_bg_color(int n)
3026{
3027 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
3028 if (*T_CAB)
3029 term_color(T_CAB, n);
3030 else if (*T_CSB)
3031 term_color(T_CSB, n);
3032}
3033
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003034#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003035
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003036#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3037#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3038#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003039
3040 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003041term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003042{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003043#define MAX_COLOR_STR_LEN 100
3044 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003045
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003046 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003047 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003048 OUT_STR(buf);
3049}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003050
3051 void
3052term_fg_rgb_color(guicolor_T rgb)
3053{
3054 term_rgb_color(T_8F, rgb);
3055}
3056
3057 void
3058term_bg_rgb_color(guicolor_T rgb)
3059{
3060 term_rgb_color(T_8B, rgb);
3061}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003062#endif
3063
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003064#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3065 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066/*
3067 * Generic function to set window title, using t_ts and t_fs.
3068 */
3069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003070term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 /* t_ts takes one argument: column in status line */
3073 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
3074 out_str_nf(title);
3075 out_str(T_FS); /* set title end */
3076 out_flush();
3077}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003078
3079/*
3080 * Tell the terminal to push (save) the title and/or icon, so that it can be
3081 * popped (restored) later.
3082 */
3083 void
3084term_push_title(int which)
3085{
Bram Moolenaar27821262019-05-08 16:41:09 +02003086 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003087 {
3088 OUT_STR(T_CST);
3089 out_flush();
3090 }
3091
Bram Moolenaar27821262019-05-08 16:41:09 +02003092 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003093 {
3094 OUT_STR(T_SSI);
3095 out_flush();
3096 }
3097}
3098
3099/*
3100 * Tell the terminal to pop the title and/or icon.
3101 */
3102 void
3103term_pop_title(int which)
3104{
Bram Moolenaar27821262019-05-08 16:41:09 +02003105 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003106 {
3107 OUT_STR(T_CRT);
3108 out_flush();
3109 }
3110
Bram Moolenaar27821262019-05-08 16:41:09 +02003111 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003112 {
3113 OUT_STR(T_SRI);
3114 out_flush();
3115 }
3116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
3118
3119/*
3120 * Make sure we have a valid set or terminal options.
3121 * Replace all entries that are NULL by empty_option
3122 */
3123 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003124ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003126 char_u *env_colors;
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 check_options(); /* make sure no options are NULL */
3129
3130 /*
3131 * MUST have "cm": cursor motion.
3132 */
3133 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003134 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * if "cs" defined, use a scroll region, it's faster.
3138 */
3139 if (*T_CS != NUL)
3140 scroll_region = TRUE;
3141 else
3142 scroll_region = FALSE;
3143
3144 if (pairs)
3145 {
3146 /*
3147 * optional pairs
3148 */
3149 /* TP goes to normal mode for TI (invert) and TB (bold) */
3150 if (*T_ME == NUL)
3151 T_ME = T_MR = T_MD = T_MB = empty_option;
3152 if (*T_SO == NUL || *T_SE == NUL)
3153 T_SO = T_SE = empty_option;
3154 if (*T_US == NUL || *T_UE == NUL)
3155 T_US = T_UE = empty_option;
3156 if (*T_CZH == NUL || *T_CZR == NUL)
3157 T_CZH = T_CZR = empty_option;
3158
3159 /* T_VE is needed even though T_VI is not defined */
3160 if (*T_VE == NUL)
3161 T_VI = empty_option;
3162
3163 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3164 if (*T_ME == NUL)
3165 {
3166 T_ME = T_SE;
3167 T_MR = T_SO;
3168 T_MD = T_SO;
3169 }
3170
3171 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3172 if (*T_SO == NUL)
3173 {
3174 T_SE = T_ME;
3175 if (*T_MR == NUL)
3176 T_SO = T_MD;
3177 else
3178 T_SO = T_MR;
3179 }
3180
3181 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3182 if (*T_CZH == NUL)
3183 {
3184 T_CZR = T_ME;
3185 if (*T_MR == NUL)
3186 T_CZH = T_MD;
3187 else
3188 T_CZH = T_MR;
3189 }
3190
3191 /* "Sb" and "Sf" come in pairs */
3192 if (*T_CSB == NUL || *T_CSF == NUL)
3193 {
3194 T_CSB = empty_option;
3195 T_CSF = empty_option;
3196 }
3197
3198 /* "AB" and "AF" come in pairs */
3199 if (*T_CAB == NUL || *T_CAF == NUL)
3200 {
3201 T_CAB = empty_option;
3202 T_CAF = empty_option;
3203 }
3204
3205 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3206 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003207 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /* Set 'weirdinvert' according to value of 't_xs' */
3210 p_wiv = (*T_XS != NUL);
3211 }
3212 need_gather = TRUE;
3213
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003214 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003216 env_colors = mch_getenv((char_u *)"COLORS");
3217 if (env_colors != NULL && isdigit(*env_colors))
3218 {
3219 int colors = atoi((char *)env_colors);
3220
3221 if (colors != t_colors)
3222 set_color_count(colors);
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3227 || defined(PROTO)
3228/*
3229 * Represent the given long_u as individual bytes, with the most significant
3230 * byte first, and store them in dst.
3231 */
3232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 int i;
3236 int shift;
3237
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003238 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 shift = 8 * (sizeof(long_u) - i);
3241 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3242 }
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245/*
3246 * Interpret the next string of bytes in buf as a long integer, with the most
3247 * significant byte first. Note that it is assumed that buf has been through
3248 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3249 * Puts result in val, and returns the number of bytes read from buf
3250 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3251 * were present.
3252 */
3253 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 int len;
3257 char_u bytes[sizeof(long_u)];
3258 int i;
3259 int shift;
3260
3261 *val = 0;
3262 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3263 if (len != -1)
3264 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003265 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 shift = 8 * (sizeof(long_u) - 1 - i);
3268 *val += (long_u)bytes[i] << shift;
3269 }
3270 }
3271 return len;
3272}
3273#endif
3274
3275#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003276 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3277 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
3279 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3280 * that buf has been through inchar(). Returns the actual number of bytes used
3281 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3282 * available.
3283 */
3284 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003285get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 int len = 0;
3288 int i;
3289 char_u c;
3290
3291 for (i = 0; i < num_bytes; i++)
3292 {
3293 if ((c = buf[len++]) == NUL)
3294 return -1;
3295 if (c == K_SPECIAL)
3296 {
3297 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3298 return -1;
3299 if (buf[len++] == (int)KS_ZERO)
3300 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003301 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3302 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3303 if (buf[len++] == (int)KE_CSI)
3304 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003306 else if (c == CSI && buf[len] == KS_EXTRA
3307 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3309 * the start of a special key, see add_to_input_buf_csi(). */
3310 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 bytes[i] = c;
3312 }
3313 return len;
3314}
3315#endif
3316
3317/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003318 * Check if the new shell size is valid, correct it if it's too small or way
3319 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (Rows < min_rows()) /* need room for one window and command line */
3325 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003326 limit_screen_size();
3327}
3328
3329/*
3330 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3331 */
3332 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003333limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003334{
3335 if (Columns < MIN_COLUMNS)
3336 Columns = MIN_COLUMNS;
3337 else if (Columns > 10000)
3338 Columns = 10000;
3339 if (Rows > 1000)
3340 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
Bram Moolenaar86b68352004-12-27 21:59:20 +00003343/*
3344 * Invoked just before the screen structures are going to be (re)allocated.
3345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003347win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 static int old_Rows = 0;
3350 static int old_Columns = 0;
3351
3352 if (old_Rows != Rows || old_Columns != Columns)
3353 ui_new_shellsize();
3354 if (old_Rows != Rows)
3355 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003356 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003357 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003358 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 old_Rows = Rows;
3360 shell_new_rows(); /* update window sizes */
3361 }
3362 if (old_Columns != Columns)
3363 {
3364 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367}
3368
3369/*
3370 * Call this function when the Vim shell has been resized in any way.
3371 * Will obtain the current size and redraw (also when size didn't change).
3372 */
3373 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 set_shellsize(0, 0, FALSE);
3377}
3378
3379/*
3380 * Check if the shell size changed. Handle a resize.
3381 * When the size didn't change, nothing happens.
3382 */
3383 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003384shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 int old_Rows = Rows;
3387 int old_Columns = Columns;
3388
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003389 if (!exiting
3390#ifdef FEAT_GUI
3391 /* Do not get the size when executing a shell command during
3392 * startup. */
3393 && !gui.starting
3394#endif
3395 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003396 {
3397 (void)ui_get_shellsize();
3398 check_shellsize();
3399 if (old_Rows != Rows || old_Columns != Columns)
3400 shell_resized();
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
3404/*
3405 * Set size of the Vim shell.
3406 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3407 * window size (this is used for the :win command).
3408 * If 'mustset' is FALSE, we may try to get the real window size and if
3409 * it fails use 'width' and 'height'.
3410 */
3411 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003412set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 static int busy = FALSE;
3415
3416 /*
3417 * Avoid recursiveness, can happen when setting the window size causes
3418 * another window-changed signal.
3419 */
3420 if (busy)
3421 return;
3422
3423 if (width < 0 || height < 0) /* just checking... */
3424 return;
3425
Bram Moolenaara971b822011-09-14 14:43:25 +02003426 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003428 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 State = SETWSIZE;
3430 return;
3431 }
3432
Bram Moolenaara971b822011-09-14 14:43:25 +02003433 /* curwin->w_buffer can be NULL when we are closing a window and the
3434 * buffer has already been closed and removing a scrollbar causes a resize
3435 * event. Don't resize then, it will happen after entering another buffer.
3436 */
3437 if (curwin->w_buffer == NULL)
3438 return;
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 ++busy;
3441
3442#ifdef AMIGA
3443 out_flush(); /* must do this before mch_get_shellsize() for
3444 some obscure reason */
3445#endif
3446
3447 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3448 {
3449 Rows = height;
3450 Columns = width;
3451 check_shellsize();
3452 ui_set_shellsize(mustset);
3453 }
3454 else
3455 check_shellsize();
3456
3457 /* The window layout used to be adjusted here, but it now happens in
3458 * screenalloc() (also invoked from screenclear()). That is because the
3459 * "busy" check above may skip this, but not screenalloc(). */
3460
3461 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3462 screenclear();
3463 else
3464 screen_start(); /* don't know where cursor is now */
3465
3466 if (starting != NO_SCREEN)
3467 {
3468#ifdef FEAT_TITLE
3469 maketitle();
3470#endif
3471 changed_line_abv_curs();
3472 invalidate_botline();
3473
3474 /*
3475 * We only redraw when it's needed:
3476 * - While at the more prompt or executing an external command, don't
3477 * redraw, but position the cursor.
3478 * - While editing the command line, only redraw that.
3479 * - in Ex mode, don't redraw anything.
3480 * - Otherwise, redraw right now, and position the cursor.
3481 * Always need to call update_screen() or screenalloc(), to make
3482 * sure Rows/Columns and the size of ScreenLines[] is correct!
3483 */
3484 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3485 || exmode_active)
3486 {
3487 screenalloc(FALSE);
3488 repeat_message();
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 else
3491 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003492 if (curwin->w_p_scb)
3493 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003494 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003495 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003496 update_screen(NOT_VALID);
3497 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003498 }
3499 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003500 {
3501 update_topline();
3502#if defined(FEAT_INS_EXPAND)
3503 if (pum_visible())
3504 {
3505 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003506 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003507 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003508#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003509 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003510 if (redrawing())
3511 setcursor();
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
3514 cursor_on(); /* redrawing may have switched it off */
3515 }
3516 out_flush();
3517 --busy;
3518}
3519
3520/*
3521 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3522 * commands and Ex mode).
3523 */
3524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003525settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527#ifdef FEAT_GUI
3528 /* don't set the term where gvim was started to any mode */
3529 if (gui.in_use)
3530 return;
3531#endif
3532
3533 if (full_screen)
3534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /*
3536 * When returning after calling a shell we want to really set the
3537 * terminal to raw mode, even though we think it already is, because
3538 * the shell program may have reset the terminal mode.
3539 * When we think the terminal is normal, don't try to set it to
3540 * normal again, because that causes problems (logout!) on some
3541 * machines.
3542 */
3543 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3544 {
3545#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003546# ifdef FEAT_GUI
3547 if (!gui.in_use && !gui.starting)
3548# endif
3549 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003550 // May need to check for T_CRV response and termcodes, it
3551 // doesn't work in Cooked mode, an external program may get
3552 // them.
3553 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003554 (void)vpeekc_nomap();
3555 check_for_codes_from_term();
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557#endif
3558#ifdef FEAT_MOUSE_TTY
3559 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003560 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003562 if (termcap_active)
3563 {
3564 if (tmode != TMODE_RAW)
3565 out_str(T_BD); // disable bracketed paste mode
3566 else
3567 out_str(T_BE); // enable bracketed paste mode (should
3568 // be before mch_settmode().
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003571 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 cur_tmode = tmode;
3573#ifdef FEAT_MOUSE
3574 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003575 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
3577 out_flush();
3578 }
3579#ifdef FEAT_TERMRESPONSE
3580 may_req_termresponse();
3581#endif
3582 }
3583}
3584
3585 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003586starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
3588 if (full_screen && !termcap_active)
3589 {
3590 out_str(T_TI); /* start termcap mode */
3591 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003592 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 out_flush();
3594 termcap_active = TRUE;
3595 screen_start(); /* don't know where cursor is now */
3596#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003597# ifdef FEAT_GUI
3598 if (!gui.in_use && !gui.starting)
3599# endif
3600 {
3601 may_req_termresponse();
3602 /* Immediately check for a response. If t_Co changes, we don't
3603 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003604 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003605 check_for_codes_from_term();
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607#endif
3608 }
3609}
3610
3611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003612stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
3614 screen_stop_highlight();
3615 reset_cterm_colors();
3616 if (termcap_active)
3617 {
3618#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003619# ifdef FEAT_GUI
3620 if (!gui.in_use && !gui.starting)
3621# endif
3622 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003623 // May need to discard T_CRV, T_U7 or T_RBG response.
3624 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003625 {
3626# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003627 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003628 mch_delay(100L, FALSE);
3629# endif
3630# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003631 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003632 if (exiting)
3633 tcflush(fileno(stdin), TCIFLUSH);
3634# endif
3635 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003636 /* Check for termcodes first, otherwise an external program may
3637 * get them. */
3638 check_for_codes_from_term();
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003641 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 out_str(T_KE); /* stop "keypad transmit" mode */
3643 out_flush();
3644 termcap_active = FALSE;
3645 cursor_on(); /* just in case it is still off */
3646 out_str(T_TE); /* stop termcap mode */
3647 screen_start(); /* don't know where cursor is now */
3648 out_flush();
3649 }
3650}
3651
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003652#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653/*
3654 * Request version string (for xterm) when needed.
3655 * Only do this after switching to raw mode, otherwise the result will be
3656 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003657 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003658 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 * Only do this after termcap mode has been started, otherwise the codes for
3660 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003661 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3662 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003663 * On Unix only do it when both output and input are a tty (avoid writing
3664 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * The result is caught in check_termcode().
3666 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003667 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003668may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003670 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003671 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003672 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 && *T_CRV != NUL)
3674 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003675 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003677 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 /* check for the characters now, otherwise they might be eaten by
3679 * get_keystroke() */
3680 out_flush();
3681 (void)vpeekc_nomap();
3682 }
3683}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003684
Bram Moolenaar9584b312013-03-13 19:29:28 +01003685/*
3686 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003687 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003688 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003689 * If the terminal treats \u25bd as single width, the position is (1, 1),
3690 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003691 * This function has the side effect that changes cursor position, so
3692 * it must be called immediately after entering termcap mode.
3693 */
3694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003695may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003696{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003697 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003698 && can_get_termresponse()
3699 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003700 && *T_U7 != NUL
3701 && !option_was_set((char_u *)"ambiwidth"))
3702 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003703 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003704
Bram Moolenaarafd78262019-05-10 23:10:31 +02003705 LOG_TR(("Sending U7 request"));
3706 /* Do this in the second row. In the first row the returned sequence
3707 * may be CSI 1;2R, which is the same as <S-F3>. */
3708 term_windgoto(1, 0);
3709 buf[mb_char2bytes(0x25bd, buf)] = 0;
3710 out_str(buf);
3711 out_str(T_U7);
3712 termrequest_sent(&u7_status);
3713 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003714
Bram Moolenaarafd78262019-05-10 23:10:31 +02003715 /* This overwrites a few characters on the screen, a redraw is needed
3716 * after this. Clear them out for now. */
3717 term_windgoto(1, 0);
3718 out_str((char_u *)" ");
3719 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003720
Bram Moolenaarafd78262019-05-10 23:10:31 +02003721 /* Need to reset the known cursor position. */
3722 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003723
Bram Moolenaarafd78262019-05-10 23:10:31 +02003724 /* check for the characters now, otherwise they might be eaten by
3725 * get_keystroke() */
3726 out_flush();
3727 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003728 }
3729}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003730
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003731/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003732 * Similar to requesting the version string: Request the terminal background
3733 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003734 */
3735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003736may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003737{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003738 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003739 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003740 int didit = FALSE;
3741
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003742# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003743 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003744 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003745 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003746 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003747 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003748 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003749 didit = TRUE;
3750 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003751# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003752
3753 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003754 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003755 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003756 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003757 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003758 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003759 didit = TRUE;
3760 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003761
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003762 if (didit)
3763 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003764 /* check for the characters now, otherwise they might be eaten by
3765 * get_keystroke() */
3766 out_flush();
3767 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003768 }
3769 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003770}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003771
Bram Moolenaar2951b772013-07-03 12:45:31 +02003772# ifdef DEBUG_TERMRESPONSE
3773 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003774log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003775{
3776 static FILE *fd_tr = NULL;
3777 static proftime_T start;
3778 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003779 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003780
3781 if (fd_tr == NULL)
3782 {
3783 fd_tr = fopen("termresponse.log", "w");
3784 profile_start(&start);
3785 }
3786 now = start;
3787 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003788 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3789 must_redraw == NOT_VALID ? "NV"
3790 : must_redraw == CLEAR ? "CL" : " ");
3791 va_start(ap, fmt);
3792 vfprintf(fd_tr, fmt, ap);
3793 va_end(ap);
3794 fputc('\n', fd_tr);
3795 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003796}
3797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#endif
3799
3800/*
3801 * Return TRUE when saving and restoring the screen.
3802 */
3803 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003804swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 return (full_screen && *T_TI != NUL);
3807}
3808
Bram Moolenaarecadf432018-03-20 11:17:04 +01003809#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810/*
3811 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3812 */
3813 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003814setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816# ifdef FEAT_MOUSE_TTY
3817 int checkfor;
3818# endif
3819
3820# ifdef FEAT_MOUSESHAPE
3821 update_mouseshape(-1);
3822# endif
3823
3824# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3825# ifdef FEAT_GUI
3826 /* In the GUI the mouse is always enabled. */
3827 if (gui.in_use)
3828 return;
3829# endif
3830 /* be quick when mouse is off */
3831 if (*p_mouse == NUL || has_mouse_termcode == 0)
3832 return;
3833
3834 /* don't switch mouse on when not in raw mode (Ex mode) */
3835 if (cur_tmode != TMODE_RAW)
3836 {
3837 mch_setmouse(FALSE);
3838 return;
3839 }
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (VIsual_active)
3842 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003843 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 checkfor = MOUSE_RETURN;
3845 else if (State & INSERT)
3846 checkfor = MOUSE_INSERT;
3847 else if (State & CMDLINE)
3848 checkfor = MOUSE_COMMAND;
3849 else if (State == CONFIRM || State == EXTERNCMD)
3850 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3851 else
3852 checkfor = MOUSE_NORMAL; /* assume normal mode */
3853
3854 if (mouse_has(checkfor))
3855 mch_setmouse(TRUE);
3856 else
3857 mch_setmouse(FALSE);
3858# endif
3859}
3860
3861/*
3862 * Return TRUE if
3863 * - "c" is in 'mouse', or
3864 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3865 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3866 * normal editing mode (not at hit-return message).
3867 */
3868 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003869mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 char_u *p;
3872
3873 for (p = p_mouse; *p; ++p)
3874 switch (*p)
3875 {
3876 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3877 return TRUE;
3878 break;
3879 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3880 return TRUE;
3881 break;
3882 default: if (c == *p) return TRUE; break;
3883 }
3884 return FALSE;
3885}
3886
3887/*
3888 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3889 */
3890 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003891mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 return (p_mousem[0] == 'p');
3894}
3895#endif
3896
3897/*
3898 * By outputting the 'cursor very visible' termcap code, for some windowed
3899 * terminals this makes the screen scrolled to the correct position.
3900 * Used when starting Vim or returning from a shell.
3901 */
3902 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003903scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003905 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003908 out_str(T_CVS);
3909 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911}
3912
3913static int cursor_is_off = FALSE;
3914
3915/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003916 * Enable the cursor without checking if it's already enabled.
3917 */
3918 void
3919cursor_on_force(void)
3920{
3921 out_str(T_VE);
3922 cursor_is_off = FALSE;
3923}
3924
3925/*
3926 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
3928 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003929cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003932 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933}
3934
3935/*
3936 * Disable the cursor.
3937 */
3938 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003939cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003941 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003943 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 cursor_is_off = TRUE;
3945 }
3946}
3947
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003948#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003950 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003951 */
3952 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003953term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003954{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003955 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003956 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003957
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003958 /* Only do something when redrawing the screen and we can restore the
3959 * mode. */
3960 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003961 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003962# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003963 if (forced && initial_cursor_shape > 0)
3964 /* Restore to initial values. */
3965 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003966# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003967 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003968 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003969
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003970 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003971 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003972 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003973 {
3974 if (*T_CSR != NUL)
3975 p = T_CSR; /* Replace mode cursor */
3976 else
3977 p = T_CSI; /* fall back to Insert mode cursor */
3978 if (*p != NUL)
3979 {
3980 out_str(p);
3981 showing_mode = REPLACE;
3982 }
3983 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003984 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003985 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003986 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003987 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003988 {
3989 out_str(T_CSI); /* Insert mode cursor */
3990 showing_mode = INSERT;
3991 }
3992 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003993 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003994 {
3995 out_str(T_CEI); /* non-Insert mode cursor */
3996 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997 }
3998}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003999
4000# if defined(FEAT_TERMINAL) || defined(PROTO)
4001 void
4002term_cursor_color(char_u *color)
4003{
4004 if (*T_CSC != NUL)
4005 {
4006 out_str(T_CSC); /* set cursor color start */
4007 out_str_nf(color);
4008 out_str(T_CEC); /* set cursor color end */
4009 out_flush();
4010 }
4011}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004012# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004013
Bram Moolenaar4db25542017-08-28 22:43:05 +02004014 int
4015blink_state_is_inverted()
4016{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004017#ifdef FEAT_TERMRESPONSE
Bram Moolenaarafd78262019-05-10 23:10:31 +02004018 return rbm_status.tr_progress == STATUS_GOT && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004019 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004020#else
4021 return FALSE;
4022#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004023}
4024
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004025/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004026 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004027 */
4028 void
4029term_cursor_shape(int shape, int blink)
4030{
4031 if (*T_CSH != NUL)
4032 {
4033 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4034 out_flush();
4035 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004036 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004037 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004038 int do_blink = blink;
4039
4040 /* t_SH is empty: try setting just the blink state.
4041 * The blink flags are XORed together, if the initial blinking from
4042 * style and shape differs, we need to invert the flag here. */
4043 if (blink_state_is_inverted())
4044 do_blink = !blink;
4045
4046 if (do_blink && *T_VS != NUL)
4047 {
4048 out_str(T_VS);
4049 out_flush();
4050 }
4051 else if (!do_blink && *T_CVS != NUL)
4052 {
4053 out_str(T_CVS);
4054 out_flush();
4055 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004056 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004057}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004058#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004059
4060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Set scrolling region for window 'wp'.
4062 * The region starts 'off' lines from the start of the window.
4063 * Also set the vertical scroll region for a vertically split window. Always
4064 * the full width of the window, excluding the vertical separator.
4065 */
4066 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004067scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4070 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004072 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4073 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 screen_start(); /* don't know where cursor is now */
4075}
4076
4077/*
4078 * Reset scrolling region to the whole screen.
4079 */
4080 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004081scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (*T_CSV != NUL)
4085 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 screen_start(); /* don't know where cursor is now */
4087}
4088
4089
4090/*
4091 * List of terminal codes that are currently recognized.
4092 */
4093
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004094static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 char_u name[2]; /* termcap name of entry */
4097 char_u *code; /* terminal code (in allocated memory) */
4098 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004099 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100} *termcodes = NULL;
4101
4102static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4103static int tc_len = 0; /* current number of entries in termcodes[] */
4104
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004105static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004108clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 while (tc_len > 0)
4111 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004112 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 tc_max_len = 0;
4114
4115#ifdef HAVE_TGETENT
4116 BC = (char *)empty_option;
4117 UP = (char *)empty_option;
4118 PC = NUL; /* set pad character to NUL */
4119 ospeed = 0;
4120#endif
4121
4122 need_gather = TRUE; /* need to fill termleader[] */
4123}
4124
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004125#define ATC_FROM_TERM 55
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127/*
4128 * Add a new entry to the list of terminal codes.
4129 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4131 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
4133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004134add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 struct termcode *new_tc;
4137 int i, j;
4138 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 if (string == NULL || *string == NUL)
4142 {
4143 del_termcode(name);
4144 return;
4145 }
4146
Bram Moolenaar4f974752019-02-17 17:44:42 +01004147#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02004148 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4149#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004150# ifdef VIMDLL
4151 if (!gui.in_use)
4152 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4153 else
4154# endif
4155 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 if (s == NULL)
4158 return;
4159
4160 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004161 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004163 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 s[0] = term_7to8bit(string);
4165 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004166
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004167#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4168# ifdef VIMDLL
4169 if (!gui.in_use)
4170# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004171 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004172 if (s[0] == K_NUL)
4173 {
4174 STRMOVE(s + 1, s);
4175 s[1] = 3;
4176 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004177 }
4178#endif
4179
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004180 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 need_gather = TRUE; /* need to fill termleader[] */
4183
4184 /*
4185 * need to make space for more entries
4186 */
4187 if (tc_len == tc_max_len)
4188 {
4189 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004190 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (new_tc == NULL)
4192 {
4193 tc_max_len -= 20;
4194 return;
4195 }
4196 for (i = 0; i < tc_len; ++i)
4197 new_tc[i] = termcodes[i];
4198 vim_free(termcodes);
4199 termcodes = new_tc;
4200 }
4201
4202 /*
4203 * Look for existing entry with the same name, it is replaced.
4204 * Look for an existing entry that is alphabetical higher, the new entry
4205 * is inserted in front of it.
4206 */
4207 for (i = 0; i < tc_len; ++i)
4208 {
4209 if (termcodes[i].name[0] < name[0])
4210 continue;
4211 if (termcodes[i].name[0] == name[0])
4212 {
4213 if (termcodes[i].name[1] < name[1])
4214 continue;
4215 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004216 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
4218 if (termcodes[i].name[1] == name[1])
4219 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 if (flags == ATC_FROM_TERM && (j = termcode_star(
4221 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004222 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 /* Don't replace ESC[123;*X or ESC O*X with another when
4224 * invoked from got_code_from_term(). */
4225 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004226 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004227 && s[len - 1]
4228 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004230 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004231 vim_free(s);
4232 return;
4233 }
4234 }
4235 else
4236 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004237 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238 vim_free(termcodes[i].code);
4239 --tc_len;
4240 break;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244 /*
4245 * Found alphabetical larger entry, move rest to insert new entry
4246 */
4247 for (j = tc_len; j > i; --j)
4248 termcodes[j] = termcodes[j - 1];
4249 break;
4250 }
4251
4252 termcodes[i].name[0] = name[0];
4253 termcodes[i].name[1] = name[1];
4254 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004255 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256
4257 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4258 * accept modifiers. */
4259 termcodes[i].modlen = 0;
4260 j = termcode_star(s, len);
4261 if (j > 0)
4262 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 ++tc_len;
4264}
4265
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004266/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004267 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004268 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004269 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 */
4271 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004272termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004273{
4274 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4275 if (len >= 3 && code[len - 2] == '*')
4276 {
4277 if (len >= 5 && code[len - 3] == ';')
4278 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004279 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 return 1;
4281 }
4282 return 0;
4283}
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004286find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
4288 int i;
4289
4290 for (i = 0; i < tc_len; ++i)
4291 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4292 return termcodes[i].code;
4293 return NULL;
4294}
4295
4296#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4297 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004298get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 if (i >= tc_len)
4301 return NULL;
4302 return &termcodes[i].name[0];
4303}
4304#endif
4305
4306 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004307del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 int i;
4310
4311 if (termcodes == NULL) /* nothing there yet */
4312 return;
4313
4314 need_gather = TRUE; /* need to fill termleader[] */
4315
4316 for (i = 0; i < tc_len; ++i)
4317 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4318 {
4319 del_termcode_idx(i);
4320 return;
4321 }
4322 /* not found. Give error message? */
4323}
4324
4325 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004326del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int i;
4329
4330 vim_free(termcodes[idx].code);
4331 --tc_len;
4332 for (i = idx; i < tc_len; ++i)
4333 termcodes[i] = termcodes[i + 1];
4334}
4335
4336#ifdef FEAT_TERMRESPONSE
4337/*
4338 * Called when detected that the terminal sends 8-bit codes.
4339 * Convert all 7-bit codes to their 8-bit equivalent.
4340 */
4341 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004342switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
4344 int i;
4345 int c;
4346
4347 /* Only need to do something when not already using 8-bit codes. */
4348 if (!term_is_8bit(T_NAME))
4349 {
4350 for (i = 0; i < tc_len; ++i)
4351 {
4352 c = term_7to8bit(termcodes[i].code);
4353 if (c != 0)
4354 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004355 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 termcodes[i].code[0] = c;
4357 }
4358 }
4359 need_gather = TRUE; /* need to fill termleader[] */
4360 }
4361 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004362 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363}
4364#endif
4365
4366#ifdef CHECK_DOUBLE_CLICK
4367static linenr_T orig_topline = 0;
4368# ifdef FEAT_DIFF
4369static int orig_topfill = 0;
4370# endif
4371#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004372#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373/*
4374 * Checking for double clicks ourselves.
4375 * "orig_topline" is used to avoid detecting a double-click when the window
4376 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4377 */
4378/*
4379 * Set orig_topline. Used when jumping to another window, so that a double
4380 * click still works.
4381 */
4382 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004383set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 orig_topline = wp->w_topline;
4386# ifdef FEAT_DIFF
4387 orig_topfill = wp->w_topfill;
4388# endif
4389}
4390#endif
4391
4392/*
4393 * Check if typebuf.tb_buf[] contains a terminal key code.
4394 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4395 * + max_offset].
4396 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004397 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 * With a match, the match is removed, the replacement code is inserted in
4399 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4400 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004401 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4402 * "buflen" is then the length of the string in buf[] and is updated for
4403 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
4405 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004406check_termcode(
4407 int max_offset,
4408 char_u *buf,
4409 int bufsize,
4410 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 char_u *tp;
4413 char_u *p;
4414 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004415 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004417 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int offset;
4419 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004420 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004421 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004422 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 int new_slen;
4424 int extra;
4425 char_u string[MAX_KEY_CODE_LEN + 1];
4426 int i, j;
4427 int idx = 0;
4428#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004429# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4430 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 char_u bytes[6];
4432 int num_bytes;
4433# endif
4434 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 int is_click, is_drag;
4436 int wheel_code = 0;
4437 int current_button;
4438 static int held_button = MOUSE_RELEASE;
4439 static int orig_num_clicks = 1;
4440 static int orig_mouse_code = 0x0;
4441# ifdef CHECK_DOUBLE_CLICK
4442 static int orig_mouse_col = 0;
4443 static int orig_mouse_row = 0;
4444 static struct timeval orig_mouse_time = {0, 0};
4445 /* time of previous mouse click */
4446 struct timeval mouse_time; /* time of current mouse click */
4447 long timediff; /* elapsed time in msec */
4448# endif
4449#endif
4450 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4453
4454 /*
4455 * Speed up the checks for terminal codes by gathering all first bytes
4456 * used in termleader[]. Often this is just a single <Esc>.
4457 */
4458 if (need_gather)
4459 gather_termleader();
4460
4461 /*
4462 * Check at several positions in typebuf.tb_buf[], to catch something like
4463 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4464 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004465 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 * This is used often, KEEP IT FAST!
4467 */
4468 for (offset = 0; offset < max_offset; ++offset)
4469 {
4470 if (buf == NULL)
4471 {
4472 if (offset >= typebuf.tb_len)
4473 break;
4474 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4475 len = typebuf.tb_len - offset; /* length of the input */
4476 }
4477 else
4478 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004479 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004482 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
4485 /*
4486 * Don't check characters after K_SPECIAL, those are already
4487 * translated terminal chars (avoid translating ~@^Hx).
4488 */
4489 if (*tp == K_SPECIAL)
4490 {
4491 offset += 2; /* there are always 2 extra characters */
4492 continue;
4493 }
4494
4495 /*
4496 * Skip this position if the character does not appear as the first
4497 * character in term_strings. This speeds up a lot, since most
4498 * termcodes start with the same character (ESC or CSI).
4499 */
4500 i = *tp;
4501 for (p = termleader; *p && *p != i; ++p)
4502 ;
4503 if (*p == NUL)
4504 continue;
4505
4506 /*
4507 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4508 * are in Insert mode.
4509 */
4510 if (*tp == ESC && !p_ek && (State & INSERT))
4511 continue;
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004514 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517#ifdef FEAT_GUI
4518 if (gui.in_use)
4519 {
4520 /*
4521 * GUI special key codes are all of the form [CSI xx].
4522 */
4523 if (*tp == CSI) /* Special key from GUI */
4524 {
4525 if (len < 3)
4526 return -1; /* Shouldn't happen */
4527 slen = 3;
4528 key_name[0] = tp[1];
4529 key_name[1] = tp[2];
4530 }
4531 }
4532 else
4533#endif /* FEAT_GUI */
4534 {
4535 for (idx = 0; idx < tc_len; ++idx)
4536 {
4537 /*
4538 * Ignore the entry if we are not at the start of
4539 * typebuf.tb_buf[]
4540 * and there are not enough characters to make a match.
4541 * But only when the 'K' flag is in 'cpoptions'.
4542 */
4543 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004544 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (cpo_koffset && offset && len < slen)
4546 continue;
4547 if (STRNCMP(termcodes[idx].code, tp,
4548 (size_t)(slen > len ? len : slen)) == 0)
4549 {
4550 if (len < slen) /* got a partial sequence */
4551 return -1; /* need to get more chars */
4552
4553 /*
4554 * When found a keypad key, check if there is another key
4555 * that matches and use that one. This makes <Home> to be
4556 * found instead of <kHome> when they produce the same
4557 * key code.
4558 */
4559 if (termcodes[idx].name[0] == 'K'
4560 && VIM_ISDIGIT(termcodes[idx].name[1]))
4561 {
4562 for (j = idx + 1; j < tc_len; ++j)
4563 if (termcodes[j].len == slen &&
4564 STRNCMP(termcodes[idx].code,
4565 termcodes[j].code, slen) == 0)
4566 {
4567 idx = j;
4568 break;
4569 }
4570 }
4571
4572 key_name[0] = termcodes[idx].name[0];
4573 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 break;
4575 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004576
4577 /*
4578 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 * <Esc>[123;*X (modslen == slen - 3)
4580 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4581 * When there is a modifier the * matches a number.
4582 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004583 */
4584 if (termcodes[idx].modlen > 0)
4585 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004586 modslen = termcodes[idx].modlen;
4587 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004588 continue;
4589 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004591 {
4592 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004593
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004594 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004595 return -1; /* need to get more chars */
4596
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004597 if (tp[modslen] == termcodes[idx].code[slen - 1])
4598 slen = modslen + 1; /* no modifiers */
4599 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004600 continue; /* no match */
4601 else
4602 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004603 // Skip over the digits, the final char must
4604 // follow. URXVT can use a negative value, thus
4605 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004606 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004607 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004608 ;
4609 ++j;
4610 if (len < j) /* got a partial sequence */
4611 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4613 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004614
Bram Moolenaara529ce02017-06-22 22:37:57 +02004615 modifiers_start = tp + slen - 2;
4616
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004617 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004618 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004619 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004620 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004621 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004622 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004623 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004625 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004626 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004627
4628 slen = j;
4629 }
4630 key_name[0] = termcodes[idx].name[0];
4631 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004632 break;
4633 }
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 }
4637
4638#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004639 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004640 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004641 * detecting the start of these mouse codes they might as well be
4642 * another key code or terminal response. */
4643# ifdef FEAT_MOUSE_DEC
4644 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004645# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004646# ifdef FEAT_MOUSE_PTERM
4647 || key_name[0] == KS_PTERM_MOUSE
4648# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004651 /* Check for some responses from the terminal starting with
4652 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004653 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004654 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004655 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004656 * Also eat other possible responses to t_RV, rxvt returns
4657 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4658 * mrxvt has been reported to have "+" in the version. Assume
4659 * the escape sequence ends with a letter or one of "{|}~".
4660 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004661 * - Cursor position report: <Esc>[{row};{col}R
4662 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004663 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004664 *
4665 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004666 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004667 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004668
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004669 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004670 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004671 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004672 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004674 int col = 0;
4675 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004676 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004677
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004679 for (i = 2 + (tp[0] != CSI); i < len
4680 && !(tp[i] >= '{' && tp[i] <= '~')
4681 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004682 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004683 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004684 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004685 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004688 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004689 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004690 return -1;
4691 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004692 if (extra > 0)
4693 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004694
4695 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4696 * u7_status is not "sent", it may be from a previous Vim that
4697 * just exited. But not for <S-F3>, it sends something
4698 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004699 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004700 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004701 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004702 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004703 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004704
Bram Moolenaarb255b902018-04-24 21:40:10 +02004705 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004706 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004707 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004708 if (col == 2)
4709 aw = "single";
4710 else if (col == 3)
4711 aw = "double";
4712 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4713 {
4714 /* Setting the option causes a screen redraw. Do
4715 * that right away if possible, keeping any
4716 * messages. */
4717 set_option_value((char_u *)"ambw", 0L,
4718 (char_u *)aw, 0);
4719# ifdef DEBUG_TERMRESPONSE
4720 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004721 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004722
Bram Moolenaarb255b902018-04-24 21:40:10 +02004723 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004724 }
4725# else
4726 redraw_asap(CLEAR);
4727# endif
4728 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004729 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004730 key_name[0] = (int)KS_EXTRA;
4731 key_name[1] = (int)KE_IGNORE;
4732 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004733# ifdef FEAT_EVAL
4734 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4735# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004738 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4739 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004741 int version = col;
4742
Bram Moolenaarb255b902018-04-24 21:40:10 +02004743 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004744 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004745 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 /* If this code starts with CSI, you can bet that the
4748 * terminal uses 8-bit codes. */
4749 if (tp[0] == CSI)
4750 switch_to_8bit();
4751
4752 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004753 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 * Ignore it for when the user has set 'term' to xterm,
4755 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004756 if (version > 20000)
4757 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004759 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004761 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004762 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004763 int is_mintty = FALSE;
4764
4765 // mintty 2.9.5 sends 77;20905;0c.
4766 // (77 is ASCII 'M' for mintty.)
4767 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4768 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004771 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004773 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 check_for_codes = TRUE;
4775 need_gather = TRUE;
4776 req_codes_from_term();
4777 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004778
4779 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004780 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004781 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004782 {
4783 /* If run from Vim $COLORS is set to the number of
4784 * colors the terminal supports. Otherwise assume
4785 * 256, libvterm supports even more. */
4786 if (mch_getenv((char_u *)"COLORS") == NULL)
4787 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004788 /* Libvterm can handle SGR mouse reporting. */
4789 if (!option_was_set((char_u *)"ttym"))
4790 set_option_value((char_u *)"ttym", 0L,
4791 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004792 }
4793
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004794 if (version == 95)
4795 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004796 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004797 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4798 {
4799 is_not_xterm = TRUE;
4800 is_mac_terminal = TRUE;
4801 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004802 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004803 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4804 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004805 // old iTerm2 sends 0;95;
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004806 else if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004807 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004808 }
4809
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004810 /* Only set 'ttymouse' automatically if it was not set
4811 * by the user already. */
4812 if (!option_was_set((char_u *)"ttym"))
4813 {
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004814 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004815 * Terminal.app, iTerm2 and mintty. */
4816 if (version >= 277 || is_iterm2 || is_mac_terminal
4817 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004818 set_option_value((char_u *)"ttym", 0L,
4819 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004820 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004821 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004822 set_option_value((char_u *)"ttym", 0L,
4823 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004824 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004825
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004826 /* Detect terminals that set $TERM to something like
4827 * "xterm-256colors" but are not fully xterm
4828 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004829
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004830 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004831 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004832 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004833 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004834 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004835 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004836 is_not_xterm = TRUE;
4837
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004838 /* PuTTY sends 0;136;0
4839 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004840 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004841 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004842 is_not_xterm = TRUE;
4843
4844 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004845 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004846 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004847 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004848
Bram Moolenaar668324e2018-06-30 17:09:26 +02004849 // Xterm first responded to this request at patch level
4850 // 95, so assume anything below 95 is not xterm.
4851 if (version < 95)
4852 is_not_xterm = TRUE;
4853
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004854 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004855 * set. Only supported properly by xterm since version
4856 * 279 (otherwise it returns 0x18).
4857 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004858 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004859 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004860 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004861 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004862 && *T_CSH != NUL
4863 && *T_CRS != NUL)
4864 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004865 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004866 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004867 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004868 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004869 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004870
4871 /* Only request the cursor blink mode if t_RC set. Not
4872 * for Gnome terminal, it can't handle t_RC, it
4873 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004874 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004875 && !is_not_xterm
4876 && *T_CRC != NUL)
4877 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004878 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004879 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004880 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004881 need_flush = TRUE;
4882 }
4883
4884 if (need_flush)
4885 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004887 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004889 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 apply_autocmds(EVENT_TERMRESPONSE,
4892 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 key_name[0] = (int)KS_EXTRA;
4894 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004896
Bram Moolenaar4db25542017-08-28 22:43:05 +02004897 /* Check blinking cursor from xterm:
4898 * {lead}?12;1$y set
4899 * {lead}?12;2$y not set
4900 *
4901 * {lead} can be <Esc>[ or CSI
4902 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004903 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004904 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4905 && i == j + 6
4906 && tp[j + 1] == '1'
4907 && tp[j + 2] == '2'
4908 && tp[j + 3] == ';'
4909 && tp[i - 1] == '$'
4910 && tp[i] == 'y')
4911 {
4912 initial_cursor_blink = (tp[j + 4] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004913 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004914 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004915 key_name[0] = (int)KS_EXTRA;
4916 key_name[1] = (int)KE_IGNORE;
4917 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004918# ifdef FEAT_EVAL
4919 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4920# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004921 }
4922
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004923 /*
4924 * Check for a window position response from the terminal:
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004925 * {lead}3;{x};{y}t
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004926 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004927 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004928 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4929 || (len >= 3 && tp[0] == CSI))
4930 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4931 && tp[j + 1] == ';')
4932 {
4933 j += 2;
4934 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4935 ;
4936 if (i < len && tp[i] == ';')
4937 {
4938 winpos_x = atoi((char *)tp + j);
4939 j = i + 1;
4940 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4941 ;
4942 if (i < len && tp[i] == 't')
4943 {
4944 winpos_y = atoi((char *)tp + j);
4945 /* got finished code: consume it */
4946 key_name[0] = (int)KS_EXTRA;
4947 key_name[1] = (int)KE_IGNORE;
4948 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004949
4950 if (--did_request_winpos <= 0)
Bram Moolenaarafd78262019-05-10 23:10:31 +02004951 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004952 }
4953 }
4954 if (i == len)
4955 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004956 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004957 return -1;
4958 }
4959 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004960 }
4961
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004962 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004963 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004964 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004965 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004966 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004967 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004968 * {lead} can be <Esc>] or OSC
4969 * {tail} can be '\007', <Esc>\ or STERM.
4970 *
4971 * Consume any code that starts with "{lead}11;", it's also
4972 * possible that "rgba" is following.
4973 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004974 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004975 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4976 || tp[0] == OSC))
4977 {
4978 j = 1 + (tp[0] == ESC);
4979 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004980 || (argp[1] != '1' && argp[1] != '0')
4981 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004982 i = 0; /* no match */
4983 else
4984 for (i = j; i < len; ++i)
4985 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4986 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004987 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004988 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004989 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4990 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004991
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004992 if (i - j >= 14 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4993 && (is_4digit
4994 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004995 {
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004996# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004997 int rval, gval, bval;
4998
4999 rval = hexhex2nr(tp + j + 7);
5000 gval = hexhex2nr(tp + j + (is_4digit ? 12 : 10));
5001 bval = hexhex2nr(tp + j + (is_4digit ? 17 : 13));
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005002# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005003 if (is_bg)
5004 {
5005 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005006 + tp[j+17]) ? "light" : "dark";
5007
Bram Moolenaarb255b902018-04-24 21:40:10 +02005008 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005009 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005010# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005011 bg_r = rval;
5012 bg_g = gval;
5013 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005014# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005015 if (!option_was_set((char_u *)"bg")
5016 && STRCMP(p_bg, newval) != 0)
5017 {
5018 /* value differs, apply it */
5019 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005020 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005021 reset_option_was_set((char_u *)"bg");
5022 redraw_asap(CLEAR);
5023 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005024 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005025# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005026 else
5027 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005028 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005029 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005030 fg_r = rval;
5031 fg_g = gval;
5032 fg_b = bval;
5033 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005034# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005035 }
5036
5037 /* got finished code: consume it */
5038 key_name[0] = (int)KS_EXTRA;
5039 key_name[1] = (int)KE_IGNORE;
5040 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005041# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005042 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5043 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005044# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005045 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005046 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005047 if (i == len)
5048 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005049 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005050 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005054 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005055 * {lead}{flag}+r<hex bytes><{tail}
5056 *
5057 * {lead} can be <Esc>P or DCS
5058 * {flag} can be '0' or '1'
5059 * {tail} can be Esc>\ or STERM
5060 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005061 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01005062 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005063 *
5064 * {lead} can be <Esc>P or DCS
5065 * {tail} can be Esc>\ or STERM
5066 *
5067 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005068 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02005069 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005070 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 || tp[0] == DCS))
5072 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005073 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005074 if (len < j + 3)
5075 i = len; /* need more chars */
5076 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005077 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005078 else if (argp[1] == '+')
5079 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005080 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005081 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005082 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 || tp[i] == STERM)
5084 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005085 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 got_code_from_term(tp + j, i);
5087 key_name[0] = (int)KS_EXTRA;
5088 key_name[1] = (int)KE_IGNORE;
5089 slen = i + 1 + (tp[i] == ESC);
5090 break;
5091 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005092 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005093 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005094 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005095 /* Probably the cursor shape response. Make sure that "i"
5096 * is equal to "len" when there are not sufficient
5097 * characters. */
5098 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005099 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005100 if (i - j == 3 && !isdigit(tp[i]))
5101 break;
5102 if (i - j == 4 && tp[i] != ' ')
5103 break;
5104 if (i - j == 5 && tp[i] != 'q')
5105 break;
5106 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5107 break;
5108 if ((i - j == 6 && tp[i] == STERM)
5109 || (i - j == 7 && tp[i] == '\\'))
5110 {
5111 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005112
Bram Moolenaar590ec872018-03-02 20:58:42 +01005113 /* 0, 1 = block blink, 2 = block
5114 * 3 = underline blink, 4 = underline
5115 * 5 = vertical bar blink, 6 = vertical bar */
5116 number = number == 0 ? 1 : number;
5117 initial_cursor_shape = (number + 1) / 2;
5118 /* The blink flag is actually inverted, compared to
5119 * the value set with T_SH. */
5120 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005121 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005122 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005123 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005124
Bram Moolenaar590ec872018-03-02 20:58:42 +01005125 key_name[0] = (int)KS_EXTRA;
5126 key_name[1] = (int)KE_IGNORE;
5127 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005128# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005129 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005130# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005131 break;
5132 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005133 }
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005137 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005138 /* These codes arrive many together, each code can be
5139 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005140 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005141 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 }
5145#endif
5146
5147 if (key_name[0] == NUL)
5148 continue; /* No match at this position, try next one */
5149
5150 /* We only get here when we have a complete termcode match */
5151
5152#ifdef FEAT_MOUSE
5153# ifdef FEAT_GUI
5154 /*
5155 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5156 * so that we know which window to scroll later.
5157 */
5158 if (gui.in_use
5159 && key_name[0] == (int)KS_EXTRA
5160 && (key_name[1] == (int)KE_X1MOUSE
5161 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005162 || key_name[1] == (int)KE_MOUSELEFT
5163 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 || key_name[1] == (int)KE_MOUSEDOWN
5165 || key_name[1] == (int)KE_MOUSEUP))
5166 {
5167 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5168 if (num_bytes == -1) /* not enough coordinates */
5169 return -1;
5170 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5171 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5172 slen += num_bytes;
5173 }
5174 else
5175# endif
5176 /*
5177 * If it is a mouse click, get the coordinates.
5178 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005179 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005180# ifdef FEAT_MOUSE_GPM
5181 || key_name[0] == KS_GPM_MOUSE
5182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005184 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185# endif
5186# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005187 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188# endif
5189# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005190 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191# endif
5192# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005193 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005195# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005196 || key_name[0] == KS_URXVT_MOUSE
5197# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005198 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005199 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
5201 is_click = is_drag = FALSE;
5202
Bram Moolenaar864207d2008-06-24 22:14:38 +00005203# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5204 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005205 if (key_name[0] == KS_MOUSE
5206# ifdef FEAT_MOUSE_GPM
5207 || key_name[0] == KS_GPM_MOUSE
5208# endif
5209 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005212 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 * s == encoded button state:
5214 * 0x20 = left button down
5215 * 0x21 = middle button down
5216 * 0x22 = right button down
5217 * 0x23 = any button release
5218 * 0x60 = button 4 down (scroll wheel down)
5219 * 0x61 = button 5 down (scroll wheel up)
5220 * add 0x04 for SHIFT
5221 * add 0x08 for ALT
5222 * add 0x10 for CTRL
5223 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005224 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5225 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 * c == column + ' ' + 1 == column + 33
5227 * r == row + ' ' + 1 == row + 33
5228 *
5229 * The coordinates are passed on through global variables.
5230 * Ugly, but this avoids trouble with mouse clicks at an
5231 * unexpected moment and allows for mapping them.
5232 */
5233 for (;;)
5234 {
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005235# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (gui.in_use)
5237 {
5238 /* GUI uses more bits for columns > 223 */
5239 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5240 if (num_bytes == -1) /* not enough coordinates */
5241 return -1;
5242 mouse_code = bytes[0];
5243 mouse_col = 128 * (bytes[1] - ' ' - 1)
5244 + bytes[2] - ' ' - 1;
5245 mouse_row = 128 * (bytes[3] - ' ' - 1)
5246 + bytes[4] - ' ' - 1;
5247 }
5248 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5252 if (num_bytes == -1) /* not enough coordinates */
5253 return -1;
5254 mouse_code = bytes[0];
5255 mouse_col = bytes[1] - ' ' - 1;
5256 mouse_row = bytes[2] - ' ' - 1;
5257 }
5258 slen += num_bytes;
5259
5260 /* If the following bytes is also a mouse code and it has
5261 * the same code, dump this one and get the next. This
5262 * makes dragging a whole lot faster. */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005263# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (gui.in_use)
5265 j = 3;
5266 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 j = termcodes[idx].len;
5269 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5270 && tp[slen + j] == mouse_code
5271 && tp[slen + j + 1] != NUL
5272 && tp[slen + j + 2] != NUL
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005273# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 && (!gui.in_use
5275 || (tp[slen + j + 3] != NUL
5276 && tp[slen + j + 4] != NUL))
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 )
5279 slen += j;
5280 else
5281 break;
5282 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005285 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005286 || key_name[0] == KS_SGR_MOUSE
5287 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005288 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005289 /* URXVT 1015 mouse reporting mode:
5290 * Almost identical to xterm mouse mode, except the values
5291 * are decimal instead of bytes.
5292 *
5293 * \033[%d;%d;%dM
5294 * ^-- row
5295 * ^----- column
5296 * ^-------- code
5297 *
5298 * SGR 1006 mouse reporting mode:
5299 * Almost identical to xterm mouse mode, except the values
5300 * are decimal instead of bytes.
5301 *
5302 * \033[<%d;%d;%dM
5303 * ^-- row
5304 * ^----- column
5305 * ^-------- code
5306 *
5307 * \033[<%d;%d;%dm : mouse release event
5308 * ^-- row
5309 * ^----- column
5310 * ^-------- code
5311 */
5312 p = modifiers_start;
5313 if (p == NULL)
5314 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005315
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005316 mouse_code = getdigits(&p);
5317 if (*p++ != ';')
5318 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005319
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005320 /* when mouse reporting is SGR, add 32 to mouse code */
5321 if (key_name[0] == KS_SGR_MOUSE
5322 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5323 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005324
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005325 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5326 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005327
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005328 mouse_col = getdigits(&p) - 1;
5329 if (*p++ != ';')
5330 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005331
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005332 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005333
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005334 /* The modifiers were the mouse coordinates, not the
5335 * modifier keys (alt/shift/ctrl/meta) state. */
5336 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005337 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005338
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005339 if (key_name[0] == KS_MOUSE
5340# ifdef FEAT_MOUSE_GPM
5341 || key_name[0] == KS_GPM_MOUSE
5342# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005343# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005344 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005345# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005346 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005347 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005348 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005349# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 /*
5351 * Handle mouse events.
5352 * Recognize the xterm mouse wheel, but not in the GUI, the
5353 * Linux console with GPM and the MS-DOS or Win32 console
5354 * (multi-clicks use >= 0x60).
5355 */
5356 if (mouse_code >= MOUSEWHEEL_LOW
5357# ifdef FEAT_GUI
5358 && !gui.in_use
5359# endif
5360# ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005361 && key_name[0] != KS_GPM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362# endif
5363 )
5364 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005365# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5366 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5367 /* mouse-move event, using MOUSE_DRAG works */
5368 mouse_code = MOUSE_DRAG;
5369 else
5370# endif
5371 /* Keep the mouse_code before it's changed, so that we
5372 * remember that it was a mouse wheel click. */
5373 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375# ifdef FEAT_MOUSE_XTERM
5376 else if (held_button == MOUSE_RELEASE
5377# ifdef FEAT_GUI
5378 && !gui.in_use
5379# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005380 && (mouse_code == 0x23 || mouse_code == 0x24
5381 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005383 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5384 * And 0x40 and 0x41 are used by some xterm emulator. */
5385 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5386 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388# endif
5389
5390# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5391 else if (use_xterm_mouse() > 1)
5392 {
5393 if (mouse_code & MOUSE_DRAG_XTERM)
5394 mouse_code |= MOUSE_DRAG;
5395 }
5396# endif
5397# ifdef FEAT_XCLIPBOARD
5398 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5399 {
5400 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5401 stop_xterm_trace();
5402 else
5403 start_xterm_trace(mouse_code);
5404 }
5405# endif
5406# endif
5407 }
5408# endif /* !UNIX || FEAT_MOUSE_XTERM */
5409# ifdef FEAT_MOUSE_NET
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005410 if (key_name[0] == KS_NETTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 int mc, mr;
5413
5414 /* expect a rather limited sequence like: balancing {
5415 * \033}6,45\r
5416 * '6' is the row, 45 is the column
5417 */
5418 p = tp + slen;
5419 mr = getdigits(&p);
5420 if (*p++ != ',')
5421 return -1;
5422 mc = getdigits(&p);
5423 if (*p++ != '\r')
5424 return -1;
5425
5426 mouse_col = mc - 1;
5427 mouse_row = mr - 1;
5428 mouse_code = MOUSE_LEFT;
5429 slen += (int)(p - (tp + slen));
5430 }
5431# endif /* FEAT_MOUSE_NET */
5432# ifdef FEAT_MOUSE_JSB
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005433 if (key_name[0] == KS_JSBTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 int mult, val, iter, button, status;
5436
5437 /* JSBTERM Input Model
5438 * \033[0~zw uniq escape sequence
5439 * (L-x) Left button pressed - not pressed x not reporting
5440 * (M-x) Middle button pressed - not pressed x not reporting
5441 * (R-x) Right button pressed - not pressed x not reporting
5442 * (SDmdu) Single , Double click, m mouse move d button down
5443 * u button up
5444 * ### X cursor position padded to 3 digits
5445 * ### Y cursor position padded to 3 digits
5446 * (s-x) SHIFT key pressed - not pressed x not reporting
5447 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005448 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 */
5450
5451 p = tp + slen;
5452 button = mouse_code = 0;
5453 switch (*p++)
5454 {
5455 case 'L': button = 1; break;
5456 case '-': break;
5457 case 'x': break; /* ignore sequence */
5458 default: return -1; /* Unknown Result */
5459 }
5460 switch (*p++)
5461 {
5462 case 'M': button |= 2; break;
5463 case '-': break;
5464 case 'x': break; /* ignore sequence */
5465 default: return -1; /* Unknown Result */
5466 }
5467 switch (*p++)
5468 {
5469 case 'R': button |= 4; break;
5470 case '-': break;
5471 case 'x': break; /* ignore sequence */
5472 default: return -1; /* Unknown Result */
5473 }
5474 status = *p++;
5475 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5476 mult /= 10, p++)
5477 if (*p >= '0' && *p <= '9')
5478 val += (*p - '0') * mult;
5479 else
5480 return -1;
5481 mouse_col = val;
5482 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5483 mult /= 10, p++)
5484 if (*p >= '0' && *p <= '9')
5485 val += (*p - '0') * mult;
5486 else
5487 return -1;
5488 mouse_row = val;
5489 switch (*p++)
5490 {
5491 case 's': button |= 8; break; /* SHIFT key Pressed */
5492 case '-': break; /* Not Pressed */
5493 case 'x': break; /* Not Reporting */
5494 default: return -1; /* Unknown Result */
5495 }
5496 switch (*p++)
5497 {
5498 case 'c': button |= 16; break; /* CTRL key Pressed */
5499 case '-': break; /* Not Pressed */
5500 case 'x': break; /* Not Reporting */
5501 default: return -1; /* Unknown Result */
5502 }
5503 if (*p++ != '\033')
5504 return -1;
5505 if (*p++ != '\\')
5506 return -1;
5507 switch (status)
5508 {
5509 case 'D': /* Double Click */
5510 case 'S': /* Single Click */
5511 if (button & 1) mouse_code |= MOUSE_LEFT;
5512 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5513 if (button & 4) mouse_code |= MOUSE_RIGHT;
5514 if (button & 8) mouse_code |= MOUSE_SHIFT;
5515 if (button & 16) mouse_code |= MOUSE_CTRL;
5516 break;
5517 case 'm': /* Mouse move */
5518 if (button & 1) mouse_code |= MOUSE_LEFT;
5519 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5520 if (button & 4) mouse_code |= MOUSE_RIGHT;
5521 if (button & 8) mouse_code |= MOUSE_SHIFT;
5522 if (button & 16) mouse_code |= MOUSE_CTRL;
5523 if ((button & 7) != 0)
5524 {
5525 held_button = mouse_code;
5526 mouse_code |= MOUSE_DRAG;
5527 }
5528 is_drag = TRUE;
5529 showmode();
5530 break;
5531 case 'd': /* Button Down */
5532 if (button & 1) mouse_code |= MOUSE_LEFT;
5533 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5534 if (button & 4) mouse_code |= MOUSE_RIGHT;
5535 if (button & 8) mouse_code |= MOUSE_SHIFT;
5536 if (button & 16) mouse_code |= MOUSE_CTRL;
5537 break;
5538 case 'u': /* Button Up */
5539 if (button & 1)
5540 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5541 if (button & 2)
5542 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5543 if (button & 4)
5544 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5545 if (button & 8)
5546 mouse_code |= MOUSE_SHIFT;
5547 if (button & 16)
5548 mouse_code |= MOUSE_CTRL;
5549 break;
5550 default: return -1; /* Unknown Result */
5551 }
5552
5553 slen += (p - (tp + slen));
5554 }
5555# endif /* FEAT_MOUSE_JSB */
5556# ifdef FEAT_MOUSE_DEC
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005557 if (key_name[0] == KS_DEC_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 /* The DEC Locator Input Model
5560 * Netterm delivers the code sequence:
5561 * \033[2;4;24;80&w (left button down)
5562 * \033[3;0;24;80&w (left button up)
5563 * \033[6;1;24;80&w (right button down)
5564 * \033[7;0;24;80&w (right button up)
5565 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5566 * Pe is the event code
5567 * Pb is the button code
5568 * Pr is the row coordinate
5569 * Pc is the column coordinate
5570 * Pp is the third coordinate (page number)
5571 * Pe, the event code indicates what event caused this report
5572 * The following event codes are defined:
5573 * 0 - request, the terminal received an explicit request
5574 * for a locator report, but the locator is unavailable
5575 * 1 - request, the terminal received an explicit request
5576 * for a locator report
5577 * 2 - left button down
5578 * 3 - left button up
5579 * 4 - middle button down
5580 * 5 - middle button up
5581 * 6 - right button down
5582 * 7 - right button up
5583 * 8 - fourth button down
5584 * 9 - fourth button up
5585 * 10 - locator outside filter rectangle
5586 * Pb, the button code, ASCII decimal 0-15 indicating which
5587 * buttons are down if any. The state of the four buttons
5588 * on the locator correspond to the low four bits of the
5589 * decimal value,
5590 * "1" means button depressed
5591 * 0 - no buttons down,
5592 * 1 - right,
5593 * 2 - middle,
5594 * 4 - left,
5595 * 8 - fourth
5596 * Pr is the row coordinate of the locator position in the page,
5597 * encoded as an ASCII decimal value.
5598 * If Pr is omitted, the locator position is undefined
5599 * (outside the terminal window for example).
5600 * Pc is the column coordinate of the locator position in the
5601 * page, encoded as an ASCII decimal value.
5602 * If Pc is omitted, the locator position is undefined
5603 * (outside the terminal window for example).
5604 * Pp is the page coordinate of the locator position
5605 * encoded as an ASCII decimal value.
5606 * The page coordinate may be omitted if the locator is on
5607 * page one (the default). We ignore it anyway.
5608 */
5609 int Pe, Pb, Pr, Pc;
5610
5611 p = tp + slen;
5612
5613 /* get event status */
5614 Pe = getdigits(&p);
5615 if (*p++ != ';')
5616 return -1;
5617
5618 /* get button status */
5619 Pb = getdigits(&p);
5620 if (*p++ != ';')
5621 return -1;
5622
5623 /* get row status */
5624 Pr = getdigits(&p);
5625 if (*p++ != ';')
5626 return -1;
5627
5628 /* get column status */
5629 Pc = getdigits(&p);
5630
5631 /* the page parameter is optional */
5632 if (*p == ';')
5633 {
5634 p++;
5635 (void)getdigits(&p);
5636 }
5637 if (*p++ != '&')
5638 return -1;
5639 if (*p++ != 'w')
5640 return -1;
5641
5642 mouse_code = 0;
5643 switch (Pe)
5644 {
5645 case 0: return -1; /* position request while unavailable */
5646 case 1: /* a response to a locator position request includes
5647 the status of all buttons */
5648 Pb &= 7; /* mask off and ignore fourth button */
5649 if (Pb & 4)
5650 mouse_code = MOUSE_LEFT;
5651 if (Pb & 2)
5652 mouse_code = MOUSE_MIDDLE;
5653 if (Pb & 1)
5654 mouse_code = MOUSE_RIGHT;
5655 if (Pb)
5656 {
5657 held_button = mouse_code;
5658 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005659 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 is_drag = TRUE;
5662 showmode();
5663 break;
5664 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005665 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 break;
5667 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5668 break;
5669 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005670 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 break;
5672 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5673 break;
5674 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005675 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 break;
5677 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5678 break;
5679 case 8: return -1; /* fourth button down */
5680 case 9: return -1; /* fourth button up */
5681 case 10: return -1; /* mouse outside of filter rectangle */
5682 default: return -1; /* should never occur */
5683 }
5684
5685 mouse_col = Pc - 1;
5686 mouse_row = Pr - 1;
5687
5688 slen += (int)(p - (tp + slen));
5689 }
5690# endif /* FEAT_MOUSE_DEC */
5691# ifdef FEAT_MOUSE_PTERM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005692 if (key_name[0] == KS_PTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005694 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
5696 p = tp + slen;
5697
5698 action = getdigits(&p);
5699 if (*p++ != ';')
5700 return -1;
5701
5702 mouse_row = getdigits(&p);
5703 if (*p++ != ';')
5704 return -1;
5705 mouse_col = getdigits(&p);
5706 if (*p++ != ';')
5707 return -1;
5708
5709 button = getdigits(&p);
5710 mouse_code = 0;
5711
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005712 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 case 4: mouse_code = MOUSE_LEFT; break;
5715 case 1: mouse_code = MOUSE_RIGHT; break;
5716 case 2: mouse_code = MOUSE_MIDDLE; break;
5717 default: return -1;
5718 }
5719
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005720 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 case 31: /* Initial press */
5723 if (*p++ != ';')
5724 return -1;
5725
5726 num_clicks = getdigits(&p); /* Not used */
5727 break;
5728
5729 case 32: /* Release */
5730 mouse_code |= MOUSE_RELEASE;
5731 break;
5732
5733 case 33: /* Drag */
5734 held_button = mouse_code;
5735 mouse_code |= MOUSE_DRAG;
5736 break;
5737
5738 default:
5739 return -1;
5740 }
5741
5742 if (*p++ != 't')
5743 return -1;
5744
5745 slen += (p - (tp + slen));
5746 }
5747# endif /* FEAT_MOUSE_PTERM */
5748
5749 /* Interpret the mouse code */
5750 current_button = (mouse_code & MOUSE_CLICK_MASK);
5751 if (current_button == MOUSE_RELEASE
5752# ifdef FEAT_MOUSE_XTERM
5753 && wheel_code == 0
5754# endif
5755 )
5756 {
5757 /*
5758 * If we get a mouse drag or release event when
5759 * there is no mouse button held down (held_button ==
5760 * MOUSE_RELEASE), produce a K_IGNORE below.
5761 * (can happen when you hold down two buttons
5762 * and then let them go, or click in the menu bar, but not
5763 * on a menu, and drag into the text).
5764 */
5765 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5766 is_drag = TRUE;
5767 current_button = held_button;
5768 }
5769 else if (wheel_code == 0)
5770 {
5771# ifdef CHECK_DOUBLE_CLICK
5772# ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 /*
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005774 * Only for Unix, when GUI not active, we handle
5775 * multi-clicks here, but not for GPM mouse events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005777# ifdef FEAT_GUI
5778 if (key_name[0] != KS_GPM_MOUSE && !gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779# else
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005780 if (key_name[0] != KS_GPM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781# endif
5782# else
5783# ifdef FEAT_GUI
5784 if (!gui.in_use)
5785# endif
5786# endif
5787 {
5788 /*
5789 * Compute the time elapsed since the previous mouse click.
5790 */
5791 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005792 if (orig_mouse_time.tv_sec == 0)
5793 {
5794 /*
5795 * Avoid computing the difference between mouse_time
5796 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005797 * difference would be huge and would cause
5798 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005799 */
5800 timediff = p_mouset;
5801 }
5802 else
5803 {
5804 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005805 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005806 if (timediff < 0)
5807 --orig_mouse_time.tv_sec;
5808 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005809 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 orig_mouse_time = mouse_time;
5812 if (mouse_code == orig_mouse_code
5813 && timediff < p_mouset
5814 && orig_num_clicks != 4
5815 && orig_mouse_col == mouse_col
5816 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005817 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005819 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005821 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005822 /* Double click in tab pages line also works
5823 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005824 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005825 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 ++orig_num_clicks;
5827 else
5828 orig_num_clicks = 1;
5829 orig_mouse_col = mouse_col;
5830 orig_mouse_row = mouse_row;
5831 orig_topline = curwin->w_topline;
5832#ifdef FEAT_DIFF
5833 orig_topfill = curwin->w_topfill;
5834#endif
5835 }
5836# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5837 else
5838 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5839# endif
5840# else
5841 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5842# endif
5843 is_click = TRUE;
5844 orig_mouse_code = mouse_code;
5845 }
5846 if (!is_drag)
5847 held_button = mouse_code & MOUSE_CLICK_MASK;
5848
5849 /*
5850 * Translate the actual mouse event into a pseudo mouse event.
5851 * First work out what modifiers are to be used.
5852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (orig_mouse_code & MOUSE_SHIFT)
5854 modifiers |= MOD_MASK_SHIFT;
5855 if (orig_mouse_code & MOUSE_CTRL)
5856 modifiers |= MOD_MASK_CTRL;
5857 if (orig_mouse_code & MOUSE_ALT)
5858 modifiers |= MOD_MASK_ALT;
5859 if (orig_num_clicks == 2)
5860 modifiers |= MOD_MASK_2CLICK;
5861 else if (orig_num_clicks == 3)
5862 modifiers |= MOD_MASK_3CLICK;
5863 else if (orig_num_clicks == 4)
5864 modifiers |= MOD_MASK_4CLICK;
5865
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005866 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5867 * added, then it's not mouse up/down. */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005868 key_name[0] = KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005869 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005870 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005871 {
5872 if (wheel_code & MOUSE_CTRL)
5873 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005874 if (wheel_code & MOUSE_ALT)
5875 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 key_name[1] = (wheel_code & 1)
5877 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005878 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 else
5881 key_name[1] = get_pseudo_mouse_code(current_button,
5882 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005883
5884 /* Make sure the mouse position is valid. Some terminals may
5885 * return weird values. */
5886 if (mouse_col >= Columns)
5887 mouse_col = Columns - 1;
5888 if (mouse_row >= Rows)
5889 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891#endif /* FEAT_MOUSE */
5892
5893#ifdef FEAT_GUI
5894 /*
5895 * If using the GUI, then we get menu and scrollbar events.
5896 *
5897 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5898 * four bytes which are to be taken as a pointer to the vimmenu_T
5899 * structure.
5900 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005901 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005902 * is one byte with the tab index.
5903 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5905 * by one byte representing the scrollbar number, and then four bytes
5906 * representing a long_u which is the new value of the scrollbar.
5907 *
5908 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5909 * KE_FILLER followed by four bytes representing a long_u which is the
5910 * new value of the scrollbar.
5911 */
5912# ifdef FEAT_MENU
5913 else if (key_name[0] == (int)KS_MENU)
5914 {
5915 long_u val;
5916
5917 num_bytes = get_long_from_buf(tp + slen, &val);
5918 if (num_bytes == -1)
5919 return -1;
5920 current_menu = (vimmenu_T *)val;
5921 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005922
5923 /* The menu may have been deleted right after it was used, check
5924 * for that. */
5925 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5926 {
5927 key_name[0] = KS_EXTRA;
5928 key_name[1] = (int)KE_IGNORE;
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005932# ifdef FEAT_GUI_TABLINE
5933 else if (key_name[0] == (int)KS_TABLINE)
5934 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005935 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005936 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5937 if (num_bytes == -1)
5938 return -1;
5939 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005940 if (current_tab == 255) /* -1 in a byte gives 255 */
5941 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005942 slen += num_bytes;
5943 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005944 else if (key_name[0] == (int)KS_TABMENU)
5945 {
5946 /* Selecting tabline tab or using its menu. */
5947 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5948 if (num_bytes == -1)
5949 return -1;
5950 current_tab = (int)bytes[0];
5951 current_tabmenu = (int)bytes[1];
5952 slen += num_bytes;
5953 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955# ifndef USE_ON_FLY_SCROLL
5956 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5957 {
5958 long_u val;
5959
5960 /* Get the last scrollbar event in the queue of the same type */
5961 j = 0;
5962 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5963 && tp[j + 2] != NUL; ++i)
5964 {
5965 j += 3;
5966 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5967 if (num_bytes == -1)
5968 break;
5969 if (i == 0)
5970 current_scrollbar = (int)bytes[0];
5971 else if (current_scrollbar != (int)bytes[0])
5972 break;
5973 j += num_bytes;
5974 num_bytes = get_long_from_buf(tp + j, &val);
5975 if (num_bytes == -1)
5976 break;
5977 scrollbar_value = val;
5978 j += num_bytes;
5979 slen = j;
5980 }
5981 if (i == 0) /* not enough characters to make one */
5982 return -1;
5983 }
5984 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5985 {
5986 long_u val;
5987
5988 /* Get the last horiz. scrollbar event in the queue */
5989 j = 0;
5990 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5991 && tp[j + 2] != NUL; ++i)
5992 {
5993 j += 3;
5994 num_bytes = get_long_from_buf(tp + j, &val);
5995 if (num_bytes == -1)
5996 break;
5997 scrollbar_value = val;
5998 j += num_bytes;
5999 slen = j;
6000 }
6001 if (i == 0) /* not enough characters to make one */
6002 return -1;
6003 }
6004# endif /* !USE_ON_FLY_SCROLL */
6005#endif /* FEAT_GUI */
6006
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006007 /*
6008 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6009 */
6010 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6011
6012 /*
6013 * Add any modifier codes to our string.
6014 */
6015 new_slen = 0; /* Length of what will replace the termcode */
6016 if (modifiers != 0)
6017 {
6018 /* Some keys have the modifier included. Need to handle that here
6019 * to make mappings work. */
6020 key = simplify_key(key, &modifiers);
6021 if (modifiers != 0)
6022 {
6023 string[new_slen++] = K_SPECIAL;
6024 string[new_slen++] = (int)KS_MODIFIER;
6025 string[new_slen++] = modifiers;
6026 }
6027 }
6028
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006030 key_name[0] = KEY2TERMCAP0(key);
6031 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006033 {
6034 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00006035 if (has_mbyte)
6036 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6037 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006038 string[new_slen++] = key_name[1];
6039 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006040 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6041 && key_name[1] == KE_IGNORE)
6042 {
6043 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6044 * to indicate what happened. */
6045 retval = KEYLEN_REMOVED;
6046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 else
6048 {
6049 string[new_slen++] = K_SPECIAL;
6050 string[new_slen++] = key_name[0];
6051 string[new_slen++] = key_name[1];
6052 }
6053 string[new_slen] = NUL;
6054 extra = new_slen - slen;
6055 if (buf == NULL)
6056 {
6057 if (extra < 0)
6058 /* remove matched chars, taking care of noremap */
6059 del_typebuf(-extra, offset);
6060 else if (extra > 0)
6061 /* insert the extra space we need */
6062 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
6063
6064 /*
6065 * Careful: del_typebuf() and ins_typebuf() may have reallocated
6066 * typebuf.tb_buf[]!
6067 */
6068 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
6069 (size_t)new_slen);
6070 }
6071 else
6072 {
6073 if (extra < 0)
6074 /* remove matched characters */
6075 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006076 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006078 {
6079 /* Insert the extra space we need. If there is insufficient
6080 * space return -1. */
6081 if (*buflen + extra + new_slen >= bufsize)
6082 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006084 (size_t)(*buflen - offset));
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006087 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006089 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
6091
Bram Moolenaar2951b772013-07-03 12:45:31 +02006092#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006093 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006094#endif
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return 0; /* no match found */
6097}
6098
Bram Moolenaar9377df32017-10-15 13:22:01 +02006099#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006100/*
6101 * Get the text foreground color, if known.
6102 */
6103 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006104term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006105{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006106 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006107 {
6108 *r = fg_r;
6109 *g = fg_g;
6110 *b = fg_b;
6111 }
6112}
6113
6114/*
6115 * Get the text background color, if known.
6116 */
6117 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006118term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006119{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006120 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006121 {
6122 *r = bg_r;
6123 *g = bg_g;
6124 *b = bg_b;
6125 }
6126}
6127#endif
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129/*
6130 * Replace any terminal code strings in from[] with the equivalent internal
6131 * vim representation. This is used for the "from" and "to" part of a
6132 * mapping, and the "to" part of a menu command.
6133 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6134 * '<'.
6135 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6136 *
6137 * The replacement is done in result[] and finally copied into allocated
6138 * memory. If this all works well *bufp is set to the allocated memory and a
6139 * pointer to it is returned. If something fails *bufp is set to NULL and from
6140 * is returned.
6141 *
6142 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6143 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6144 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6145 * instead of a CTRL-V.
6146 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006147 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006148replace_termcodes(
6149 char_u *from,
6150 char_u **bufp,
6151 int from_part,
6152 int do_lt, /* also translate <lt> */
6153 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 int i;
6156 int slen;
6157 int key;
6158 int dlen = 0;
6159 char_u *src;
6160 int do_backslash; /* backslash is a special character */
6161 int do_special; /* recognize <> key codes */
6162 int do_key_code; /* recognize raw key codes */
6163 char_u *result; /* buffer for resulting string */
6164
6165 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006166 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6168
6169 /*
6170 * Allocate space for the translation. Worst case a single character is
6171 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6172 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02006173 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (result == NULL) /* out of memory */
6175 {
6176 *bufp = NULL;
6177 return from;
6178 }
6179
6180 src = from;
6181
6182 /*
6183 * Check for #n at start only: function key n
6184 */
6185 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6186 {
6187 result[dlen++] = K_SPECIAL;
6188 result[dlen++] = 'k';
6189 if (src[1] == '0')
6190 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6191 else
6192 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6193 src += 2;
6194 }
6195
6196 /*
6197 * Copy each byte from *from to result[dlen]
6198 */
6199 while (*src != NUL)
6200 {
6201 /*
6202 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006203 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
6205 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6206 {
6207#ifdef FEAT_EVAL
6208 /*
6209 * Replace <SID> by K_SNR <script-nr> _.
6210 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6211 */
6212 if (STRNICMP(src, "<SID>", 5) == 0)
6213 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006214 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006215 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 else
6217 {
6218 src += 5;
6219 result[dlen++] = K_SPECIAL;
6220 result[dlen++] = (int)KS_EXTRA;
6221 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006222 sprintf((char *)result + dlen, "%ld",
6223 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 dlen += (int)STRLEN(result + dlen);
6225 result[dlen++] = '_';
6226 continue;
6227 }
6228 }
6229#endif
6230
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006231 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (slen)
6233 {
6234 dlen += slen;
6235 continue;
6236 }
6237 }
6238
6239 /*
6240 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6241 * Note that this is also checked after replacing the <> form.
6242 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6243 * it could be a character in the file.
6244 */
6245 if (do_key_code)
6246 {
6247 i = find_term_bykeys(src);
6248 if (i >= 0)
6249 {
6250 result[dlen++] = K_SPECIAL;
6251 result[dlen++] = termcodes[i].name[0];
6252 result[dlen++] = termcodes[i].name[1];
6253 src += termcodes[i].len;
6254 /* If terminal code matched, continue after it. */
6255 continue;
6256 }
6257 }
6258
6259#ifdef FEAT_EVAL
6260 if (do_special)
6261 {
6262 char_u *p, *s, len;
6263
6264 /*
6265 * Replace <Leader> by the value of "mapleader".
6266 * Replace <LocalLeader> by the value of "maplocalleader".
6267 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6268 */
6269 if (STRNICMP(src, "<Leader>", 8) == 0)
6270 {
6271 len = 8;
6272 p = get_var_value((char_u *)"g:mapleader");
6273 }
6274 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6275 {
6276 len = 13;
6277 p = get_var_value((char_u *)"g:maplocalleader");
6278 }
6279 else
6280 {
6281 len = 0;
6282 p = NULL;
6283 }
6284 if (len != 0)
6285 {
6286 /* Allow up to 8 * 6 characters for "mapleader". */
6287 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6288 s = (char_u *)"\\";
6289 else
6290 s = p;
6291 while (*s != NUL)
6292 result[dlen++] = *s++;
6293 src += len;
6294 continue;
6295 }
6296 }
6297#endif
6298
6299 /*
6300 * Remove CTRL-V and ignore the next character.
6301 * For "from" side the CTRL-V at the end is included, for the "to"
6302 * part it is removed.
6303 * If 'cpoptions' does not contain 'B', also accept a backslash.
6304 */
6305 key = *src;
6306 if (key == Ctrl_V || (do_backslash && key == '\\'))
6307 {
6308 ++src; /* skip CTRL-V or backslash */
6309 if (*src == NUL)
6310 {
6311 if (from_part)
6312 result[dlen++] = key;
6313 break;
6314 }
6315 }
6316
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006318 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
6320 /*
6321 * If the character is K_SPECIAL, replace it with K_SPECIAL
6322 * KS_SPECIAL KE_FILLER.
6323 * If compiled with the GUI replace CSI with K_CSI.
6324 */
6325 if (*src == K_SPECIAL)
6326 {
6327 result[dlen++] = K_SPECIAL;
6328 result[dlen++] = KS_SPECIAL;
6329 result[dlen++] = KE_FILLER;
6330 }
6331# ifdef FEAT_GUI
6332 else if (*src == CSI)
6333 {
6334 result[dlen++] = K_SPECIAL;
6335 result[dlen++] = KS_EXTRA;
6336 result[dlen++] = (int)KE_CSI;
6337 }
6338# endif
6339 else
6340 result[dlen++] = *src;
6341 ++src;
6342 }
6343 }
6344 result[dlen] = NUL;
6345
6346 /*
6347 * Copy the new string to allocated memory.
6348 * If this fails, just return from.
6349 */
6350 if ((*bufp = vim_strsave(result)) != NULL)
6351 from = *bufp;
6352 vim_free(result);
6353 return from;
6354}
6355
6356/*
6357 * Find a termcode with keys 'src' (must be NUL terminated).
6358 * Return the index in termcodes[], or -1 if not found.
6359 */
6360 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006361find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006364 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 for (i = 0; i < tc_len; ++i)
6367 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006368 if (slen == termcodes[i].len
6369 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 return i;
6371 }
6372 return -1;
6373}
6374
6375/*
6376 * Gather the first characters in the terminal key codes into a string.
6377 * Used to speed up check_termcode().
6378 */
6379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006380gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 int i;
6383 int len = 0;
6384
6385#ifdef FEAT_GUI
6386 if (gui.in_use)
6387 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6388#endif
6389#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006390 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 termleader[len++] = DCS; /* the termcode response starts with DCS
6392 in 8-bit mode */
6393#endif
6394 termleader[len] = NUL;
6395
6396 for (i = 0; i < tc_len; ++i)
6397 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6398 {
6399 termleader[len++] = termcodes[i].code[0];
6400 termleader[len] = NUL;
6401 }
6402
6403 need_gather = FALSE;
6404}
6405
6406/*
6407 * Show all termcodes (for ":set termcap")
6408 * This code looks a lot like showoptions(), but is different.
6409 */
6410 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006411show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int col;
6414 int *items;
6415 int item_count;
6416 int run;
6417 int row, rows;
6418 int cols;
6419 int i;
6420 int len;
6421
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006422#define INC3 27 /* try to make three columns */
6423#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424#define GAP 2 /* spaces between columns */
6425
6426 if (tc_len == 0) /* no terminal codes (must be GUI) */
6427 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006428 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (items == NULL)
6430 return;
6431
6432 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006433 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435 /*
6436 * do the loop two times:
6437 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006438 * 2. display the medium items (medium length strings)
6439 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006441 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
6443 /*
6444 * collect the items in items[]
6445 */
6446 item_count = 0;
6447 for (i = 0; i < tc_len; i++)
6448 {
6449 len = show_one_termcode(termcodes[i].name,
6450 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006451 if (len <= INC3 - GAP ? run == 1
6452 : len <= INC2 - GAP ? run == 2
6453 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 items[item_count++] = i;
6455 }
6456
6457 /*
6458 * display the items
6459 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006460 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006462 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (cols == 0)
6464 cols = 1;
6465 rows = (item_count + cols - 1) / cols;
6466 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006467 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 rows = item_count;
6469 for (row = 0; row < rows && !got_int; ++row)
6470 {
6471 msg_putchar('\n'); /* go to next line */
6472 if (got_int) /* 'q' typed in more */
6473 break;
6474 col = 0;
6475 for (i = row; i < item_count; i += rows)
6476 {
6477 msg_col = col; /* make columns */
6478 show_one_termcode(termcodes[items[i]].name,
6479 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006480 if (run == 2)
6481 col += INC2;
6482 else
6483 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 out_flush();
6486 ui_breakcheck();
6487 }
6488 }
6489 vim_free(items);
6490}
6491
6492/*
6493 * Show one termcode entry.
6494 * Output goes into IObuff[]
6495 */
6496 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006497show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 char_u *p;
6500 int len;
6501
6502 if (name[0] > '~')
6503 {
6504 IObuff[0] = ' ';
6505 IObuff[1] = ' ';
6506 IObuff[2] = ' ';
6507 IObuff[3] = ' ';
6508 }
6509 else
6510 {
6511 IObuff[0] = 't';
6512 IObuff[1] = '_';
6513 IObuff[2] = name[0];
6514 IObuff[3] = name[1];
6515 }
6516 IObuff[4] = ' ';
6517
6518 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6519 if (p[1] != 't')
6520 STRCPY(IObuff + 5, p);
6521 else
6522 IObuff[5] = NUL;
6523 len = (int)STRLEN(IObuff);
6524 do
6525 IObuff[len++] = ' ';
6526 while (len < 17);
6527 IObuff[len] = NUL;
6528 if (code == NULL)
6529 len += 4;
6530 else
6531 len += vim_strsize(code);
6532
6533 if (printit)
6534 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006535 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006537 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 else
6539 msg_outtrans(code);
6540 }
6541 return len;
6542}
6543
6544#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6545/*
6546 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6547 * termcap codes from the terminal itself.
6548 * We get them one by one to avoid a very long response string.
6549 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006550static int xt_index_in = 0;
6551static int xt_index_out = 0;
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006554req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 xt_index_out = 0;
6557 xt_index_in = 0;
6558 req_more_codes_from_term();
6559}
6560
6561 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006562req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 char buf[11];
6565 int old_idx = xt_index_out;
6566
6567 /* Don't do anything when going to exit. */
6568 if (exiting)
6569 return;
6570
6571 /* Send up to 10 more requests out than we received. Avoid sending too
6572 * many, there can be a buffer overflow somewhere. */
6573 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6574 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006575 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006576
Bram Moolenaarb255b902018-04-24 21:40:10 +02006577 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6578 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 out_str_nf((char_u *)buf);
6580 ++xt_index_out;
6581 }
6582
6583 /* Send the codes out right away. */
6584 if (xt_index_out != old_idx)
6585 out_flush();
6586}
6587
6588/*
6589 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6590 * A "0" instead of the "1" indicates a code that isn't supported.
6591 * Both <name> and <string> are encoded in hex.
6592 * "code" points to the "0" or "1".
6593 */
6594 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006595got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597#define XT_LEN 100
6598 char_u name[3];
6599 char_u str[XT_LEN];
6600 int i;
6601 int j = 0;
6602 int c;
6603
6604 /* A '1' means the code is supported, a '0' means it isn't.
6605 * When half the length is > XT_LEN we can't use it.
6606 * Our names are currently all 2 characters. */
6607 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6608 {
6609 /* Get the name from the response and find it in the table. */
6610 name[0] = hexhex2nr(code + 3);
6611 name[1] = hexhex2nr(code + 5);
6612 name[2] = NUL;
6613 for (i = 0; key_names[i] != NULL; ++i)
6614 {
6615 if (STRCMP(key_names[i], name) == 0)
6616 {
6617 xt_index_in = i;
6618 break;
6619 }
6620 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006621
Bram Moolenaarb255b902018-04-24 21:40:10 +02006622 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (key_names[i] != NULL)
6625 {
6626 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6627 str[j++] = c;
6628 str[j] = NUL;
6629 if (name[0] == 'C' && name[1] == 'o')
6630 {
6631 /* Color count is not a key code. */
6632 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006633 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
6635 else
6636 {
6637 /* First delete any existing entry with the same code. */
6638 i = find_term_bykeys(str);
6639 if (i >= 0)
6640 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006641 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643 }
6644 }
6645
6646 /* May request more codes now that we received one. */
6647 ++xt_index_in;
6648 req_more_codes_from_term();
6649}
6650
6651/*
6652 * Check if there are any unanswered requests and deal with them.
6653 * This is called before starting an external program or getting direct
6654 * keyboard input. We don't want responses to be send to that program or
6655 * handled as typed text.
6656 */
6657 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006658check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 int c;
6661
6662 /* If no codes requested or all are answered, no need to wait. */
6663 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6664 return;
6665
6666 /* Vgetc() will check for and handle any response.
6667 * Keep calling vpeekc() until we don't get any responses. */
6668 ++no_mapping;
6669 ++allow_keys;
6670 for (;;)
6671 {
6672 c = vpeekc();
6673 if (c == NUL) /* nothing available */
6674 break;
6675
6676 /* If a response is recognized it's replaced with K_IGNORE, must read
6677 * it from the input stream. If there is no K_IGNORE we can't do
6678 * anything, break here (there might be some responses further on, but
6679 * we don't want to throw away any typed chars). */
6680 if (c != K_SPECIAL && c != K_IGNORE)
6681 break;
6682 c = vgetc();
6683 if (c != K_IGNORE)
6684 {
6685 vungetc(c);
6686 break;
6687 }
6688 }
6689 --no_mapping;
6690 --allow_keys;
6691}
6692#endif
6693
6694#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6695/*
6696 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006697 * corresponding external one recognized by :map/:abbrev commands.
6698 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 *
6700 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006701 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006703 * It uses a growarray to build the translation string since the latter can be
6704 * wider than the original description. The caller has to free the string
6705 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 *
6707 * Returns NULL when there is a problem.
6708 */
6709 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006710translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 garray_T ga;
6713 int c;
6714 int modifiers;
6715 int cpo_bslash;
6716 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718 ga_init(&ga);
6719 ga.ga_itemsize = 1;
6720 ga.ga_growsize = 40;
6721
6722 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6723 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 for (; *str; ++str)
6726 {
6727 c = *str;
6728 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6729 {
6730 modifiers = 0;
6731 if (str[1] == KS_MODIFIER)
6732 {
6733 str++;
6734 modifiers = *++str;
6735 c = *++str;
6736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6738 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006739 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
6741 ga_clear(&ga);
6742 return NULL;
6743 }
6744 c = TO_SPECIAL(str[1], str[2]);
6745 if (c == K_ZERO) /* display <Nul> as ^@ */
6746 c = NUL;
6747 str += 2;
6748 }
6749 if (IS_SPECIAL(c) || modifiers) /* special key */
6750 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006751 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {
6753 ga_clear(&ga);
6754 return NULL;
6755 }
6756 ga_concat(&ga, get_special_key_name(c, modifiers));
6757 continue; /* for (str) */
6758 }
6759 }
6760 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6761 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6762 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6763 if (c)
6764 ga_append(&ga, c);
6765 }
6766 ga_append(&ga, NUL);
6767 return (char_u *)(ga.ga_data);
6768}
6769#endif
6770
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006771#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772static char ksme_str[20];
6773static char ksmr_str[20];
6774static char ksmd_str[20];
6775
6776/*
6777 * For Win32 console: update termcap codes for existing console attributes.
6778 */
6779 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006780update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
6782 struct builtin_term *p;
6783
6784 p = find_builtin_term(DEFAULT_TERM);
6785 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6786 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6787 attr | 0x08); /* FOREGROUND_INTENSITY */
6788 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6789 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6790
6791 while (p->bt_string != NULL)
6792 {
6793 if (p->bt_entry == (int)KS_ME)
6794 p->bt_string = &ksme_str[0];
6795 else if (p->bt_entry == (int)KS_MR)
6796 p->bt_string = &ksmr_str[0];
6797 else if (p->bt_entry == (int)KS_MD)
6798 p->bt_string = &ksmd_str[0];
6799 ++p;
6800 }
6801}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006802
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006803# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006804# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006805struct ks_tbl_s
6806{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006807 int code; // value of KS_
6808 char *vtp; // code in vtp mode
6809 char *vtp2; // code in vtp2 mode
6810 char buf[KSSIZE]; // save buffer in non-vtp mode
6811 char vbuf[KSSIZE]; // save buffer in vtp mode
6812 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6813 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006814};
6815
6816static struct ks_tbl_s ks_tbl[] =
6817{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006818 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6819 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6820 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6821 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6822 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6823 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6824 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6825 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6826 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006827# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006828 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6829 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006830 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6831 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006832# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006833 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6834 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006835 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6836 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006837# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006838 {(int)KS_CCO, "256", "256"}, // colors
6839 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006840};
6841
6842 static struct builtin_term *
6843find_first_tcap(
6844 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006845 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006846{
6847 struct builtin_term *p;
6848
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006849 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006850 if (p->bt_entry == code)
6851 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006852 return NULL;
6853}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006854# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006855
6856/*
6857 * For Win32 console: replace the sequence immediately after termguicolors.
6858 */
6859 void
6860swap_tcap(void)
6861{
6862# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006863 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006864 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006865 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006866 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006867 int mode;
6868 enum
6869 {
6870 CMODEINDEX,
6871 CMODE24,
6872 CMODE256
6873 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006874
6875 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006876 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006877 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006878 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006879 {
6880 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006881 if (bt != NULL)
6882 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006883 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6884 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6885 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6886
6887 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6888 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006889 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006890 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006891 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006892 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006893 }
6894
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006895 if (p_tgc)
6896 mode = CMODE24;
6897 else if (t_colors >= 256)
6898 mode = CMODE256;
6899 else
6900 mode = CMODEINDEX;
6901
6902 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006903 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006904 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6905 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006906 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006907 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006908 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006909 case CMODEINDEX:
6910 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6911 break;
6912 case CMODE24:
6913 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6914 break;
6915 default:
6916 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006917 }
6918 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006919 }
6920
6921 if (mode != curr_mode)
6922 {
6923 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006924 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006925 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6926 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006927 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006928 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006929 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006930 case CMODEINDEX:
6931 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6932 break;
6933 case CMODE24:
6934 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6935 break;
6936 default:
6937 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006938 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006939 }
6940 }
6941
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006942 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006943 }
6944# endif
6945}
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006948
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006949#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006950 static int
6951hex_digit(int c)
6952{
6953 if (isdigit(c))
6954 return c - '0';
6955 c = TOLOWER_ASC(c);
6956 if (c >= 'a' && c <= 'f')
6957 return c - 'a' + 10;
6958 return 0x1ffffff;
6959}
6960
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006961# ifdef VIMDLL
6962 static guicolor_T
6963gui_adjust_rgb(guicolor_T c)
6964{
6965 if (gui.in_use)
6966 return c;
6967 else
6968 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6969}
6970# else
6971# define gui_adjust_rgb(c) (c)
6972# endif
6973
Bram Moolenaarab302212016-04-26 20:59:29 +02006974 guicolor_T
6975gui_get_color_cmn(char_u *name)
6976{
Bram Moolenaar28726652017-01-06 18:16:19 +01006977 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6978 * values as used by the MS-Windows GDI api. It should be used only for
6979 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006980# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006981# undef RGB
6982# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006983# ifndef RGB
6984# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6985# endif
6986# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006987 FILE *fd;
6988 char line[LINE_LEN];
6989 char_u *fname;
6990 int r, g, b, i;
6991 guicolor_T color;
6992
6993 struct rgbcolor_table_S {
6994 char_u *color_name;
6995 guicolor_T color;
6996 };
6997
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006998 /* Only non X11 colors (not present in rgb.txt) and colors in
6999 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02007000 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007001 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
7002 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
7003 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
7004 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
7005 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
7006 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
7007 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
7008 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
7009 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
7010 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
7011 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
7012 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
7013 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007014 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
7015 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02007016 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01007017 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007018 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007019 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
7020 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
7021 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
7022 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
7023 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
7024 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
7025 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
7026 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
7027 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007028 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007029 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007030 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
7031 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02007032 };
7033
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007034 static struct rgbcolor_table_S *colornames_table;
7035 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02007036
7037 if (name[0] == '#' && STRLEN(name) == 7)
7038 {
7039 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007040 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02007041 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
7042 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
7043 if (color > 0xffffff)
7044 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007045 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007046 }
7047
7048 /* Check if the name is one of the colors we know */
7049 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
7050 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007051 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007052
7053 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007054 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02007055 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007056 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02007057 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007058 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02007059
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007060 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007061 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
7062 if (fname == NULL)
7063 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007064
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007065 fd = fopen((char *)fname, "rt");
7066 vim_free(fname);
7067 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02007068 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007069 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007070 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007071 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007072 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007073 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007074
7075 for (counting = 1; counting >= 0; --counting)
7076 {
7077 if (!counting)
7078 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007079 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007080 if (colornames_table == NULL)
7081 {
7082 fclose(fd);
7083 return INVALCOLOR;
7084 }
7085 rewind(fd);
7086 }
7087 size = 0;
7088
7089 while (!feof(fd))
7090 {
7091 size_t len;
7092 int pos;
7093
Bram Moolenaar42335f52018-09-13 15:33:43 +02007094 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007095 len = strlen(line);
7096
7097 if (len <= 1 || line[len - 1] != '\n')
7098 continue;
7099
7100 line[len - 1] = '\0';
7101
7102 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7103 if (i != 3)
7104 continue;
7105
7106 if (!counting)
7107 {
7108 char_u *s = vim_strsave((char_u *)line + pos);
7109
7110 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007111 {
7112 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007113 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007114 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007115 colornames_table[size].color_name = s;
7116 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7117 }
7118 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007119
7120 // The distributed rgb.txt has less than 1000 entries. Limit to
7121 // 10000, just in case the file was messed up.
7122 if (size == 10000)
7123 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007124 }
7125 }
7126 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007127 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007128
7129 for (i = 0; i < size; i++)
7130 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007131 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007132
Bram Moolenaarab302212016-04-26 20:59:29 +02007133 return INVALCOLOR;
7134}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007135
7136 guicolor_T
7137gui_get_rgb_color_cmn(int r, int g, int b)
7138{
7139 guicolor_T color = RGB(r, g, b);
7140
7141 if (color > 0xffffff)
7142 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007143 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007144}
Bram Moolenaarab302212016-04-26 20:59:29 +02007145#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007146
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007147#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007148 || defined(PROTO)
7149static int cube_value[] = {
7150 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7151};
7152
7153static int grey_ramp[] = {
7154 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7155 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7156};
7157
7158# ifdef FEAT_TERMINAL
7159# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007160# else
7161# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007162# endif
7163
Bram Moolenaar9894e392018-05-05 14:29:06 +02007164static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007165// R G B idx
7166 { 0, 0, 0, 1}, // black
7167 {224, 0, 0, 2}, // dark red
7168 { 0, 224, 0, 3}, // dark green
7169 {224, 224, 0, 4}, // dark yellow / brown
7170 { 0, 0, 224, 5}, // dark blue
7171 {224, 0, 224, 6}, // dark magenta
7172 { 0, 224, 224, 7}, // dark cyan
7173 {224, 224, 224, 8}, // light grey
7174
7175 {128, 128, 128, 9}, // dark grey
7176 {255, 64, 64, 10}, // light red
7177 { 64, 255, 64, 11}, // light green
7178 {255, 255, 64, 12}, // yellow
7179 { 64, 64, 255, 13}, // light blue
7180 {255, 64, 255, 14}, // light magenta
7181 { 64, 255, 255, 15}, // light cyan
7182 {255, 255, 255, 16}, // white
7183};
7184
7185 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007186cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007187{
7188 int idx;
7189
7190 if (nr < 16)
7191 {
7192 *r = ansi_table[nr][0];
7193 *g = ansi_table[nr][1];
7194 *b = ansi_table[nr][2];
7195 *ansi_idx = ansi_table[nr][3];
7196 }
7197 else if (nr < 232)
7198 {
7199 /* 216 color cube */
7200 idx = nr - 16;
7201 *r = cube_value[idx / 36 % 6];
7202 *g = cube_value[idx / 6 % 6];
7203 *b = cube_value[idx % 6];
7204 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7205 }
7206 else if (nr < 256)
7207 {
7208 /* 24 grey scale ramp */
7209 idx = nr - 232;
7210 *r = grey_ramp[idx];
7211 *g = grey_ramp[idx];
7212 *b = grey_ramp[idx];
7213 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7214 }
7215 else
7216 {
7217 *r = 0;
7218 *g = 0;
7219 *b = 0;
7220 *ansi_idx = 0;
7221 }
7222}
7223#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007224