blob: 7d41a6100ee8e909c130fcc28f48717a73945ae8 [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);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020091static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static int term_is_builtin(char_u *name);
93static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010096static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
98/*
99 * Here is our own prototype for tgetstr(), any prototypes from the include
100 * files have been disabled by the define at the start of this file.
101 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100102char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200105 /* Change this to "if 1" to debug what happens with termresponse. */
106# if 0
107# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200108static void log_tr(const char *fmt, ...);
109# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200110# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200111# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200112# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200113
Bram Moolenaarafd78262019-05-10 23:10:31 +0200114typedef enum {
115 STATUS_GET, // send request when switching to RAW mode
116 STATUS_SENT, // did send request, checking for response
117 STATUS_GOT, // received response
118 STATUS_FAIL // timed out
119} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200120
Bram Moolenaarafd78262019-05-10 23:10:31 +0200121typedef struct {
122 request_progress_T tr_progress;
123 time_t tr_start; // when request was sent, -1 for never
124} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200125
Bram Moolenaarafd78262019-05-10 23:10:31 +0200126# define TERMREQUEST_INIT {STATUS_GET, -1}
127
128// Request Terminal Version status:
129static termrequest_T crv_status = TERMREQUEST_INIT;
130
131// Request Cursor position report:
132static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200133
Bram Moolenaar9377df32017-10-15 13:22:01 +0200134# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200135// Request foreground color report:
136static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200137static int fg_r = 0;
138static int fg_g = 0;
139static int fg_b = 0;
140static int bg_r = 255;
141static int bg_g = 255;
142static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200143# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200144
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200145/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200146static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200147
Bram Moolenaar4db25542017-08-28 22:43:05 +0200148/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200149static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200150
151/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200152static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100153
154/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200155static termrequest_T winpos_status = TERMREQUEST_INIT;
156
157static termrequest_T *all_termrequests[] = {
158 &crv_status,
159 &u7_status,
160# ifdef FEAT_TERMINAL
161 &rfg_status,
162# endif
163 &rbg_status,
164 &rbm_status,
165 &rcs_status,
166 &winpos_status,
167 NULL
168};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169# endif
170
171/*
172 * Don't declare these variables if termcap.h contains them.
173 * Autoconf checks if these variables should be declared extern (not all
174 * systems have them).
175 * Some versions define ospeed to be speed_t, but that is incompatible with
176 * BSD, where ospeed is short and speed_t is long.
177 */
178# ifndef HAVE_OSPEED
179# ifdef OSPEED_EXTERN
180extern short ospeed;
181# else
182short ospeed;
183# endif
184# endif
185# ifndef HAVE_UP_BC_PC
186# ifdef UP_BC_PC_EXTERN
187extern char *UP, *BC, PC;
188# else
189char *UP, *BC, PC;
190# endif
191# endif
192
193# define TGETSTR(s, p) vim_tgetstr((s), (p))
194# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100195static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif /* HAVE_TGETENT */
197
198static int detected_8bit = FALSE; /* detected 8-bit terminal */
199
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200200#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200201/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200202 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200203static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200204
205/* The blink flag from the style response may be inverted from the actual
206 * blinking state, xterm XORs the flags. */
207static int initial_cursor_shape_blink = FALSE;
208
209/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200210static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200211#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200212
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000213static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215
216#if defined(FEAT_GUI)
217/*
218 * GUI pseudo term-cap.
219 */
220 {(int)KS_NAME, "gui"},
221 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
222 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
223# ifdef TERMINFO
224 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
225# else
226 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
227# endif
228 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
229# ifdef TERMINFO
230 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
231 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# else
234 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
235 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# endif
238 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
239 /* attributes switched on with 'h', off with * 'H' */
240 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
241 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
242 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
243 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
244 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
245 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
246 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000247 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
248 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200249 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
250 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
252 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
253 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
254 {(int)KS_MS, "y"},
255 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100256 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {(int)KS_LE, "\b"}, /* cursor-left = BS */
258 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
259# ifdef TERMINFO
260 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
261# else
262 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
263# endif
264 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000265 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#endif
267
268#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
271/*
272 * Amiga console window, default for Amiga
273 */
274 {(int)KS_NAME, "amiga"},
275 {(int)KS_CE, "\033[K"},
276 {(int)KS_CD, "\033[J"},
277 {(int)KS_AL, "\033[L"},
278# ifdef TERMINFO
279 {(int)KS_CAL, "\033[%p1%dL"},
280# else
281 {(int)KS_CAL, "\033[%dL"},
282# endif
283 {(int)KS_DL, "\033[M"},
284# ifdef TERMINFO
285 {(int)KS_CDL, "\033[%p1%dM"},
286# else
287 {(int)KS_CDL, "\033[%dM"},
288# endif
289 {(int)KS_CL, "\014"},
290 {(int)KS_VI, "\033[0 p"},
291 {(int)KS_VE, "\033[1 p"},
292 {(int)KS_ME, "\033[0m"},
293 {(int)KS_MR, "\033[7m"},
294 {(int)KS_MD, "\033[1m"},
295 {(int)KS_SE, "\033[0m"},
296 {(int)KS_SO, "\033[33m"},
297 {(int)KS_US, "\033[4m"},
298 {(int)KS_UE, "\033[0m"},
299 {(int)KS_CZH, "\033[3m"},
300 {(int)KS_CZR, "\033[0m"},
301#if defined(__MORPHOS__) || defined(__AROS__)
302 {(int)KS_CCO, "8"}, /* allow 8 colors */
303# ifdef TERMINFO
304 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
305 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
306# else
307 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
308 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
309# endif
310 {(int)KS_OP, "\033[m"}, /* reset colors */
311#endif
312 {(int)KS_MS, "y"},
313 {(int)KS_UT, "y"}, /* guessed */
314 {(int)KS_LE, "\b"},
315# ifdef TERMINFO
316 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
317# else
318 {(int)KS_CM, "\033[%i%d;%dH"},
319# endif
320#if defined(__MORPHOS__)
321 {(int)KS_SR, "\033M"},
322#endif
323# ifdef TERMINFO
324 {(int)KS_CRI, "\033[%p1%dC"},
325# else
326 {(int)KS_CRI, "\033[%dC"},
327# endif
328 {K_UP, "\233A"},
329 {K_DOWN, "\233B"},
330 {K_LEFT, "\233D"},
331 {K_RIGHT, "\233C"},
332 {K_S_UP, "\233T"},
333 {K_S_DOWN, "\233S"},
334 {K_S_LEFT, "\233 A"},
335 {K_S_RIGHT, "\233 @"},
336 {K_S_TAB, "\233Z"},
337 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
338 {K_F2, "\233\061~"},
339 {K_F3, "\233\062~"},
340 {K_F4, "\233\063~"},
341 {K_F5, "\233\064~"},
342 {K_F6, "\233\065~"},
343 {K_F7, "\233\066~"},
344 {K_F8, "\233\067~"},
345 {K_F9, "\233\070~"},
346 {K_F10, "\233\071~"},
347 {K_S_F1, "\233\061\060~"},
348 {K_S_F2, "\233\061\061~"},
349 {K_S_F3, "\233\061\062~"},
350 {K_S_F4, "\233\061\063~"},
351 {K_S_F5, "\233\061\064~"},
352 {K_S_F6, "\233\061\065~"},
353 {K_S_F7, "\233\061\066~"},
354 {K_S_F8, "\233\061\067~"},
355 {K_S_F9, "\233\061\070~"},
356 {K_S_F10, "\233\061\071~"},
357 {K_HELP, "\233?~"},
358 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
359 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
360 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
361 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
362 {K_END, "\233\064\065~"}, /* 101 key keyboard */
363
364 {BT_EXTRA_KEYS, ""},
365 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
366 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
367 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
368# endif
369
370# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
371/*
372 * almost standard ANSI terminal, default for bebox
373 */
374 {(int)KS_NAME, "beos-ansi"},
375 {(int)KS_CE, "\033[K"},
376 {(int)KS_CD, "\033[J"},
377 {(int)KS_AL, "\033[L"},
378# ifdef TERMINFO
379 {(int)KS_CAL, "\033[%p1%dL"},
380# else
381 {(int)KS_CAL, "\033[%dL"},
382# endif
383 {(int)KS_DL, "\033[M"},
384# ifdef TERMINFO
385 {(int)KS_CDL, "\033[%p1%dM"},
386# else
387 {(int)KS_CDL, "\033[%dM"},
388# endif
389#ifdef BEOS_PR_OR_BETTER
390# ifdef TERMINFO
391 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
392# else
393 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
394# endif
395#endif
396 {(int)KS_CL, "\033[H\033[2J"},
397#ifdef notyet
398 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
399 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
400#endif
401 {(int)KS_ME, "\033[m"}, /* normal mode */
402 {(int)KS_MR, "\033[7m"}, /* reverse */
403 {(int)KS_MD, "\033[1m"}, /* bold */
404 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
405 {(int)KS_SE, "\033[m"}, /* standout end */
406 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
407 {(int)KS_CZR, "\033[m"}, /* italic end */
408 {(int)KS_US, "\033[4m"}, /* underscore mode */
409 {(int)KS_UE, "\033[m"}, /* underscore end */
410 {(int)KS_CCO, "8"}, /* allow 8 colors */
411# ifdef TERMINFO
412 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
413 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
414# else
415 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
416 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
417# endif
418 {(int)KS_OP, "\033[m"}, /* reset colors */
419 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
420 {(int)KS_UT, "y"}, /* guessed */
421 {(int)KS_LE, "\b"},
422# ifdef TERMINFO
423 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
424# else
425 {(int)KS_CM, "\033[%i%d;%dH"},
426# endif
427 {(int)KS_SR, "\033M"},
428# ifdef TERMINFO
429 {(int)KS_CRI, "\033[%p1%dC"},
430# else
431 {(int)KS_CRI, "\033[%dC"},
432# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200433# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 {K_UP, "\033[A"},
438 {K_DOWN, "\033[B"},
439 {K_LEFT, "\033[D"},
440 {K_RIGHT, "\033[C"},
441# endif
442
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200443# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444/*
445 * standard ANSI terminal, default for unix
446 */
447 {(int)KS_NAME, "ansi"},
448 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
449 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
450# ifdef TERMINFO
451 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
452# else
453 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
454# endif
455 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
456# ifdef TERMINFO
457 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
458# else
459 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
460# endif
461 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
462 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
463 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
464 {(int)KS_MS, "y"},
465 {(int)KS_UT, "y"}, /* guessed */
466 {(int)KS_LE, "\b"},
467# ifdef TERMINFO
468 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
469# else
470 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
471# endif
472# ifdef TERMINFO
473 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
474# else
475 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
476# endif
477# endif
478
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200479# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * These codes are valid when nansi.sys or equivalent has been installed.
482 * Function keys on a PC are preceded with a NUL. These are converted into
483 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
484 * CTRL-arrow is used instead of SHIFT-arrow.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {(int)KS_NAME, "pcansi"},
487 {(int)KS_DL, "\033[M"},
488 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {(int)KS_CE, "\033[K"},
490 {(int)KS_CL, "\033[2J"},
491 {(int)KS_ME, "\033[0m"},
492 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
493 {(int)KS_MD, "\033[1m"}, /* bold: white text */
494 {(int)KS_SE, "\033[0m"}, /* standout end */
495 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
496 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
497 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
498 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
499 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
500 {(int)KS_CCO, "8"}, /* allow 8 colors */
501# ifdef TERMINFO
502 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
503 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
504# else
505 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
506 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
507# endif
508 {(int)KS_OP, "\033[0m"}, /* reset colors */
509 {(int)KS_MS, "y"},
510 {(int)KS_UT, "y"}, /* guessed */
511 {(int)KS_LE, "\b"},
512# ifdef TERMINFO
513 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
514# else
515 {(int)KS_CM, "\033[%i%d;%dH"},
516# endif
517# ifdef TERMINFO
518 {(int)KS_CRI, "\033[%p1%dC"},
519# else
520 {(int)KS_CRI, "\033[%dC"},
521# endif
522 {K_UP, "\316H"},
523 {K_DOWN, "\316P"},
524 {K_LEFT, "\316K"},
525 {K_RIGHT, "\316M"},
526 {K_S_LEFT, "\316s"},
527 {K_S_RIGHT, "\316t"},
528 {K_F1, "\316;"},
529 {K_F2, "\316<"},
530 {K_F3, "\316="},
531 {K_F4, "\316>"},
532 {K_F5, "\316?"},
533 {K_F6, "\316@"},
534 {K_F7, "\316A"},
535 {K_F8, "\316B"},
536 {K_F9, "\316C"},
537 {K_F10, "\316D"},
538 {K_F11, "\316\205"}, /* guessed */
539 {K_F12, "\316\206"}, /* guessed */
540 {K_S_F1, "\316T"},
541 {K_S_F2, "\316U"},
542 {K_S_F3, "\316V"},
543 {K_S_F4, "\316W"},
544 {K_S_F5, "\316X"},
545 {K_S_F6, "\316Y"},
546 {K_S_F7, "\316Z"},
547 {K_S_F8, "\316["},
548 {K_S_F9, "\316\\"},
549 {K_S_F10, "\316]"},
550 {K_S_F11, "\316\207"}, /* guessed */
551 {K_S_F12, "\316\210"}, /* guessed */
552 {K_INS, "\316R"},
553 {K_DEL, "\316S"},
554 {K_HOME, "\316G"},
555 {K_END, "\316O"},
556 {K_PAGEDOWN, "\316Q"},
557 {K_PAGEUP, "\316I"},
558# endif
559
Bram Moolenaar4f974752019-02-17 17:44:42 +0100560# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
562 * These codes are valid for the Win32 Console . The entries that start with
563 * ESC | are translated into console calls in os_win32.c. The function keys
564 * are also translated in os_win32.c.
565 */
566 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CE, "\033|K"}, // clear to end of line
568 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100572 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100576 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
577 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
580 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CL, "\033|J"}, // clear screen
583 {(int)KS_CD, "\033|j"}, // clear to end of display
584 {(int)KS_VI, "\033|v"}, // cursor invisible
585 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_ME, "\033|0m"}, // normal
588 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
589 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_SO, "\033|31m"}, // standout: white on blue
592 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_SO, "\033|F"}, // standout: high intensity
595 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100597 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
598 {(int)KS_CZR, "\033|0m"}, // italic end
599 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
600 {(int)KS_UE, "\033|0m"}, // underscore end
601 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_CAB, "\033|%p1%db"}, // set background color
604 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100606 {(int)KS_CAB, "\033|%db"}, // set background color
607 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# endif
609
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100612 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {(int)KS_LE, "\b"},
614# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100615 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100617 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100619 {(int)KS_VB, "\033|B"}, // visual bell
620 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
621 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100623 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100625 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100627# ifdef FEAT_TERMGUICOLORS
628 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
629 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 {K_UP, "\316H"},
633 {K_DOWN, "\316P"},
634 {K_LEFT, "\316K"},
635 {K_RIGHT, "\316M"},
636 {K_S_UP, "\316\304"},
637 {K_S_DOWN, "\316\317"},
638 {K_S_LEFT, "\316\311"},
639 {K_C_LEFT, "\316s"},
640 {K_S_RIGHT, "\316\313"},
641 {K_C_RIGHT, "\316t"},
642 {K_S_TAB, "\316\017"},
643 {K_F1, "\316;"},
644 {K_F2, "\316<"},
645 {K_F3, "\316="},
646 {K_F4, "\316>"},
647 {K_F5, "\316?"},
648 {K_F6, "\316@"},
649 {K_F7, "\316A"},
650 {K_F8, "\316B"},
651 {K_F9, "\316C"},
652 {K_F10, "\316D"},
653 {K_F11, "\316\205"},
654 {K_F12, "\316\206"},
655 {K_S_F1, "\316T"},
656 {K_S_F2, "\316U"},
657 {K_S_F3, "\316V"},
658 {K_S_F4, "\316W"},
659 {K_S_F5, "\316X"},
660 {K_S_F6, "\316Y"},
661 {K_S_F7, "\316Z"},
662 {K_S_F8, "\316["},
663 {K_S_F9, "\316\\"},
664 {K_S_F10, "\316]"},
665 {K_S_F11, "\316\207"},
666 {K_S_F12, "\316\210"},
667 {K_INS, "\316R"},
668 {K_DEL, "\316S"},
669 {K_HOME, "\316G"},
670 {K_S_HOME, "\316\302"},
671 {K_C_HOME, "\316w"},
672 {K_END, "\316O"},
673 {K_S_END, "\316\315"},
674 {K_C_END, "\316u"},
675 {K_PAGEDOWN, "\316Q"},
676 {K_PAGEUP, "\316I"},
677 {K_KPLUS, "\316N"},
678 {K_KMINUS, "\316J"},
679 {K_KMULTIPLY, "\316\067"},
680 {K_K0, "\316\332"},
681 {K_K1, "\316\336"},
682 {K_K2, "\316\342"},
683 {K_K3, "\316\346"},
684 {K_K4, "\316\352"},
685 {K_K5, "\316\356"},
686 {K_K6, "\316\362"},
687 {K_K7, "\316\366"},
688 {K_K8, "\316\372"},
689 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100690 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691# endif
692
693# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
694/*
695 * VT320 is working as an ANSI terminal compatible DEC terminal.
696 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 * TODO:- rewrite ESC[ codes to CSI
698 * - keyboard languages (CSI ? 26 n)
699 */
700 {(int)KS_NAME, "vt320"},
701 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
702 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
703# ifdef TERMINFO
704 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
705# else
706 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
707# endif
708 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
709# ifdef TERMINFO
710 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
711# else
712 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
713# endif
714 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000715 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
716 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
718 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
720 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
721 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
722 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
723 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
724 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
725 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
726 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
727 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
728 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {(int)KS_MS, "y"},
730 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100731 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {(int)KS_LE, "\b"},
733# ifdef TERMINFO
734 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
735 ESC_STR "[%i%p1%d;%p2%dH")},
736# else
737 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
738# endif
739# ifdef TERMINFO
740 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
741# else
742 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
743# endif
744 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
745 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
746 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
747 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200748 // Note: cursor key sequences for application cursor mode are omitted,
749 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000750 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
751 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
752 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
753 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
754 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
756 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
757 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
758 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
759 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000760 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
762 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
763 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
764 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
765 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
766 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
767 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
768 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
769 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
770 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
771 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
772 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
773 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
774 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
775 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200776 // These sequences starting with <Esc> O may interfere with what the user
777 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
779 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
780 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
781 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
782 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200783 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
784 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
785 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
786 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
787 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
788 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
789 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
790 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
791 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
792 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
794# endif
795
796# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
797/*
798 * Ordinary vt52
799 */
800 {(int)KS_NAME, "vt52"},
801 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
802 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# ifdef TERMINFO
804 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
805 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
806# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100810 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
812 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {K_UP, IF_EB("\033A", ESC_STR "A")},
814 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
815 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
816 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100817 {K_F1, IF_EB("\033P", ESC_STR "P")},
818 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
819 {K_F3, IF_EB("\033R", ESC_STR "R")},
820# ifdef __MINT__
821 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
822 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
823 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
824 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
825 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
827 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
828 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
829 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {K_F4, IF_EB("\033S", ESC_STR "S")},
831 {K_F5, IF_EB("\033T", ESC_STR "T")},
832 {K_F6, IF_EB("\033U", ESC_STR "U")},
833 {K_F7, IF_EB("\033V", ESC_STR "V")},
834 {K_F8, IF_EB("\033W", ESC_STR "W")},
835 {K_F9, IF_EB("\033X", ESC_STR "X")},
836 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
837 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
838 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
839 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
840 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
841 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
842 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
843 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
844 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
845 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
846 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
847 {K_INS, IF_EB("\033I", ESC_STR "I")},
848 {K_HOME, IF_EB("\033E", ESC_STR "E")},
849 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
850 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {(int)KS_MS, "y"},
854# endif
855# endif
856
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200857# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200858 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
860 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
861# ifdef TERMINFO
862 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
863# else
864 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
865# endif
866 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
867# ifdef TERMINFO
868 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
869# else
870 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
871# endif
872# ifdef TERMINFO
873 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
874 ESC_STR "[%i%p1%d;%p2%dr")},
875# else
876 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
877# endif
878 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
879 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
880 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
881 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
882 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
883 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
884 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200885 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
886 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {(int)KS_MS, "y"},
888 {(int)KS_UT, "y"},
889 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200890 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
891 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200892 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200893 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200894# ifdef TERMINFO
895 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
896# else
897 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
898# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200899 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200900 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901# ifdef TERMINFO
902 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
903 ESC_STR "[%i%p1%d;%p2%dH")},
904# else
905 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
906# endif
907 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
908# ifdef TERMINFO
909 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
910# else
911 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
912# endif
913 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
914 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
915# ifdef FEAT_XTERM_SAVE
916 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
917 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
918 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
919# endif
920 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
921 {(int)KS_CIE, "\007"},
922 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
923 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200924 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
925 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926# ifdef TERMINFO
927 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
928 ESC_STR "[8;%p1%d;%p2%dt")},
929 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
930 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200931 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932# else
933 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
934 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200935 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936# endif
937 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200938 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200939 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100940 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200941# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200942 /* These are printf strings, not terminal codes. */
943 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
944 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
945# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100946 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
947 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200948 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
949 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
950 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
951 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000952
953 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
954 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
955 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
956 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
957 /* An extra set of cursor keys for vt100 mode */
958 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
959 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
960 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
961 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000963 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
964 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
965 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
966 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000967 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
968 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
969 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
970 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
971 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
972 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
973 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
974 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
975 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
976 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
977 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
978 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000980 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
981 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
982 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
983 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000984 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
985 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000987 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000988 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000989 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000990 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
991 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000992 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000993 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000995 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
996 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100997 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
998 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
999 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1000 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1001 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1002 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +02001003 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
1004 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1005 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1006 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1007 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1008 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1009 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1010 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1011 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1012 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001013 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1014 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1015 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001018 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1019 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1020 /* F14 and F15 are missing, because they send the same codes as the undo
1021 * and help key, although they don't work on all keyboards. */
1022 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1023 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1024 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1025 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1026 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1027
1028 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1029 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1030 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1031 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1032 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1033 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1034 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1035 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1036 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1037 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1038
1039 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1040 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1041 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1042 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1043 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1044 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1045 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# endif
1047
1048# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1049/*
1050 * iris-ansi for Silicon Graphics machines.
1051 */
1052 {(int)KS_NAME, "iris-ansi"},
1053 {(int)KS_CE, "\033[K"},
1054 {(int)KS_CD, "\033[J"},
1055 {(int)KS_AL, "\033[L"},
1056# ifdef TERMINFO
1057 {(int)KS_CAL, "\033[%p1%dL"},
1058# else
1059 {(int)KS_CAL, "\033[%dL"},
1060# endif
1061 {(int)KS_DL, "\033[M"},
1062# ifdef TERMINFO
1063 {(int)KS_CDL, "\033[%p1%dM"},
1064# else
1065 {(int)KS_CDL, "\033[%dM"},
1066# endif
1067#if 0 /* The scroll region is not working as Vim expects. */
1068# ifdef TERMINFO
1069 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1070# else
1071 {(int)KS_CS, "\033[%i%d;%dr"},
1072# endif
1073#endif
1074 {(int)KS_CL, "\033[H\033[2J"},
1075 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1076 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1077 {(int)KS_TI, "\033[=6h"},
1078 {(int)KS_TE, "\033[=6l"},
1079 {(int)KS_SE, "\033[21;27m"},
1080 {(int)KS_SO, "\033[1;7m"},
1081 {(int)KS_ME, "\033[m"},
1082 {(int)KS_MR, "\033[7m"},
1083 {(int)KS_MD, "\033[1m"},
1084 {(int)KS_CCO, "8"}, /* allow 8 colors */
1085 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1086 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1087 {(int)KS_US, "\033[4m"}, /* underline on */
1088 {(int)KS_UE, "\033[24m"}, /* underline off */
1089# ifdef TERMINFO
1090 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1091 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1092 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1093 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1094# else
1095 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1096 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1097 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1098 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1099# endif
1100 {(int)KS_MS, "y"}, /* guessed */
1101 {(int)KS_UT, "y"}, /* guessed */
1102 {(int)KS_LE, "\b"},
1103# ifdef TERMINFO
1104 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1105# else
1106 {(int)KS_CM, "\033[%i%d;%dH"},
1107# endif
1108 {(int)KS_SR, "\033M"},
1109# ifdef TERMINFO
1110 {(int)KS_CRI, "\033[%p1%dC"},
1111# else
1112 {(int)KS_CRI, "\033[%dC"},
1113# endif
1114 {(int)KS_CIS, "\033P3.y"},
1115 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1116 {(int)KS_TS, "\033P1.y"},
1117 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1118# ifdef TERMINFO
1119 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1120 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1121# else
1122 {(int)KS_CWS, "\033[203;%d;%d/y"},
1123 {(int)KS_CWP, "\033[205;%d;%d/y"},
1124# endif
1125 {K_UP, "\033[A"},
1126 {K_DOWN, "\033[B"},
1127 {K_LEFT, "\033[D"},
1128 {K_RIGHT, "\033[C"},
1129 {K_S_UP, "\033[161q"},
1130 {K_S_DOWN, "\033[164q"},
1131 {K_S_LEFT, "\033[158q"},
1132 {K_S_RIGHT, "\033[167q"},
1133 {K_F1, "\033[001q"},
1134 {K_F2, "\033[002q"},
1135 {K_F3, "\033[003q"},
1136 {K_F4, "\033[004q"},
1137 {K_F5, "\033[005q"},
1138 {K_F6, "\033[006q"},
1139 {K_F7, "\033[007q"},
1140 {K_F8, "\033[008q"},
1141 {K_F9, "\033[009q"},
1142 {K_F10, "\033[010q"},
1143 {K_F11, "\033[011q"},
1144 {K_F12, "\033[012q"},
1145 {K_S_F1, "\033[013q"},
1146 {K_S_F2, "\033[014q"},
1147 {K_S_F3, "\033[015q"},
1148 {K_S_F4, "\033[016q"},
1149 {K_S_F5, "\033[017q"},
1150 {K_S_F6, "\033[018q"},
1151 {K_S_F7, "\033[019q"},
1152 {K_S_F8, "\033[020q"},
1153 {K_S_F9, "\033[021q"},
1154 {K_S_F10, "\033[022q"},
1155 {K_S_F11, "\033[023q"},
1156 {K_S_F12, "\033[024q"},
1157 {K_INS, "\033[139q"},
1158 {K_HOME, "\033[H"},
1159 {K_END, "\033[146q"},
1160 {K_PAGEUP, "\033[150q"},
1161 {K_PAGEDOWN, "\033[154q"},
1162# endif
1163
1164# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1165/*
1166 * for debugging
1167 */
1168 {(int)KS_NAME, "debug"},
1169 {(int)KS_CE, "[CE]"},
1170 {(int)KS_CD, "[CD]"},
1171 {(int)KS_AL, "[AL]"},
1172# ifdef TERMINFO
1173 {(int)KS_CAL, "[CAL%p1%d]"},
1174# else
1175 {(int)KS_CAL, "[CAL%d]"},
1176# endif
1177 {(int)KS_DL, "[DL]"},
1178# ifdef TERMINFO
1179 {(int)KS_CDL, "[CDL%p1%d]"},
1180# else
1181 {(int)KS_CDL, "[CDL%d]"},
1182# endif
1183# ifdef TERMINFO
1184 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1185# else
1186 {(int)KS_CS, "[%dCS%d]"},
1187# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001188# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001190# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192# endif
1193# ifdef TERMINFO
1194 {(int)KS_CAB, "[CAB%p1%d]"},
1195 {(int)KS_CAF, "[CAF%p1%d]"},
1196 {(int)KS_CSB, "[CSB%p1%d]"},
1197 {(int)KS_CSF, "[CSF%p1%d]"},
1198# else
1199 {(int)KS_CAB, "[CAB%d]"},
1200 {(int)KS_CAF, "[CAF%d]"},
1201 {(int)KS_CSB, "[CSB%d]"},
1202 {(int)KS_CSF, "[CSF%d]"},
1203# endif
1204 {(int)KS_OP, "[OP]"},
1205 {(int)KS_LE, "[LE]"},
1206 {(int)KS_CL, "[CL]"},
1207 {(int)KS_VI, "[VI]"},
1208 {(int)KS_VE, "[VE]"},
1209 {(int)KS_VS, "[VS]"},
1210 {(int)KS_ME, "[ME]"},
1211 {(int)KS_MR, "[MR]"},
1212 {(int)KS_MB, "[MB]"},
1213 {(int)KS_MD, "[MD]"},
1214 {(int)KS_SE, "[SE]"},
1215 {(int)KS_SO, "[SO]"},
1216 {(int)KS_UE, "[UE]"},
1217 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001218 {(int)KS_UCE, "[UCE]"},
1219 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001220 {(int)KS_STE, "[STE]"},
1221 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {(int)KS_MS, "[MS]"},
1223 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001224 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225# ifdef TERMINFO
1226 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1227# else
1228 {(int)KS_CM, "[%dCM%d]"},
1229# endif
1230 {(int)KS_SR, "[SR]"},
1231# ifdef TERMINFO
1232 {(int)KS_CRI, "[CRI%p1%d]"},
1233# else
1234 {(int)KS_CRI, "[CRI%d]"},
1235# endif
1236 {(int)KS_VB, "[VB]"},
1237 {(int)KS_KS, "[KS]"},
1238 {(int)KS_KE, "[KE]"},
1239 {(int)KS_TI, "[TI]"},
1240 {(int)KS_TE, "[TE]"},
1241 {(int)KS_CIS, "[CIS]"},
1242 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001243 {(int)KS_CSC, "[CSC]"},
1244 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {(int)KS_TS, "[TS]"},
1246 {(int)KS_FS, "[FS]"},
1247# ifdef TERMINFO
1248 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1249 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1250# else
1251 {(int)KS_CWS, "[%dCWS%d]"},
1252 {(int)KS_CWP, "[%dCWP%d]"},
1253# endif
1254 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001255 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001256 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001257 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {K_UP, "[KU]"},
1259 {K_DOWN, "[KD]"},
1260 {K_LEFT, "[KL]"},
1261 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001262 {K_XUP, "[xKU]"},
1263 {K_XDOWN, "[xKD]"},
1264 {K_XLEFT, "[xKL]"},
1265 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {K_S_UP, "[S-KU]"},
1267 {K_S_DOWN, "[S-KD]"},
1268 {K_S_LEFT, "[S-KL]"},
1269 {K_C_LEFT, "[C-KL]"},
1270 {K_S_RIGHT, "[S-KR]"},
1271 {K_C_RIGHT, "[C-KR]"},
1272 {K_F1, "[F1]"},
1273 {K_XF1, "[xF1]"},
1274 {K_F2, "[F2]"},
1275 {K_XF2, "[xF2]"},
1276 {K_F3, "[F3]"},
1277 {K_XF3, "[xF3]"},
1278 {K_F4, "[F4]"},
1279 {K_XF4, "[xF4]"},
1280 {K_F5, "[F5]"},
1281 {K_F6, "[F6]"},
1282 {K_F7, "[F7]"},
1283 {K_F8, "[F8]"},
1284 {K_F9, "[F9]"},
1285 {K_F10, "[F10]"},
1286 {K_F11, "[F11]"},
1287 {K_F12, "[F12]"},
1288 {K_S_F1, "[S-F1]"},
1289 {K_S_XF1, "[S-xF1]"},
1290 {K_S_F2, "[S-F2]"},
1291 {K_S_XF2, "[S-xF2]"},
1292 {K_S_F3, "[S-F3]"},
1293 {K_S_XF3, "[S-xF3]"},
1294 {K_S_F4, "[S-F4]"},
1295 {K_S_XF4, "[S-xF4]"},
1296 {K_S_F5, "[S-F5]"},
1297 {K_S_F6, "[S-F6]"},
1298 {K_S_F7, "[S-F7]"},
1299 {K_S_F8, "[S-F8]"},
1300 {K_S_F9, "[S-F9]"},
1301 {K_S_F10, "[S-F10]"},
1302 {K_S_F11, "[S-F11]"},
1303 {K_S_F12, "[S-F12]"},
1304 {K_HELP, "[HELP]"},
1305 {K_UNDO, "[UNDO]"},
1306 {K_BS, "[BS]"},
1307 {K_INS, "[INS]"},
1308 {K_KINS, "[KINS]"},
1309 {K_DEL, "[DEL]"},
1310 {K_KDEL, "[KDEL]"},
1311 {K_HOME, "[HOME]"},
1312 {K_S_HOME, "[C-HOME]"},
1313 {K_C_HOME, "[C-HOME]"},
1314 {K_KHOME, "[KHOME]"},
1315 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001316 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {K_END, "[END]"},
1318 {K_S_END, "[C-END]"},
1319 {K_C_END, "[C-END]"},
1320 {K_KEND, "[KEND]"},
1321 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001322 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {K_PAGEUP, "[PAGEUP]"},
1324 {K_PAGEDOWN, "[PAGEDOWN]"},
1325 {K_KPAGEUP, "[KPAGEUP]"},
1326 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1327 {K_MOUSE, "[MOUSE]"},
1328 {K_KPLUS, "[KPLUS]"},
1329 {K_KMINUS, "[KMINUS]"},
1330 {K_KDIVIDE, "[KDIVIDE]"},
1331 {K_KMULTIPLY, "[KMULTIPLY]"},
1332 {K_KENTER, "[KENTER]"},
1333 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001334 {K_PS, "[PASTE-START]"},
1335 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {K_K0, "[K0]"},
1337 {K_K1, "[K1]"},
1338 {K_K2, "[K2]"},
1339 {K_K3, "[K3]"},
1340 {K_K4, "[K4]"},
1341 {K_K5, "[K5]"},
1342 {K_K6, "[K6]"},
1343 {K_K7, "[K7]"},
1344 {K_K8, "[K8]"},
1345 {K_K9, "[K9]"},
1346# endif
1347
1348#endif /* NO_BUILTIN_TCAPS */
1349
1350/*
1351 * The most minimal terminal: only clear screen and cursor positioning
1352 * Always included.
1353 */
1354 {(int)KS_NAME, "dumb"},
1355 {(int)KS_CL, "\014"},
1356#ifdef TERMINFO
1357 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1358 ESC_STR "[%i%p1%d;%p2%dH")},
1359#else
1360 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1361#endif
1362
1363/*
1364 * end marker
1365 */
1366 {(int)KS_NAME, NULL}
1367
1368}; /* end of builtin_termcaps */
1369
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001370#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001371 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001372termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001373{
Bram Moolenaarab302212016-04-26 20:59:29 +02001374 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001375}
1376
1377 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001378termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379{
1380 guicolor_T t;
1381
1382 if (*name == NUL)
1383 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001384 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385
1386 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001387 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388 return t;
1389}
1390
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001391 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001392termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001393{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001394 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001395}
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#ifdef AMIGA
1402# define DEFAULT_TERM (char_u *)"amiga"
1403#endif
1404
1405#ifdef MSWIN
1406# define DEFAULT_TERM (char_u *)"win32"
1407#endif
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#if defined(UNIX) && !defined(__MINT__)
1410# define DEFAULT_TERM (char_u *)"ansi"
1411#endif
1412
1413#ifdef __MINT__
1414# define DEFAULT_TERM (char_u *)"vt52"
1415#endif
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#ifdef VMS
1418# define DEFAULT_TERM (char_u *)"vt320"
1419#endif
1420
1421#ifdef __BEOS__
1422# undef DEFAULT_TERM
1423# define DEFAULT_TERM (char_u *)"beos-ansi"
1424#endif
1425
1426#ifndef DEFAULT_TERM
1427# define DEFAULT_TERM (char_u *)"dumb"
1428#endif
1429
1430/*
1431 * Term_strings contains currently used terminal output strings.
1432 * It is initialized with the default values by parse_builtin_tcap().
1433 * The values can be changed by setting the option with the same name.
1434 */
1435char_u *(term_strings[(int)KS_LAST + 1]);
1436
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001437static int need_gather = FALSE; /* need to fill termleader[] */
1438static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001440static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001441static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#endif
1443
1444 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001445find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 struct builtin_term *p;
1448
1449 p = builtin_termcaps;
1450 while (p->bt_string != NULL)
1451 {
1452 if (p->bt_entry == (int)KS_NAME)
1453 {
1454#ifdef UNIX
1455 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1456 return p;
1457 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1458 return p;
1459 else
1460#endif
1461#ifdef VMS
1462 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1463 return p;
1464 else
1465#endif
1466 if (STRCMP(term, p->bt_string) == 0)
1467 return p;
1468 }
1469 ++p;
1470 }
1471 return p;
1472}
1473
1474/*
1475 * Parsing of the builtin termcap entries.
1476 * Caller should check if 'name' is a valid builtin term.
1477 * The terminal's name is not set, as this is already done in termcapinit().
1478 */
1479 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001480parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 struct builtin_term *p;
1483 char_u name[2];
1484 int term_8bit;
1485
1486 p = find_builtin_term(term);
1487 term_8bit = term_is_8bit(term);
1488
1489 /* Do not parse if builtin term not found */
1490 if (p->bt_string == NULL)
1491 return;
1492
1493 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1494 {
1495 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1496 {
1497 /* Only set the value if it wasn't set yet. */
1498 if (term_strings[p->bt_entry] == NULL
1499 || term_strings[p->bt_entry] == empty_option)
1500 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001501#ifdef FEAT_EVAL
1502 int opt_idx = -1;
1503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /* 8bit terminal: use CSI instead of <Esc>[ */
1505 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1506 {
1507 char_u *s, *t;
1508
1509 s = vim_strsave((char_u *)p->bt_string);
1510 if (s != NULL)
1511 {
1512 for (t = s; *t; ++t)
1513 if (term_7to8bit(t))
1514 {
1515 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001516 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001519#ifdef FEAT_EVAL
1520 opt_idx =
1521#endif
1522 set_term_option_alloced(
1523 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525 }
1526 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001529#ifdef FEAT_EVAL
1530 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1531#endif
1532 }
1533#ifdef FEAT_EVAL
1534 set_term_option_sctx_idx(NULL, opt_idx);
1535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537 }
1538 else
1539 {
1540 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1541 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1542 if (find_termcode(name) == NULL)
1543 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1544 }
1545 }
1546}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548/*
1549 * Set number of colors.
1550 * Store it as a number in t_colors.
1551 * Store it as a string in T_CCO (using nr_colors[]).
1552 */
1553 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001554set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 char_u nr_colors[20]; /* string for number of colors */
1557
1558 t_colors = nr;
1559 if (t_colors > 1)
1560 sprintf((char *)nr_colors, "%d", t_colors);
1561 else
1562 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001563 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001565
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001566#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001567/*
1568 * Set the color count to "val" and redraw if it changed.
1569 */
1570 static void
1571may_adjust_color_count(int val)
1572{
1573 if (val != t_colors)
1574 {
1575 /* Nr of colors changed, initialize highlighting and
1576 * redraw everything. This causes a redraw, which usually
1577 * clears the message. Try keeping the message if it
1578 * might work. */
1579 set_keep_msg_from_hist();
1580 set_color_count(val);
1581 init_highlight(TRUE, FALSE);
1582# ifdef DEBUG_TERMRESPONSE
1583 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001584 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001585
Bram Moolenaarb255b902018-04-24 21:40:10 +02001586 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001587 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001588#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001589 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001590#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001591 }
1592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594
1595#ifdef HAVE_TGETENT
1596static char *(key_names[]) =
1597{
1598#ifdef FEAT_TERMRESPONSE
1599 /* Do this one first, it may cause a screen redraw. */
1600 "Co",
1601#endif
1602 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 "#2", "#4", "%i", "*7",
1604 "k1", "k2", "k3", "k4", "k5", "k6",
1605 "k7", "k8", "k9", "k;", "F1", "F2",
1606 "%1", "&8", "kb", "kI", "kD", "kh",
1607 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1608 NULL
1609};
1610#endif
1611
Bram Moolenaar9289df52018-05-10 14:40:57 +02001612#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001613 static void
1614get_term_entries(int *height, int *width)
1615{
1616 static struct {
1617 enum SpecialKey dest; /* index in term_strings[] */
1618 char *name; /* termcap name for string */
1619 } string_names[] =
1620 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1621 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1622 {KS_CL, "cl"}, {KS_CD, "cd"},
1623 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1624 {KS_ME, "me"}, {KS_MR, "mr"},
1625 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1626 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1627 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1628 {KS_STE,"Te"}, {KS_STS,"Ts"},
1629 {KS_CM, "cm"}, {KS_SR, "sr"},
1630 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1631 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1632 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1633 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1634 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1635 {KS_VS, "vs"}, {KS_CVS, "VS"},
1636 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1637 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1638 {KS_TS, "ts"}, {KS_FS, "fs"},
1639 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1640 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1641 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1642 {KS_8F, "8f"}, {KS_8B, "8b"},
1643 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1644 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001645 {KS_CST, "ST"}, {KS_CRT, "RT"},
1646 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001647 {(enum SpecialKey)0, NULL}
1648 };
1649 int i;
1650 char_u *p;
1651 static char_u tstrbuf[TBUFSZ];
1652 char_u *tp = tstrbuf;
1653
1654 /*
1655 * get output strings
1656 */
1657 for (i = 0; string_names[i].name != NULL; ++i)
1658 {
1659 if (TERM_STR(string_names[i].dest) == NULL
1660 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001661 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001662 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001663#ifdef FEAT_EVAL
1664 set_term_option_sctx_idx(string_names[i].name, -1);
1665#endif
1666 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001667 }
1668
1669 /* tgetflag() returns 1 if the flag is present, 0 if not and
1670 * possibly -1 if the flag doesn't exist. */
1671 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1672 T_MS = (char_u *)"y";
1673 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1674 T_XS = (char_u *)"y";
1675 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1676 T_XN = (char_u *)"y";
1677 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1678 T_DB = (char_u *)"y";
1679 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1680 T_DA = (char_u *)"y";
1681 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1682 T_UT = (char_u *)"y";
1683
1684 /*
1685 * get key codes
1686 */
1687 for (i = 0; key_names[i] != NULL; ++i)
1688 if (find_termcode((char_u *)key_names[i]) == NULL)
1689 {
1690 p = TGETSTR(key_names[i], &tp);
1691 /* if cursor-left == backspace, ignore it (televideo 925) */
1692 if (p != NULL
1693 && (*p != Ctrl_H
1694 || key_names[i][0] != 'k'
1695 || key_names[i][1] != 'l'))
1696 add_termcode((char_u *)key_names[i], p, FALSE);
1697 }
1698
1699 if (*height == 0)
1700 *height = tgetnum("li");
1701 if (*width == 0)
1702 *width = tgetnum("co");
1703
1704 /*
1705 * Get number of colors (if not done already).
1706 */
1707 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001708 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001709 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001710#ifdef FEAT_EVAL
1711 set_term_option_sctx_idx("Co", -1);
1712#endif
1713 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001714
1715# ifndef hpux
1716 BC = (char *)TGETSTR("bc", &tp);
1717 UP = (char *)TGETSTR("up", &tp);
1718 p = TGETSTR("pc", &tp);
1719 if (p)
1720 PC = *p;
1721# endif
1722}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001723#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001724
1725 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001726report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001727{
1728 struct builtin_term *termp;
1729
1730 mch_errmsg("\r\n");
1731 if (error_msg != NULL)
1732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001733 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001734 mch_errmsg("\r\n");
1735 }
1736 mch_errmsg("'");
1737 mch_errmsg((char *)term);
1738 mch_errmsg(_("' not known. Available builtin terminals are:"));
1739 mch_errmsg("\r\n");
1740 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1741 {
1742 if (termp->bt_entry == (int)KS_NAME)
1743 {
1744#ifdef HAVE_TGETENT
1745 mch_errmsg(" builtin_");
1746#else
1747 mch_errmsg(" ");
1748#endif
1749 mch_errmsg(termp->bt_string);
1750 mch_errmsg("\r\n");
1751 }
1752 }
1753}
1754
1755 static void
1756report_default_term(char_u *term)
1757{
1758 mch_errmsg(_("defaulting to '"));
1759 mch_errmsg((char *)term);
1760 mch_errmsg("'\r\n");
1761 if (emsg_silent == 0)
1762 {
1763 screen_start(); /* don't know where cursor is now */
1764 out_flush();
1765 if (!is_not_a_term())
1766 ui_delay(2000L, TRUE);
1767 }
1768}
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770/*
1771 * Set terminal options for terminal "term".
1772 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1773 *
1774 * While doing this, until ttest(), some options may be NULL, be careful.
1775 */
1776 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001777set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 struct builtin_term *termp;
1780#ifdef HAVE_TGETENT
1781 int builtin_first = p_tbi;
1782 int try;
1783 int termcap_cleared = FALSE;
1784#endif
1785 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001786 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 char_u *bs_p, *del_p;
1788
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001789 /* In silect mode (ex -s) we don't use the 'term' option. */
1790 if (silent_mode)
1791 return OK;
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 detected_8bit = FALSE; /* reset 8-bit detection */
1794
1795 if (term_is_builtin(term))
1796 {
1797 term += 8;
1798#ifdef HAVE_TGETENT
1799 builtin_first = 1;
1800#endif
1801 }
1802
1803/*
1804 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1805 * If builtin_first is TRUE:
1806 * 0. try builtin termcap
1807 * 1. try external termcap
1808 * 2. if both fail default to a builtin terminal
1809 * If builtin_first is FALSE:
1810 * 1. try external termcap
1811 * 2. try builtin termcap, if both fail default to a builtin terminal
1812 */
1813#ifdef HAVE_TGETENT
1814 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1815 {
1816 /*
1817 * Use external termcap
1818 */
1819 if (try == 1)
1820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 /*
1824 * If the external termcap does not have a matching entry, try the
1825 * builtin ones.
1826 */
1827 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (!termcap_cleared)
1830 {
1831 clear_termoptions(); /* clear old options */
1832 termcap_cleared = TRUE;
1833 }
1834
Bram Moolenaar69e05692018-05-10 14:11:52 +02001835 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 }
1838 else /* try == 0 || try == 2 */
1839#endif /* HAVE_TGETENT */
1840 /*
1841 * Use builtin termcap
1842 */
1843 {
1844#ifdef HAVE_TGETENT
1845 /*
1846 * If builtin termcap was already used, there is no need to search
1847 * for the builtin termcap again, quit now.
1848 */
1849 if (try == 2 && builtin_first && termcap_cleared)
1850 break;
1851#endif
1852 /*
1853 * search for 'term' in builtin_termcaps[]
1854 */
1855 termp = find_builtin_term(term);
1856 if (termp->bt_string == NULL) /* did not find it */
1857 {
1858#ifdef HAVE_TGETENT
1859 /*
1860 * If try == 0, first try the external termcap. If that is not
1861 * found we'll get back here with try == 2.
1862 * If termcap_cleared is set we used the external termcap,
1863 * don't complain about not finding the term in the builtin
1864 * termcap.
1865 */
1866 if (try == 0) /* try external one */
1867 continue;
1868 if (termcap_cleared) /* found in external termcap */
1869 break;
1870#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001871 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 /* when user typed :set term=xxx, quit here */
1874 if (starting != NO_SCREEN)
1875 {
1876 screen_start(); /* don't know where cursor is now */
1877 wait_return(TRUE);
1878 return FAIL;
1879 }
1880 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001881 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001882 set_string_option_direct((char_u *)"term", -1, term,
1883 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 display_errors();
1885 }
1886 out_flush();
1887#ifdef HAVE_TGETENT
1888 if (!termcap_cleared)
1889 {
1890#endif
1891 clear_termoptions(); /* clear old options */
1892#ifdef HAVE_TGETENT
1893 termcap_cleared = TRUE;
1894 }
1895#endif
1896 parse_builtin_tcap(term);
1897#ifdef FEAT_GUI
1898 if (term_is_gui(term))
1899 {
1900 out_flush();
1901 gui_init();
1902 /* If starting the GUI failed, don't do any of the other
1903 * things for this terminal */
1904 if (!gui.in_use)
1905 return FAIL;
1906#ifdef HAVE_TGETENT
1907 break; /* don't try using external termcap */
1908#endif
1909 }
1910#endif /* FEAT_GUI */
1911 }
1912#ifdef HAVE_TGETENT
1913 }
1914#endif
1915
1916/*
1917 * special: There is no info in the termcap about whether the cursor
1918 * positioning is relative to the start of the screen or to the start of the
1919 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1920 * relative.
1921 */
1922 if (STRCMP(term, "pcterm") == 0)
1923 T_CCS = (char_u *)"yes";
1924 else
1925 T_CCS = empty_option;
1926
1927#ifdef UNIX
1928/*
1929 * Any "stty" settings override the default for t_kb from the termcap.
1930 * This is in os_unix.c, because it depends a lot on the version of unix that
1931 * is being used.
1932 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1933 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001934# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 get_stty();
1938#endif
1939
1940/*
1941 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1942 * didn't work, use the default CTRL-H
1943 * The default for t_kD is DEL, unless t_kb is DEL.
1944 * The vim_strsave'd strings are probably lost forever, well it's only two
1945 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1946 * directly.
1947 */
1948#ifdef FEAT_GUI
1949 if (!gui.in_use)
1950#endif
1951 {
1952 bs_p = find_termcode((char_u *)"kb");
1953 del_p = find_termcode((char_u *)"kD");
1954 if (bs_p == NULL || *bs_p == NUL)
1955 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1956 if ((del_p == NULL || *del_p == NUL) &&
1957 (bs_p == NULL || *bs_p != DEL))
1958 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1959 }
1960
1961#if defined(UNIX) || defined(VMS)
1962 term_is_xterm = vim_is_xterm(term);
1963#endif
1964
1965#ifdef FEAT_MOUSE
1966# if defined(UNIX) || defined(VMS)
1967# ifdef FEAT_MOUSE_TTY
1968 /*
1969 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1970 * The termcode for the mouse is added as a side effect in option.c.
1971 */
1972 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001973 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001976 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 if (use_xterm_mouse())
1979 p = NULL; /* keep existing value, might be "xterm2" */
1980 else
1981 p = (char_u *)"xterm";
1982 }
1983# endif
1984 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001987 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1988 * "xterm2" in check_termcode(). */
1989 reset_option_was_set((char_u *)"ttym");
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (p == NULL
1992# ifdef FEAT_GUI
1993 || gui.in_use
1994# endif
1995 )
1996 check_mouse_termcode(); /* set mouse termcode anyway */
1997 }
1998# endif
1999# else
2000 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
2001# endif
2002#endif /* FEAT_MOUSE */
2003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef USE_TERM_CONSOLE
2005 /* DEFAULT_TERM indicates that it is the machine console. */
2006 if (STRCMP(term, DEFAULT_TERM) != 0)
2007 term_console = FALSE;
2008 else
2009 {
2010 term_console = TRUE;
2011# ifdef AMIGA
2012 win_resize_on(); /* enable window resizing reports */
2013# endif
2014 }
2015#endif
2016
2017#if defined(UNIX) || defined(VMS)
2018 /*
2019 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2020 */
2021 if (vim_is_fastterm(term))
2022 p_tf = TRUE;
2023#endif
2024#ifdef USE_TERM_CONSOLE
2025 /*
2026 * 'ttyfast' is default on consoles
2027 */
2028 if (term_console)
2029 p_tf = TRUE;
2030#endif
2031
2032 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2033
2034 full_screen = TRUE; /* we can use termcap codes from now on */
2035 set_term_defaults(); /* use current values as defaults */
2036#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002037 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002038 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040
2041 /*
2042 * Initialize the terminal with the appropriate termcap codes.
2043 * Set the mouse and window title if possible.
2044 * Don't do this when starting, need to parse the .vimrc first, because it
2045 * may redefine t_TI etc.
2046 */
2047 if (starting != NO_SCREEN)
2048 {
2049 starttermcap(); /* may change terminal mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 setmouse(); /* may start using the mouse */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#ifdef FEAT_TITLE
2052 maketitle(); /* may display window title */
2053#endif
2054 }
2055
2056 /* display initial screen after ttest() checking. jw. */
2057 if (width <= 0 || height <= 0)
2058 {
2059 /* termcap failed to report size */
2060 /* set defaults, in case ui_get_shellsize() also fails */
2061 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002062#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 height = 25; /* console is often 25 lines */
2064#else
2065 height = 24; /* most terminals are 24 lines */
2066#endif
2067 }
2068 set_shellsize(width, height, FALSE); /* may change Rows */
2069 if (starting != NO_SCREEN)
2070 {
2071 if (scroll_region)
2072 scroll_region_reset(); /* In case Rows changed */
2073 check_map_keycodes(); /* check mappings for terminal codes used */
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002076 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Execute the TermChanged autocommands for each buffer that is
2080 * loaded.
2081 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002082 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002083 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 if (curbuf->b_ml.ml_mfp != NULL)
2086 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2087 curbuf);
2088 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002089 if (bufref_valid(&old_curbuf))
2090 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093
2094#ifdef FEAT_TERMRESPONSE
2095 may_req_termresponse();
2096#endif
2097
2098 return OK;
2099}
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef HAVE_TGETENT
2102/*
2103 * Call tgetent()
2104 * Return error message if it fails, NULL if it's OK.
2105 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002107tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 int i;
2110
2111 i = TGETENT(tbuf, term);
2112 if (i < 0 /* -1 is always an error */
2113# ifdef TGETENT_ZERO_ERR
2114 || i == 0 /* sometimes zero is also an error */
2115# endif
2116 )
2117 {
2118 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2119 * tgetent() with the always existing "dumb" entry to avoid a crash or
2120 * hang. */
2121 (void)TGETENT(tbuf, "dumb");
2122
2123 if (i < 0)
2124# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (i == 0)
2127# endif
2128#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133 }
2134 return NULL;
2135}
2136
2137/*
2138 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2139 * Fix that here.
2140 */
2141 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002142vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 char *p;
2145
2146 p = tgetstr(s, (char **)pp);
2147 if (p == (char *)-1)
2148 p = NULL;
2149 return (char_u *)p;
2150}
2151#endif /* HAVE_TGETENT */
2152
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002153#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154/*
2155 * Get Columns and Rows from the termcap. Used after a window signal if the
2156 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2157 * and "li" entries never change. But on some systems this works.
2158 * Errors while getting the entries are ignored.
2159 */
2160 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002161getlinecol(
2162 long *cp, /* pointer to columns */
2163 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 char_u tbuf[TBUFSZ];
2166
2167 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2168 {
2169 if (*cp == 0)
2170 *cp = tgetnum("co");
2171 if (*rp == 0)
2172 *rp = tgetnum("li");
2173 }
2174}
2175#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2176
2177/*
2178 * Get a string entry from the termcap and add it to the list of termcodes.
2179 * Used for <t_xx> special keys.
2180 * Give an error message for failure when not sourcing.
2181 * If force given, replace an existing entry.
2182 * Return FAIL if the entry was not found, OK if the entry was added.
2183 */
2184 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002185add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 char_u *term;
2188 int key;
2189 struct builtin_term *termp;
2190#ifdef HAVE_TGETENT
2191 char_u *string;
2192 int i;
2193 int builtin_first;
2194 char_u tbuf[TBUFSZ];
2195 char_u tstrbuf[TBUFSZ];
2196 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200/*
2201 * If the GUI is running or will start in a moment, we only support the keys
2202 * that the GUI can produce.
2203 */
2204#ifdef FEAT_GUI
2205 if (gui.in_use || gui.starting)
2206 return gui_mch_haskey(name);
2207#endif
2208
2209 if (!force && find_termcode(name) != NULL) /* it's already there */
2210 return OK;
2211
2212 term = T_NAME;
2213 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2214 return FAIL;
2215
2216 if (term_is_builtin(term)) /* name starts with "builtin_" */
2217 {
2218 term += 8;
2219#ifdef HAVE_TGETENT
2220 builtin_first = TRUE;
2221#endif
2222 }
2223#ifdef HAVE_TGETENT
2224 else
2225 builtin_first = p_tbi;
2226#endif
2227
2228#ifdef HAVE_TGETENT
2229/*
2230 * We can get the entry from the builtin termcap and from the external one.
2231 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2232 * builtin termcap first.
2233 * If 'ttybuiltin' is off, try external termcap first.
2234 */
2235 for (i = 0; i < 2; ++i)
2236 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002237 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239 /*
2240 * Search in builtin termcap
2241 */
2242 {
2243 termp = find_builtin_term(term);
2244 if (termp->bt_string != NULL) /* found it */
2245 {
2246 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002247 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 while (termp->bt_entry != (int)KS_NAME)
2249 {
2250 if ((int)termp->bt_entry == key)
2251 {
2252 add_termcode(name, (char_u *)termp->bt_string,
2253 term_is_8bit(term));
2254 return OK;
2255 }
2256 ++termp;
2257 }
2258 }
2259 }
2260#ifdef HAVE_TGETENT
2261 else
2262 /*
2263 * Search in external termcap
2264 */
2265 {
2266 error_msg = tgetent_error(tbuf, term);
2267 if (error_msg == NULL)
2268 {
2269 string = TGETSTR((char *)name, &tp);
2270 if (string != NULL && *string != NUL)
2271 {
2272 add_termcode(name, string, FALSE);
2273 return OK;
2274 }
2275 }
2276 }
2277 }
2278#endif
2279
2280 if (sourcing_name == NULL)
2281 {
2282#ifdef HAVE_TGETENT
2283 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else
2286#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 return FAIL;
2290}
2291
2292 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002293term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2296}
2297
2298/*
2299 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2300 * Assume that the terminal is using 8-bit controls when the name contains
2301 * "8bit", like in "xterm-8bit".
2302 */
2303 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2307}
2308
2309/*
2310 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002311 * <Esc>[ -> CSI <M_C_[>
2312 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * <Esc>O -> <M-C-O>
2314 */
2315 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (*p == ESC)
2319 {
2320 if (p[1] == '[')
2321 return CSI;
2322 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002323 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (p[1] == 'O')
2325 return 0x8f;
2326 }
2327 return 0;
2328}
2329
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002330#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002332term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2335}
2336#endif
2337
2338#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2339
2340 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 static char_u buf[16];
2344 char_u *p;
2345
2346 p = buf + 15;
2347 *p = '\0';
2348 do
2349 {
2350 --p;
2351 *p = (char_u) (i % 10 + '0');
2352 i /= 10;
2353 }
2354 while (i > 0 && p > buf);
2355 return p;
2356}
2357#endif
2358
2359#ifndef HAVE_TGETENT
2360
2361/*
2362 * minimal tgoto() implementation.
2363 * no padding and we only parse for %i %d and %+char
2364 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002365 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002366tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 static char buf[30];
2369 char *p, *s, *e;
2370
2371 if (!cm)
2372 return "OOPS";
2373 e = buf + 29;
2374 for (s = buf; s < e && *cm; cm++)
2375 {
2376 if (*cm != '%')
2377 {
2378 *s++ = *cm;
2379 continue;
2380 }
2381 switch (*++cm)
2382 {
2383 case 'd':
2384 p = (char *)tltoa((unsigned long)y);
2385 y = x;
2386 while (*p)
2387 *s++ = *p++;
2388 break;
2389 case 'i':
2390 x++;
2391 y++;
2392 break;
2393 case '+':
2394 *s++ = (char)(*++cm + y);
2395 y = x;
2396 break;
2397 case '%':
2398 *s++ = *cm;
2399 break;
2400 default:
2401 return "OOPS";
2402 }
2403 }
2404 *s = '\0';
2405 return buf;
2406}
2407
2408#endif /* HAVE_TGETENT */
2409
2410/*
2411 * Set the terminal name and initialize the terminal options.
2412 * If "name" is NULL or empty, get the terminal name from the environment.
2413 * If that fails, use the default terminal name.
2414 */
2415 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002416termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 char_u *term;
2419
2420 if (name != NULL && *name == NUL)
2421 name = NULL; /* empty name is equal to no name */
2422 term = name;
2423
2424#ifdef __BEOS__
2425 /*
2426 * TERM environment variable is normally set to 'ansi' on the Bebox;
2427 * Since the BeBox doesn't quite support full ANSI yet, we use our
2428 * own custom 'ansi-beos' termcap instead, unless the -T option has
2429 * been given on the command line.
2430 */
2431 if (term == NULL
2432 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2433 term = DEFAULT_TERM;
2434#endif
2435#ifndef MSWIN
2436 if (term == NULL)
2437 term = mch_getenv((char_u *)"TERM");
2438#endif
2439 if (term == NULL || *term == NUL)
2440 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002441 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 /* Set the default terminal name. */
2444 set_string_default("term", term);
2445 set_string_default("ttytype", term);
2446
2447 /*
2448 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2449 */
2450 set_termname(T_NAME != NULL ? T_NAME : term);
2451}
2452
2453/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002454 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002456#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002457
2458// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002460
2461static int out_pos = 0; // number of chars in out_buf
2462
2463// Since the maximum number of SGR parameters shown as a normal value range is
2464// 16, the escape sequence length can be 4 * 16 + lead + tail.
2465#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467/*
2468 * out_flush(): flush the output buffer
2469 */
2470 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002471out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 int len;
2474
2475 if (out_pos != 0)
2476 {
2477 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2478 len = out_pos;
2479 out_pos = 0;
2480 ui_write(out_buf, len);
2481 }
2482}
2483
Bram Moolenaara338adc2018-01-31 20:51:47 +01002484/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002485 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2486 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002487 */
2488 void
2489out_flush_cursor(
2490 int force UNUSED, /* when TRUE, update cursor even when not moved */
2491 int clear_selection UNUSED) /* clear selection under cursor */
2492{
2493 mch_disable_flush();
2494 out_flush();
2495 mch_enable_flush();
2496#ifdef FEAT_GUI
2497 if (gui.in_use)
2498 {
2499 gui_update_cursor(force, clear_selection);
2500 gui_may_flush();
2501 }
2502#endif
2503}
2504
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
2507 * Sometimes a byte out of a multi-byte character is written with out_char().
2508 * To avoid flushing half of the character, call this function first.
2509 */
2510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002511out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2514 out_flush();
2515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517#ifdef FEAT_GUI
2518/*
2519 * out_trash(): Throw away the contents of the output buffer
2520 */
2521 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 out_pos = 0;
2525}
2526#endif
2527
2528/*
2529 * out_char(c): put a byte into the output buffer.
2530 * Flush it if it becomes full.
2531 * This should not be used for outputting text on the screen (use functions
2532 * like msg_puts() and screen_putchar() for that).
2533 */
2534 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002535out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaard0573012017-10-28 21:11:06 +02002537#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2539 out_char('\r');
2540#endif
2541
2542 out_buf[out_pos++] = c;
2543
2544 /* For testing we flush each time. */
2545 if (out_pos >= OUT_SIZE || p_wd)
2546 out_flush();
2547}
2548
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002549static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551/*
2552 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2553 */
2554 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002555out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
Bram Moolenaard0573012017-10-28 21:11:06 +02002557#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2559 out_char_nf('\r');
2560#endif
2561
2562 out_buf[out_pos++] = c;
2563
2564 if (out_pos >= OUT_SIZE)
2565 out_flush();
2566}
2567
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002568#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2569 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/*
2571 * A never-padding out_str.
2572 * use this whenever you don't want to run the string through tputs.
2573 * tputs above is harmless, but tputs from the termcap library
2574 * is likely to strip off leading digits, that it mistakes for padding
2575 * information, and "%i", "%d", etc.
2576 * This should only be used for writing terminal codes, not for outputting
2577 * normal text (use functions like msg_puts() and screen_putchar() for that).
2578 */
2579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002580out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002582 // avoid terminal strings being split up
2583 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 while (*s)
2587 out_char_nf(*s++);
2588
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002589 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (p_wd)
2591 out_flush();
2592}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002596 * A conditional-flushing out_str, mainly for visualbell.
2597 * Handles a delay internally, because termlib may not respect the delay or do
2598 * it at the wrong time.
2599 * Note: Only for terminal strings.
2600 */
2601 void
2602out_str_cf(char_u *s)
2603{
2604 if (s != NULL && *s)
2605 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002606#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002607 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002608#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002609
2610#ifdef FEAT_GUI
2611 /* Don't use tputs() when GUI is used, ncurses crashes. */
2612 if (gui.in_use)
2613 {
2614 out_str_nf(s);
2615 return;
2616 }
2617#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002618 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002619 out_flush();
2620#ifdef HAVE_TGETENT
2621 for (p = s; *s; ++s)
2622 {
2623 /* flush just before delay command */
2624 if (*s == '$' && *(s + 1) == '<')
2625 {
2626 char_u save_c = *s;
2627 int duration = atoi((char *)s + 2);
2628
2629 *s = NUL;
2630 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2631 *s = save_c;
2632 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002633# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002634 /* Only sleep here if we can limit this happening in
2635 * vim_beep(). */
2636 p = vim_strchr(s, '>');
2637 if (p == NULL || duration <= 0)
2638 {
2639 /* can't parse the time, don't sleep here */
2640 p = s;
2641 }
2642 else
2643 {
2644 ++p;
2645 do_sleep(duration);
2646 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002647# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002648 /* Rely on the terminal library to sleep. */
2649 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002650# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002651 break;
2652 }
2653 }
2654 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2655#else
2656 while (*s)
2657 out_char_nf(*s++);
2658#endif
2659
2660 /* For testing we write one string at a time. */
2661 if (p_wd)
2662 out_flush();
2663 }
2664}
2665
2666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 * out_str(s): Put a character string a byte at a time into the output buffer.
2668 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2669 * This should only be used for writing terminal codes, not for outputting
2670 * normal text (use functions like msg_puts() and screen_putchar() for that).
2671 */
2672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 if (s != NULL && *s)
2676 {
2677#ifdef FEAT_GUI
2678 /* Don't use tputs() when GUI is used, ncurses crashes. */
2679 if (gui.in_use)
2680 {
2681 out_str_nf(s);
2682 return;
2683 }
2684#endif
2685 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002686 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 out_flush();
2688#ifdef HAVE_TGETENT
2689 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2690#else
2691 while (*s)
2692 out_char_nf(*s++);
2693#endif
2694
2695 /* For testing we write one string at a time. */
2696 if (p_wd)
2697 out_flush();
2698 }
2699}
2700
2701/*
2702 * cursor positioning using termcap parser. (jw)
2703 */
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 OUT_STR(tgoto((char *)T_CM, col, row));
2708}
2709
2710 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002711term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 OUT_STR(tgoto((char *)T_CRI, 0, i));
2714}
2715
2716 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002717term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2720}
2721
2722 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002723term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2726}
2727
2728#if defined(HAVE_TGETENT) || defined(PROTO)
2729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 /* Can't handle a negative value here */
2733 if (x < 0)
2734 x = 0;
2735 if (y < 0)
2736 y = 0;
2737 OUT_STR(tgoto((char *)T_CWP, y, x));
2738}
2739
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002740# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2741/*
2742 * Return TRUE if we can request the terminal for a response.
2743 */
2744 static int
2745can_get_termresponse()
2746{
2747 return cur_tmode == TMODE_RAW
2748 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002749# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002750 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002751# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002752 && p_ek;
2753}
2754
Bram Moolenaarafd78262019-05-10 23:10:31 +02002755/*
2756 * Set "status" to STATUS_SENT.
2757 */
2758 static void
2759termrequest_sent(termrequest_T *status)
2760{
2761 status->tr_progress = STATUS_SENT;
2762 status->tr_start = time(NULL);
2763}
2764
2765/*
2766 * Return TRUE if any of the requests are in STATUS_SENT.
2767 */
2768 static int
2769termrequest_any_pending()
2770{
2771 int i;
2772 time_t now = time(NULL);
2773
2774 for (i = 0; all_termrequests[i] != NULL; ++i)
2775 {
2776 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2777 {
2778 if (all_termrequests[i]->tr_start > 0 && now > 0
2779 && all_termrequests[i]->tr_start + 2 < now)
2780 // Sent the request more than 2 seconds ago and didn't get a
2781 // response, assume it failed.
2782 all_termrequests[i]->tr_progress = STATUS_FAIL;
2783 else
2784 return TRUE;
2785 }
2786 }
2787 return FALSE;
2788}
2789
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002790static int winpos_x = -1;
2791static int winpos_y = -1;
2792static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002793
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002794# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002795/*
2796 * Try getting the Vim window position from the terminal.
2797 * Returns OK or FAIL.
2798 */
2799 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002800term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002801{
2802 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002803 int prev_winpos_x = winpos_x;
2804 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002805
2806 if (*T_CGP == NUL || !can_get_termresponse())
2807 return FAIL;
2808 winpos_x = -1;
2809 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002810 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002811 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812 OUT_STR(T_CGP);
2813 out_flush();
2814
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002815 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002816 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002817 {
2818 (void)vpeekc_nomap();
2819 if (winpos_x >= 0 && winpos_y >= 0)
2820 {
2821 *x = winpos_x;
2822 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002823 return OK;
2824 }
2825 ui_delay(10, FALSE);
2826 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002827 /* Do not reset "did_request_winpos", if we timed out the response might
2828 * still come later and we must consume it. */
2829
2830 winpos_x = prev_winpos_x;
2831 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002832 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002833 {
2834 /* Polling: return previous values if we have them. */
2835 *x = winpos_x;
2836 *y = winpos_y;
2837 return OK;
2838 }
2839
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002840 return FALSE;
2841}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002842# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002843# endif
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002846term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002848 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850#endif
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002853term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002856 int i = *s == CSI ? 1 : 2;
2857 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /* Special handling of 16 colors, because termcap can't handle it */
2860 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2861 /* Also accept CSI instead of <Esc>[ */
2862 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002863 && ((s[0] == ESC && s[1] == '[')
2864#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2865 || (s[0] == ESC && s[1] == '|')
2866#endif
2867 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 && s[i] != NUL
2869 && (STRCMP(s + i + 1, "%p1%dm") == 0
2870 || STRCMP(s + i + 1, "%dm") == 0)
2871 && (s[i] == '3' || s[i] == '4'))
2872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002874 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002876 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002878 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002879#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002880 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002881#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002882 IF_EB("\033[", ESC_STR "[")) : "\233";
2883 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2884 : (n >= 16 ? "48;5;" : "10");
2885
2886 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2888 }
2889 else
2890 OUT_STR(tgoto((char *)s, 0, n));
2891}
2892
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002893 void
2894term_fg_color(int n)
2895{
2896 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2897 if (*T_CAF)
2898 term_color(T_CAF, n);
2899 else if (*T_CSF)
2900 term_color(T_CSF, n);
2901}
2902
2903 void
2904term_bg_color(int n)
2905{
2906 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2907 if (*T_CAB)
2908 term_color(T_CAB, n);
2909 else if (*T_CSB)
2910 term_color(T_CSB, n);
2911}
2912
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002913#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002914
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002915#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2916#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2917#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002918
2919 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002920term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002921{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002922#define MAX_COLOR_STR_LEN 100
2923 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002924
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002925 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002926 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002927 OUT_STR(buf);
2928}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002929
2930 void
2931term_fg_rgb_color(guicolor_T rgb)
2932{
2933 term_rgb_color(T_8F, rgb);
2934}
2935
2936 void
2937term_bg_rgb_color(guicolor_T rgb)
2938{
2939 term_rgb_color(T_8B, rgb);
2940}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002941#endif
2942
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002943#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2944 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
2946 * Generic function to set window title, using t_ts and t_fs.
2947 */
2948 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002949term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 /* t_ts takes one argument: column in status line */
2952 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2953 out_str_nf(title);
2954 out_str(T_FS); /* set title end */
2955 out_flush();
2956}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002957
2958/*
2959 * Tell the terminal to push (save) the title and/or icon, so that it can be
2960 * popped (restored) later.
2961 */
2962 void
2963term_push_title(int which)
2964{
Bram Moolenaar27821262019-05-08 16:41:09 +02002965 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002966 {
2967 OUT_STR(T_CST);
2968 out_flush();
2969 }
2970
Bram Moolenaar27821262019-05-08 16:41:09 +02002971 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002972 {
2973 OUT_STR(T_SSI);
2974 out_flush();
2975 }
2976}
2977
2978/*
2979 * Tell the terminal to pop the title and/or icon.
2980 */
2981 void
2982term_pop_title(int which)
2983{
Bram Moolenaar27821262019-05-08 16:41:09 +02002984 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002985 {
2986 OUT_STR(T_CRT);
2987 out_flush();
2988 }
2989
Bram Moolenaar27821262019-05-08 16:41:09 +02002990 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002991 {
2992 OUT_STR(T_SRI);
2993 out_flush();
2994 }
2995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#endif
2997
2998/*
2999 * Make sure we have a valid set or terminal options.
3000 * Replace all entries that are NULL by empty_option
3001 */
3002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003003ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003005 char_u *env_colors;
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 check_options(); /* make sure no options are NULL */
3008
3009 /*
3010 * MUST have "cm": cursor motion.
3011 */
3012 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /*
3016 * if "cs" defined, use a scroll region, it's faster.
3017 */
3018 if (*T_CS != NUL)
3019 scroll_region = TRUE;
3020 else
3021 scroll_region = FALSE;
3022
3023 if (pairs)
3024 {
3025 /*
3026 * optional pairs
3027 */
3028 /* TP goes to normal mode for TI (invert) and TB (bold) */
3029 if (*T_ME == NUL)
3030 T_ME = T_MR = T_MD = T_MB = empty_option;
3031 if (*T_SO == NUL || *T_SE == NUL)
3032 T_SO = T_SE = empty_option;
3033 if (*T_US == NUL || *T_UE == NUL)
3034 T_US = T_UE = empty_option;
3035 if (*T_CZH == NUL || *T_CZR == NUL)
3036 T_CZH = T_CZR = empty_option;
3037
3038 /* T_VE is needed even though T_VI is not defined */
3039 if (*T_VE == NUL)
3040 T_VI = empty_option;
3041
3042 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3043 if (*T_ME == NUL)
3044 {
3045 T_ME = T_SE;
3046 T_MR = T_SO;
3047 T_MD = T_SO;
3048 }
3049
3050 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3051 if (*T_SO == NUL)
3052 {
3053 T_SE = T_ME;
3054 if (*T_MR == NUL)
3055 T_SO = T_MD;
3056 else
3057 T_SO = T_MR;
3058 }
3059
3060 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3061 if (*T_CZH == NUL)
3062 {
3063 T_CZR = T_ME;
3064 if (*T_MR == NUL)
3065 T_CZH = T_MD;
3066 else
3067 T_CZH = T_MR;
3068 }
3069
3070 /* "Sb" and "Sf" come in pairs */
3071 if (*T_CSB == NUL || *T_CSF == NUL)
3072 {
3073 T_CSB = empty_option;
3074 T_CSF = empty_option;
3075 }
3076
3077 /* "AB" and "AF" come in pairs */
3078 if (*T_CAB == NUL || *T_CAF == NUL)
3079 {
3080 T_CAB = empty_option;
3081 T_CAF = empty_option;
3082 }
3083
3084 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3085 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003086 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 /* Set 'weirdinvert' according to value of 't_xs' */
3089 p_wiv = (*T_XS != NUL);
3090 }
3091 need_gather = TRUE;
3092
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003093 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003095 env_colors = mch_getenv((char_u *)"COLORS");
3096 if (env_colors != NULL && isdigit(*env_colors))
3097 {
3098 int colors = atoi((char *)env_colors);
3099
3100 if (colors != t_colors)
3101 set_color_count(colors);
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3106 || defined(PROTO)
3107/*
3108 * Represent the given long_u as individual bytes, with the most significant
3109 * byte first, and store them in dst.
3110 */
3111 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int i;
3115 int shift;
3116
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003117 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 shift = 8 * (sizeof(long_u) - i);
3120 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3121 }
3122}
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124/*
3125 * Interpret the next string of bytes in buf as a long integer, with the most
3126 * significant byte first. Note that it is assumed that buf has been through
3127 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3128 * Puts result in val, and returns the number of bytes read from buf
3129 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3130 * were present.
3131 */
3132 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003133get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 int len;
3136 char_u bytes[sizeof(long_u)];
3137 int i;
3138 int shift;
3139
3140 *val = 0;
3141 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3142 if (len != -1)
3143 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003144 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 shift = 8 * (sizeof(long_u) - 1 - i);
3147 *val += (long_u)bytes[i] << shift;
3148 }
3149 }
3150 return len;
3151}
3152#endif
3153
3154#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003155 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3156 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157/*
3158 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3159 * that buf has been through inchar(). Returns the actual number of bytes used
3160 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3161 * available.
3162 */
3163 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003164get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 int len = 0;
3167 int i;
3168 char_u c;
3169
3170 for (i = 0; i < num_bytes; i++)
3171 {
3172 if ((c = buf[len++]) == NUL)
3173 return -1;
3174 if (c == K_SPECIAL)
3175 {
3176 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3177 return -1;
3178 if (buf[len++] == (int)KS_ZERO)
3179 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003180 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3181 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3182 if (buf[len++] == (int)KE_CSI)
3183 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003185 else if (c == CSI && buf[len] == KS_EXTRA
3186 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3188 * the start of a special key, see add_to_input_buf_csi(). */
3189 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 bytes[i] = c;
3191 }
3192 return len;
3193}
3194#endif
3195
3196/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003197 * Check if the new shell size is valid, correct it if it's too small or way
3198 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003201check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (Rows < min_rows()) /* need room for one window and command line */
3204 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003205 limit_screen_size();
3206}
3207
3208/*
3209 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3210 */
3211 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003212limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003213{
3214 if (Columns < MIN_COLUMNS)
3215 Columns = MIN_COLUMNS;
3216 else if (Columns > 10000)
3217 Columns = 10000;
3218 if (Rows > 1000)
3219 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
Bram Moolenaar86b68352004-12-27 21:59:20 +00003222/*
3223 * Invoked just before the screen structures are going to be (re)allocated.
3224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003226win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 static int old_Rows = 0;
3229 static int old_Columns = 0;
3230
3231 if (old_Rows != Rows || old_Columns != Columns)
3232 ui_new_shellsize();
3233 if (old_Rows != Rows)
3234 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003235 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003236 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003237 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 old_Rows = Rows;
3239 shell_new_rows(); /* update window sizes */
3240 }
3241 if (old_Columns != Columns)
3242 {
3243 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246}
3247
3248/*
3249 * Call this function when the Vim shell has been resized in any way.
3250 * Will obtain the current size and redraw (also when size didn't change).
3251 */
3252 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003253shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 set_shellsize(0, 0, FALSE);
3256}
3257
3258/*
3259 * Check if the shell size changed. Handle a resize.
3260 * When the size didn't change, nothing happens.
3261 */
3262 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003263shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int old_Rows = Rows;
3266 int old_Columns = Columns;
3267
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003268 if (!exiting
3269#ifdef FEAT_GUI
3270 /* Do not get the size when executing a shell command during
3271 * startup. */
3272 && !gui.starting
3273#endif
3274 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003275 {
3276 (void)ui_get_shellsize();
3277 check_shellsize();
3278 if (old_Rows != Rows || old_Columns != Columns)
3279 shell_resized();
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Set size of the Vim shell.
3285 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3286 * window size (this is used for the :win command).
3287 * If 'mustset' is FALSE, we may try to get the real window size and if
3288 * it fails use 'width' and 'height'.
3289 */
3290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 static int busy = FALSE;
3294
3295 /*
3296 * Avoid recursiveness, can happen when setting the window size causes
3297 * another window-changed signal.
3298 */
3299 if (busy)
3300 return;
3301
3302 if (width < 0 || height < 0) /* just checking... */
3303 return;
3304
Bram Moolenaara971b822011-09-14 14:43:25 +02003305 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003307 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 State = SETWSIZE;
3309 return;
3310 }
3311
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003312 if (updating_screen)
3313 // resizing while in update_screen() may cause a crash
3314 return;
3315
Bram Moolenaara971b822011-09-14 14:43:25 +02003316 /* curwin->w_buffer can be NULL when we are closing a window and the
3317 * buffer has already been closed and removing a scrollbar causes a resize
3318 * event. Don't resize then, it will happen after entering another buffer.
3319 */
3320 if (curwin->w_buffer == NULL)
3321 return;
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 ++busy;
3324
3325#ifdef AMIGA
3326 out_flush(); /* must do this before mch_get_shellsize() for
3327 some obscure reason */
3328#endif
3329
3330 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3331 {
3332 Rows = height;
3333 Columns = width;
3334 check_shellsize();
3335 ui_set_shellsize(mustset);
3336 }
3337 else
3338 check_shellsize();
3339
3340 /* The window layout used to be adjusted here, but it now happens in
3341 * screenalloc() (also invoked from screenclear()). That is because the
3342 * "busy" check above may skip this, but not screenalloc(). */
3343
3344 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3345 screenclear();
3346 else
3347 screen_start(); /* don't know where cursor is now */
3348
3349 if (starting != NO_SCREEN)
3350 {
3351#ifdef FEAT_TITLE
3352 maketitle();
3353#endif
3354 changed_line_abv_curs();
3355 invalidate_botline();
3356
3357 /*
3358 * We only redraw when it's needed:
3359 * - While at the more prompt or executing an external command, don't
3360 * redraw, but position the cursor.
3361 * - While editing the command line, only redraw that.
3362 * - in Ex mode, don't redraw anything.
3363 * - Otherwise, redraw right now, and position the cursor.
3364 * Always need to call update_screen() or screenalloc(), to make
3365 * sure Rows/Columns and the size of ScreenLines[] is correct!
3366 */
3367 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3368 || exmode_active)
3369 {
3370 screenalloc(FALSE);
3371 repeat_message();
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
3374 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003375 if (curwin->w_p_scb)
3376 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003377 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003378 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003379 update_screen(NOT_VALID);
3380 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003381 }
3382 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003383 {
3384 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003385 if (pum_visible())
3386 {
3387 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003388 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003389 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003390 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003391 if (redrawing())
3392 setcursor();
3393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 cursor_on(); /* redrawing may have switched it off */
3396 }
3397 out_flush();
3398 --busy;
3399}
3400
3401/*
3402 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3403 * commands and Ex mode).
3404 */
3405 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003406settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408#ifdef FEAT_GUI
3409 /* don't set the term where gvim was started to any mode */
3410 if (gui.in_use)
3411 return;
3412#endif
3413
3414 if (full_screen)
3415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 /*
3417 * When returning after calling a shell we want to really set the
3418 * terminal to raw mode, even though we think it already is, because
3419 * the shell program may have reset the terminal mode.
3420 * When we think the terminal is normal, don't try to set it to
3421 * normal again, because that causes problems (logout!) on some
3422 * machines.
3423 */
3424 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3425 {
3426#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003427# ifdef FEAT_GUI
3428 if (!gui.in_use && !gui.starting)
3429# endif
3430 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003431 // May need to check for T_CRV response and termcodes, it
3432 // doesn't work in Cooked mode, an external program may get
3433 // them.
3434 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003435 (void)vpeekc_nomap();
3436 check_for_codes_from_term();
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#endif
3439#ifdef FEAT_MOUSE_TTY
3440 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003441 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003443 if (termcap_active)
3444 {
3445 if (tmode != TMODE_RAW)
3446 out_str(T_BD); // disable bracketed paste mode
3447 else
3448 out_str(T_BE); // enable bracketed paste mode (should
3449 // be before mch_settmode().
3450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003452 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003455 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 out_flush();
3457 }
3458#ifdef FEAT_TERMRESPONSE
3459 may_req_termresponse();
3460#endif
3461 }
3462}
3463
3464 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003465starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (full_screen && !termcap_active)
3468 {
3469 out_str(T_TI); /* start termcap mode */
3470 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003471 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 out_flush();
3473 termcap_active = TRUE;
3474 screen_start(); /* don't know where cursor is now */
3475#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003476# ifdef FEAT_GUI
3477 if (!gui.in_use && !gui.starting)
3478# endif
3479 {
3480 may_req_termresponse();
3481 /* Immediately check for a response. If t_Co changes, we don't
3482 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003483 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003484 check_for_codes_from_term();
3485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#endif
3487 }
3488}
3489
3490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003491stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493 screen_stop_highlight();
3494 reset_cterm_colors();
3495 if (termcap_active)
3496 {
3497#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003498# ifdef FEAT_GUI
3499 if (!gui.in_use && !gui.starting)
3500# endif
3501 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003502 // May need to discard T_CRV, T_U7 or T_RBG response.
3503 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003504 {
3505# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003506 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003507 mch_delay(100L, FALSE);
3508# endif
3509# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003510 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003511 if (exiting)
3512 tcflush(fileno(stdin), TCIFLUSH);
3513# endif
3514 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003515 /* Check for termcodes first, otherwise an external program may
3516 * get them. */
3517 check_for_codes_from_term();
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003520 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 out_str(T_KE); /* stop "keypad transmit" mode */
3522 out_flush();
3523 termcap_active = FALSE;
3524 cursor_on(); /* just in case it is still off */
3525 out_str(T_TE); /* stop termcap mode */
3526 screen_start(); /* don't know where cursor is now */
3527 out_flush();
3528 }
3529}
3530
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003531#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532/*
3533 * Request version string (for xterm) when needed.
3534 * Only do this after switching to raw mode, otherwise the result will be
3535 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003536 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003537 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * Only do this after termcap mode has been started, otherwise the codes for
3539 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003540 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3541 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003542 * On Unix only do it when both output and input are a tty (avoid writing
3543 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * The result is caught in check_termcode().
3545 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003546 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003547may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003549 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003550 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003551 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 && *T_CRV != NUL)
3553 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003554 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003556 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* check for the characters now, otherwise they might be eaten by
3558 * get_keystroke() */
3559 out_flush();
3560 (void)vpeekc_nomap();
3561 }
3562}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003563
Bram Moolenaar9584b312013-03-13 19:29:28 +01003564/*
3565 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003566 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003567 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003568 * If the terminal treats \u25bd as single width, the position is (1, 1),
3569 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003570 * This function has the side effect that changes cursor position, so
3571 * it must be called immediately after entering termcap mode.
3572 */
3573 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003574may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003575{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003576 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003577 && can_get_termresponse()
3578 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003579 && *T_U7 != NUL
3580 && !option_was_set((char_u *)"ambiwidth"))
3581 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003582 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003583
Bram Moolenaarafd78262019-05-10 23:10:31 +02003584 LOG_TR(("Sending U7 request"));
3585 /* Do this in the second row. In the first row the returned sequence
3586 * may be CSI 1;2R, which is the same as <S-F3>. */
3587 term_windgoto(1, 0);
3588 buf[mb_char2bytes(0x25bd, buf)] = 0;
3589 out_str(buf);
3590 out_str(T_U7);
3591 termrequest_sent(&u7_status);
3592 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003593
Bram Moolenaarafd78262019-05-10 23:10:31 +02003594 /* This overwrites a few characters on the screen, a redraw is needed
3595 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003596 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003597 term_windgoto(1, 0);
3598 out_str((char_u *)" ");
3599 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003600
Bram Moolenaarafd78262019-05-10 23:10:31 +02003601 /* Need to reset the known cursor position. */
3602 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003603
Bram Moolenaarafd78262019-05-10 23:10:31 +02003604 /* check for the characters now, otherwise they might be eaten by
3605 * get_keystroke() */
3606 out_flush();
3607 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003608 }
3609}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003610
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003611/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003612 * Similar to requesting the version string: Request the terminal background
3613 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003614 */
3615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003616may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003617{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003618 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003619 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003620 int didit = FALSE;
3621
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003622# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003623 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003624 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003625 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003626 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003627 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003628 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003629 didit = TRUE;
3630 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003631# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003632
3633 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003634 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003635 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003636 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003637 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003638 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003639 didit = TRUE;
3640 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003641
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003642 if (didit)
3643 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003644 /* check for the characters now, otherwise they might be eaten by
3645 * get_keystroke() */
3646 out_flush();
3647 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003648 }
3649 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003650}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003651
Bram Moolenaar2951b772013-07-03 12:45:31 +02003652# ifdef DEBUG_TERMRESPONSE
3653 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003654log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003655{
3656 static FILE *fd_tr = NULL;
3657 static proftime_T start;
3658 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003659 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003660
3661 if (fd_tr == NULL)
3662 {
3663 fd_tr = fopen("termresponse.log", "w");
3664 profile_start(&start);
3665 }
3666 now = start;
3667 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003668 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3669 must_redraw == NOT_VALID ? "NV"
3670 : must_redraw == CLEAR ? "CL" : " ");
3671 va_start(ap, fmt);
3672 vfprintf(fd_tr, fmt, ap);
3673 va_end(ap);
3674 fputc('\n', fd_tr);
3675 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003676}
3677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678#endif
3679
3680/*
3681 * Return TRUE when saving and restoring the screen.
3682 */
3683 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003684swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 return (full_screen && *T_TI != NUL);
3687}
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
3690 * By outputting the 'cursor very visible' termcap code, for some windowed
3691 * terminals this makes the screen scrolled to the correct position.
3692 * Used when starting Vim or returning from a shell.
3693 */
3694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003695scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003697 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
3699 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003700 out_str(T_CVS);
3701 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703}
3704
3705static int cursor_is_off = FALSE;
3706
3707/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003708 * Enable the cursor without checking if it's already enabled.
3709 */
3710 void
3711cursor_on_force(void)
3712{
3713 out_str(T_VE);
3714 cursor_is_off = FALSE;
3715}
3716
3717/*
3718 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003721cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003724 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725}
3726
3727/*
3728 * Disable the cursor.
3729 */
3730 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003731cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003733 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003735 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 cursor_is_off = TRUE;
3737 }
3738}
3739
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003740#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003742 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003743 */
3744 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003745term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003746{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003747 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003748 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003749
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003750 /* Only do something when redrawing the screen and we can restore the
3751 * mode. */
3752 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003753 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003754# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003755 if (forced && initial_cursor_shape > 0)
3756 /* Restore to initial values. */
3757 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003758# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003759 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003760 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003761
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003762 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003763 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003764 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003765 {
3766 if (*T_CSR != NUL)
3767 p = T_CSR; /* Replace mode cursor */
3768 else
3769 p = T_CSI; /* fall back to Insert mode cursor */
3770 if (*p != NUL)
3771 {
3772 out_str(p);
3773 showing_mode = REPLACE;
3774 }
3775 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003776 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003777 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003778 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003779 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003780 {
3781 out_str(T_CSI); /* Insert mode cursor */
3782 showing_mode = INSERT;
3783 }
3784 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003785 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003786 {
3787 out_str(T_CEI); /* non-Insert mode cursor */
3788 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003789 }
3790}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003791
3792# if defined(FEAT_TERMINAL) || defined(PROTO)
3793 void
3794term_cursor_color(char_u *color)
3795{
3796 if (*T_CSC != NUL)
3797 {
3798 out_str(T_CSC); /* set cursor color start */
3799 out_str_nf(color);
3800 out_str(T_CEC); /* set cursor color end */
3801 out_flush();
3802 }
3803}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003804# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003805
Bram Moolenaar4db25542017-08-28 22:43:05 +02003806 int
3807blink_state_is_inverted()
3808{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003809#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003810 return rbm_status.tr_progress == STATUS_GOT
3811 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003812 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003813#else
3814 return FALSE;
3815#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003816}
3817
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003818/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003819 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003820 */
3821 void
3822term_cursor_shape(int shape, int blink)
3823{
3824 if (*T_CSH != NUL)
3825 {
3826 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3827 out_flush();
3828 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003829 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003830 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003831 int do_blink = blink;
3832
3833 /* t_SH is empty: try setting just the blink state.
3834 * The blink flags are XORed together, if the initial blinking from
3835 * style and shape differs, we need to invert the flag here. */
3836 if (blink_state_is_inverted())
3837 do_blink = !blink;
3838
3839 if (do_blink && *T_VS != NUL)
3840 {
3841 out_str(T_VS);
3842 out_flush();
3843 }
3844 else if (!do_blink && *T_CVS != NUL)
3845 {
3846 out_str(T_CVS);
3847 out_flush();
3848 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003849 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003850}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003851#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003852
3853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 * Set scrolling region for window 'wp'.
3855 * The region starts 'off' lines from the start of the window.
3856 * Also set the vertical scroll region for a vertically split window. Always
3857 * the full width of the window, excluding the vertical separator.
3858 */
3859 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003860scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3863 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003865 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3866 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 screen_start(); /* don't know where cursor is now */
3868}
3869
3870/*
3871 * Reset scrolling region to the whole screen.
3872 */
3873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (*T_CSV != NUL)
3878 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 screen_start(); /* don't know where cursor is now */
3880}
3881
3882
3883/*
3884 * List of terminal codes that are currently recognized.
3885 */
3886
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003887static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 char_u name[2]; /* termcap name of entry */
3890 char_u *code; /* terminal code (in allocated memory) */
3891 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003892 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893} *termcodes = NULL;
3894
3895static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3896static int tc_len = 0; /* current number of entries in termcodes[] */
3897
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003898static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003901clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 while (tc_len > 0)
3904 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003905 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 tc_max_len = 0;
3907
3908#ifdef HAVE_TGETENT
3909 BC = (char *)empty_option;
3910 UP = (char *)empty_option;
3911 PC = NUL; /* set pad character to NUL */
3912 ospeed = 0;
3913#endif
3914
3915 need_gather = TRUE; /* need to fill termleader[] */
3916}
3917
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918#define ATC_FROM_TERM 55
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920/*
3921 * Add a new entry to the list of terminal codes.
3922 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003923 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3924 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 */
3926 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003927add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
3929 struct termcode *new_tc;
3930 int i, j;
3931 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003932 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 if (string == NULL || *string == NUL)
3935 {
3936 del_termcode(name);
3937 return;
3938 }
3939
Bram Moolenaar4f974752019-02-17 17:44:42 +01003940#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003941 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3942#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003943# ifdef VIMDLL
3944 if (!gui.in_use)
3945 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3946 else
3947# endif
3948 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (s == NULL)
3951 return;
3952
3953 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003956 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 s[0] = term_7to8bit(string);
3958 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003959
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003960#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
3961# ifdef VIMDLL
3962 if (!gui.in_use)
3963# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02003964 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003965 if (s[0] == K_NUL)
3966 {
3967 STRMOVE(s + 1, s);
3968 s[1] = 3;
3969 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003970 }
3971#endif
3972
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003973 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 need_gather = TRUE; /* need to fill termleader[] */
3976
3977 /*
3978 * need to make space for more entries
3979 */
3980 if (tc_len == tc_max_len)
3981 {
3982 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003983 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (new_tc == NULL)
3985 {
3986 tc_max_len -= 20;
3987 return;
3988 }
3989 for (i = 0; i < tc_len; ++i)
3990 new_tc[i] = termcodes[i];
3991 vim_free(termcodes);
3992 termcodes = new_tc;
3993 }
3994
3995 /*
3996 * Look for existing entry with the same name, it is replaced.
3997 * Look for an existing entry that is alphabetical higher, the new entry
3998 * is inserted in front of it.
3999 */
4000 for (i = 0; i < tc_len; ++i)
4001 {
4002 if (termcodes[i].name[0] < name[0])
4003 continue;
4004 if (termcodes[i].name[0] == name[0])
4005 {
4006 if (termcodes[i].name[1] < name[1])
4007 continue;
4008 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 */
4011 if (termcodes[i].name[1] == name[1])
4012 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004013 if (flags == ATC_FROM_TERM && (j = termcode_star(
4014 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004015 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 /* Don't replace ESC[123;*X or ESC O*X with another when
4017 * invoked from got_code_from_term(). */
4018 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004019 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004020 && s[len - 1]
4021 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004022 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004023 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004024 vim_free(s);
4025 return;
4026 }
4027 }
4028 else
4029 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004030 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004031 vim_free(termcodes[i].code);
4032 --tc_len;
4033 break;
4034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036 }
4037 /*
4038 * Found alphabetical larger entry, move rest to insert new entry
4039 */
4040 for (j = tc_len; j > i; --j)
4041 termcodes[j] = termcodes[j - 1];
4042 break;
4043 }
4044
4045 termcodes[i].name[0] = name[0];
4046 termcodes[i].name[1] = name[1];
4047 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004048 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004049
4050 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4051 * accept modifiers. */
4052 termcodes[i].modlen = 0;
4053 j = termcode_star(s, len);
4054 if (j > 0)
4055 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 ++tc_len;
4057}
4058
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004059/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004060 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004062 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 */
4064 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004065termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004066{
4067 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4068 if (len >= 3 && code[len - 2] == '*')
4069 {
4070 if (len >= 5 && code[len - 3] == ';')
4071 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004072 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004073 return 1;
4074 }
4075 return 0;
4076}
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004079find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081 int i;
4082
4083 for (i = 0; i < tc_len; ++i)
4084 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4085 return termcodes[i].code;
4086 return NULL;
4087}
4088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004090get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 if (i >= tc_len)
4093 return NULL;
4094 return &termcodes[i].name[0];
4095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004097 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004098del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 int i;
4101
4102 if (termcodes == NULL) /* nothing there yet */
4103 return;
4104
4105 need_gather = TRUE; /* need to fill termleader[] */
4106
4107 for (i = 0; i < tc_len; ++i)
4108 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4109 {
4110 del_termcode_idx(i);
4111 return;
4112 }
4113 /* not found. Give error message? */
4114}
4115
4116 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004117del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 int i;
4120
4121 vim_free(termcodes[idx].code);
4122 --tc_len;
4123 for (i = idx; i < tc_len; ++i)
4124 termcodes[i] = termcodes[i + 1];
4125}
4126
4127#ifdef FEAT_TERMRESPONSE
4128/*
4129 * Called when detected that the terminal sends 8-bit codes.
4130 * Convert all 7-bit codes to their 8-bit equivalent.
4131 */
4132 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 int i;
4136 int c;
4137
4138 /* Only need to do something when not already using 8-bit codes. */
4139 if (!term_is_8bit(T_NAME))
4140 {
4141 for (i = 0; i < tc_len; ++i)
4142 {
4143 c = term_7to8bit(termcodes[i].code);
4144 if (c != 0)
4145 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004146 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 termcodes[i].code[0] = c;
4148 }
4149 }
4150 need_gather = TRUE; /* need to fill termleader[] */
4151 }
4152 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004153 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155#endif
4156
4157#ifdef CHECK_DOUBLE_CLICK
4158static linenr_T orig_topline = 0;
4159# ifdef FEAT_DIFF
4160static int orig_topfill = 0;
4161# endif
4162#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004163#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164/*
4165 * Checking for double clicks ourselves.
4166 * "orig_topline" is used to avoid detecting a double-click when the window
4167 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4168 */
4169/*
4170 * Set orig_topline. Used when jumping to another window, so that a double
4171 * click still works.
4172 */
4173 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004174set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 orig_topline = wp->w_topline;
4177# ifdef FEAT_DIFF
4178 orig_topfill = wp->w_topfill;
4179# endif
4180}
4181#endif
4182
4183/*
4184 * Check if typebuf.tb_buf[] contains a terminal key code.
4185 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4186 * + max_offset].
4187 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004188 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 * With a match, the match is removed, the replacement code is inserted in
4190 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4191 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004192 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4193 * "buflen" is then the length of the string in buf[] and is updated for
4194 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
4196 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004197check_termcode(
4198 int max_offset,
4199 char_u *buf,
4200 int bufsize,
4201 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 char_u *tp;
4204 char_u *p;
4205 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004206 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004208 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 int offset;
4210 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004211 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004212 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004213 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int new_slen;
4215 int extra;
4216 char_u string[MAX_KEY_CODE_LEN + 1];
4217 int i, j;
4218 int idx = 0;
4219#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004220# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4221 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 char_u bytes[6];
4223 int num_bytes;
4224# endif
4225 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 int is_click, is_drag;
4227 int wheel_code = 0;
4228 int current_button;
4229 static int held_button = MOUSE_RELEASE;
4230 static int orig_num_clicks = 1;
4231 static int orig_mouse_code = 0x0;
4232# ifdef CHECK_DOUBLE_CLICK
4233 static int orig_mouse_col = 0;
4234 static int orig_mouse_row = 0;
4235 static struct timeval orig_mouse_time = {0, 0};
4236 /* time of previous mouse click */
4237 struct timeval mouse_time; /* time of current mouse click */
4238 long timediff; /* elapsed time in msec */
4239# endif
4240#endif
4241 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4244
4245 /*
4246 * Speed up the checks for terminal codes by gathering all first bytes
4247 * used in termleader[]. Often this is just a single <Esc>.
4248 */
4249 if (need_gather)
4250 gather_termleader();
4251
4252 /*
4253 * Check at several positions in typebuf.tb_buf[], to catch something like
4254 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4255 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004256 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 * This is used often, KEEP IT FAST!
4258 */
4259 for (offset = 0; offset < max_offset; ++offset)
4260 {
4261 if (buf == NULL)
4262 {
4263 if (offset >= typebuf.tb_len)
4264 break;
4265 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4266 len = typebuf.tb_len - offset; /* length of the input */
4267 }
4268 else
4269 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004270 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004273 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275
4276 /*
4277 * Don't check characters after K_SPECIAL, those are already
4278 * translated terminal chars (avoid translating ~@^Hx).
4279 */
4280 if (*tp == K_SPECIAL)
4281 {
4282 offset += 2; /* there are always 2 extra characters */
4283 continue;
4284 }
4285
4286 /*
4287 * Skip this position if the character does not appear as the first
4288 * character in term_strings. This speeds up a lot, since most
4289 * termcodes start with the same character (ESC or CSI).
4290 */
4291 i = *tp;
4292 for (p = termleader; *p && *p != i; ++p)
4293 ;
4294 if (*p == NUL)
4295 continue;
4296
4297 /*
4298 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4299 * are in Insert mode.
4300 */
4301 if (*tp == ESC && !p_ek && (State & INSERT))
4302 continue;
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004305 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004306 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308#ifdef FEAT_GUI
4309 if (gui.in_use)
4310 {
4311 /*
4312 * GUI special key codes are all of the form [CSI xx].
4313 */
4314 if (*tp == CSI) /* Special key from GUI */
4315 {
4316 if (len < 3)
4317 return -1; /* Shouldn't happen */
4318 slen = 3;
4319 key_name[0] = tp[1];
4320 key_name[1] = tp[2];
4321 }
4322 }
4323 else
4324#endif /* FEAT_GUI */
4325 {
4326 for (idx = 0; idx < tc_len; ++idx)
4327 {
4328 /*
4329 * Ignore the entry if we are not at the start of
4330 * typebuf.tb_buf[]
4331 * and there are not enough characters to make a match.
4332 * But only when the 'K' flag is in 'cpoptions'.
4333 */
4334 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004335 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (cpo_koffset && offset && len < slen)
4337 continue;
4338 if (STRNCMP(termcodes[idx].code, tp,
4339 (size_t)(slen > len ? len : slen)) == 0)
4340 {
4341 if (len < slen) /* got a partial sequence */
4342 return -1; /* need to get more chars */
4343
4344 /*
4345 * When found a keypad key, check if there is another key
4346 * that matches and use that one. This makes <Home> to be
4347 * found instead of <kHome> when they produce the same
4348 * key code.
4349 */
4350 if (termcodes[idx].name[0] == 'K'
4351 && VIM_ISDIGIT(termcodes[idx].name[1]))
4352 {
4353 for (j = idx + 1; j < tc_len; ++j)
4354 if (termcodes[j].len == slen &&
4355 STRNCMP(termcodes[idx].code,
4356 termcodes[j].code, slen) == 0)
4357 {
4358 idx = j;
4359 break;
4360 }
4361 }
4362
4363 key_name[0] = termcodes[idx].name[0];
4364 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 break;
4366 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004367
4368 /*
4369 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 * <Esc>[123;*X (modslen == slen - 3)
4371 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4372 * When there is a modifier the * matches a number.
4373 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004374 */
4375 if (termcodes[idx].modlen > 0)
4376 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004377 modslen = termcodes[idx].modlen;
4378 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004379 continue;
4380 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004381 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004382 {
4383 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004384
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004385 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004386 return -1; /* need to get more chars */
4387
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004388 if (tp[modslen] == termcodes[idx].code[slen - 1])
4389 slen = modslen + 1; /* no modifiers */
4390 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004391 continue; /* no match */
4392 else
4393 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004394 // Skip over the digits, the final char must
4395 // follow. URXVT can use a negative value, thus
4396 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004397 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004398 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004399 ;
4400 ++j;
4401 if (len < j) /* got a partial sequence */
4402 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004403 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4404 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004405
Bram Moolenaara529ce02017-06-22 22:37:57 +02004406 modifiers_start = tp + slen - 2;
4407
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004408 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004409 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004410 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004411 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004412 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004413 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004414 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004415 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004416 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004417 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004418
4419 slen = j;
4420 }
4421 key_name[0] = termcodes[idx].name[0];
4422 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004423 break;
4424 }
4425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427 }
4428
4429#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004430 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004431 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004432 * detecting the start of these mouse codes they might as well be
4433 * another key code or terminal response. */
4434# ifdef FEAT_MOUSE_DEC
4435 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004436# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004437# ifdef FEAT_MOUSE_PTERM
4438 || key_name[0] == KS_PTERM_MOUSE
4439# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004440 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004442 /* Check for some responses from the terminal starting with
4443 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004444 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004445 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004446 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004447 * Also eat other possible responses to t_RV, rxvt returns
4448 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4449 * mrxvt has been reported to have "+" in the version. Assume
4450 * the escape sequence ends with a letter or one of "{|}~".
4451 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004452 * - Cursor position report: <Esc>[{row};{col}R
4453 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004454 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004455 *
4456 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004457 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004458 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004459
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004460 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004461 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004462 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004463 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004465 int col = 0;
4466 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004467 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004470 for (i = 2 + (tp[0] != CSI); i < len
4471 && !(tp[i] >= '{' && tp[i] <= '~')
4472 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004473 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004474 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004475 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004476 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004479 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004480 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004481 return -1;
4482 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004483 if (extra > 0)
4484 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004485
4486 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4487 * u7_status is not "sent", it may be from a previous Vim that
4488 * just exited. But not for <S-F3>, it sends something
4489 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004490 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004491 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004492 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004493 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004494 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004495
Bram Moolenaarb255b902018-04-24 21:40:10 +02004496 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004497 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004498 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004499 if (col == 2)
4500 aw = "single";
4501 else if (col == 3)
4502 aw = "double";
4503 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4504 {
4505 /* Setting the option causes a screen redraw. Do
4506 * that right away if possible, keeping any
4507 * messages. */
4508 set_option_value((char_u *)"ambw", 0L,
4509 (char_u *)aw, 0);
4510# ifdef DEBUG_TERMRESPONSE
4511 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004512 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004513
Bram Moolenaarb255b902018-04-24 21:40:10 +02004514 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004515 }
4516# else
4517 redraw_asap(CLEAR);
4518# endif
4519 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004520 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004521 key_name[0] = (int)KS_EXTRA;
4522 key_name[1] = (int)KE_IGNORE;
4523 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004524# ifdef FEAT_EVAL
4525 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4526# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004529 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4530 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004532 int version = col;
4533
Bram Moolenaarb255b902018-04-24 21:40:10 +02004534 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004535 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004536 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 /* If this code starts with CSI, you can bet that the
4539 * terminal uses 8-bit codes. */
4540 if (tp[0] == CSI)
4541 switch_to_8bit();
4542
4543 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004544 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 * Ignore it for when the user has set 'term' to xterm,
4546 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004547 if (version > 20000)
4548 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004550 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004552 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004553 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004554 int is_mintty = FALSE;
4555
4556 // mintty 2.9.5 sends 77;20905;0c.
4557 // (77 is ASCII 'M' for mintty.)
4558 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4559 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004562 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004564 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 check_for_codes = TRUE;
4566 need_gather = TRUE;
4567 req_codes_from_term();
4568 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004569
4570 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004571 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004572 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004573 {
4574 /* If run from Vim $COLORS is set to the number of
4575 * colors the terminal supports. Otherwise assume
4576 * 256, libvterm supports even more. */
4577 if (mch_getenv((char_u *)"COLORS") == NULL)
4578 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004579 /* Libvterm can handle SGR mouse reporting. */
4580 if (!option_was_set((char_u *)"ttym"))
4581 set_option_value((char_u *)"ttym", 0L,
4582 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004583 }
4584
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004585 if (version == 95)
4586 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004587 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004588 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4589 {
4590 is_not_xterm = TRUE;
4591 is_mac_terminal = TRUE;
4592 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004593 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004594 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4595 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004596 // old iTerm2 sends 0;95;
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004597 else if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004598 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004599 }
4600
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004601 /* Only set 'ttymouse' automatically if it was not set
4602 * by the user already. */
4603 if (!option_was_set((char_u *)"ttym"))
4604 {
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004605 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004606 * Terminal.app, iTerm2 and mintty. */
4607 if (version >= 277 || is_iterm2 || is_mac_terminal
4608 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004609 set_option_value((char_u *)"ttym", 0L,
4610 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004611 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004612 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004613 set_option_value((char_u *)"ttym", 0L,
4614 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004615 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004616
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004617 /* Detect terminals that set $TERM to something like
4618 * "xterm-256colors" but are not fully xterm
4619 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004620
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004621 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004622 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004623 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004624 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004625 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004626 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004627 is_not_xterm = TRUE;
4628
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004629 /* PuTTY sends 0;136;0
4630 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004631 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004632 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004633 is_not_xterm = TRUE;
4634
4635 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004636 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004637 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004638 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004639
Bram Moolenaar668324e2018-06-30 17:09:26 +02004640 // Xterm first responded to this request at patch level
4641 // 95, so assume anything below 95 is not xterm.
4642 if (version < 95)
4643 is_not_xterm = TRUE;
4644
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004645 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004646 * set. Only supported properly by xterm since version
4647 * 279 (otherwise it returns 0x18).
4648 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004649 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004650 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004651 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004652 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004653 && *T_CSH != NUL
4654 && *T_CRS != NUL)
4655 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004656 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004657 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004658 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004659 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004660 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004661
4662 /* Only request the cursor blink mode if t_RC set. Not
4663 * for Gnome terminal, it can't handle t_RC, it
4664 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004665 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004666 && !is_not_xterm
4667 && *T_CRC != NUL)
4668 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004669 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004670 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004671 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004672 need_flush = TRUE;
4673 }
4674
4675 if (need_flush)
4676 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004678 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004680 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 apply_autocmds(EVENT_TERMRESPONSE,
4683 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 key_name[0] = (int)KS_EXTRA;
4685 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004687
Bram Moolenaar4db25542017-08-28 22:43:05 +02004688 /* Check blinking cursor from xterm:
4689 * {lead}?12;1$y set
4690 * {lead}?12;2$y not set
4691 *
4692 * {lead} can be <Esc>[ or CSI
4693 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004694 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004695 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4696 && i == j + 6
4697 && tp[j + 1] == '1'
4698 && tp[j + 2] == '2'
4699 && tp[j + 3] == ';'
4700 && tp[i - 1] == '$'
4701 && tp[i] == 'y')
4702 {
4703 initial_cursor_blink = (tp[j + 4] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004704 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004705 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004706 key_name[0] = (int)KS_EXTRA;
4707 key_name[1] = (int)KE_IGNORE;
4708 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004709# ifdef FEAT_EVAL
4710 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4711# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004712 }
4713
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004714 /*
4715 * Check for a window position response from the terminal:
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004716 * {lead}3;{x};{y}t
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004717 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004718 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004719 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4720 || (len >= 3 && tp[0] == CSI))
4721 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4722 && tp[j + 1] == ';')
4723 {
4724 j += 2;
4725 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4726 ;
4727 if (i < len && tp[i] == ';')
4728 {
4729 winpos_x = atoi((char *)tp + j);
4730 j = i + 1;
4731 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4732 ;
4733 if (i < len && tp[i] == 't')
4734 {
4735 winpos_y = atoi((char *)tp + j);
4736 /* got finished code: consume it */
4737 key_name[0] = (int)KS_EXTRA;
4738 key_name[1] = (int)KE_IGNORE;
4739 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004740
4741 if (--did_request_winpos <= 0)
Bram Moolenaarafd78262019-05-10 23:10:31 +02004742 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004743 }
4744 }
4745 if (i == len)
4746 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004747 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004748 return -1;
4749 }
4750 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004751 }
4752
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004753 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004754 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004755 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004756 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004757 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004758 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004759 * {lead} can be <Esc>] or OSC
4760 * {tail} can be '\007', <Esc>\ or STERM.
4761 *
4762 * Consume any code that starts with "{lead}11;", it's also
4763 * possible that "rgba" is following.
4764 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004765 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004766 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4767 || tp[0] == OSC))
4768 {
4769 j = 1 + (tp[0] == ESC);
4770 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004771 || (argp[1] != '1' && argp[1] != '0')
4772 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004773 i = 0; /* no match */
4774 else
4775 for (i = j; i < len; ++i)
4776 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4777 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004778 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004779 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004780 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4781 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004782
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004783 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004784 && (is_4digit
4785 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004786 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004787 char_u *tp_r = tp + j + 7;
4788 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4789 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004790# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004791 int rval, gval, bval;
4792
Bram Moolenaar32e19772019-06-05 22:57:04 +02004793 rval = hexhex2nr(tp_r);
4794 gval = hexhex2nr(tp_b);
4795 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004796# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004797 if (is_bg)
4798 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004799 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4800 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004801
Bram Moolenaarb255b902018-04-24 21:40:10 +02004802 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004803 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004804# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004805 bg_r = rval;
4806 bg_g = gval;
4807 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004808# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004809 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02004810 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004811 {
4812 /* value differs, apply it */
4813 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02004814 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004815 reset_option_was_set((char_u *)"bg");
4816 redraw_asap(CLEAR);
4817 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004818 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004819# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004820 else
4821 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004822 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004823 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004824 fg_r = rval;
4825 fg_g = gval;
4826 fg_b = bval;
4827 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004828# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004829 }
4830
4831 /* got finished code: consume it */
4832 key_name[0] = (int)KS_EXTRA;
4833 key_name[1] = (int)KE_IGNORE;
4834 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004835# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004836 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4837 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004838# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004839 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004840 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004841 if (i == len)
4842 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004843 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004844 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004848 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004849 * {lead}{flag}+r<hex bytes><{tail}
4850 *
4851 * {lead} can be <Esc>P or DCS
4852 * {flag} can be '0' or '1'
4853 * {tail} can be Esc>\ or STERM
4854 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004855 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004856 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004857 *
4858 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02004859 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004860 *
4861 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004862 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004863 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004864 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 || tp[0] == DCS))
4866 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004867 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004868 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02004869 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004870 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02004871 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004872 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02004873 // key code response
4874 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02004876 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4877 || tp[i] == STERM)
4878 {
4879 if (i - j >= 3)
4880 got_code_from_term(tp + j, i);
4881 key_name[0] = (int)KS_EXTRA;
4882 key_name[1] = (int)KE_IGNORE;
4883 slen = i + 1 + (tp[i] == ESC);
4884 break;
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004887 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004888 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02004889 // Probably the cursor shape response. Make sure that "i"
4890 // is equal to "len" when there are not sufficient
4891 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01004892 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004893 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004894 if (i - j == 3 && !isdigit(tp[i]))
4895 break;
4896 if (i - j == 4 && tp[i] != ' ')
4897 break;
4898 if (i - j == 5 && tp[i] != 'q')
4899 break;
4900 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4901 break;
4902 if ((i - j == 6 && tp[i] == STERM)
4903 || (i - j == 7 && tp[i] == '\\'))
4904 {
4905 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004906
Bram Moolenaar66761db2019-06-05 22:07:51 +02004907 // 0, 1 = block blink, 2 = block
4908 // 3 = underline blink, 4 = underline
4909 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01004910 number = number == 0 ? 1 : number;
4911 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02004912 // The blink flag is actually inverted, compared to
4913 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01004914 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004915 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004916 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004917 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004918
Bram Moolenaar590ec872018-03-02 20:58:42 +01004919 key_name[0] = (int)KS_EXTRA;
4920 key_name[1] = (int)KE_IGNORE;
4921 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004922# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004923 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004924# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004925 break;
4926 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004927 }
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004931 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004932 /* These codes arrive many together, each code can be
4933 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02004934 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004935 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939#endif
4940
4941 if (key_name[0] == NUL)
4942 continue; /* No match at this position, try next one */
4943
4944 /* We only get here when we have a complete termcode match */
4945
4946#ifdef FEAT_MOUSE
4947# ifdef FEAT_GUI
4948 /*
4949 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4950 * so that we know which window to scroll later.
4951 */
4952 if (gui.in_use
4953 && key_name[0] == (int)KS_EXTRA
4954 && (key_name[1] == (int)KE_X1MOUSE
4955 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004956 || key_name[1] == (int)KE_MOUSELEFT
4957 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 || key_name[1] == (int)KE_MOUSEDOWN
4959 || key_name[1] == (int)KE_MOUSEUP))
4960 {
4961 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4962 if (num_bytes == -1) /* not enough coordinates */
4963 return -1;
4964 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4965 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4966 slen += num_bytes;
4967 }
4968 else
4969# endif
4970 /*
4971 * If it is a mouse click, get the coordinates.
4972 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004973 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004974# ifdef FEAT_MOUSE_GPM
4975 || key_name[0] == KS_GPM_MOUSE
4976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004978 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979# endif
4980# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004981 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982# endif
4983# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004984 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985# endif
4986# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004987 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004989# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004990 || key_name[0] == KS_URXVT_MOUSE
4991# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004992 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004993 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 is_click = is_drag = FALSE;
4996
Bram Moolenaar864207d2008-06-24 22:14:38 +00004997# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4998 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004999 if (key_name[0] == KS_MOUSE
5000# ifdef FEAT_MOUSE_GPM
5001 || key_name[0] == KS_GPM_MOUSE
5002# endif
5003 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005006 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * s == encoded button state:
5008 * 0x20 = left button down
5009 * 0x21 = middle button down
5010 * 0x22 = right button down
5011 * 0x23 = any button release
5012 * 0x60 = button 4 down (scroll wheel down)
5013 * 0x61 = button 5 down (scroll wheel up)
5014 * add 0x04 for SHIFT
5015 * add 0x08 for ALT
5016 * add 0x10 for CTRL
5017 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005018 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5019 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * c == column + ' ' + 1 == column + 33
5021 * r == row + ' ' + 1 == row + 33
5022 *
5023 * The coordinates are passed on through global variables.
5024 * Ugly, but this avoids trouble with mouse clicks at an
5025 * unexpected moment and allows for mapping them.
5026 */
5027 for (;;)
5028 {
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005029# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (gui.in_use)
5031 {
5032 /* GUI uses more bits for columns > 223 */
5033 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5034 if (num_bytes == -1) /* not enough coordinates */
5035 return -1;
5036 mouse_code = bytes[0];
5037 mouse_col = 128 * (bytes[1] - ' ' - 1)
5038 + bytes[2] - ' ' - 1;
5039 mouse_row = 128 * (bytes[3] - ' ' - 1)
5040 + bytes[4] - ' ' - 1;
5041 }
5042 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5046 if (num_bytes == -1) /* not enough coordinates */
5047 return -1;
5048 mouse_code = bytes[0];
5049 mouse_col = bytes[1] - ' ' - 1;
5050 mouse_row = bytes[2] - ' ' - 1;
5051 }
5052 slen += num_bytes;
5053
5054 /* If the following bytes is also a mouse code and it has
5055 * the same code, dump this one and get the next. This
5056 * makes dragging a whole lot faster. */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005057# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if (gui.in_use)
5059 j = 3;
5060 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 j = termcodes[idx].len;
5063 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5064 && tp[slen + j] == mouse_code
5065 && tp[slen + j + 1] != NUL
5066 && tp[slen + j + 2] != NUL
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005067# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 && (!gui.in_use
5069 || (tp[slen + j + 3] != NUL
5070 && tp[slen + j + 4] != NUL))
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 )
5073 slen += j;
5074 else
5075 break;
5076 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005079 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005080 || key_name[0] == KS_SGR_MOUSE
5081 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005082 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005083 /* URXVT 1015 mouse reporting mode:
5084 * Almost identical to xterm mouse mode, except the values
5085 * are decimal instead of bytes.
5086 *
5087 * \033[%d;%d;%dM
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005088 * ^-- row
5089 * ^----- column
5090 * ^-------- code
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005091 *
5092 * SGR 1006 mouse reporting mode:
5093 * Almost identical to xterm mouse mode, except the values
5094 * are decimal instead of bytes.
5095 *
5096 * \033[<%d;%d;%dM
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005097 * ^-- row
5098 * ^----- column
5099 * ^-------- code
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005100 *
5101 * \033[<%d;%d;%dm : mouse release event
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005102 * ^-- row
5103 * ^----- column
5104 * ^-------- code
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005105 */
5106 p = modifiers_start;
5107 if (p == NULL)
5108 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005109
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005110 mouse_code = getdigits(&p);
5111 if (*p++ != ';')
5112 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005113
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005114 /* when mouse reporting is SGR, add 32 to mouse code */
5115 if (key_name[0] == KS_SGR_MOUSE
5116 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5117 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005118
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005119 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5120 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005121
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005122 mouse_col = getdigits(&p) - 1;
5123 if (*p++ != ';')
5124 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005125
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005126 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005127
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005128 /* The modifiers were the mouse coordinates, not the
5129 * modifier keys (alt/shift/ctrl/meta) state. */
5130 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005131 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005132
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005133 if (key_name[0] == KS_MOUSE
5134# ifdef FEAT_MOUSE_GPM
5135 || key_name[0] == KS_GPM_MOUSE
5136# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005137# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005138 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005139# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005140 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005141 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005142 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005143# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 /*
5145 * Handle mouse events.
5146 * Recognize the xterm mouse wheel, but not in the GUI, the
5147 * Linux console with GPM and the MS-DOS or Win32 console
5148 * (multi-clicks use >= 0x60).
5149 */
5150 if (mouse_code >= MOUSEWHEEL_LOW
5151# ifdef FEAT_GUI
5152 && !gui.in_use
5153# endif
5154# ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005155 && key_name[0] != KS_GPM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156# endif
5157 )
5158 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005159# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5160 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5161 /* mouse-move event, using MOUSE_DRAG works */
5162 mouse_code = MOUSE_DRAG;
5163 else
5164# endif
5165 /* Keep the mouse_code before it's changed, so that we
5166 * remember that it was a mouse wheel click. */
5167 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
5169# ifdef FEAT_MOUSE_XTERM
5170 else if (held_button == MOUSE_RELEASE
5171# ifdef FEAT_GUI
5172 && !gui.in_use
5173# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005174 && (mouse_code == 0x23 || mouse_code == 0x24
5175 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005177 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5178 * And 0x40 and 0x41 are used by some xterm emulator. */
5179 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5180 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182# endif
5183
5184# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5185 else if (use_xterm_mouse() > 1)
5186 {
5187 if (mouse_code & MOUSE_DRAG_XTERM)
5188 mouse_code |= MOUSE_DRAG;
5189 }
5190# endif
5191# ifdef FEAT_XCLIPBOARD
5192 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5193 {
5194 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5195 stop_xterm_trace();
5196 else
5197 start_xterm_trace(mouse_code);
5198 }
5199# endif
5200# endif
5201 }
5202# endif /* !UNIX || FEAT_MOUSE_XTERM */
5203# ifdef FEAT_MOUSE_NET
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005204 if (key_name[0] == KS_NETTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 int mc, mr;
5207
5208 /* expect a rather limited sequence like: balancing {
5209 * \033}6,45\r
5210 * '6' is the row, 45 is the column
5211 */
5212 p = tp + slen;
5213 mr = getdigits(&p);
5214 if (*p++ != ',')
5215 return -1;
5216 mc = getdigits(&p);
5217 if (*p++ != '\r')
5218 return -1;
5219
5220 mouse_col = mc - 1;
5221 mouse_row = mr - 1;
5222 mouse_code = MOUSE_LEFT;
5223 slen += (int)(p - (tp + slen));
5224 }
5225# endif /* FEAT_MOUSE_NET */
5226# ifdef FEAT_MOUSE_JSB
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005227 if (key_name[0] == KS_JSBTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 int mult, val, iter, button, status;
5230
5231 /* JSBTERM Input Model
5232 * \033[0~zw uniq escape sequence
5233 * (L-x) Left button pressed - not pressed x not reporting
5234 * (M-x) Middle button pressed - not pressed x not reporting
5235 * (R-x) Right button pressed - not pressed x not reporting
5236 * (SDmdu) Single , Double click, m mouse move d button down
5237 * u button up
5238 * ### X cursor position padded to 3 digits
5239 * ### Y cursor position padded to 3 digits
5240 * (s-x) SHIFT key pressed - not pressed x not reporting
5241 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005242 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
5244
5245 p = tp + slen;
5246 button = mouse_code = 0;
5247 switch (*p++)
5248 {
5249 case 'L': button = 1; break;
5250 case '-': break;
5251 case 'x': break; /* ignore sequence */
5252 default: return -1; /* Unknown Result */
5253 }
5254 switch (*p++)
5255 {
5256 case 'M': button |= 2; break;
5257 case '-': break;
5258 case 'x': break; /* ignore sequence */
5259 default: return -1; /* Unknown Result */
5260 }
5261 switch (*p++)
5262 {
5263 case 'R': button |= 4; break;
5264 case '-': break;
5265 case 'x': break; /* ignore sequence */
5266 default: return -1; /* Unknown Result */
5267 }
5268 status = *p++;
5269 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5270 mult /= 10, p++)
5271 if (*p >= '0' && *p <= '9')
5272 val += (*p - '0') * mult;
5273 else
5274 return -1;
5275 mouse_col = val;
5276 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5277 mult /= 10, p++)
5278 if (*p >= '0' && *p <= '9')
5279 val += (*p - '0') * mult;
5280 else
5281 return -1;
5282 mouse_row = val;
5283 switch (*p++)
5284 {
5285 case 's': button |= 8; break; /* SHIFT key Pressed */
5286 case '-': break; /* Not Pressed */
5287 case 'x': break; /* Not Reporting */
5288 default: return -1; /* Unknown Result */
5289 }
5290 switch (*p++)
5291 {
5292 case 'c': button |= 16; break; /* CTRL key Pressed */
5293 case '-': break; /* Not Pressed */
5294 case 'x': break; /* Not Reporting */
5295 default: return -1; /* Unknown Result */
5296 }
5297 if (*p++ != '\033')
5298 return -1;
5299 if (*p++ != '\\')
5300 return -1;
5301 switch (status)
5302 {
5303 case 'D': /* Double Click */
5304 case 'S': /* Single Click */
5305 if (button & 1) mouse_code |= MOUSE_LEFT;
5306 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5307 if (button & 4) mouse_code |= MOUSE_RIGHT;
5308 if (button & 8) mouse_code |= MOUSE_SHIFT;
5309 if (button & 16) mouse_code |= MOUSE_CTRL;
5310 break;
5311 case 'm': /* Mouse move */
5312 if (button & 1) mouse_code |= MOUSE_LEFT;
5313 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5314 if (button & 4) mouse_code |= MOUSE_RIGHT;
5315 if (button & 8) mouse_code |= MOUSE_SHIFT;
5316 if (button & 16) mouse_code |= MOUSE_CTRL;
5317 if ((button & 7) != 0)
5318 {
5319 held_button = mouse_code;
5320 mouse_code |= MOUSE_DRAG;
5321 }
5322 is_drag = TRUE;
5323 showmode();
5324 break;
5325 case 'd': /* Button Down */
5326 if (button & 1) mouse_code |= MOUSE_LEFT;
5327 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5328 if (button & 4) mouse_code |= MOUSE_RIGHT;
5329 if (button & 8) mouse_code |= MOUSE_SHIFT;
5330 if (button & 16) mouse_code |= MOUSE_CTRL;
5331 break;
5332 case 'u': /* Button Up */
5333 if (button & 1)
5334 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5335 if (button & 2)
5336 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5337 if (button & 4)
5338 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5339 if (button & 8)
5340 mouse_code |= MOUSE_SHIFT;
5341 if (button & 16)
5342 mouse_code |= MOUSE_CTRL;
5343 break;
5344 default: return -1; /* Unknown Result */
5345 }
5346
5347 slen += (p - (tp + slen));
5348 }
5349# endif /* FEAT_MOUSE_JSB */
5350# ifdef FEAT_MOUSE_DEC
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005351 if (key_name[0] == KS_DEC_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 /* The DEC Locator Input Model
5354 * Netterm delivers the code sequence:
5355 * \033[2;4;24;80&w (left button down)
5356 * \033[3;0;24;80&w (left button up)
5357 * \033[6;1;24;80&w (right button down)
5358 * \033[7;0;24;80&w (right button up)
5359 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5360 * Pe is the event code
5361 * Pb is the button code
5362 * Pr is the row coordinate
5363 * Pc is the column coordinate
5364 * Pp is the third coordinate (page number)
5365 * Pe, the event code indicates what event caused this report
5366 * The following event codes are defined:
5367 * 0 - request, the terminal received an explicit request
5368 * for a locator report, but the locator is unavailable
5369 * 1 - request, the terminal received an explicit request
5370 * for a locator report
5371 * 2 - left button down
5372 * 3 - left button up
5373 * 4 - middle button down
5374 * 5 - middle button up
5375 * 6 - right button down
5376 * 7 - right button up
5377 * 8 - fourth button down
5378 * 9 - fourth button up
5379 * 10 - locator outside filter rectangle
5380 * Pb, the button code, ASCII decimal 0-15 indicating which
5381 * buttons are down if any. The state of the four buttons
5382 * on the locator correspond to the low four bits of the
5383 * decimal value,
5384 * "1" means button depressed
5385 * 0 - no buttons down,
5386 * 1 - right,
5387 * 2 - middle,
5388 * 4 - left,
5389 * 8 - fourth
5390 * Pr is the row coordinate of the locator position in the page,
5391 * encoded as an ASCII decimal value.
5392 * If Pr is omitted, the locator position is undefined
5393 * (outside the terminal window for example).
5394 * Pc is the column coordinate of the locator position in the
5395 * page, encoded as an ASCII decimal value.
5396 * If Pc is omitted, the locator position is undefined
5397 * (outside the terminal window for example).
5398 * Pp is the page coordinate of the locator position
5399 * encoded as an ASCII decimal value.
5400 * The page coordinate may be omitted if the locator is on
5401 * page one (the default). We ignore it anyway.
5402 */
5403 int Pe, Pb, Pr, Pc;
5404
5405 p = tp + slen;
5406
5407 /* get event status */
5408 Pe = getdigits(&p);
5409 if (*p++ != ';')
5410 return -1;
5411
5412 /* get button status */
5413 Pb = getdigits(&p);
5414 if (*p++ != ';')
5415 return -1;
5416
5417 /* get row status */
5418 Pr = getdigits(&p);
5419 if (*p++ != ';')
5420 return -1;
5421
5422 /* get column status */
5423 Pc = getdigits(&p);
5424
5425 /* the page parameter is optional */
5426 if (*p == ';')
5427 {
5428 p++;
5429 (void)getdigits(&p);
5430 }
5431 if (*p++ != '&')
5432 return -1;
5433 if (*p++ != 'w')
5434 return -1;
5435
5436 mouse_code = 0;
5437 switch (Pe)
5438 {
5439 case 0: return -1; /* position request while unavailable */
5440 case 1: /* a response to a locator position request includes
5441 the status of all buttons */
5442 Pb &= 7; /* mask off and ignore fourth button */
5443 if (Pb & 4)
5444 mouse_code = MOUSE_LEFT;
5445 if (Pb & 2)
5446 mouse_code = MOUSE_MIDDLE;
5447 if (Pb & 1)
5448 mouse_code = MOUSE_RIGHT;
5449 if (Pb)
5450 {
5451 held_button = mouse_code;
5452 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005453 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
5455 is_drag = TRUE;
5456 showmode();
5457 break;
5458 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005459 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 break;
5461 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5462 break;
5463 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005464 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 break;
5466 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5467 break;
5468 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005469 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 break;
5471 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5472 break;
5473 case 8: return -1; /* fourth button down */
5474 case 9: return -1; /* fourth button up */
5475 case 10: return -1; /* mouse outside of filter rectangle */
5476 default: return -1; /* should never occur */
5477 }
5478
5479 mouse_col = Pc - 1;
5480 mouse_row = Pr - 1;
5481
5482 slen += (int)(p - (tp + slen));
5483 }
5484# endif /* FEAT_MOUSE_DEC */
5485# ifdef FEAT_MOUSE_PTERM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005486 if (key_name[0] == KS_PTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005488 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490 p = tp + slen;
5491
5492 action = getdigits(&p);
5493 if (*p++ != ';')
5494 return -1;
5495
5496 mouse_row = getdigits(&p);
5497 if (*p++ != ';')
5498 return -1;
5499 mouse_col = getdigits(&p);
5500 if (*p++ != ';')
5501 return -1;
5502
5503 button = getdigits(&p);
5504 mouse_code = 0;
5505
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005506 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508 case 4: mouse_code = MOUSE_LEFT; break;
5509 case 1: mouse_code = MOUSE_RIGHT; break;
5510 case 2: mouse_code = MOUSE_MIDDLE; break;
5511 default: return -1;
5512 }
5513
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005514 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 case 31: /* Initial press */
5517 if (*p++ != ';')
5518 return -1;
5519
5520 num_clicks = getdigits(&p); /* Not used */
5521 break;
5522
5523 case 32: /* Release */
5524 mouse_code |= MOUSE_RELEASE;
5525 break;
5526
5527 case 33: /* Drag */
5528 held_button = mouse_code;
5529 mouse_code |= MOUSE_DRAG;
5530 break;
5531
5532 default:
5533 return -1;
5534 }
5535
5536 if (*p++ != 't')
5537 return -1;
5538
5539 slen += (p - (tp + slen));
5540 }
5541# endif /* FEAT_MOUSE_PTERM */
5542
5543 /* Interpret the mouse code */
5544 current_button = (mouse_code & MOUSE_CLICK_MASK);
5545 if (current_button == MOUSE_RELEASE
5546# ifdef FEAT_MOUSE_XTERM
5547 && wheel_code == 0
5548# endif
5549 )
5550 {
5551 /*
5552 * If we get a mouse drag or release event when
5553 * there is no mouse button held down (held_button ==
5554 * MOUSE_RELEASE), produce a K_IGNORE below.
5555 * (can happen when you hold down two buttons
5556 * and then let them go, or click in the menu bar, but not
5557 * on a menu, and drag into the text).
5558 */
5559 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5560 is_drag = TRUE;
5561 current_button = held_button;
5562 }
5563 else if (wheel_code == 0)
5564 {
5565# ifdef CHECK_DOUBLE_CLICK
5566# ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 /*
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005568 * Only for Unix, when GUI not active, we handle
5569 * multi-clicks here, but not for GPM mouse events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005571# ifdef FEAT_GUI
5572 if (key_name[0] != KS_GPM_MOUSE && !gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573# else
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005574 if (key_name[0] != KS_GPM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575# endif
5576# else
5577# ifdef FEAT_GUI
5578 if (!gui.in_use)
5579# endif
5580# endif
5581 {
5582 /*
5583 * Compute the time elapsed since the previous mouse click.
5584 */
5585 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005586 if (orig_mouse_time.tv_sec == 0)
5587 {
5588 /*
5589 * Avoid computing the difference between mouse_time
5590 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005591 * difference would be huge and would cause
5592 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005593 */
5594 timediff = p_mouset;
5595 }
5596 else
5597 {
5598 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005599 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005600 if (timediff < 0)
5601 --orig_mouse_time.tv_sec;
5602 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005603 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 orig_mouse_time = mouse_time;
5606 if (mouse_code == orig_mouse_code
5607 && timediff < p_mouset
5608 && orig_num_clicks != 4
5609 && orig_mouse_col == mouse_col
5610 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005611 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005613 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005615 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005616 /* Double click in tab pages line also works
5617 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005618 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005619 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 ++orig_num_clicks;
5621 else
5622 orig_num_clicks = 1;
5623 orig_mouse_col = mouse_col;
5624 orig_mouse_row = mouse_row;
5625 orig_topline = curwin->w_topline;
5626#ifdef FEAT_DIFF
5627 orig_topfill = curwin->w_topfill;
5628#endif
5629 }
5630# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5631 else
5632 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5633# endif
5634# else
5635 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5636# endif
5637 is_click = TRUE;
5638 orig_mouse_code = mouse_code;
5639 }
5640 if (!is_drag)
5641 held_button = mouse_code & MOUSE_CLICK_MASK;
5642
5643 /*
5644 * Translate the actual mouse event into a pseudo mouse event.
5645 * First work out what modifiers are to be used.
5646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (orig_mouse_code & MOUSE_SHIFT)
5648 modifiers |= MOD_MASK_SHIFT;
5649 if (orig_mouse_code & MOUSE_CTRL)
5650 modifiers |= MOD_MASK_CTRL;
5651 if (orig_mouse_code & MOUSE_ALT)
5652 modifiers |= MOD_MASK_ALT;
5653 if (orig_num_clicks == 2)
5654 modifiers |= MOD_MASK_2CLICK;
5655 else if (orig_num_clicks == 3)
5656 modifiers |= MOD_MASK_3CLICK;
5657 else if (orig_num_clicks == 4)
5658 modifiers |= MOD_MASK_4CLICK;
5659
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005660 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5661 * added, then it's not mouse up/down. */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005662 key_name[0] = KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005663 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005664 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005665 {
5666 if (wheel_code & MOUSE_CTRL)
5667 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005668 if (wheel_code & MOUSE_ALT)
5669 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 key_name[1] = (wheel_code & 1)
5671 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005672 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 else
5675 key_name[1] = get_pseudo_mouse_code(current_button,
5676 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005677
5678 /* Make sure the mouse position is valid. Some terminals may
5679 * return weird values. */
5680 if (mouse_col >= Columns)
5681 mouse_col = Columns - 1;
5682 if (mouse_row >= Rows)
5683 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685#endif /* FEAT_MOUSE */
5686
5687#ifdef FEAT_GUI
5688 /*
5689 * If using the GUI, then we get menu and scrollbar events.
5690 *
5691 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5692 * four bytes which are to be taken as a pointer to the vimmenu_T
5693 * structure.
5694 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005695 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005696 * is one byte with the tab index.
5697 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5699 * by one byte representing the scrollbar number, and then four bytes
5700 * representing a long_u which is the new value of the scrollbar.
5701 *
5702 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5703 * KE_FILLER followed by four bytes representing a long_u which is the
5704 * new value of the scrollbar.
5705 */
5706# ifdef FEAT_MENU
5707 else if (key_name[0] == (int)KS_MENU)
5708 {
5709 long_u val;
5710
5711 num_bytes = get_long_from_buf(tp + slen, &val);
5712 if (num_bytes == -1)
5713 return -1;
5714 current_menu = (vimmenu_T *)val;
5715 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005716
5717 /* The menu may have been deleted right after it was used, check
5718 * for that. */
5719 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5720 {
5721 key_name[0] = KS_EXTRA;
5722 key_name[1] = (int)KE_IGNORE;
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005726# ifdef FEAT_GUI_TABLINE
5727 else if (key_name[0] == (int)KS_TABLINE)
5728 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005729 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005730 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5731 if (num_bytes == -1)
5732 return -1;
5733 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005734 if (current_tab == 255) /* -1 in a byte gives 255 */
5735 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005736 slen += num_bytes;
5737 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005738 else if (key_name[0] == (int)KS_TABMENU)
5739 {
5740 /* Selecting tabline tab or using its menu. */
5741 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5742 if (num_bytes == -1)
5743 return -1;
5744 current_tab = (int)bytes[0];
5745 current_tabmenu = (int)bytes[1];
5746 slen += num_bytes;
5747 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749# ifndef USE_ON_FLY_SCROLL
5750 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5751 {
5752 long_u val;
5753
5754 /* Get the last scrollbar event in the queue of the same type */
5755 j = 0;
5756 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5757 && tp[j + 2] != NUL; ++i)
5758 {
5759 j += 3;
5760 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5761 if (num_bytes == -1)
5762 break;
5763 if (i == 0)
5764 current_scrollbar = (int)bytes[0];
5765 else if (current_scrollbar != (int)bytes[0])
5766 break;
5767 j += num_bytes;
5768 num_bytes = get_long_from_buf(tp + j, &val);
5769 if (num_bytes == -1)
5770 break;
5771 scrollbar_value = val;
5772 j += num_bytes;
5773 slen = j;
5774 }
5775 if (i == 0) /* not enough characters to make one */
5776 return -1;
5777 }
5778 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5779 {
5780 long_u val;
5781
5782 /* Get the last horiz. scrollbar event in the queue */
5783 j = 0;
5784 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5785 && tp[j + 2] != NUL; ++i)
5786 {
5787 j += 3;
5788 num_bytes = get_long_from_buf(tp + j, &val);
5789 if (num_bytes == -1)
5790 break;
5791 scrollbar_value = val;
5792 j += num_bytes;
5793 slen = j;
5794 }
5795 if (i == 0) /* not enough characters to make one */
5796 return -1;
5797 }
5798# endif /* !USE_ON_FLY_SCROLL */
5799#endif /* FEAT_GUI */
5800
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005801 /*
5802 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5803 */
5804 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5805
5806 /*
5807 * Add any modifier codes to our string.
5808 */
5809 new_slen = 0; /* Length of what will replace the termcode */
5810 if (modifiers != 0)
5811 {
5812 /* Some keys have the modifier included. Need to handle that here
5813 * to make mappings work. */
5814 key = simplify_key(key, &modifiers);
5815 if (modifiers != 0)
5816 {
5817 string[new_slen++] = K_SPECIAL;
5818 string[new_slen++] = (int)KS_MODIFIER;
5819 string[new_slen++] = modifiers;
5820 }
5821 }
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005824 key_name[0] = KEY2TERMCAP0(key);
5825 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005827 {
5828 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005829 if (has_mbyte)
5830 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5831 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005832 string[new_slen++] = key_name[1];
5833 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005834 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5835 && key_name[1] == KE_IGNORE)
5836 {
5837 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5838 * to indicate what happened. */
5839 retval = KEYLEN_REMOVED;
5840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 else
5842 {
5843 string[new_slen++] = K_SPECIAL;
5844 string[new_slen++] = key_name[0];
5845 string[new_slen++] = key_name[1];
5846 }
5847 string[new_slen] = NUL;
5848 extra = new_slen - slen;
5849 if (buf == NULL)
5850 {
5851 if (extra < 0)
5852 /* remove matched chars, taking care of noremap */
5853 del_typebuf(-extra, offset);
5854 else if (extra > 0)
5855 /* insert the extra space we need */
5856 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5857
5858 /*
5859 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5860 * typebuf.tb_buf[]!
5861 */
5862 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5863 (size_t)new_slen);
5864 }
5865 else
5866 {
5867 if (extra < 0)
5868 /* remove matched characters */
5869 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005870 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005872 {
5873 /* Insert the extra space we need. If there is insufficient
5874 * space return -1. */
5875 if (*buflen + extra + new_slen >= bufsize)
5876 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005878 (size_t)(*buflen - offset));
5879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005881 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005883 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885
Bram Moolenaar2951b772013-07-03 12:45:31 +02005886#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005887 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005888#endif
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return 0; /* no match found */
5891}
5892
Bram Moolenaar9377df32017-10-15 13:22:01 +02005893#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005894/*
5895 * Get the text foreground color, if known.
5896 */
5897 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005898term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005899{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005900 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005901 {
5902 *r = fg_r;
5903 *g = fg_g;
5904 *b = fg_b;
5905 }
5906}
5907
5908/*
5909 * Get the text background color, if known.
5910 */
5911 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005912term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005913{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005914 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005915 {
5916 *r = bg_r;
5917 *g = bg_g;
5918 *b = bg_b;
5919 }
5920}
5921#endif
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923/*
5924 * Replace any terminal code strings in from[] with the equivalent internal
5925 * vim representation. This is used for the "from" and "to" part of a
5926 * mapping, and the "to" part of a menu command.
5927 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5928 * '<'.
5929 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5930 *
5931 * The replacement is done in result[] and finally copied into allocated
5932 * memory. If this all works well *bufp is set to the allocated memory and a
5933 * pointer to it is returned. If something fails *bufp is set to NULL and from
5934 * is returned.
5935 *
5936 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5937 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5938 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5939 * instead of a CTRL-V.
5940 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005941 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005942replace_termcodes(
5943 char_u *from,
5944 char_u **bufp,
5945 int from_part,
5946 int do_lt, /* also translate <lt> */
5947 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 int i;
5950 int slen;
5951 int key;
5952 int dlen = 0;
5953 char_u *src;
5954 int do_backslash; /* backslash is a special character */
5955 int do_special; /* recognize <> key codes */
5956 int do_key_code; /* recognize raw key codes */
5957 char_u *result; /* buffer for resulting string */
5958
5959 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005960 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5962
5963 /*
5964 * Allocate space for the translation. Worst case a single character is
5965 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5966 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005967 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (result == NULL) /* out of memory */
5969 {
5970 *bufp = NULL;
5971 return from;
5972 }
5973
5974 src = from;
5975
5976 /*
5977 * Check for #n at start only: function key n
5978 */
5979 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5980 {
5981 result[dlen++] = K_SPECIAL;
5982 result[dlen++] = 'k';
5983 if (src[1] == '0')
5984 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5985 else
5986 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5987 src += 2;
5988 }
5989
5990 /*
5991 * Copy each byte from *from to result[dlen]
5992 */
5993 while (*src != NUL)
5994 {
5995 /*
5996 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005997 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6000 {
6001#ifdef FEAT_EVAL
6002 /*
6003 * Replace <SID> by K_SNR <script-nr> _.
6004 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6005 */
6006 if (STRNICMP(src, "<SID>", 5) == 0)
6007 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006008 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006009 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 else
6011 {
6012 src += 5;
6013 result[dlen++] = K_SPECIAL;
6014 result[dlen++] = (int)KS_EXTRA;
6015 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006016 sprintf((char *)result + dlen, "%ld",
6017 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 dlen += (int)STRLEN(result + dlen);
6019 result[dlen++] = '_';
6020 continue;
6021 }
6022 }
6023#endif
6024
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006025 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (slen)
6027 {
6028 dlen += slen;
6029 continue;
6030 }
6031 }
6032
6033 /*
6034 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6035 * Note that this is also checked after replacing the <> form.
6036 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6037 * it could be a character in the file.
6038 */
6039 if (do_key_code)
6040 {
6041 i = find_term_bykeys(src);
6042 if (i >= 0)
6043 {
6044 result[dlen++] = K_SPECIAL;
6045 result[dlen++] = termcodes[i].name[0];
6046 result[dlen++] = termcodes[i].name[1];
6047 src += termcodes[i].len;
6048 /* If terminal code matched, continue after it. */
6049 continue;
6050 }
6051 }
6052
6053#ifdef FEAT_EVAL
6054 if (do_special)
6055 {
6056 char_u *p, *s, len;
6057
6058 /*
6059 * Replace <Leader> by the value of "mapleader".
6060 * Replace <LocalLeader> by the value of "maplocalleader".
6061 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6062 */
6063 if (STRNICMP(src, "<Leader>", 8) == 0)
6064 {
6065 len = 8;
6066 p = get_var_value((char_u *)"g:mapleader");
6067 }
6068 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6069 {
6070 len = 13;
6071 p = get_var_value((char_u *)"g:maplocalleader");
6072 }
6073 else
6074 {
6075 len = 0;
6076 p = NULL;
6077 }
6078 if (len != 0)
6079 {
6080 /* Allow up to 8 * 6 characters for "mapleader". */
6081 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6082 s = (char_u *)"\\";
6083 else
6084 s = p;
6085 while (*s != NUL)
6086 result[dlen++] = *s++;
6087 src += len;
6088 continue;
6089 }
6090 }
6091#endif
6092
6093 /*
6094 * Remove CTRL-V and ignore the next character.
6095 * For "from" side the CTRL-V at the end is included, for the "to"
6096 * part it is removed.
6097 * If 'cpoptions' does not contain 'B', also accept a backslash.
6098 */
6099 key = *src;
6100 if (key == Ctrl_V || (do_backslash && key == '\\'))
6101 {
6102 ++src; /* skip CTRL-V or backslash */
6103 if (*src == NUL)
6104 {
6105 if (from_part)
6106 result[dlen++] = key;
6107 break;
6108 }
6109 }
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006112 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 /*
6115 * If the character is K_SPECIAL, replace it with K_SPECIAL
6116 * KS_SPECIAL KE_FILLER.
6117 * If compiled with the GUI replace CSI with K_CSI.
6118 */
6119 if (*src == K_SPECIAL)
6120 {
6121 result[dlen++] = K_SPECIAL;
6122 result[dlen++] = KS_SPECIAL;
6123 result[dlen++] = KE_FILLER;
6124 }
6125# ifdef FEAT_GUI
6126 else if (*src == CSI)
6127 {
6128 result[dlen++] = K_SPECIAL;
6129 result[dlen++] = KS_EXTRA;
6130 result[dlen++] = (int)KE_CSI;
6131 }
6132# endif
6133 else
6134 result[dlen++] = *src;
6135 ++src;
6136 }
6137 }
6138 result[dlen] = NUL;
6139
6140 /*
6141 * Copy the new string to allocated memory.
6142 * If this fails, just return from.
6143 */
6144 if ((*bufp = vim_strsave(result)) != NULL)
6145 from = *bufp;
6146 vim_free(result);
6147 return from;
6148}
6149
6150/*
6151 * Find a termcode with keys 'src' (must be NUL terminated).
6152 * Return the index in termcodes[], or -1 if not found.
6153 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006154 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006155find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006158 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 for (i = 0; i < tc_len; ++i)
6161 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006162 if (slen == termcodes[i].len
6163 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 return i;
6165 }
6166 return -1;
6167}
6168
6169/*
6170 * Gather the first characters in the terminal key codes into a string.
6171 * Used to speed up check_termcode().
6172 */
6173 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006174gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 int i;
6177 int len = 0;
6178
6179#ifdef FEAT_GUI
6180 if (gui.in_use)
6181 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6182#endif
6183#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006184 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 termleader[len++] = DCS; /* the termcode response starts with DCS
6186 in 8-bit mode */
6187#endif
6188 termleader[len] = NUL;
6189
6190 for (i = 0; i < tc_len; ++i)
6191 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6192 {
6193 termleader[len++] = termcodes[i].code[0];
6194 termleader[len] = NUL;
6195 }
6196
6197 need_gather = FALSE;
6198}
6199
6200/*
6201 * Show all termcodes (for ":set termcap")
6202 * This code looks a lot like showoptions(), but is different.
6203 */
6204 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006205show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 int col;
6208 int *items;
6209 int item_count;
6210 int run;
6211 int row, rows;
6212 int cols;
6213 int i;
6214 int len;
6215
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006216#define INC3 27 /* try to make three columns */
6217#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218#define GAP 2 /* spaces between columns */
6219
6220 if (tc_len == 0) /* no terminal codes (must be GUI) */
6221 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006222 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (items == NULL)
6224 return;
6225
6226 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006227 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229 /*
6230 * do the loop two times:
6231 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006232 * 2. display the medium items (medium length strings)
6233 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006235 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
6237 /*
6238 * collect the items in items[]
6239 */
6240 item_count = 0;
6241 for (i = 0; i < tc_len; i++)
6242 {
6243 len = show_one_termcode(termcodes[i].name,
6244 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006245 if (len <= INC3 - GAP ? run == 1
6246 : len <= INC2 - GAP ? run == 2
6247 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 items[item_count++] = i;
6249 }
6250
6251 /*
6252 * display the items
6253 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006254 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006256 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 if (cols == 0)
6258 cols = 1;
6259 rows = (item_count + cols - 1) / cols;
6260 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006261 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 rows = item_count;
6263 for (row = 0; row < rows && !got_int; ++row)
6264 {
6265 msg_putchar('\n'); /* go to next line */
6266 if (got_int) /* 'q' typed in more */
6267 break;
6268 col = 0;
6269 for (i = row; i < item_count; i += rows)
6270 {
6271 msg_col = col; /* make columns */
6272 show_one_termcode(termcodes[items[i]].name,
6273 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006274 if (run == 2)
6275 col += INC2;
6276 else
6277 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 out_flush();
6280 ui_breakcheck();
6281 }
6282 }
6283 vim_free(items);
6284}
6285
6286/*
6287 * Show one termcode entry.
6288 * Output goes into IObuff[]
6289 */
6290 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006291show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 char_u *p;
6294 int len;
6295
6296 if (name[0] > '~')
6297 {
6298 IObuff[0] = ' ';
6299 IObuff[1] = ' ';
6300 IObuff[2] = ' ';
6301 IObuff[3] = ' ';
6302 }
6303 else
6304 {
6305 IObuff[0] = 't';
6306 IObuff[1] = '_';
6307 IObuff[2] = name[0];
6308 IObuff[3] = name[1];
6309 }
6310 IObuff[4] = ' ';
6311
6312 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6313 if (p[1] != 't')
6314 STRCPY(IObuff + 5, p);
6315 else
6316 IObuff[5] = NUL;
6317 len = (int)STRLEN(IObuff);
6318 do
6319 IObuff[len++] = ' ';
6320 while (len < 17);
6321 IObuff[len] = NUL;
6322 if (code == NULL)
6323 len += 4;
6324 else
6325 len += vim_strsize(code);
6326
6327 if (printit)
6328 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006329 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006331 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 else
6333 msg_outtrans(code);
6334 }
6335 return len;
6336}
6337
6338#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6339/*
6340 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6341 * termcap codes from the terminal itself.
6342 * We get them one by one to avoid a very long response string.
6343 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006344static int xt_index_in = 0;
6345static int xt_index_out = 0;
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006348req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 xt_index_out = 0;
6351 xt_index_in = 0;
6352 req_more_codes_from_term();
6353}
6354
6355 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006356req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 char buf[11];
6359 int old_idx = xt_index_out;
6360
6361 /* Don't do anything when going to exit. */
6362 if (exiting)
6363 return;
6364
6365 /* Send up to 10 more requests out than we received. Avoid sending too
6366 * many, there can be a buffer overflow somewhere. */
6367 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6368 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006369 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006370
Bram Moolenaarb255b902018-04-24 21:40:10 +02006371 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6372 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 out_str_nf((char_u *)buf);
6374 ++xt_index_out;
6375 }
6376
6377 /* Send the codes out right away. */
6378 if (xt_index_out != old_idx)
6379 out_flush();
6380}
6381
6382/*
6383 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6384 * A "0" instead of the "1" indicates a code that isn't supported.
6385 * Both <name> and <string> are encoded in hex.
6386 * "code" points to the "0" or "1".
6387 */
6388 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006389got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391#define XT_LEN 100
6392 char_u name[3];
6393 char_u str[XT_LEN];
6394 int i;
6395 int j = 0;
6396 int c;
6397
6398 /* A '1' means the code is supported, a '0' means it isn't.
6399 * When half the length is > XT_LEN we can't use it.
6400 * Our names are currently all 2 characters. */
6401 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6402 {
6403 /* Get the name from the response and find it in the table. */
6404 name[0] = hexhex2nr(code + 3);
6405 name[1] = hexhex2nr(code + 5);
6406 name[2] = NUL;
6407 for (i = 0; key_names[i] != NULL; ++i)
6408 {
6409 if (STRCMP(key_names[i], name) == 0)
6410 {
6411 xt_index_in = i;
6412 break;
6413 }
6414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006415
Bram Moolenaarb255b902018-04-24 21:40:10 +02006416 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (key_names[i] != NULL)
6419 {
6420 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6421 str[j++] = c;
6422 str[j] = NUL;
6423 if (name[0] == 'C' && name[1] == 'o')
6424 {
6425 /* Color count is not a key code. */
6426 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006427 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
6429 else
6430 {
6431 /* First delete any existing entry with the same code. */
6432 i = find_term_bykeys(str);
6433 if (i >= 0)
6434 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006435 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437 }
6438 }
6439
6440 /* May request more codes now that we received one. */
6441 ++xt_index_in;
6442 req_more_codes_from_term();
6443}
6444
6445/*
6446 * Check if there are any unanswered requests and deal with them.
6447 * This is called before starting an external program or getting direct
6448 * keyboard input. We don't want responses to be send to that program or
6449 * handled as typed text.
6450 */
6451 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006452check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 int c;
6455
6456 /* If no codes requested or all are answered, no need to wait. */
6457 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6458 return;
6459
6460 /* Vgetc() will check for and handle any response.
6461 * Keep calling vpeekc() until we don't get any responses. */
6462 ++no_mapping;
6463 ++allow_keys;
6464 for (;;)
6465 {
6466 c = vpeekc();
6467 if (c == NUL) /* nothing available */
6468 break;
6469
6470 /* If a response is recognized it's replaced with K_IGNORE, must read
6471 * it from the input stream. If there is no K_IGNORE we can't do
6472 * anything, break here (there might be some responses further on, but
6473 * we don't want to throw away any typed chars). */
6474 if (c != K_SPECIAL && c != K_IGNORE)
6475 break;
6476 c = vgetc();
6477 if (c != K_IGNORE)
6478 {
6479 vungetc(c);
6480 break;
6481 }
6482 }
6483 --no_mapping;
6484 --allow_keys;
6485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006490 * corresponding external one recognized by :map/:abbrev commands.
6491 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 *
6493 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006494 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006496 * It uses a growarray to build the translation string since the latter can be
6497 * wider than the original description. The caller has to free the string
6498 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 *
6500 * Returns NULL when there is a problem.
6501 */
6502 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006503translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 garray_T ga;
6506 int c;
6507 int modifiers;
6508 int cpo_bslash;
6509 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 ga_init(&ga);
6512 ga.ga_itemsize = 1;
6513 ga.ga_growsize = 40;
6514
6515 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6516 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
6518 for (; *str; ++str)
6519 {
6520 c = *str;
6521 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6522 {
6523 modifiers = 0;
6524 if (str[1] == KS_MODIFIER)
6525 {
6526 str++;
6527 modifiers = *++str;
6528 c = *++str;
6529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6531 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006532 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 ga_clear(&ga);
6535 return NULL;
6536 }
6537 c = TO_SPECIAL(str[1], str[2]);
6538 if (c == K_ZERO) /* display <Nul> as ^@ */
6539 c = NUL;
6540 str += 2;
6541 }
6542 if (IS_SPECIAL(c) || modifiers) /* special key */
6543 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006544 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
6546 ga_clear(&ga);
6547 return NULL;
6548 }
6549 ga_concat(&ga, get_special_key_name(c, modifiers));
6550 continue; /* for (str) */
6551 }
6552 }
6553 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6554 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6555 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6556 if (c)
6557 ga_append(&ga, c);
6558 }
6559 ga_append(&ga, NUL);
6560 return (char_u *)(ga.ga_data);
6561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006563#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564static char ksme_str[20];
6565static char ksmr_str[20];
6566static char ksmd_str[20];
6567
6568/*
6569 * For Win32 console: update termcap codes for existing console attributes.
6570 */
6571 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006572update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 struct builtin_term *p;
6575
6576 p = find_builtin_term(DEFAULT_TERM);
6577 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6578 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6579 attr | 0x08); /* FOREGROUND_INTENSITY */
6580 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6581 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6582
6583 while (p->bt_string != NULL)
6584 {
6585 if (p->bt_entry == (int)KS_ME)
6586 p->bt_string = &ksme_str[0];
6587 else if (p->bt_entry == (int)KS_MR)
6588 p->bt_string = &ksmr_str[0];
6589 else if (p->bt_entry == (int)KS_MD)
6590 p->bt_string = &ksmd_str[0];
6591 ++p;
6592 }
6593}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006594
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006595# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006596# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006597struct ks_tbl_s
6598{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006599 int code; // value of KS_
6600 char *vtp; // code in vtp mode
6601 char *vtp2; // code in vtp2 mode
6602 char buf[KSSIZE]; // save buffer in non-vtp mode
6603 char vbuf[KSSIZE]; // save buffer in vtp mode
6604 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6605 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006606};
6607
6608static struct ks_tbl_s ks_tbl[] =
6609{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006610 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6611 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6612 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6613 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6614 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6615 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6616 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6617 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6618 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006619# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006620 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6621 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006622 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6623 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006624# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006625 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6626 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006627 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6628 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006629# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006630 {(int)KS_CCO, "256", "256"}, // colors
6631 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006632};
6633
6634 static struct builtin_term *
6635find_first_tcap(
6636 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006637 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006638{
6639 struct builtin_term *p;
6640
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006641 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006642 if (p->bt_entry == code)
6643 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006644 return NULL;
6645}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006646# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006647
6648/*
6649 * For Win32 console: replace the sequence immediately after termguicolors.
6650 */
6651 void
6652swap_tcap(void)
6653{
6654# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006655 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006656 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006657 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006658 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006659 int mode;
6660 enum
6661 {
6662 CMODEINDEX,
6663 CMODE24,
6664 CMODE256
6665 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006666
6667 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006668 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006669 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006670 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006671 {
6672 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006673 if (bt != NULL)
6674 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006675 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6676 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6677 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6678
6679 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6680 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006681 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006682 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006683 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006684 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006685 }
6686
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006687 if (p_tgc)
6688 mode = CMODE24;
6689 else if (t_colors >= 256)
6690 mode = CMODE256;
6691 else
6692 mode = CMODEINDEX;
6693
6694 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006695 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006696 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6697 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006698 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006699 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006700 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006701 case CMODEINDEX:
6702 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6703 break;
6704 case CMODE24:
6705 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6706 break;
6707 default:
6708 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006709 }
6710 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711 }
6712
6713 if (mode != curr_mode)
6714 {
6715 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006716 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006717 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6718 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006719 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006720 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006721 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006722 case CMODEINDEX:
6723 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6724 break;
6725 case CMODE24:
6726 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6727 break;
6728 default:
6729 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006730 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006731 }
6732 }
6733
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006734 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006735 }
6736# endif
6737}
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006740
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006741#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006742 static int
6743hex_digit(int c)
6744{
6745 if (isdigit(c))
6746 return c - '0';
6747 c = TOLOWER_ASC(c);
6748 if (c >= 'a' && c <= 'f')
6749 return c - 'a' + 10;
6750 return 0x1ffffff;
6751}
6752
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006753# ifdef VIMDLL
6754 static guicolor_T
6755gui_adjust_rgb(guicolor_T c)
6756{
6757 if (gui.in_use)
6758 return c;
6759 else
6760 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6761}
6762# else
6763# define gui_adjust_rgb(c) (c)
6764# endif
6765
Bram Moolenaarab302212016-04-26 20:59:29 +02006766 guicolor_T
6767gui_get_color_cmn(char_u *name)
6768{
Bram Moolenaar28726652017-01-06 18:16:19 +01006769 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6770 * values as used by the MS-Windows GDI api. It should be used only for
6771 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006772# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006773# undef RGB
6774# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006775# ifndef RGB
6776# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6777# endif
6778# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006779 FILE *fd;
6780 char line[LINE_LEN];
6781 char_u *fname;
6782 int r, g, b, i;
6783 guicolor_T color;
6784
6785 struct rgbcolor_table_S {
6786 char_u *color_name;
6787 guicolor_T color;
6788 };
6789
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006790 /* Only non X11 colors (not present in rgb.txt) and colors in
6791 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006792 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006793 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6794 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6795 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6796 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6797 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6798 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6799 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6800 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6801 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6802 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6803 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6804 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6805 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006806 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6807 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006808 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006809 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006810 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006811 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6812 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6813 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6814 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6815 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6816 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6817 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6818 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6819 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006820 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006821 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006822 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6823 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006824 };
6825
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006826 static struct rgbcolor_table_S *colornames_table;
6827 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006828
6829 if (name[0] == '#' && STRLEN(name) == 7)
6830 {
6831 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006832 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006833 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6834 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6835 if (color > 0xffffff)
6836 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006837 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006838 }
6839
6840 /* Check if the name is one of the colors we know */
6841 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6842 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006843 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006844
6845 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006846 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006847 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006848 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006849 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006850 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006851
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006852 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006853 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6854 if (fname == NULL)
6855 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006856
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006857 fd = fopen((char *)fname, "rt");
6858 vim_free(fname);
6859 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006860 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006861 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006862 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006863 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006864 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006865 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006866
6867 for (counting = 1; counting >= 0; --counting)
6868 {
6869 if (!counting)
6870 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006871 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006872 if (colornames_table == NULL)
6873 {
6874 fclose(fd);
6875 return INVALCOLOR;
6876 }
6877 rewind(fd);
6878 }
6879 size = 0;
6880
6881 while (!feof(fd))
6882 {
6883 size_t len;
6884 int pos;
6885
Bram Moolenaar42335f52018-09-13 15:33:43 +02006886 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006887 len = strlen(line);
6888
6889 if (len <= 1 || line[len - 1] != '\n')
6890 continue;
6891
6892 line[len - 1] = '\0';
6893
6894 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6895 if (i != 3)
6896 continue;
6897
6898 if (!counting)
6899 {
6900 char_u *s = vim_strsave((char_u *)line + pos);
6901
6902 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006903 {
6904 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006905 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006906 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006907 colornames_table[size].color_name = s;
6908 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6909 }
6910 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006911
6912 // The distributed rgb.txt has less than 1000 entries. Limit to
6913 // 10000, just in case the file was messed up.
6914 if (size == 10000)
6915 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006916 }
6917 }
6918 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006919 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006920
6921 for (i = 0; i < size; i++)
6922 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006923 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006924
Bram Moolenaarab302212016-04-26 20:59:29 +02006925 return INVALCOLOR;
6926}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006927
6928 guicolor_T
6929gui_get_rgb_color_cmn(int r, int g, int b)
6930{
6931 guicolor_T color = RGB(r, g, b);
6932
6933 if (color > 0xffffff)
6934 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006935 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006936}
Bram Moolenaarab302212016-04-26 20:59:29 +02006937#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006938
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006939#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006940 || defined(PROTO)
6941static int cube_value[] = {
6942 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6943};
6944
6945static int grey_ramp[] = {
6946 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6947 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6948};
6949
6950# ifdef FEAT_TERMINAL
6951# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006952# else
6953# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006954# endif
6955
Bram Moolenaar9894e392018-05-05 14:29:06 +02006956static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006957// R G B idx
6958 { 0, 0, 0, 1}, // black
6959 {224, 0, 0, 2}, // dark red
6960 { 0, 224, 0, 3}, // dark green
6961 {224, 224, 0, 4}, // dark yellow / brown
6962 { 0, 0, 224, 5}, // dark blue
6963 {224, 0, 224, 6}, // dark magenta
6964 { 0, 224, 224, 7}, // dark cyan
6965 {224, 224, 224, 8}, // light grey
6966
6967 {128, 128, 128, 9}, // dark grey
6968 {255, 64, 64, 10}, // light red
6969 { 64, 255, 64, 11}, // light green
6970 {255, 255, 64, 12}, // yellow
6971 { 64, 64, 255, 13}, // light blue
6972 {255, 64, 255, 14}, // light magenta
6973 { 64, 255, 255, 15}, // light cyan
6974 {255, 255, 255, 16}, // white
6975};
6976
6977 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006978cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006979{
6980 int idx;
6981
6982 if (nr < 16)
6983 {
6984 *r = ansi_table[nr][0];
6985 *g = ansi_table[nr][1];
6986 *b = ansi_table[nr][2];
6987 *ansi_idx = ansi_table[nr][3];
6988 }
6989 else if (nr < 232)
6990 {
6991 /* 216 color cube */
6992 idx = nr - 16;
6993 *r = cube_value[idx / 36 % 6];
6994 *g = cube_value[idx / 6 % 6];
6995 *b = cube_value[idx % 6];
6996 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6997 }
6998 else if (nr < 256)
6999 {
7000 /* 24 grey scale ramp */
7001 idx = nr - 232;
7002 *r = grey_ramp[idx];
7003 *g = grey_ramp[idx];
7004 *b = grey_ramp[idx];
7005 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7006 }
7007 else
7008 {
7009 *r = 0;
7010 *g = 0;
7011 *b = 0;
7012 *ansi_idx = 0;
7013 }
7014}
7015#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007016