blob: 028e6c2bf7a22b5063c45a85f7f9ea313238d1b7 [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 Moolenaar5843f5f2019-08-20 20:13:45 +020090static void del_termcode(char_u *name);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020092static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010093static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
96#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010097static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99/*
100 * Here is our own prototype for tgetstr(), any prototypes from the include
101 * files have been disabled by the define at the start of this file.
102 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200106 /* Change this to "if 1" to debug what happens with termresponse. */
107# if 0
108# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200109static void log_tr(const char *fmt, ...);
110# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200111# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200112# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200113# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200114
Bram Moolenaarafd78262019-05-10 23:10:31 +0200115typedef enum {
116 STATUS_GET, // send request when switching to RAW mode
117 STATUS_SENT, // did send request, checking for response
118 STATUS_GOT, // received response
119 STATUS_FAIL // timed out
120} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200121
Bram Moolenaarafd78262019-05-10 23:10:31 +0200122typedef struct {
123 request_progress_T tr_progress;
124 time_t tr_start; // when request was sent, -1 for never
125} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126
Bram Moolenaarafd78262019-05-10 23:10:31 +0200127# define TERMREQUEST_INIT {STATUS_GET, -1}
128
129// Request Terminal Version status:
130static termrequest_T crv_status = TERMREQUEST_INIT;
131
132// Request Cursor position report:
133static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200134
Bram Moolenaar9377df32017-10-15 13:22:01 +0200135# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200136// Request foreground color report:
137static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200138static int fg_r = 0;
139static int fg_g = 0;
140static int fg_b = 0;
141static int bg_r = 255;
142static int bg_g = 255;
143static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200144# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200145
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200146/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200148
Bram Moolenaar4db25542017-08-28 22:43:05 +0200149/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200151
152/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200153static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100154
155/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200156static termrequest_T winpos_status = TERMREQUEST_INIT;
157
158static termrequest_T *all_termrequests[] = {
159 &crv_status,
160 &u7_status,
161# ifdef FEAT_TERMINAL
162 &rfg_status,
163# endif
164 &rbg_status,
165 &rbm_status,
166 &rcs_status,
167 &winpos_status,
168 NULL
169};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# endif
171
172/*
173 * Don't declare these variables if termcap.h contains them.
174 * Autoconf checks if these variables should be declared extern (not all
175 * systems have them).
176 * Some versions define ospeed to be speed_t, but that is incompatible with
177 * BSD, where ospeed is short and speed_t is long.
178 */
179# ifndef HAVE_OSPEED
180# ifdef OSPEED_EXTERN
181extern short ospeed;
182# else
183short ospeed;
184# endif
185# endif
186# ifndef HAVE_UP_BC_PC
187# ifdef UP_BC_PC_EXTERN
188extern char *UP, *BC, PC;
189# else
190char *UP, *BC, PC;
191# endif
192# endif
193
194# define TGETSTR(s, p) vim_tgetstr((s), (p))
195# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100196static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* HAVE_TGETENT */
198
199static int detected_8bit = FALSE; /* detected 8-bit terminal */
200
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200201#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200204static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200205
206/* The blink flag from the style response may be inverted from the actual
207 * blinking state, xterm XORs the flags. */
208static int initial_cursor_shape_blink = FALSE;
209
210/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200211static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200212#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200213
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000214static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216
217#if defined(FEAT_GUI)
218/*
219 * GUI pseudo term-cap.
220 */
221 {(int)KS_NAME, "gui"},
222 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
223 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
224# ifdef TERMINFO
225 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
226# else
227 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
228# endif
229 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
230# ifdef TERMINFO
231 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
232 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# else
235 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
236 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# endif
239 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
240 /* attributes switched on with 'h', off with * 'H' */
241 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
242 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
243 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
244 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
245 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
246 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
247 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000248 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
249 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200250 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
251 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
253 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
254 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
255 {(int)KS_MS, "y"},
256 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100257 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 {(int)KS_LE, "\b"}, /* cursor-left = BS */
259 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
260# ifdef TERMINFO
261 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
262# else
263 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
264# endif
265 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000266 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#endif
268
269#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
272/*
273 * Amiga console window, default for Amiga
274 */
275 {(int)KS_NAME, "amiga"},
276 {(int)KS_CE, "\033[K"},
277 {(int)KS_CD, "\033[J"},
278 {(int)KS_AL, "\033[L"},
279# ifdef TERMINFO
280 {(int)KS_CAL, "\033[%p1%dL"},
281# else
282 {(int)KS_CAL, "\033[%dL"},
283# endif
284 {(int)KS_DL, "\033[M"},
285# ifdef TERMINFO
286 {(int)KS_CDL, "\033[%p1%dM"},
287# else
288 {(int)KS_CDL, "\033[%dM"},
289# endif
290 {(int)KS_CL, "\014"},
291 {(int)KS_VI, "\033[0 p"},
292 {(int)KS_VE, "\033[1 p"},
293 {(int)KS_ME, "\033[0m"},
294 {(int)KS_MR, "\033[7m"},
295 {(int)KS_MD, "\033[1m"},
296 {(int)KS_SE, "\033[0m"},
297 {(int)KS_SO, "\033[33m"},
298 {(int)KS_US, "\033[4m"},
299 {(int)KS_UE, "\033[0m"},
300 {(int)KS_CZH, "\033[3m"},
301 {(int)KS_CZR, "\033[0m"},
302#if defined(__MORPHOS__) || defined(__AROS__)
303 {(int)KS_CCO, "8"}, /* allow 8 colors */
304# ifdef TERMINFO
305 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
306 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
307# else
308 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
309 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
310# endif
311 {(int)KS_OP, "\033[m"}, /* reset colors */
312#endif
313 {(int)KS_MS, "y"},
314 {(int)KS_UT, "y"}, /* guessed */
315 {(int)KS_LE, "\b"},
316# ifdef TERMINFO
317 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
318# else
319 {(int)KS_CM, "\033[%i%d;%dH"},
320# endif
321#if defined(__MORPHOS__)
322 {(int)KS_SR, "\033M"},
323#endif
324# ifdef TERMINFO
325 {(int)KS_CRI, "\033[%p1%dC"},
326# else
327 {(int)KS_CRI, "\033[%dC"},
328# endif
329 {K_UP, "\233A"},
330 {K_DOWN, "\233B"},
331 {K_LEFT, "\233D"},
332 {K_RIGHT, "\233C"},
333 {K_S_UP, "\233T"},
334 {K_S_DOWN, "\233S"},
335 {K_S_LEFT, "\233 A"},
336 {K_S_RIGHT, "\233 @"},
337 {K_S_TAB, "\233Z"},
338 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
339 {K_F2, "\233\061~"},
340 {K_F3, "\233\062~"},
341 {K_F4, "\233\063~"},
342 {K_F5, "\233\064~"},
343 {K_F6, "\233\065~"},
344 {K_F7, "\233\066~"},
345 {K_F8, "\233\067~"},
346 {K_F9, "\233\070~"},
347 {K_F10, "\233\071~"},
348 {K_S_F1, "\233\061\060~"},
349 {K_S_F2, "\233\061\061~"},
350 {K_S_F3, "\233\061\062~"},
351 {K_S_F4, "\233\061\063~"},
352 {K_S_F5, "\233\061\064~"},
353 {K_S_F6, "\233\061\065~"},
354 {K_S_F7, "\233\061\066~"},
355 {K_S_F8, "\233\061\067~"},
356 {K_S_F9, "\233\061\070~"},
357 {K_S_F10, "\233\061\071~"},
358 {K_HELP, "\233?~"},
359 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
360 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
361 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
362 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
363 {K_END, "\233\064\065~"}, /* 101 key keyboard */
364
365 {BT_EXTRA_KEYS, ""},
366 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
367 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
368 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
369# endif
370
371# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
372/*
373 * almost standard ANSI terminal, default for bebox
374 */
375 {(int)KS_NAME, "beos-ansi"},
376 {(int)KS_CE, "\033[K"},
377 {(int)KS_CD, "\033[J"},
378 {(int)KS_AL, "\033[L"},
379# ifdef TERMINFO
380 {(int)KS_CAL, "\033[%p1%dL"},
381# else
382 {(int)KS_CAL, "\033[%dL"},
383# endif
384 {(int)KS_DL, "\033[M"},
385# ifdef TERMINFO
386 {(int)KS_CDL, "\033[%p1%dM"},
387# else
388 {(int)KS_CDL, "\033[%dM"},
389# endif
390#ifdef BEOS_PR_OR_BETTER
391# ifdef TERMINFO
392 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
393# else
394 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
395# endif
396#endif
397 {(int)KS_CL, "\033[H\033[2J"},
398#ifdef notyet
399 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
400 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
401#endif
402 {(int)KS_ME, "\033[m"}, /* normal mode */
403 {(int)KS_MR, "\033[7m"}, /* reverse */
404 {(int)KS_MD, "\033[1m"}, /* bold */
405 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
406 {(int)KS_SE, "\033[m"}, /* standout end */
407 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
408 {(int)KS_CZR, "\033[m"}, /* italic end */
409 {(int)KS_US, "\033[4m"}, /* underscore mode */
410 {(int)KS_UE, "\033[m"}, /* underscore end */
411 {(int)KS_CCO, "8"}, /* allow 8 colors */
412# ifdef TERMINFO
413 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
414 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
415# else
416 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
417 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
418# endif
419 {(int)KS_OP, "\033[m"}, /* reset colors */
420 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
421 {(int)KS_UT, "y"}, /* guessed */
422 {(int)KS_LE, "\b"},
423# ifdef TERMINFO
424 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
425# else
426 {(int)KS_CM, "\033[%i%d;%dH"},
427# endif
428 {(int)KS_SR, "\033M"},
429# ifdef TERMINFO
430 {(int)KS_CRI, "\033[%p1%dC"},
431# else
432 {(int)KS_CRI, "\033[%dC"},
433# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200434# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438 {K_UP, "\033[A"},
439 {K_DOWN, "\033[B"},
440 {K_LEFT, "\033[D"},
441 {K_RIGHT, "\033[C"},
442# endif
443
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200444# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445/*
446 * standard ANSI terminal, default for unix
447 */
448 {(int)KS_NAME, "ansi"},
449 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
450 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
451# ifdef TERMINFO
452 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
453# else
454 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
455# endif
456 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
457# ifdef TERMINFO
458 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
459# else
460 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
461# endif
462 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
463 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
464 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
465 {(int)KS_MS, "y"},
466 {(int)KS_UT, "y"}, /* guessed */
467 {(int)KS_LE, "\b"},
468# ifdef TERMINFO
469 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
470# else
471 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
472# endif
473# ifdef TERMINFO
474 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
475# else
476 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
477# endif
478# endif
479
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200480# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481/*
482 * These codes are valid when nansi.sys or equivalent has been installed.
483 * Function keys on a PC are preceded with a NUL. These are converted into
484 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
485 * CTRL-arrow is used instead of SHIFT-arrow.
486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {(int)KS_NAME, "pcansi"},
488 {(int)KS_DL, "\033[M"},
489 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {(int)KS_CE, "\033[K"},
491 {(int)KS_CL, "\033[2J"},
492 {(int)KS_ME, "\033[0m"},
493 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
494 {(int)KS_MD, "\033[1m"}, /* bold: white text */
495 {(int)KS_SE, "\033[0m"}, /* standout end */
496 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
497 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
498 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
499 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
500 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
501 {(int)KS_CCO, "8"}, /* allow 8 colors */
502# ifdef TERMINFO
503 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
504 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
505# else
506 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
507 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
508# endif
509 {(int)KS_OP, "\033[0m"}, /* reset colors */
510 {(int)KS_MS, "y"},
511 {(int)KS_UT, "y"}, /* guessed */
512 {(int)KS_LE, "\b"},
513# ifdef TERMINFO
514 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
515# else
516 {(int)KS_CM, "\033[%i%d;%dH"},
517# endif
518# ifdef TERMINFO
519 {(int)KS_CRI, "\033[%p1%dC"},
520# else
521 {(int)KS_CRI, "\033[%dC"},
522# endif
523 {K_UP, "\316H"},
524 {K_DOWN, "\316P"},
525 {K_LEFT, "\316K"},
526 {K_RIGHT, "\316M"},
527 {K_S_LEFT, "\316s"},
528 {K_S_RIGHT, "\316t"},
529 {K_F1, "\316;"},
530 {K_F2, "\316<"},
531 {K_F3, "\316="},
532 {K_F4, "\316>"},
533 {K_F5, "\316?"},
534 {K_F6, "\316@"},
535 {K_F7, "\316A"},
536 {K_F8, "\316B"},
537 {K_F9, "\316C"},
538 {K_F10, "\316D"},
539 {K_F11, "\316\205"}, /* guessed */
540 {K_F12, "\316\206"}, /* guessed */
541 {K_S_F1, "\316T"},
542 {K_S_F2, "\316U"},
543 {K_S_F3, "\316V"},
544 {K_S_F4, "\316W"},
545 {K_S_F5, "\316X"},
546 {K_S_F6, "\316Y"},
547 {K_S_F7, "\316Z"},
548 {K_S_F8, "\316["},
549 {K_S_F9, "\316\\"},
550 {K_S_F10, "\316]"},
551 {K_S_F11, "\316\207"}, /* guessed */
552 {K_S_F12, "\316\210"}, /* guessed */
553 {K_INS, "\316R"},
554 {K_DEL, "\316S"},
555 {K_HOME, "\316G"},
556 {K_END, "\316O"},
557 {K_PAGEDOWN, "\316Q"},
558 {K_PAGEUP, "\316I"},
559# endif
560
Bram Moolenaar4f974752019-02-17 17:44:42 +0100561# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562/*
563 * These codes are valid for the Win32 Console . The entries that start with
564 * ESC | are translated into console calls in os_win32.c. The function keys
565 * are also translated in os_win32.c.
566 */
567 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100568 {(int)KS_CE, "\033|K"}, // clear to end of line
569 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100573 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100577 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
578 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100580 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
581 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100583 {(int)KS_CL, "\033|J"}, // clear screen
584 {(int)KS_CD, "\033|j"}, // clear to end of display
585 {(int)KS_VI, "\033|v"}, // cursor invisible
586 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar6982f422019-02-16 16:48:01 +0100588 {(int)KS_ME, "\033|0m"}, // normal
589 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
590 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100592 {(int)KS_SO, "\033|31m"}, // standout: white on blue
593 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100595 {(int)KS_SO, "\033|F"}, // standout: high intensity
596 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100598 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
599 {(int)KS_CZR, "\033|0m"}, // italic end
600 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
601 {(int)KS_UE, "\033|0m"}, // underscore end
602 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100604 {(int)KS_CAB, "\033|%p1%db"}, // set background color
605 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100607 {(int)KS_CAB, "\033|%db"}, // set background color
608 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609# endif
610
Bram Moolenaar6982f422019-02-16 16:48:01 +0100611 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100613 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {(int)KS_LE, "\b"},
615# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100616 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100620 {(int)KS_VB, "\033|B"}, // visual bell
621 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
622 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100624 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100626 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100628# ifdef FEAT_TERMGUICOLORS
629 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
630 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633 {K_UP, "\316H"},
634 {K_DOWN, "\316P"},
635 {K_LEFT, "\316K"},
636 {K_RIGHT, "\316M"},
637 {K_S_UP, "\316\304"},
638 {K_S_DOWN, "\316\317"},
639 {K_S_LEFT, "\316\311"},
640 {K_C_LEFT, "\316s"},
641 {K_S_RIGHT, "\316\313"},
642 {K_C_RIGHT, "\316t"},
643 {K_S_TAB, "\316\017"},
644 {K_F1, "\316;"},
645 {K_F2, "\316<"},
646 {K_F3, "\316="},
647 {K_F4, "\316>"},
648 {K_F5, "\316?"},
649 {K_F6, "\316@"},
650 {K_F7, "\316A"},
651 {K_F8, "\316B"},
652 {K_F9, "\316C"},
653 {K_F10, "\316D"},
654 {K_F11, "\316\205"},
655 {K_F12, "\316\206"},
656 {K_S_F1, "\316T"},
657 {K_S_F2, "\316U"},
658 {K_S_F3, "\316V"},
659 {K_S_F4, "\316W"},
660 {K_S_F5, "\316X"},
661 {K_S_F6, "\316Y"},
662 {K_S_F7, "\316Z"},
663 {K_S_F8, "\316["},
664 {K_S_F9, "\316\\"},
665 {K_S_F10, "\316]"},
666 {K_S_F11, "\316\207"},
667 {K_S_F12, "\316\210"},
668 {K_INS, "\316R"},
669 {K_DEL, "\316S"},
670 {K_HOME, "\316G"},
671 {K_S_HOME, "\316\302"},
672 {K_C_HOME, "\316w"},
673 {K_END, "\316O"},
674 {K_S_END, "\316\315"},
675 {K_C_END, "\316u"},
676 {K_PAGEDOWN, "\316Q"},
677 {K_PAGEUP, "\316I"},
678 {K_KPLUS, "\316N"},
679 {K_KMINUS, "\316J"},
680 {K_KMULTIPLY, "\316\067"},
681 {K_K0, "\316\332"},
682 {K_K1, "\316\336"},
683 {K_K2, "\316\342"},
684 {K_K3, "\316\346"},
685 {K_K4, "\316\352"},
686 {K_K5, "\316\356"},
687 {K_K6, "\316\362"},
688 {K_K7, "\316\366"},
689 {K_K8, "\316\372"},
690 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100691 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# endif
693
694# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
695/*
696 * VT320 is working as an ANSI terminal compatible DEC terminal.
697 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * TODO:- rewrite ESC[ codes to CSI
699 * - keyboard languages (CSI ? 26 n)
700 */
701 {(int)KS_NAME, "vt320"},
702 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
703 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
704# ifdef TERMINFO
705 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
706# else
707 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
708# endif
709 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
710# ifdef TERMINFO
711 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
712# else
713 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
714# endif
715 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000716 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
717 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
719 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000720 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
721 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
722 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
723 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
724 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
725 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
726 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
727 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
728 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
729 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {(int)KS_MS, "y"},
731 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100732 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {(int)KS_LE, "\b"},
734# ifdef TERMINFO
735 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
736 ESC_STR "[%i%p1%d;%p2%dH")},
737# else
738 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
739# endif
740# ifdef TERMINFO
741 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
742# else
743 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
744# endif
745 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
746 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
747 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
748 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200749 // Note: cursor key sequences for application cursor mode are omitted,
750 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000751 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
752 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
753 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
754 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
755 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
757 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
758 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
759 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
760 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
763 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
764 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
765 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
766 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
767 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
768 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
769 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
770 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
771 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
772 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
773 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
774 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
775 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
776 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200777 // These sequences starting with <Esc> O may interfere with what the user
778 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
780 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
781 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
782 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
783 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200784 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
785 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
786 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
787 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
788 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
789 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
790 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
791 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
792 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
793 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
795# endif
796
797# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
798/*
799 * Ordinary vt52
800 */
801 {(int)KS_NAME, "vt52"},
802 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
803 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100804# ifdef TERMINFO
805 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
806 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
807# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100811 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
813 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {K_UP, IF_EB("\033A", ESC_STR "A")},
815 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
816 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
817 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100818 {K_F1, IF_EB("\033P", ESC_STR "P")},
819 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
820 {K_F3, IF_EB("\033R", ESC_STR "R")},
821# ifdef __MINT__
822 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
823 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
824 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
825 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
826 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
828 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
829 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
830 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {K_F4, IF_EB("\033S", ESC_STR "S")},
832 {K_F5, IF_EB("\033T", ESC_STR "T")},
833 {K_F6, IF_EB("\033U", ESC_STR "U")},
834 {K_F7, IF_EB("\033V", ESC_STR "V")},
835 {K_F8, IF_EB("\033W", ESC_STR "W")},
836 {K_F9, IF_EB("\033X", ESC_STR "X")},
837 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
838 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
839 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
840 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
841 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
842 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
843 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
844 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
845 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
846 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
847 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
848 {K_INS, IF_EB("\033I", ESC_STR "I")},
849 {K_HOME, IF_EB("\033E", ESC_STR "E")},
850 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
851 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
852# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {(int)KS_MS, "y"},
855# endif
856# endif
857
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200858# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200859 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
861 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
862# ifdef TERMINFO
863 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
864# else
865 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
866# endif
867 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
868# ifdef TERMINFO
869 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
870# else
871 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
872# endif
873# ifdef TERMINFO
874 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
875 ESC_STR "[%i%p1%d;%p2%dr")},
876# else
877 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
878# endif
879 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
880 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
881 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
882 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
883 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
884 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
885 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200886 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
887 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {(int)KS_MS, "y"},
889 {(int)KS_UT, "y"},
890 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200891 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
892 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200893 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200894 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200895# ifdef TERMINFO
896 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
897# else
898 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
899# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200900 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200901 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902# ifdef TERMINFO
903 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
904 ESC_STR "[%i%p1%d;%p2%dH")},
905# else
906 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
907# endif
908 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
909# ifdef TERMINFO
910 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
911# else
912 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
913# endif
914 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
915 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
916# ifdef FEAT_XTERM_SAVE
917 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
918 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
919 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
920# endif
921 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
922 {(int)KS_CIE, "\007"},
923 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
924 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200925 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
926 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927# ifdef TERMINFO
928 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
929 ESC_STR "[8;%p1%d;%p2%dt")},
930 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
931 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200932 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933# else
934 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
935 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200936 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937# endif
938 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200939 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200940 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100941 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200942# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200943 /* These are printf strings, not terminal codes. */
944 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
945 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
946# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100947 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
948 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200949 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
950 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
951 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
952 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953
954 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
955 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
956 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
957 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
958 /* An extra set of cursor keys for vt100 mode */
959 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
960 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
961 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
962 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000964 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
965 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
966 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
967 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
969 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
970 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
971 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
972 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
973 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
974 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
975 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
976 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
977 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
978 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
979 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000981 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
982 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
983 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
984 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
986 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000991 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
992 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000994 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000995 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
997 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100998 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
999 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
1000 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1001 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1002 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1003 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +02001004 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
1005 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1006 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1007 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1008 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1009 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1010 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1011 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1012 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1013 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001014 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1015 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1016 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001019 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1020 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1021 /* F14 and F15 are missing, because they send the same codes as the undo
1022 * and help key, although they don't work on all keyboards. */
1023 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1024 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1025 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1026 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1027 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1028
1029 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1030 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1031 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1032 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1033 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1034 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1035 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1036 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1037 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1038 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1039
1040 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1041 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1042 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1043 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1044 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1045 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1046 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047# endif
1048
1049# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1050/*
1051 * iris-ansi for Silicon Graphics machines.
1052 */
1053 {(int)KS_NAME, "iris-ansi"},
1054 {(int)KS_CE, "\033[K"},
1055 {(int)KS_CD, "\033[J"},
1056 {(int)KS_AL, "\033[L"},
1057# ifdef TERMINFO
1058 {(int)KS_CAL, "\033[%p1%dL"},
1059# else
1060 {(int)KS_CAL, "\033[%dL"},
1061# endif
1062 {(int)KS_DL, "\033[M"},
1063# ifdef TERMINFO
1064 {(int)KS_CDL, "\033[%p1%dM"},
1065# else
1066 {(int)KS_CDL, "\033[%dM"},
1067# endif
1068#if 0 /* The scroll region is not working as Vim expects. */
1069# ifdef TERMINFO
1070 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1071# else
1072 {(int)KS_CS, "\033[%i%d;%dr"},
1073# endif
1074#endif
1075 {(int)KS_CL, "\033[H\033[2J"},
1076 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1077 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1078 {(int)KS_TI, "\033[=6h"},
1079 {(int)KS_TE, "\033[=6l"},
1080 {(int)KS_SE, "\033[21;27m"},
1081 {(int)KS_SO, "\033[1;7m"},
1082 {(int)KS_ME, "\033[m"},
1083 {(int)KS_MR, "\033[7m"},
1084 {(int)KS_MD, "\033[1m"},
1085 {(int)KS_CCO, "8"}, /* allow 8 colors */
1086 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1087 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1088 {(int)KS_US, "\033[4m"}, /* underline on */
1089 {(int)KS_UE, "\033[24m"}, /* underline off */
1090# ifdef TERMINFO
1091 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1092 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1093 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1094 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1095# else
1096 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1097 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1098 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1099 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1100# endif
1101 {(int)KS_MS, "y"}, /* guessed */
1102 {(int)KS_UT, "y"}, /* guessed */
1103 {(int)KS_LE, "\b"},
1104# ifdef TERMINFO
1105 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1106# else
1107 {(int)KS_CM, "\033[%i%d;%dH"},
1108# endif
1109 {(int)KS_SR, "\033M"},
1110# ifdef TERMINFO
1111 {(int)KS_CRI, "\033[%p1%dC"},
1112# else
1113 {(int)KS_CRI, "\033[%dC"},
1114# endif
1115 {(int)KS_CIS, "\033P3.y"},
1116 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1117 {(int)KS_TS, "\033P1.y"},
1118 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1119# ifdef TERMINFO
1120 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1121 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1122# else
1123 {(int)KS_CWS, "\033[203;%d;%d/y"},
1124 {(int)KS_CWP, "\033[205;%d;%d/y"},
1125# endif
1126 {K_UP, "\033[A"},
1127 {K_DOWN, "\033[B"},
1128 {K_LEFT, "\033[D"},
1129 {K_RIGHT, "\033[C"},
1130 {K_S_UP, "\033[161q"},
1131 {K_S_DOWN, "\033[164q"},
1132 {K_S_LEFT, "\033[158q"},
1133 {K_S_RIGHT, "\033[167q"},
1134 {K_F1, "\033[001q"},
1135 {K_F2, "\033[002q"},
1136 {K_F3, "\033[003q"},
1137 {K_F4, "\033[004q"},
1138 {K_F5, "\033[005q"},
1139 {K_F6, "\033[006q"},
1140 {K_F7, "\033[007q"},
1141 {K_F8, "\033[008q"},
1142 {K_F9, "\033[009q"},
1143 {K_F10, "\033[010q"},
1144 {K_F11, "\033[011q"},
1145 {K_F12, "\033[012q"},
1146 {K_S_F1, "\033[013q"},
1147 {K_S_F2, "\033[014q"},
1148 {K_S_F3, "\033[015q"},
1149 {K_S_F4, "\033[016q"},
1150 {K_S_F5, "\033[017q"},
1151 {K_S_F6, "\033[018q"},
1152 {K_S_F7, "\033[019q"},
1153 {K_S_F8, "\033[020q"},
1154 {K_S_F9, "\033[021q"},
1155 {K_S_F10, "\033[022q"},
1156 {K_S_F11, "\033[023q"},
1157 {K_S_F12, "\033[024q"},
1158 {K_INS, "\033[139q"},
1159 {K_HOME, "\033[H"},
1160 {K_END, "\033[146q"},
1161 {K_PAGEUP, "\033[150q"},
1162 {K_PAGEDOWN, "\033[154q"},
1163# endif
1164
1165# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1166/*
1167 * for debugging
1168 */
1169 {(int)KS_NAME, "debug"},
1170 {(int)KS_CE, "[CE]"},
1171 {(int)KS_CD, "[CD]"},
1172 {(int)KS_AL, "[AL]"},
1173# ifdef TERMINFO
1174 {(int)KS_CAL, "[CAL%p1%d]"},
1175# else
1176 {(int)KS_CAL, "[CAL%d]"},
1177# endif
1178 {(int)KS_DL, "[DL]"},
1179# ifdef TERMINFO
1180 {(int)KS_CDL, "[CDL%p1%d]"},
1181# else
1182 {(int)KS_CDL, "[CDL%d]"},
1183# endif
1184# ifdef TERMINFO
1185 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1186# else
1187 {(int)KS_CS, "[%dCS%d]"},
1188# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001189# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001191# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# endif
1194# ifdef TERMINFO
1195 {(int)KS_CAB, "[CAB%p1%d]"},
1196 {(int)KS_CAF, "[CAF%p1%d]"},
1197 {(int)KS_CSB, "[CSB%p1%d]"},
1198 {(int)KS_CSF, "[CSF%p1%d]"},
1199# else
1200 {(int)KS_CAB, "[CAB%d]"},
1201 {(int)KS_CAF, "[CAF%d]"},
1202 {(int)KS_CSB, "[CSB%d]"},
1203 {(int)KS_CSF, "[CSF%d]"},
1204# endif
1205 {(int)KS_OP, "[OP]"},
1206 {(int)KS_LE, "[LE]"},
1207 {(int)KS_CL, "[CL]"},
1208 {(int)KS_VI, "[VI]"},
1209 {(int)KS_VE, "[VE]"},
1210 {(int)KS_VS, "[VS]"},
1211 {(int)KS_ME, "[ME]"},
1212 {(int)KS_MR, "[MR]"},
1213 {(int)KS_MB, "[MB]"},
1214 {(int)KS_MD, "[MD]"},
1215 {(int)KS_SE, "[SE]"},
1216 {(int)KS_SO, "[SO]"},
1217 {(int)KS_UE, "[UE]"},
1218 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001219 {(int)KS_UCE, "[UCE]"},
1220 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001221 {(int)KS_STE, "[STE]"},
1222 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {(int)KS_MS, "[MS]"},
1224 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001225 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226# ifdef TERMINFO
1227 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1228# else
1229 {(int)KS_CM, "[%dCM%d]"},
1230# endif
1231 {(int)KS_SR, "[SR]"},
1232# ifdef TERMINFO
1233 {(int)KS_CRI, "[CRI%p1%d]"},
1234# else
1235 {(int)KS_CRI, "[CRI%d]"},
1236# endif
1237 {(int)KS_VB, "[VB]"},
1238 {(int)KS_KS, "[KS]"},
1239 {(int)KS_KE, "[KE]"},
1240 {(int)KS_TI, "[TI]"},
1241 {(int)KS_TE, "[TE]"},
1242 {(int)KS_CIS, "[CIS]"},
1243 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001244 {(int)KS_CSC, "[CSC]"},
1245 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {(int)KS_TS, "[TS]"},
1247 {(int)KS_FS, "[FS]"},
1248# ifdef TERMINFO
1249 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1250 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1251# else
1252 {(int)KS_CWS, "[%dCWS%d]"},
1253 {(int)KS_CWP, "[%dCWP%d]"},
1254# endif
1255 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001256 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001257 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001258 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {K_UP, "[KU]"},
1260 {K_DOWN, "[KD]"},
1261 {K_LEFT, "[KL]"},
1262 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001263 {K_XUP, "[xKU]"},
1264 {K_XDOWN, "[xKD]"},
1265 {K_XLEFT, "[xKL]"},
1266 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {K_S_UP, "[S-KU]"},
1268 {K_S_DOWN, "[S-KD]"},
1269 {K_S_LEFT, "[S-KL]"},
1270 {K_C_LEFT, "[C-KL]"},
1271 {K_S_RIGHT, "[S-KR]"},
1272 {K_C_RIGHT, "[C-KR]"},
1273 {K_F1, "[F1]"},
1274 {K_XF1, "[xF1]"},
1275 {K_F2, "[F2]"},
1276 {K_XF2, "[xF2]"},
1277 {K_F3, "[F3]"},
1278 {K_XF3, "[xF3]"},
1279 {K_F4, "[F4]"},
1280 {K_XF4, "[xF4]"},
1281 {K_F5, "[F5]"},
1282 {K_F6, "[F6]"},
1283 {K_F7, "[F7]"},
1284 {K_F8, "[F8]"},
1285 {K_F9, "[F9]"},
1286 {K_F10, "[F10]"},
1287 {K_F11, "[F11]"},
1288 {K_F12, "[F12]"},
1289 {K_S_F1, "[S-F1]"},
1290 {K_S_XF1, "[S-xF1]"},
1291 {K_S_F2, "[S-F2]"},
1292 {K_S_XF2, "[S-xF2]"},
1293 {K_S_F3, "[S-F3]"},
1294 {K_S_XF3, "[S-xF3]"},
1295 {K_S_F4, "[S-F4]"},
1296 {K_S_XF4, "[S-xF4]"},
1297 {K_S_F5, "[S-F5]"},
1298 {K_S_F6, "[S-F6]"},
1299 {K_S_F7, "[S-F7]"},
1300 {K_S_F8, "[S-F8]"},
1301 {K_S_F9, "[S-F9]"},
1302 {K_S_F10, "[S-F10]"},
1303 {K_S_F11, "[S-F11]"},
1304 {K_S_F12, "[S-F12]"},
1305 {K_HELP, "[HELP]"},
1306 {K_UNDO, "[UNDO]"},
1307 {K_BS, "[BS]"},
1308 {K_INS, "[INS]"},
1309 {K_KINS, "[KINS]"},
1310 {K_DEL, "[DEL]"},
1311 {K_KDEL, "[KDEL]"},
1312 {K_HOME, "[HOME]"},
1313 {K_S_HOME, "[C-HOME]"},
1314 {K_C_HOME, "[C-HOME]"},
1315 {K_KHOME, "[KHOME]"},
1316 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001317 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {K_END, "[END]"},
1319 {K_S_END, "[C-END]"},
1320 {K_C_END, "[C-END]"},
1321 {K_KEND, "[KEND]"},
1322 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001323 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {K_PAGEUP, "[PAGEUP]"},
1325 {K_PAGEDOWN, "[PAGEDOWN]"},
1326 {K_KPAGEUP, "[KPAGEUP]"},
1327 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1328 {K_MOUSE, "[MOUSE]"},
1329 {K_KPLUS, "[KPLUS]"},
1330 {K_KMINUS, "[KMINUS]"},
1331 {K_KDIVIDE, "[KDIVIDE]"},
1332 {K_KMULTIPLY, "[KMULTIPLY]"},
1333 {K_KENTER, "[KENTER]"},
1334 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001335 {K_PS, "[PASTE-START]"},
1336 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {K_K0, "[K0]"},
1338 {K_K1, "[K1]"},
1339 {K_K2, "[K2]"},
1340 {K_K3, "[K3]"},
1341 {K_K4, "[K4]"},
1342 {K_K5, "[K5]"},
1343 {K_K6, "[K6]"},
1344 {K_K7, "[K7]"},
1345 {K_K8, "[K8]"},
1346 {K_K9, "[K9]"},
1347# endif
1348
1349#endif /* NO_BUILTIN_TCAPS */
1350
1351/*
1352 * The most minimal terminal: only clear screen and cursor positioning
1353 * Always included.
1354 */
1355 {(int)KS_NAME, "dumb"},
1356 {(int)KS_CL, "\014"},
1357#ifdef TERMINFO
1358 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1359 ESC_STR "[%i%p1%d;%p2%dH")},
1360#else
1361 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1362#endif
1363
1364/*
1365 * end marker
1366 */
1367 {(int)KS_NAME, NULL}
1368
1369}; /* end of builtin_termcaps */
1370
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001371#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001372 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001373termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374{
Bram Moolenaarab302212016-04-26 20:59:29 +02001375 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001376}
1377
1378 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001379termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001380{
1381 guicolor_T t;
1382
1383 if (*name == NUL)
1384 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001385 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386
1387 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001388 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001389 return t;
1390}
1391
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001392 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001393termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001394{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001395 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001396}
1397#endif
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef AMIGA
1403# define DEFAULT_TERM (char_u *)"amiga"
1404#endif
1405
1406#ifdef MSWIN
1407# define DEFAULT_TERM (char_u *)"win32"
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#if defined(UNIX) && !defined(__MINT__)
1411# define DEFAULT_TERM (char_u *)"ansi"
1412#endif
1413
1414#ifdef __MINT__
1415# define DEFAULT_TERM (char_u *)"vt52"
1416#endif
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#ifdef VMS
1419# define DEFAULT_TERM (char_u *)"vt320"
1420#endif
1421
1422#ifdef __BEOS__
1423# undef DEFAULT_TERM
1424# define DEFAULT_TERM (char_u *)"beos-ansi"
1425#endif
1426
1427#ifndef DEFAULT_TERM
1428# define DEFAULT_TERM (char_u *)"dumb"
1429#endif
1430
1431/*
1432 * Term_strings contains currently used terminal output strings.
1433 * It is initialized with the default values by parse_builtin_tcap().
1434 * The values can be changed by setting the option with the same name.
1435 */
1436char_u *(term_strings[(int)KS_LAST + 1]);
1437
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001438static int need_gather = FALSE; /* need to fill termleader[] */
1439static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001441static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001442static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#endif
1444
1445 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001446find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 struct builtin_term *p;
1449
1450 p = builtin_termcaps;
1451 while (p->bt_string != NULL)
1452 {
1453 if (p->bt_entry == (int)KS_NAME)
1454 {
1455#ifdef UNIX
1456 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1457 return p;
1458 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1459 return p;
1460 else
1461#endif
1462#ifdef VMS
1463 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1464 return p;
1465 else
1466#endif
1467 if (STRCMP(term, p->bt_string) == 0)
1468 return p;
1469 }
1470 ++p;
1471 }
1472 return p;
1473}
1474
1475/*
1476 * Parsing of the builtin termcap entries.
1477 * Caller should check if 'name' is a valid builtin term.
1478 * The terminal's name is not set, as this is already done in termcapinit().
1479 */
1480 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001481parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 struct builtin_term *p;
1484 char_u name[2];
1485 int term_8bit;
1486
1487 p = find_builtin_term(term);
1488 term_8bit = term_is_8bit(term);
1489
1490 /* Do not parse if builtin term not found */
1491 if (p->bt_string == NULL)
1492 return;
1493
1494 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1495 {
1496 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1497 {
1498 /* Only set the value if it wasn't set yet. */
1499 if (term_strings[p->bt_entry] == NULL
1500 || term_strings[p->bt_entry] == empty_option)
1501 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001502#ifdef FEAT_EVAL
1503 int opt_idx = -1;
1504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /* 8bit terminal: use CSI instead of <Esc>[ */
1506 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1507 {
1508 char_u *s, *t;
1509
1510 s = vim_strsave((char_u *)p->bt_string);
1511 if (s != NULL)
1512 {
1513 for (t = s; *t; ++t)
1514 if (term_7to8bit(t))
1515 {
1516 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001517 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001520#ifdef FEAT_EVAL
1521 opt_idx =
1522#endif
1523 set_term_option_alloced(
1524 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526 }
1527 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001530#ifdef FEAT_EVAL
1531 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1532#endif
1533 }
1534#ifdef FEAT_EVAL
1535 set_term_option_sctx_idx(NULL, opt_idx);
1536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 }
1539 else
1540 {
1541 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1542 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1543 if (find_termcode(name) == NULL)
1544 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1545 }
1546 }
1547}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
1550 * Set number of colors.
1551 * Store it as a number in t_colors.
1552 * Store it as a string in T_CCO (using nr_colors[]).
1553 */
1554 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001555set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
1557 char_u nr_colors[20]; /* string for number of colors */
1558
1559 t_colors = nr;
1560 if (t_colors > 1)
1561 sprintf((char *)nr_colors, "%d", t_colors);
1562 else
1563 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001564 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001566
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001567#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001568/*
1569 * Set the color count to "val" and redraw if it changed.
1570 */
1571 static void
1572may_adjust_color_count(int val)
1573{
1574 if (val != t_colors)
1575 {
1576 /* Nr of colors changed, initialize highlighting and
1577 * redraw everything. This causes a redraw, which usually
1578 * clears the message. Try keeping the message if it
1579 * might work. */
1580 set_keep_msg_from_hist();
1581 set_color_count(val);
1582 init_highlight(TRUE, FALSE);
1583# ifdef DEBUG_TERMRESPONSE
1584 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001585 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001586
Bram Moolenaarb255b902018-04-24 21:40:10 +02001587 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001588 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001589#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001590 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001591#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001592 }
1593}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#endif
1595
1596#ifdef HAVE_TGETENT
1597static char *(key_names[]) =
1598{
1599#ifdef FEAT_TERMRESPONSE
1600 /* Do this one first, it may cause a screen redraw. */
1601 "Co",
1602#endif
1603 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 "#2", "#4", "%i", "*7",
1605 "k1", "k2", "k3", "k4", "k5", "k6",
1606 "k7", "k8", "k9", "k;", "F1", "F2",
1607 "%1", "&8", "kb", "kI", "kD", "kh",
1608 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1609 NULL
1610};
1611#endif
1612
Bram Moolenaar9289df52018-05-10 14:40:57 +02001613#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001614 static void
1615get_term_entries(int *height, int *width)
1616{
1617 static struct {
1618 enum SpecialKey dest; /* index in term_strings[] */
1619 char *name; /* termcap name for string */
1620 } string_names[] =
1621 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1622 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1623 {KS_CL, "cl"}, {KS_CD, "cd"},
1624 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1625 {KS_ME, "me"}, {KS_MR, "mr"},
1626 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1627 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1628 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1629 {KS_STE,"Te"}, {KS_STS,"Ts"},
1630 {KS_CM, "cm"}, {KS_SR, "sr"},
1631 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1632 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1633 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1634 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1635 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1636 {KS_VS, "vs"}, {KS_CVS, "VS"},
1637 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1638 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1639 {KS_TS, "ts"}, {KS_FS, "fs"},
1640 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1641 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1642 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1643 {KS_8F, "8f"}, {KS_8B, "8b"},
1644 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1645 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001646 {KS_CST, "ST"}, {KS_CRT, "RT"},
1647 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001648 {(enum SpecialKey)0, NULL}
1649 };
1650 int i;
1651 char_u *p;
1652 static char_u tstrbuf[TBUFSZ];
1653 char_u *tp = tstrbuf;
1654
1655 /*
1656 * get output strings
1657 */
1658 for (i = 0; string_names[i].name != NULL; ++i)
1659 {
1660 if (TERM_STR(string_names[i].dest) == NULL
1661 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001662 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001664#ifdef FEAT_EVAL
1665 set_term_option_sctx_idx(string_names[i].name, -1);
1666#endif
1667 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001668 }
1669
1670 /* tgetflag() returns 1 if the flag is present, 0 if not and
1671 * possibly -1 if the flag doesn't exist. */
1672 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1673 T_MS = (char_u *)"y";
1674 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1675 T_XS = (char_u *)"y";
1676 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1677 T_XN = (char_u *)"y";
1678 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1679 T_DB = (char_u *)"y";
1680 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1681 T_DA = (char_u *)"y";
1682 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1683 T_UT = (char_u *)"y";
1684
1685 /*
1686 * get key codes
1687 */
1688 for (i = 0; key_names[i] != NULL; ++i)
1689 if (find_termcode((char_u *)key_names[i]) == NULL)
1690 {
1691 p = TGETSTR(key_names[i], &tp);
1692 /* if cursor-left == backspace, ignore it (televideo 925) */
1693 if (p != NULL
1694 && (*p != Ctrl_H
1695 || key_names[i][0] != 'k'
1696 || key_names[i][1] != 'l'))
1697 add_termcode((char_u *)key_names[i], p, FALSE);
1698 }
1699
1700 if (*height == 0)
1701 *height = tgetnum("li");
1702 if (*width == 0)
1703 *width = tgetnum("co");
1704
1705 /*
1706 * Get number of colors (if not done already).
1707 */
1708 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001710 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001711#ifdef FEAT_EVAL
1712 set_term_option_sctx_idx("Co", -1);
1713#endif
1714 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001715
1716# ifndef hpux
1717 BC = (char *)TGETSTR("bc", &tp);
1718 UP = (char *)TGETSTR("up", &tp);
1719 p = TGETSTR("pc", &tp);
1720 if (p)
1721 PC = *p;
1722# endif
1723}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001724#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001725
1726 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001727report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001728{
1729 struct builtin_term *termp;
1730
1731 mch_errmsg("\r\n");
1732 if (error_msg != NULL)
1733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001734 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001735 mch_errmsg("\r\n");
1736 }
1737 mch_errmsg("'");
1738 mch_errmsg((char *)term);
1739 mch_errmsg(_("' not known. Available builtin terminals are:"));
1740 mch_errmsg("\r\n");
1741 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1742 {
1743 if (termp->bt_entry == (int)KS_NAME)
1744 {
1745#ifdef HAVE_TGETENT
1746 mch_errmsg(" builtin_");
1747#else
1748 mch_errmsg(" ");
1749#endif
1750 mch_errmsg(termp->bt_string);
1751 mch_errmsg("\r\n");
1752 }
1753 }
1754}
1755
1756 static void
1757report_default_term(char_u *term)
1758{
1759 mch_errmsg(_("defaulting to '"));
1760 mch_errmsg((char *)term);
1761 mch_errmsg("'\r\n");
1762 if (emsg_silent == 0)
1763 {
1764 screen_start(); /* don't know where cursor is now */
1765 out_flush();
1766 if (!is_not_a_term())
1767 ui_delay(2000L, TRUE);
1768 }
1769}
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
1772 * Set terminal options for terminal "term".
1773 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1774 *
1775 * While doing this, until ttest(), some options may be NULL, be careful.
1776 */
1777 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001778set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 struct builtin_term *termp;
1781#ifdef HAVE_TGETENT
1782 int builtin_first = p_tbi;
1783 int try;
1784 int termcap_cleared = FALSE;
1785#endif
1786 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 char_u *bs_p, *del_p;
1789
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001790 /* In silect mode (ex -s) we don't use the 'term' option. */
1791 if (silent_mode)
1792 return OK;
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 detected_8bit = FALSE; /* reset 8-bit detection */
1795
1796 if (term_is_builtin(term))
1797 {
1798 term += 8;
1799#ifdef HAVE_TGETENT
1800 builtin_first = 1;
1801#endif
1802 }
1803
1804/*
1805 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1806 * If builtin_first is TRUE:
1807 * 0. try builtin termcap
1808 * 1. try external termcap
1809 * 2. if both fail default to a builtin terminal
1810 * If builtin_first is FALSE:
1811 * 1. try external termcap
1812 * 2. try builtin termcap, if both fail default to a builtin terminal
1813 */
1814#ifdef HAVE_TGETENT
1815 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1816 {
1817 /*
1818 * Use external termcap
1819 */
1820 if (try == 1)
1821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 /*
1825 * If the external termcap does not have a matching entry, try the
1826 * builtin ones.
1827 */
1828 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (!termcap_cleared)
1831 {
1832 clear_termoptions(); /* clear old options */
1833 termcap_cleared = TRUE;
1834 }
1835
Bram Moolenaar69e05692018-05-10 14:11:52 +02001836 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 }
1839 else /* try == 0 || try == 2 */
1840#endif /* HAVE_TGETENT */
1841 /*
1842 * Use builtin termcap
1843 */
1844 {
1845#ifdef HAVE_TGETENT
1846 /*
1847 * If builtin termcap was already used, there is no need to search
1848 * for the builtin termcap again, quit now.
1849 */
1850 if (try == 2 && builtin_first && termcap_cleared)
1851 break;
1852#endif
1853 /*
1854 * search for 'term' in builtin_termcaps[]
1855 */
1856 termp = find_builtin_term(term);
1857 if (termp->bt_string == NULL) /* did not find it */
1858 {
1859#ifdef HAVE_TGETENT
1860 /*
1861 * If try == 0, first try the external termcap. If that is not
1862 * found we'll get back here with try == 2.
1863 * If termcap_cleared is set we used the external termcap,
1864 * don't complain about not finding the term in the builtin
1865 * termcap.
1866 */
1867 if (try == 0) /* try external one */
1868 continue;
1869 if (termcap_cleared) /* found in external termcap */
1870 break;
1871#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001872 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 /* when user typed :set term=xxx, quit here */
1875 if (starting != NO_SCREEN)
1876 {
1877 screen_start(); /* don't know where cursor is now */
1878 wait_return(TRUE);
1879 return FAIL;
1880 }
1881 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001882 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001883 set_string_option_direct((char_u *)"term", -1, term,
1884 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 display_errors();
1886 }
1887 out_flush();
1888#ifdef HAVE_TGETENT
1889 if (!termcap_cleared)
1890 {
1891#endif
1892 clear_termoptions(); /* clear old options */
1893#ifdef HAVE_TGETENT
1894 termcap_cleared = TRUE;
1895 }
1896#endif
1897 parse_builtin_tcap(term);
1898#ifdef FEAT_GUI
1899 if (term_is_gui(term))
1900 {
1901 out_flush();
1902 gui_init();
1903 /* If starting the GUI failed, don't do any of the other
1904 * things for this terminal */
1905 if (!gui.in_use)
1906 return FAIL;
1907#ifdef HAVE_TGETENT
1908 break; /* don't try using external termcap */
1909#endif
1910 }
1911#endif /* FEAT_GUI */
1912 }
1913#ifdef HAVE_TGETENT
1914 }
1915#endif
1916
1917/*
1918 * special: There is no info in the termcap about whether the cursor
1919 * positioning is relative to the start of the screen or to the start of the
1920 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1921 * relative.
1922 */
1923 if (STRCMP(term, "pcterm") == 0)
1924 T_CCS = (char_u *)"yes";
1925 else
1926 T_CCS = empty_option;
1927
1928#ifdef UNIX
1929/*
1930 * Any "stty" settings override the default for t_kb from the termcap.
1931 * This is in os_unix.c, because it depends a lot on the version of unix that
1932 * is being used.
1933 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1934 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001935# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 get_stty();
1939#endif
1940
1941/*
1942 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1943 * didn't work, use the default CTRL-H
1944 * The default for t_kD is DEL, unless t_kb is DEL.
1945 * The vim_strsave'd strings are probably lost forever, well it's only two
1946 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1947 * directly.
1948 */
1949#ifdef FEAT_GUI
1950 if (!gui.in_use)
1951#endif
1952 {
1953 bs_p = find_termcode((char_u *)"kb");
1954 del_p = find_termcode((char_u *)"kD");
1955 if (bs_p == NULL || *bs_p == NUL)
1956 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1957 if ((del_p == NULL || *del_p == NUL) &&
1958 (bs_p == NULL || *bs_p != DEL))
1959 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1960 }
1961
1962#if defined(UNIX) || defined(VMS)
1963 term_is_xterm = vim_is_xterm(term);
1964#endif
1965
1966#ifdef FEAT_MOUSE
1967# if defined(UNIX) || defined(VMS)
1968# ifdef FEAT_MOUSE_TTY
1969 /*
1970 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1971 * The termcode for the mouse is added as a side effect in option.c.
1972 */
1973 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001974 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001977 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 if (use_xterm_mouse())
1980 p = NULL; /* keep existing value, might be "xterm2" */
1981 else
1982 p = (char_u *)"xterm";
1983 }
1984# endif
1985 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001988 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1989 * "xterm2" in check_termcode(). */
1990 reset_option_was_set((char_u *)"ttym");
1991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (p == NULL
1993# ifdef FEAT_GUI
1994 || gui.in_use
1995# endif
1996 )
1997 check_mouse_termcode(); /* set mouse termcode anyway */
1998 }
1999# endif
2000# else
2001 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
2002# endif
2003#endif /* FEAT_MOUSE */
2004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005#ifdef USE_TERM_CONSOLE
2006 /* DEFAULT_TERM indicates that it is the machine console. */
2007 if (STRCMP(term, DEFAULT_TERM) != 0)
2008 term_console = FALSE;
2009 else
2010 {
2011 term_console = TRUE;
2012# ifdef AMIGA
2013 win_resize_on(); /* enable window resizing reports */
2014# endif
2015 }
2016#endif
2017
2018#if defined(UNIX) || defined(VMS)
2019 /*
2020 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2021 */
2022 if (vim_is_fastterm(term))
2023 p_tf = TRUE;
2024#endif
2025#ifdef USE_TERM_CONSOLE
2026 /*
2027 * 'ttyfast' is default on consoles
2028 */
2029 if (term_console)
2030 p_tf = TRUE;
2031#endif
2032
2033 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2034
2035 full_screen = TRUE; /* we can use termcap codes from now on */
2036 set_term_defaults(); /* use current values as defaults */
2037#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002038 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002039 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041
2042 /*
2043 * Initialize the terminal with the appropriate termcap codes.
2044 * Set the mouse and window title if possible.
2045 * Don't do this when starting, need to parse the .vimrc first, because it
2046 * may redefine t_TI etc.
2047 */
2048 if (starting != NO_SCREEN)
2049 {
2050 starttermcap(); /* may change terminal mode */
2051#ifdef FEAT_MOUSE
2052 setmouse(); /* may start using the mouse */
2053#endif
2054#ifdef FEAT_TITLE
2055 maketitle(); /* may display window title */
2056#endif
2057 }
2058
2059 /* display initial screen after ttest() checking. jw. */
2060 if (width <= 0 || height <= 0)
2061 {
2062 /* termcap failed to report size */
2063 /* set defaults, in case ui_get_shellsize() also fails */
2064 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002065#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 height = 25; /* console is often 25 lines */
2067#else
2068 height = 24; /* most terminals are 24 lines */
2069#endif
2070 }
2071 set_shellsize(width, height, FALSE); /* may change Rows */
2072 if (starting != NO_SCREEN)
2073 {
2074 if (scroll_region)
2075 scroll_region_reset(); /* In case Rows changed */
2076 check_map_keycodes(); /* check mappings for terminal codes used */
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002079 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 /*
2082 * Execute the TermChanged autocommands for each buffer that is
2083 * loaded.
2084 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002085 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002086 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 if (curbuf->b_ml.ml_mfp != NULL)
2089 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2090 curbuf);
2091 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002092 if (bufref_valid(&old_curbuf))
2093 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096
2097#ifdef FEAT_TERMRESPONSE
2098 may_req_termresponse();
2099#endif
2100
2101 return OK;
2102}
2103
2104#if defined(FEAT_MOUSE) || defined(PROTO)
2105
2106# ifdef FEAT_MOUSE_TTY
2107# define HMT_NORMAL 1
2108# define HMT_NETTERM 2
2109# define HMT_DEC 4
2110# define HMT_JSBTERM 8
2111# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002112# define HMT_URXVT 32
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002113# define HMT_GPM 64
2114# define HMT_SGR 128
2115# define HMT_SGR_REL 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116static int has_mouse_termcode = 0;
2117# endif
2118
Bram Moolenaar864207d2008-06-24 22:14:38 +00002119# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002121set_mouse_termcode(
2122 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2123 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 char_u name[2];
2126
2127 name[0] = n;
2128 name[1] = KE_FILLER;
2129 add_termcode(name, s, FALSE);
2130# ifdef FEAT_MOUSE_TTY
2131# ifdef FEAT_MOUSE_JSB
2132 if (n == KS_JSBTERM_MOUSE)
2133 has_mouse_termcode |= HMT_JSBTERM;
2134 else
2135# endif
2136# ifdef FEAT_MOUSE_NET
2137 if (n == KS_NETTERM_MOUSE)
2138 has_mouse_termcode |= HMT_NETTERM;
2139 else
2140# endif
2141# ifdef FEAT_MOUSE_DEC
2142 if (n == KS_DEC_MOUSE)
2143 has_mouse_termcode |= HMT_DEC;
2144 else
2145# endif
2146# ifdef FEAT_MOUSE_PTERM
2147 if (n == KS_PTERM_MOUSE)
2148 has_mouse_termcode |= HMT_PTERM;
2149 else
2150# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002151# ifdef FEAT_MOUSE_URXVT
2152 if (n == KS_URXVT_MOUSE)
2153 has_mouse_termcode |= HMT_URXVT;
2154 else
2155# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002156# ifdef FEAT_MOUSE_GPM
2157 if (n == KS_GPM_MOUSE)
2158 has_mouse_termcode |= HMT_GPM;
2159 else
2160# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002161 if (n == KS_SGR_MOUSE)
2162 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002163 else if (n == KS_SGR_MOUSE_RELEASE)
2164 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 has_mouse_termcode |= HMT_NORMAL;
2167# endif
2168}
2169# endif
2170
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002171# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002172 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002174del_mouse_termcode(
2175 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 char_u name[2];
2178
2179 name[0] = n;
2180 name[1] = KE_FILLER;
2181 del_termcode(name);
2182# ifdef FEAT_MOUSE_TTY
2183# ifdef FEAT_MOUSE_JSB
2184 if (n == KS_JSBTERM_MOUSE)
2185 has_mouse_termcode &= ~HMT_JSBTERM;
2186 else
2187# endif
2188# ifdef FEAT_MOUSE_NET
2189 if (n == KS_NETTERM_MOUSE)
2190 has_mouse_termcode &= ~HMT_NETTERM;
2191 else
2192# endif
2193# ifdef FEAT_MOUSE_DEC
2194 if (n == KS_DEC_MOUSE)
2195 has_mouse_termcode &= ~HMT_DEC;
2196 else
2197# endif
2198# ifdef FEAT_MOUSE_PTERM
2199 if (n == KS_PTERM_MOUSE)
2200 has_mouse_termcode &= ~HMT_PTERM;
2201 else
2202# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002203# ifdef FEAT_MOUSE_URXVT
2204 if (n == KS_URXVT_MOUSE)
2205 has_mouse_termcode &= ~HMT_URXVT;
2206 else
2207# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002208# ifdef FEAT_MOUSE_GPM
2209 if (n == KS_GPM_MOUSE)
2210 has_mouse_termcode &= ~HMT_GPM;
2211 else
2212# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002213 if (n == KS_SGR_MOUSE)
2214 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002215 else if (n == KS_SGR_MOUSE_RELEASE)
2216 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 has_mouse_termcode &= ~HMT_NORMAL;
2219# endif
2220}
2221# endif
2222#endif
2223
2224#ifdef HAVE_TGETENT
2225/*
2226 * Call tgetent()
2227 * Return error message if it fails, NULL if it's OK.
2228 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002230tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 int i;
2233
2234 i = TGETENT(tbuf, term);
2235 if (i < 0 /* -1 is always an error */
2236# ifdef TGETENT_ZERO_ERR
2237 || i == 0 /* sometimes zero is also an error */
2238# endif
2239 )
2240 {
2241 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2242 * tgetent() with the always existing "dumb" entry to avoid a crash or
2243 * hang. */
2244 (void)TGETENT(tbuf, "dumb");
2245
2246 if (i < 0)
2247# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002248 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (i == 0)
2250# endif
2251#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002252 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255#endif
2256 }
2257 return NULL;
2258}
2259
2260/*
2261 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2262 * Fix that here.
2263 */
2264 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002265vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 char *p;
2268
2269 p = tgetstr(s, (char **)pp);
2270 if (p == (char *)-1)
2271 p = NULL;
2272 return (char_u *)p;
2273}
2274#endif /* HAVE_TGETENT */
2275
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002276#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277/*
2278 * Get Columns and Rows from the termcap. Used after a window signal if the
2279 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2280 * and "li" entries never change. But on some systems this works.
2281 * Errors while getting the entries are ignored.
2282 */
2283 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002284getlinecol(
2285 long *cp, /* pointer to columns */
2286 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 char_u tbuf[TBUFSZ];
2289
2290 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2291 {
2292 if (*cp == 0)
2293 *cp = tgetnum("co");
2294 if (*rp == 0)
2295 *rp = tgetnum("li");
2296 }
2297}
2298#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2299
2300/*
2301 * Get a string entry from the termcap and add it to the list of termcodes.
2302 * Used for <t_xx> special keys.
2303 * Give an error message for failure when not sourcing.
2304 * If force given, replace an existing entry.
2305 * Return FAIL if the entry was not found, OK if the entry was added.
2306 */
2307 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002308add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 char_u *term;
2311 int key;
2312 struct builtin_term *termp;
2313#ifdef HAVE_TGETENT
2314 char_u *string;
2315 int i;
2316 int builtin_first;
2317 char_u tbuf[TBUFSZ];
2318 char_u tstrbuf[TBUFSZ];
2319 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002320 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#endif
2322
2323/*
2324 * If the GUI is running or will start in a moment, we only support the keys
2325 * that the GUI can produce.
2326 */
2327#ifdef FEAT_GUI
2328 if (gui.in_use || gui.starting)
2329 return gui_mch_haskey(name);
2330#endif
2331
2332 if (!force && find_termcode(name) != NULL) /* it's already there */
2333 return OK;
2334
2335 term = T_NAME;
2336 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2337 return FAIL;
2338
2339 if (term_is_builtin(term)) /* name starts with "builtin_" */
2340 {
2341 term += 8;
2342#ifdef HAVE_TGETENT
2343 builtin_first = TRUE;
2344#endif
2345 }
2346#ifdef HAVE_TGETENT
2347 else
2348 builtin_first = p_tbi;
2349#endif
2350
2351#ifdef HAVE_TGETENT
2352/*
2353 * We can get the entry from the builtin termcap and from the external one.
2354 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2355 * builtin termcap first.
2356 * If 'ttybuiltin' is off, try external termcap first.
2357 */
2358 for (i = 0; i < 2; ++i)
2359 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002360 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#endif
2362 /*
2363 * Search in builtin termcap
2364 */
2365 {
2366 termp = find_builtin_term(term);
2367 if (termp->bt_string != NULL) /* found it */
2368 {
2369 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002370 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 while (termp->bt_entry != (int)KS_NAME)
2372 {
2373 if ((int)termp->bt_entry == key)
2374 {
2375 add_termcode(name, (char_u *)termp->bt_string,
2376 term_is_8bit(term));
2377 return OK;
2378 }
2379 ++termp;
2380 }
2381 }
2382 }
2383#ifdef HAVE_TGETENT
2384 else
2385 /*
2386 * Search in external termcap
2387 */
2388 {
2389 error_msg = tgetent_error(tbuf, term);
2390 if (error_msg == NULL)
2391 {
2392 string = TGETSTR((char *)name, &tp);
2393 if (string != NULL && *string != NUL)
2394 {
2395 add_termcode(name, string, FALSE);
2396 return OK;
2397 }
2398 }
2399 }
2400 }
2401#endif
2402
2403 if (sourcing_name == NULL)
2404 {
2405#ifdef HAVE_TGETENT
2406 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002407 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else
2409#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002410 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 return FAIL;
2413}
2414
2415 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002416term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2419}
2420
2421/*
2422 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2423 * Assume that the terminal is using 8-bit controls when the name contains
2424 * "8bit", like in "xterm-8bit".
2425 */
2426 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002427term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2430}
2431
2432/*
2433 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002434 * <Esc>[ -> CSI <M_C_[>
2435 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 * <Esc>O -> <M-C-O>
2437 */
2438 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002439term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 if (*p == ESC)
2442 {
2443 if (p[1] == '[')
2444 return CSI;
2445 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002446 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (p[1] == 'O')
2448 return 0x8f;
2449 }
2450 return 0;
2451}
2452
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002453#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002455term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2458}
2459#endif
2460
2461#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2462
2463 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002464tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 static char_u buf[16];
2467 char_u *p;
2468
2469 p = buf + 15;
2470 *p = '\0';
2471 do
2472 {
2473 --p;
2474 *p = (char_u) (i % 10 + '0');
2475 i /= 10;
2476 }
2477 while (i > 0 && p > buf);
2478 return p;
2479}
2480#endif
2481
2482#ifndef HAVE_TGETENT
2483
2484/*
2485 * minimal tgoto() implementation.
2486 * no padding and we only parse for %i %d and %+char
2487 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002488 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002489tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 static char buf[30];
2492 char *p, *s, *e;
2493
2494 if (!cm)
2495 return "OOPS";
2496 e = buf + 29;
2497 for (s = buf; s < e && *cm; cm++)
2498 {
2499 if (*cm != '%')
2500 {
2501 *s++ = *cm;
2502 continue;
2503 }
2504 switch (*++cm)
2505 {
2506 case 'd':
2507 p = (char *)tltoa((unsigned long)y);
2508 y = x;
2509 while (*p)
2510 *s++ = *p++;
2511 break;
2512 case 'i':
2513 x++;
2514 y++;
2515 break;
2516 case '+':
2517 *s++ = (char)(*++cm + y);
2518 y = x;
2519 break;
2520 case '%':
2521 *s++ = *cm;
2522 break;
2523 default:
2524 return "OOPS";
2525 }
2526 }
2527 *s = '\0';
2528 return buf;
2529}
2530
2531#endif /* HAVE_TGETENT */
2532
2533/*
2534 * Set the terminal name and initialize the terminal options.
2535 * If "name" is NULL or empty, get the terminal name from the environment.
2536 * If that fails, use the default terminal name.
2537 */
2538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 char_u *term;
2542
2543 if (name != NULL && *name == NUL)
2544 name = NULL; /* empty name is equal to no name */
2545 term = name;
2546
2547#ifdef __BEOS__
2548 /*
2549 * TERM environment variable is normally set to 'ansi' on the Bebox;
2550 * Since the BeBox doesn't quite support full ANSI yet, we use our
2551 * own custom 'ansi-beos' termcap instead, unless the -T option has
2552 * been given on the command line.
2553 */
2554 if (term == NULL
2555 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2556 term = DEFAULT_TERM;
2557#endif
2558#ifndef MSWIN
2559 if (term == NULL)
2560 term = mch_getenv((char_u *)"TERM");
2561#endif
2562 if (term == NULL || *term == NUL)
2563 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002564 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 /* Set the default terminal name. */
2567 set_string_default("term", term);
2568 set_string_default("ttytype", term);
2569
2570 /*
2571 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2572 */
2573 set_termname(T_NAME != NULL ? T_NAME : term);
2574}
2575
2576/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002577 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002579#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002580
2581// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002583
2584static int out_pos = 0; // number of chars in out_buf
2585
2586// Since the maximum number of SGR parameters shown as a normal value range is
2587// 16, the escape sequence length can be 4 * 16 + lead + tail.
2588#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590/*
2591 * out_flush(): flush the output buffer
2592 */
2593 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002594out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 int len;
2597
2598 if (out_pos != 0)
2599 {
2600 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2601 len = out_pos;
2602 out_pos = 0;
2603 ui_write(out_buf, len);
2604 }
2605}
2606
Bram Moolenaara338adc2018-01-31 20:51:47 +01002607/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002608 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2609 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002610 */
2611 void
2612out_flush_cursor(
2613 int force UNUSED, /* when TRUE, update cursor even when not moved */
2614 int clear_selection UNUSED) /* clear selection under cursor */
2615{
2616 mch_disable_flush();
2617 out_flush();
2618 mch_enable_flush();
2619#ifdef FEAT_GUI
2620 if (gui.in_use)
2621 {
2622 gui_update_cursor(force, clear_selection);
2623 gui_may_flush();
2624 }
2625#endif
2626}
2627
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
2630 * Sometimes a byte out of a multi-byte character is written with out_char().
2631 * To avoid flushing half of the character, call this function first.
2632 */
2633 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002634out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2637 out_flush();
2638}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640#ifdef FEAT_GUI
2641/*
2642 * out_trash(): Throw away the contents of the output buffer
2643 */
2644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002645out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 out_pos = 0;
2648}
2649#endif
2650
2651/*
2652 * out_char(c): put a byte into the output buffer.
2653 * Flush it if it becomes full.
2654 * This should not be used for outputting text on the screen (use functions
2655 * like msg_puts() and screen_putchar() for that).
2656 */
2657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaard0573012017-10-28 21:11:06 +02002660#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2662 out_char('\r');
2663#endif
2664
2665 out_buf[out_pos++] = c;
2666
2667 /* For testing we flush each time. */
2668 if (out_pos >= OUT_SIZE || p_wd)
2669 out_flush();
2670}
2671
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002672static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674/*
2675 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2676 */
2677 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002678out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
Bram Moolenaard0573012017-10-28 21:11:06 +02002680#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2682 out_char_nf('\r');
2683#endif
2684
2685 out_buf[out_pos++] = c;
2686
2687 if (out_pos >= OUT_SIZE)
2688 out_flush();
2689}
2690
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002691#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2692 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693/*
2694 * A never-padding out_str.
2695 * use this whenever you don't want to run the string through tputs.
2696 * tputs above is harmless, but tputs from the termcap library
2697 * is likely to strip off leading digits, that it mistakes for padding
2698 * information, and "%i", "%d", etc.
2699 * This should only be used for writing terminal codes, not for outputting
2700 * normal text (use functions like msg_puts() and screen_putchar() for that).
2701 */
2702 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002703out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002705 // avoid terminal strings being split up
2706 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 while (*s)
2710 out_char_nf(*s++);
2711
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002712 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (p_wd)
2714 out_flush();
2715}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002719 * A conditional-flushing out_str, mainly for visualbell.
2720 * Handles a delay internally, because termlib may not respect the delay or do
2721 * it at the wrong time.
2722 * Note: Only for terminal strings.
2723 */
2724 void
2725out_str_cf(char_u *s)
2726{
2727 if (s != NULL && *s)
2728 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002730 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002731#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002732
2733#ifdef FEAT_GUI
2734 /* Don't use tputs() when GUI is used, ncurses crashes. */
2735 if (gui.in_use)
2736 {
2737 out_str_nf(s);
2738 return;
2739 }
2740#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002741 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002742 out_flush();
2743#ifdef HAVE_TGETENT
2744 for (p = s; *s; ++s)
2745 {
2746 /* flush just before delay command */
2747 if (*s == '$' && *(s + 1) == '<')
2748 {
2749 char_u save_c = *s;
2750 int duration = atoi((char *)s + 2);
2751
2752 *s = NUL;
2753 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2754 *s = save_c;
2755 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002756# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002757 /* Only sleep here if we can limit this happening in
2758 * vim_beep(). */
2759 p = vim_strchr(s, '>');
2760 if (p == NULL || duration <= 0)
2761 {
2762 /* can't parse the time, don't sleep here */
2763 p = s;
2764 }
2765 else
2766 {
2767 ++p;
2768 do_sleep(duration);
2769 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002770# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002771 /* Rely on the terminal library to sleep. */
2772 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002773# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002774 break;
2775 }
2776 }
2777 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2778#else
2779 while (*s)
2780 out_char_nf(*s++);
2781#endif
2782
2783 /* For testing we write one string at a time. */
2784 if (p_wd)
2785 out_flush();
2786 }
2787}
2788
2789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 * out_str(s): Put a character string a byte at a time into the output buffer.
2791 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2792 * This should only be used for writing terminal codes, not for outputting
2793 * normal text (use functions like msg_puts() and screen_putchar() for that).
2794 */
2795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 if (s != NULL && *s)
2799 {
2800#ifdef FEAT_GUI
2801 /* Don't use tputs() when GUI is used, ncurses crashes. */
2802 if (gui.in_use)
2803 {
2804 out_str_nf(s);
2805 return;
2806 }
2807#endif
2808 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002809 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 out_flush();
2811#ifdef HAVE_TGETENT
2812 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2813#else
2814 while (*s)
2815 out_char_nf(*s++);
2816#endif
2817
2818 /* For testing we write one string at a time. */
2819 if (p_wd)
2820 out_flush();
2821 }
2822}
2823
2824/*
2825 * cursor positioning using termcap parser. (jw)
2826 */
2827 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002828term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
2830 OUT_STR(tgoto((char *)T_CM, col, row));
2831}
2832
2833 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002834term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 OUT_STR(tgoto((char *)T_CRI, 0, i));
2837}
2838
2839 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002840term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2843}
2844
2845 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002846term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
2848 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2849}
2850
2851#if defined(HAVE_TGETENT) || defined(PROTO)
2852 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002853term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 /* Can't handle a negative value here */
2856 if (x < 0)
2857 x = 0;
2858 if (y < 0)
2859 y = 0;
2860 OUT_STR(tgoto((char *)T_CWP, y, x));
2861}
2862
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002863# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2864/*
2865 * Return TRUE if we can request the terminal for a response.
2866 */
2867 static int
2868can_get_termresponse()
2869{
2870 return cur_tmode == TMODE_RAW
2871 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002872# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002873 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002874# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002875 && p_ek;
2876}
2877
Bram Moolenaarafd78262019-05-10 23:10:31 +02002878/*
2879 * Set "status" to STATUS_SENT.
2880 */
2881 static void
2882termrequest_sent(termrequest_T *status)
2883{
2884 status->tr_progress = STATUS_SENT;
2885 status->tr_start = time(NULL);
2886}
2887
2888/*
2889 * Return TRUE if any of the requests are in STATUS_SENT.
2890 */
2891 static int
2892termrequest_any_pending()
2893{
2894 int i;
2895 time_t now = time(NULL);
2896
2897 for (i = 0; all_termrequests[i] != NULL; ++i)
2898 {
2899 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2900 {
2901 if (all_termrequests[i]->tr_start > 0 && now > 0
2902 && all_termrequests[i]->tr_start + 2 < now)
2903 // Sent the request more than 2 seconds ago and didn't get a
2904 // response, assume it failed.
2905 all_termrequests[i]->tr_progress = STATUS_FAIL;
2906 else
2907 return TRUE;
2908 }
2909 }
2910 return FALSE;
2911}
2912
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002913static int winpos_x = -1;
2914static int winpos_y = -1;
2915static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002916
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002917# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002918/*
2919 * Try getting the Vim window position from the terminal.
2920 * Returns OK or FAIL.
2921 */
2922 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002923term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002924{
2925 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002926 int prev_winpos_x = winpos_x;
2927 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002928
2929 if (*T_CGP == NUL || !can_get_termresponse())
2930 return FAIL;
2931 winpos_x = -1;
2932 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002933 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002934 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002935 OUT_STR(T_CGP);
2936 out_flush();
2937
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002938 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002939 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002940 {
2941 (void)vpeekc_nomap();
2942 if (winpos_x >= 0 && winpos_y >= 0)
2943 {
2944 *x = winpos_x;
2945 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002946 return OK;
2947 }
2948 ui_delay(10, FALSE);
2949 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002950 /* Do not reset "did_request_winpos", if we timed out the response might
2951 * still come later and we must consume it. */
2952
2953 winpos_x = prev_winpos_x;
2954 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002955 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002956 {
2957 /* Polling: return previous values if we have them. */
2958 *x = winpos_x;
2959 *y = winpos_y;
2960 return OK;
2961 }
2962
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002963 return FALSE;
2964}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002965# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002966# endif
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002969term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002971 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973#endif
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002976term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002979 int i = *s == CSI ? 1 : 2;
2980 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 /* Special handling of 16 colors, because termcap can't handle it */
2983 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2984 /* Also accept CSI instead of <Esc>[ */
2985 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002986 && ((s[0] == ESC && s[1] == '[')
2987#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2988 || (s[0] == ESC && s[1] == '|')
2989#endif
2990 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 && s[i] != NUL
2992 && (STRCMP(s + i + 1, "%p1%dm") == 0
2993 || STRCMP(s + i + 1, "%dm") == 0)
2994 && (s[i] == '3' || s[i] == '4'))
2995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002997 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002999 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003001 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003002#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02003003 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003004#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003005 IF_EB("\033[", ESC_STR "[")) : "\233";
3006 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3007 : (n >= 16 ? "48;5;" : "10");
3008
3009 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3011 }
3012 else
3013 OUT_STR(tgoto((char *)s, 0, n));
3014}
3015
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003016 void
3017term_fg_color(int n)
3018{
3019 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
3020 if (*T_CAF)
3021 term_color(T_CAF, n);
3022 else if (*T_CSF)
3023 term_color(T_CSF, n);
3024}
3025
3026 void
3027term_bg_color(int n)
3028{
3029 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
3030 if (*T_CAB)
3031 term_color(T_CAB, n);
3032 else if (*T_CSB)
3033 term_color(T_CSB, n);
3034}
3035
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003036#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003037
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003038#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3039#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3040#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003041
3042 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003043term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003044{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003045#define MAX_COLOR_STR_LEN 100
3046 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003047
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003048 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003049 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003050 OUT_STR(buf);
3051}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003052
3053 void
3054term_fg_rgb_color(guicolor_T rgb)
3055{
3056 term_rgb_color(T_8F, rgb);
3057}
3058
3059 void
3060term_bg_rgb_color(guicolor_T rgb)
3061{
3062 term_rgb_color(T_8B, rgb);
3063}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003064#endif
3065
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003066#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3067 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068/*
3069 * Generic function to set window title, using t_ts and t_fs.
3070 */
3071 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003072term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 /* t_ts takes one argument: column in status line */
3075 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
3076 out_str_nf(title);
3077 out_str(T_FS); /* set title end */
3078 out_flush();
3079}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003080
3081/*
3082 * Tell the terminal to push (save) the title and/or icon, so that it can be
3083 * popped (restored) later.
3084 */
3085 void
3086term_push_title(int which)
3087{
Bram Moolenaar27821262019-05-08 16:41:09 +02003088 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003089 {
3090 OUT_STR(T_CST);
3091 out_flush();
3092 }
3093
Bram Moolenaar27821262019-05-08 16:41:09 +02003094 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003095 {
3096 OUT_STR(T_SSI);
3097 out_flush();
3098 }
3099}
3100
3101/*
3102 * Tell the terminal to pop the title and/or icon.
3103 */
3104 void
3105term_pop_title(int which)
3106{
Bram Moolenaar27821262019-05-08 16:41:09 +02003107 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003108 {
3109 OUT_STR(T_CRT);
3110 out_flush();
3111 }
3112
Bram Moolenaar27821262019-05-08 16:41:09 +02003113 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003114 {
3115 OUT_STR(T_SRI);
3116 out_flush();
3117 }
3118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120
3121/*
3122 * Make sure we have a valid set or terminal options.
3123 * Replace all entries that are NULL by empty_option
3124 */
3125 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003126ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003128 char_u *env_colors;
3129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 check_options(); /* make sure no options are NULL */
3131
3132 /*
3133 * MUST have "cm": cursor motion.
3134 */
3135 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003136 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 /*
3139 * if "cs" defined, use a scroll region, it's faster.
3140 */
3141 if (*T_CS != NUL)
3142 scroll_region = TRUE;
3143 else
3144 scroll_region = FALSE;
3145
3146 if (pairs)
3147 {
3148 /*
3149 * optional pairs
3150 */
3151 /* TP goes to normal mode for TI (invert) and TB (bold) */
3152 if (*T_ME == NUL)
3153 T_ME = T_MR = T_MD = T_MB = empty_option;
3154 if (*T_SO == NUL || *T_SE == NUL)
3155 T_SO = T_SE = empty_option;
3156 if (*T_US == NUL || *T_UE == NUL)
3157 T_US = T_UE = empty_option;
3158 if (*T_CZH == NUL || *T_CZR == NUL)
3159 T_CZH = T_CZR = empty_option;
3160
3161 /* T_VE is needed even though T_VI is not defined */
3162 if (*T_VE == NUL)
3163 T_VI = empty_option;
3164
3165 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3166 if (*T_ME == NUL)
3167 {
3168 T_ME = T_SE;
3169 T_MR = T_SO;
3170 T_MD = T_SO;
3171 }
3172
3173 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3174 if (*T_SO == NUL)
3175 {
3176 T_SE = T_ME;
3177 if (*T_MR == NUL)
3178 T_SO = T_MD;
3179 else
3180 T_SO = T_MR;
3181 }
3182
3183 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3184 if (*T_CZH == NUL)
3185 {
3186 T_CZR = T_ME;
3187 if (*T_MR == NUL)
3188 T_CZH = T_MD;
3189 else
3190 T_CZH = T_MR;
3191 }
3192
3193 /* "Sb" and "Sf" come in pairs */
3194 if (*T_CSB == NUL || *T_CSF == NUL)
3195 {
3196 T_CSB = empty_option;
3197 T_CSF = empty_option;
3198 }
3199
3200 /* "AB" and "AF" come in pairs */
3201 if (*T_CAB == NUL || *T_CAF == NUL)
3202 {
3203 T_CAB = empty_option;
3204 T_CAF = empty_option;
3205 }
3206
3207 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3208 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003209 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 /* Set 'weirdinvert' according to value of 't_xs' */
3212 p_wiv = (*T_XS != NUL);
3213 }
3214 need_gather = TRUE;
3215
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003216 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003218 env_colors = mch_getenv((char_u *)"COLORS");
3219 if (env_colors != NULL && isdigit(*env_colors))
3220 {
3221 int colors = atoi((char *)env_colors);
3222
3223 if (colors != t_colors)
3224 set_color_count(colors);
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226}
3227
3228#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3229 || defined(PROTO)
3230/*
3231 * Represent the given long_u as individual bytes, with the most significant
3232 * byte first, and store them in dst.
3233 */
3234 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003235add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 int i;
3238 int shift;
3239
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003240 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 shift = 8 * (sizeof(long_u) - i);
3243 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3244 }
3245}
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247/*
3248 * Interpret the next string of bytes in buf as a long integer, with the most
3249 * significant byte first. Note that it is assumed that buf has been through
3250 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3251 * Puts result in val, and returns the number of bytes read from buf
3252 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3253 * were present.
3254 */
3255 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003256get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 int len;
3259 char_u bytes[sizeof(long_u)];
3260 int i;
3261 int shift;
3262
3263 *val = 0;
3264 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3265 if (len != -1)
3266 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003267 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 shift = 8 * (sizeof(long_u) - 1 - i);
3270 *val += (long_u)bytes[i] << shift;
3271 }
3272 }
3273 return len;
3274}
3275#endif
3276
3277#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003278 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3279 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280/*
3281 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3282 * that buf has been through inchar(). Returns the actual number of bytes used
3283 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3284 * available.
3285 */
3286 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003287get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 int len = 0;
3290 int i;
3291 char_u c;
3292
3293 for (i = 0; i < num_bytes; i++)
3294 {
3295 if ((c = buf[len++]) == NUL)
3296 return -1;
3297 if (c == K_SPECIAL)
3298 {
3299 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3300 return -1;
3301 if (buf[len++] == (int)KS_ZERO)
3302 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003303 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3304 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3305 if (buf[len++] == (int)KE_CSI)
3306 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003308 else if (c == CSI && buf[len] == KS_EXTRA
3309 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003310 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3311 * the start of a special key, see add_to_input_buf_csi(). */
3312 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 bytes[i] = c;
3314 }
3315 return len;
3316}
3317#endif
3318
3319/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003320 * Check if the new shell size is valid, correct it if it's too small or way
3321 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
3323 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003324check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (Rows < min_rows()) /* need room for one window and command line */
3327 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003328 limit_screen_size();
3329}
3330
3331/*
3332 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3333 */
3334 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003335limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003336{
3337 if (Columns < MIN_COLUMNS)
3338 Columns = MIN_COLUMNS;
3339 else if (Columns > 10000)
3340 Columns = 10000;
3341 if (Rows > 1000)
3342 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343}
3344
Bram Moolenaar86b68352004-12-27 21:59:20 +00003345/*
3346 * Invoked just before the screen structures are going to be (re)allocated.
3347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003349win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 static int old_Rows = 0;
3352 static int old_Columns = 0;
3353
3354 if (old_Rows != Rows || old_Columns != Columns)
3355 ui_new_shellsize();
3356 if (old_Rows != Rows)
3357 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003358 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003359 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003360 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 old_Rows = Rows;
3362 shell_new_rows(); /* update window sizes */
3363 }
3364 if (old_Columns != Columns)
3365 {
3366 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369}
3370
3371/*
3372 * Call this function when the Vim shell has been resized in any way.
3373 * Will obtain the current size and redraw (also when size didn't change).
3374 */
3375 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003376shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 set_shellsize(0, 0, FALSE);
3379}
3380
3381/*
3382 * Check if the shell size changed. Handle a resize.
3383 * When the size didn't change, nothing happens.
3384 */
3385 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003386shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 int old_Rows = Rows;
3389 int old_Columns = Columns;
3390
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003391 if (!exiting
3392#ifdef FEAT_GUI
3393 /* Do not get the size when executing a shell command during
3394 * startup. */
3395 && !gui.starting
3396#endif
3397 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003398 {
3399 (void)ui_get_shellsize();
3400 check_shellsize();
3401 if (old_Rows != Rows || old_Columns != Columns)
3402 shell_resized();
3403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404}
3405
3406/*
3407 * Set size of the Vim shell.
3408 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3409 * window size (this is used for the :win command).
3410 * If 'mustset' is FALSE, we may try to get the real window size and if
3411 * it fails use 'width' and 'height'.
3412 */
3413 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003414set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 static int busy = FALSE;
3417
3418 /*
3419 * Avoid recursiveness, can happen when setting the window size causes
3420 * another window-changed signal.
3421 */
3422 if (busy)
3423 return;
3424
3425 if (width < 0 || height < 0) /* just checking... */
3426 return;
3427
Bram Moolenaara971b822011-09-14 14:43:25 +02003428 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003430 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 State = SETWSIZE;
3432 return;
3433 }
3434
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003435 if (updating_screen)
3436 // resizing while in update_screen() may cause a crash
3437 return;
3438
Bram Moolenaara971b822011-09-14 14:43:25 +02003439 /* curwin->w_buffer can be NULL when we are closing a window and the
3440 * buffer has already been closed and removing a scrollbar causes a resize
3441 * event. Don't resize then, it will happen after entering another buffer.
3442 */
3443 if (curwin->w_buffer == NULL)
3444 return;
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 ++busy;
3447
3448#ifdef AMIGA
3449 out_flush(); /* must do this before mch_get_shellsize() for
3450 some obscure reason */
3451#endif
3452
3453 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3454 {
3455 Rows = height;
3456 Columns = width;
3457 check_shellsize();
3458 ui_set_shellsize(mustset);
3459 }
3460 else
3461 check_shellsize();
3462
3463 /* The window layout used to be adjusted here, but it now happens in
3464 * screenalloc() (also invoked from screenclear()). That is because the
3465 * "busy" check above may skip this, but not screenalloc(). */
3466
3467 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3468 screenclear();
3469 else
3470 screen_start(); /* don't know where cursor is now */
3471
3472 if (starting != NO_SCREEN)
3473 {
3474#ifdef FEAT_TITLE
3475 maketitle();
3476#endif
3477 changed_line_abv_curs();
3478 invalidate_botline();
3479
3480 /*
3481 * We only redraw when it's needed:
3482 * - While at the more prompt or executing an external command, don't
3483 * redraw, but position the cursor.
3484 * - While editing the command line, only redraw that.
3485 * - in Ex mode, don't redraw anything.
3486 * - Otherwise, redraw right now, and position the cursor.
3487 * Always need to call update_screen() or screenalloc(), to make
3488 * sure Rows/Columns and the size of ScreenLines[] is correct!
3489 */
3490 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3491 || exmode_active)
3492 {
3493 screenalloc(FALSE);
3494 repeat_message();
3495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else
3497 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003498 if (curwin->w_p_scb)
3499 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003500 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003501 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003502 update_screen(NOT_VALID);
3503 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003504 }
3505 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003506 {
3507 update_topline();
3508#if defined(FEAT_INS_EXPAND)
3509 if (pum_visible())
3510 {
3511 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003512 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003513 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003514#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003515 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003516 if (redrawing())
3517 setcursor();
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 cursor_on(); /* redrawing may have switched it off */
3521 }
3522 out_flush();
3523 --busy;
3524}
3525
3526/*
3527 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3528 * commands and Ex mode).
3529 */
3530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003531settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533#ifdef FEAT_GUI
3534 /* don't set the term where gvim was started to any mode */
3535 if (gui.in_use)
3536 return;
3537#endif
3538
3539 if (full_screen)
3540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /*
3542 * When returning after calling a shell we want to really set the
3543 * terminal to raw mode, even though we think it already is, because
3544 * the shell program may have reset the terminal mode.
3545 * When we think the terminal is normal, don't try to set it to
3546 * normal again, because that causes problems (logout!) on some
3547 * machines.
3548 */
3549 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3550 {
3551#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003552# ifdef FEAT_GUI
3553 if (!gui.in_use && !gui.starting)
3554# endif
3555 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003556 // May need to check for T_CRV response and termcodes, it
3557 // doesn't work in Cooked mode, an external program may get
3558 // them.
3559 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003560 (void)vpeekc_nomap();
3561 check_for_codes_from_term();
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#endif
3564#ifdef FEAT_MOUSE_TTY
3565 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003566 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003568 if (termcap_active)
3569 {
3570 if (tmode != TMODE_RAW)
3571 out_str(T_BD); // disable bracketed paste mode
3572 else
3573 out_str(T_BE); // enable bracketed paste mode (should
3574 // be before mch_settmode().
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003577 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 cur_tmode = tmode;
3579#ifdef FEAT_MOUSE
3580 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003581 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582#endif
3583 out_flush();
3584 }
3585#ifdef FEAT_TERMRESPONSE
3586 may_req_termresponse();
3587#endif
3588 }
3589}
3590
3591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003592starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
3594 if (full_screen && !termcap_active)
3595 {
3596 out_str(T_TI); /* start termcap mode */
3597 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003598 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 out_flush();
3600 termcap_active = TRUE;
3601 screen_start(); /* don't know where cursor is now */
3602#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003603# ifdef FEAT_GUI
3604 if (!gui.in_use && !gui.starting)
3605# endif
3606 {
3607 may_req_termresponse();
3608 /* Immediately check for a response. If t_Co changes, we don't
3609 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003610 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003611 check_for_codes_from_term();
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#endif
3614 }
3615}
3616
3617 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003618stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 screen_stop_highlight();
3621 reset_cterm_colors();
3622 if (termcap_active)
3623 {
3624#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003625# ifdef FEAT_GUI
3626 if (!gui.in_use && !gui.starting)
3627# endif
3628 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003629 // May need to discard T_CRV, T_U7 or T_RBG response.
3630 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003631 {
3632# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003633 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003634 mch_delay(100L, FALSE);
3635# endif
3636# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003637 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003638 if (exiting)
3639 tcflush(fileno(stdin), TCIFLUSH);
3640# endif
3641 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003642 /* Check for termcodes first, otherwise an external program may
3643 * get them. */
3644 check_for_codes_from_term();
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003647 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 out_str(T_KE); /* stop "keypad transmit" mode */
3649 out_flush();
3650 termcap_active = FALSE;
3651 cursor_on(); /* just in case it is still off */
3652 out_str(T_TE); /* stop termcap mode */
3653 screen_start(); /* don't know where cursor is now */
3654 out_flush();
3655 }
3656}
3657
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003658#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659/*
3660 * Request version string (for xterm) when needed.
3661 * Only do this after switching to raw mode, otherwise the result will be
3662 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003663 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003664 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Only do this after termcap mode has been started, otherwise the codes for
3666 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003667 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3668 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003669 * On Unix only do it when both output and input are a tty (avoid writing
3670 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * The result is caught in check_termcode().
3672 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003673 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003674may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003676 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003677 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003678 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 && *T_CRV != NUL)
3680 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003681 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003683 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /* check for the characters now, otherwise they might be eaten by
3685 * get_keystroke() */
3686 out_flush();
3687 (void)vpeekc_nomap();
3688 }
3689}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003690
Bram Moolenaar9584b312013-03-13 19:29:28 +01003691/*
3692 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003693 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003694 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003695 * If the terminal treats \u25bd as single width, the position is (1, 1),
3696 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003697 * This function has the side effect that changes cursor position, so
3698 * it must be called immediately after entering termcap mode.
3699 */
3700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003701may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003702{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003703 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003704 && can_get_termresponse()
3705 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003706 && *T_U7 != NUL
3707 && !option_was_set((char_u *)"ambiwidth"))
3708 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003709 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003710
Bram Moolenaarafd78262019-05-10 23:10:31 +02003711 LOG_TR(("Sending U7 request"));
3712 /* Do this in the second row. In the first row the returned sequence
3713 * may be CSI 1;2R, which is the same as <S-F3>. */
3714 term_windgoto(1, 0);
3715 buf[mb_char2bytes(0x25bd, buf)] = 0;
3716 out_str(buf);
3717 out_str(T_U7);
3718 termrequest_sent(&u7_status);
3719 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003720
Bram Moolenaarafd78262019-05-10 23:10:31 +02003721 /* This overwrites a few characters on the screen, a redraw is needed
3722 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003723 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003724 term_windgoto(1, 0);
3725 out_str((char_u *)" ");
3726 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003727
Bram Moolenaarafd78262019-05-10 23:10:31 +02003728 /* Need to reset the known cursor position. */
3729 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003730
Bram Moolenaarafd78262019-05-10 23:10:31 +02003731 /* check for the characters now, otherwise they might be eaten by
3732 * get_keystroke() */
3733 out_flush();
3734 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003735 }
3736}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003737
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003738/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003739 * Similar to requesting the version string: Request the terminal background
3740 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003741 */
3742 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003743may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003744{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003745 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003746 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003747 int didit = FALSE;
3748
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003749# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003750 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003751 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003752 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003753 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003754 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003755 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003756 didit = TRUE;
3757 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003758# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003759
3760 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003761 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003762 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003763 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003764 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003765 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003766 didit = TRUE;
3767 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003768
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003769 if (didit)
3770 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003771 /* check for the characters now, otherwise they might be eaten by
3772 * get_keystroke() */
3773 out_flush();
3774 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003775 }
3776 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003777}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003778
Bram Moolenaar2951b772013-07-03 12:45:31 +02003779# ifdef DEBUG_TERMRESPONSE
3780 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003781log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003782{
3783 static FILE *fd_tr = NULL;
3784 static proftime_T start;
3785 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003786 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003787
3788 if (fd_tr == NULL)
3789 {
3790 fd_tr = fopen("termresponse.log", "w");
3791 profile_start(&start);
3792 }
3793 now = start;
3794 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003795 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3796 must_redraw == NOT_VALID ? "NV"
3797 : must_redraw == CLEAR ? "CL" : " ");
3798 va_start(ap, fmt);
3799 vfprintf(fd_tr, fmt, ap);
3800 va_end(ap);
3801 fputc('\n', fd_tr);
3802 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003803}
3804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#endif
3806
3807/*
3808 * Return TRUE when saving and restoring the screen.
3809 */
3810 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003811swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 return (full_screen && *T_TI != NUL);
3814}
3815
Bram Moolenaarecadf432018-03-20 11:17:04 +01003816#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/*
3818 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3819 */
3820 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003821setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823# ifdef FEAT_MOUSE_TTY
3824 int checkfor;
3825# endif
3826
3827# ifdef FEAT_MOUSESHAPE
3828 update_mouseshape(-1);
3829# endif
3830
3831# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3832# ifdef FEAT_GUI
3833 /* In the GUI the mouse is always enabled. */
3834 if (gui.in_use)
3835 return;
3836# endif
3837 /* be quick when mouse is off */
3838 if (*p_mouse == NUL || has_mouse_termcode == 0)
3839 return;
3840
3841 /* don't switch mouse on when not in raw mode (Ex mode) */
3842 if (cur_tmode != TMODE_RAW)
3843 {
3844 mch_setmouse(FALSE);
3845 return;
3846 }
3847
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (VIsual_active)
3849 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003850 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 checkfor = MOUSE_RETURN;
3852 else if (State & INSERT)
3853 checkfor = MOUSE_INSERT;
3854 else if (State & CMDLINE)
3855 checkfor = MOUSE_COMMAND;
3856 else if (State == CONFIRM || State == EXTERNCMD)
3857 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3858 else
3859 checkfor = MOUSE_NORMAL; /* assume normal mode */
3860
3861 if (mouse_has(checkfor))
3862 mch_setmouse(TRUE);
3863 else
3864 mch_setmouse(FALSE);
3865# endif
3866}
3867
3868/*
3869 * Return TRUE if
3870 * - "c" is in 'mouse', or
3871 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3872 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3873 * normal editing mode (not at hit-return message).
3874 */
3875 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003876mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 char_u *p;
3879
3880 for (p = p_mouse; *p; ++p)
3881 switch (*p)
3882 {
3883 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3884 return TRUE;
3885 break;
3886 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3887 return TRUE;
3888 break;
3889 default: if (c == *p) return TRUE; break;
3890 }
3891 return FALSE;
3892}
3893
3894/*
3895 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3896 */
3897 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003898mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899{
3900 return (p_mousem[0] == 'p');
3901}
3902#endif
3903
3904/*
3905 * By outputting the 'cursor very visible' termcap code, for some windowed
3906 * terminals this makes the screen scrolled to the correct position.
3907 * Used when starting Vim or returning from a shell.
3908 */
3909 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003910scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003912 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
3914 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003915 out_str(T_CVS);
3916 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918}
3919
3920static int cursor_is_off = FALSE;
3921
3922/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003923 * Enable the cursor without checking if it's already enabled.
3924 */
3925 void
3926cursor_on_force(void)
3927{
3928 out_str(T_VE);
3929 cursor_is_off = FALSE;
3930}
3931
3932/*
3933 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 */
3935 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003936cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003939 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941
3942/*
3943 * Disable the cursor.
3944 */
3945 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003946cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003948 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003950 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 cursor_is_off = TRUE;
3952 }
3953}
3954
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003955#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003957 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003958 */
3959 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003960term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003961{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003962 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003963 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003964
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003965 /* Only do something when redrawing the screen and we can restore the
3966 * mode. */
3967 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003968 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003969# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003970 if (forced && initial_cursor_shape > 0)
3971 /* Restore to initial values. */
3972 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003973# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003975 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003976
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003977 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003978 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003979 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003980 {
3981 if (*T_CSR != NUL)
3982 p = T_CSR; /* Replace mode cursor */
3983 else
3984 p = T_CSI; /* fall back to Insert mode cursor */
3985 if (*p != NUL)
3986 {
3987 out_str(p);
3988 showing_mode = REPLACE;
3989 }
3990 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003991 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003992 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003993 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003994 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003995 {
3996 out_str(T_CSI); /* Insert mode cursor */
3997 showing_mode = INSERT;
3998 }
3999 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004000 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004001 {
4002 out_str(T_CEI); /* non-Insert mode cursor */
4003 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004004 }
4005}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004006
4007# if defined(FEAT_TERMINAL) || defined(PROTO)
4008 void
4009term_cursor_color(char_u *color)
4010{
4011 if (*T_CSC != NUL)
4012 {
4013 out_str(T_CSC); /* set cursor color start */
4014 out_str_nf(color);
4015 out_str(T_CEC); /* set cursor color end */
4016 out_flush();
4017 }
4018}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004019# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004020
Bram Moolenaar4db25542017-08-28 22:43:05 +02004021 int
4022blink_state_is_inverted()
4023{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004024#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004025 return rbm_status.tr_progress == STATUS_GOT
4026 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004027 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004028#else
4029 return FALSE;
4030#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004031}
4032
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004033/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004034 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004035 */
4036 void
4037term_cursor_shape(int shape, int blink)
4038{
4039 if (*T_CSH != NUL)
4040 {
4041 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4042 out_flush();
4043 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004044 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004045 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004046 int do_blink = blink;
4047
4048 /* t_SH is empty: try setting just the blink state.
4049 * The blink flags are XORed together, if the initial blinking from
4050 * style and shape differs, we need to invert the flag here. */
4051 if (blink_state_is_inverted())
4052 do_blink = !blink;
4053
4054 if (do_blink && *T_VS != NUL)
4055 {
4056 out_str(T_VS);
4057 out_flush();
4058 }
4059 else if (!do_blink && *T_CVS != NUL)
4060 {
4061 out_str(T_CVS);
4062 out_flush();
4063 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004064 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004065}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004066#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004067
4068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * Set scrolling region for window 'wp'.
4070 * The region starts 'off' lines from the start of the window.
4071 * Also set the vertical scroll region for a vertically split window. Always
4072 * the full width of the window, excluding the vertical separator.
4073 */
4074 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004075scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4078 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004080 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4081 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 screen_start(); /* don't know where cursor is now */
4083}
4084
4085/*
4086 * Reset scrolling region to the whole screen.
4087 */
4088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004089scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
4091 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (*T_CSV != NUL)
4093 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 screen_start(); /* don't know where cursor is now */
4095}
4096
4097
4098/*
4099 * List of terminal codes that are currently recognized.
4100 */
4101
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004102static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
4104 char_u name[2]; /* termcap name of entry */
4105 char_u *code; /* terminal code (in allocated memory) */
4106 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004107 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108} *termcodes = NULL;
4109
4110static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4111static int tc_len = 0; /* current number of entries in termcodes[] */
4112
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004113static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004116clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 while (tc_len > 0)
4119 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004120 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 tc_max_len = 0;
4122
4123#ifdef HAVE_TGETENT
4124 BC = (char *)empty_option;
4125 UP = (char *)empty_option;
4126 PC = NUL; /* set pad character to NUL */
4127 ospeed = 0;
4128#endif
4129
4130 need_gather = TRUE; /* need to fill termleader[] */
4131}
4132
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133#define ATC_FROM_TERM 55
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135/*
4136 * Add a new entry to the list of terminal codes.
4137 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004138 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4139 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 */
4141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004142add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 struct termcode *new_tc;
4145 int i, j;
4146 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004147 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 if (string == NULL || *string == NUL)
4150 {
4151 del_termcode(name);
4152 return;
4153 }
4154
Bram Moolenaar4f974752019-02-17 17:44:42 +01004155#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02004156 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4157#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004158# ifdef VIMDLL
4159 if (!gui.in_use)
4160 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4161 else
4162# endif
4163 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (s == NULL)
4166 return;
4167
4168 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004169 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004171 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 s[0] = term_7to8bit(string);
4173 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004174
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004175#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4176# ifdef VIMDLL
4177 if (!gui.in_use)
4178# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004179 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004180 if (s[0] == K_NUL)
4181 {
4182 STRMOVE(s + 1, s);
4183 s[1] = 3;
4184 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004185 }
4186#endif
4187
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004188 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
4190 need_gather = TRUE; /* need to fill termleader[] */
4191
4192 /*
4193 * need to make space for more entries
4194 */
4195 if (tc_len == tc_max_len)
4196 {
4197 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004198 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 if (new_tc == NULL)
4200 {
4201 tc_max_len -= 20;
4202 return;
4203 }
4204 for (i = 0; i < tc_len; ++i)
4205 new_tc[i] = termcodes[i];
4206 vim_free(termcodes);
4207 termcodes = new_tc;
4208 }
4209
4210 /*
4211 * Look for existing entry with the same name, it is replaced.
4212 * Look for an existing entry that is alphabetical higher, the new entry
4213 * is inserted in front of it.
4214 */
4215 for (i = 0; i < tc_len; ++i)
4216 {
4217 if (termcodes[i].name[0] < name[0])
4218 continue;
4219 if (termcodes[i].name[0] == name[0])
4220 {
4221 if (termcodes[i].name[1] < name[1])
4222 continue;
4223 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 */
4226 if (termcodes[i].name[1] == name[1])
4227 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004228 if (flags == ATC_FROM_TERM && (j = termcode_star(
4229 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 /* Don't replace ESC[123;*X or ESC O*X with another when
4232 * invoked from got_code_from_term(). */
4233 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 && s[len - 1]
4236 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004238 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004239 vim_free(s);
4240 return;
4241 }
4242 }
4243 else
4244 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004246 vim_free(termcodes[i].code);
4247 --tc_len;
4248 break;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252 /*
4253 * Found alphabetical larger entry, move rest to insert new entry
4254 */
4255 for (j = tc_len; j > i; --j)
4256 termcodes[j] = termcodes[j - 1];
4257 break;
4258 }
4259
4260 termcodes[i].name[0] = name[0];
4261 termcodes[i].name[1] = name[1];
4262 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004264
4265 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4266 * accept modifiers. */
4267 termcodes[i].modlen = 0;
4268 j = termcode_star(s, len);
4269 if (j > 0)
4270 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 ++tc_len;
4272}
4273
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004275 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004277 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 */
4279 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004280termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004281{
4282 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4283 if (len >= 3 && code[len - 2] == '*')
4284 {
4285 if (len >= 5 && code[len - 3] == ';')
4286 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004287 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004288 return 1;
4289 }
4290 return 0;
4291}
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004294find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 int i;
4297
4298 for (i = 0; i < tc_len; ++i)
4299 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4300 return termcodes[i].code;
4301 return NULL;
4302}
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004305get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
4307 if (i >= tc_len)
4308 return NULL;
4309 return &termcodes[i].name[0];
4310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004312 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004313del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
4315 int i;
4316
4317 if (termcodes == NULL) /* nothing there yet */
4318 return;
4319
4320 need_gather = TRUE; /* need to fill termleader[] */
4321
4322 for (i = 0; i < tc_len; ++i)
4323 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4324 {
4325 del_termcode_idx(i);
4326 return;
4327 }
4328 /* not found. Give error message? */
4329}
4330
4331 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004332del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
4334 int i;
4335
4336 vim_free(termcodes[idx].code);
4337 --tc_len;
4338 for (i = idx; i < tc_len; ++i)
4339 termcodes[i] = termcodes[i + 1];
4340}
4341
4342#ifdef FEAT_TERMRESPONSE
4343/*
4344 * Called when detected that the terminal sends 8-bit codes.
4345 * Convert all 7-bit codes to their 8-bit equivalent.
4346 */
4347 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004348switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 int i;
4351 int c;
4352
4353 /* Only need to do something when not already using 8-bit codes. */
4354 if (!term_is_8bit(T_NAME))
4355 {
4356 for (i = 0; i < tc_len; ++i)
4357 {
4358 c = term_7to8bit(termcodes[i].code);
4359 if (c != 0)
4360 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004361 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 termcodes[i].code[0] = c;
4363 }
4364 }
4365 need_gather = TRUE; /* need to fill termleader[] */
4366 }
4367 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004368 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370#endif
4371
4372#ifdef CHECK_DOUBLE_CLICK
4373static linenr_T orig_topline = 0;
4374# ifdef FEAT_DIFF
4375static int orig_topfill = 0;
4376# endif
4377#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004378#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379/*
4380 * Checking for double clicks ourselves.
4381 * "orig_topline" is used to avoid detecting a double-click when the window
4382 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4383 */
4384/*
4385 * Set orig_topline. Used when jumping to another window, so that a double
4386 * click still works.
4387 */
4388 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004389set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 orig_topline = wp->w_topline;
4392# ifdef FEAT_DIFF
4393 orig_topfill = wp->w_topfill;
4394# endif
4395}
4396#endif
4397
4398/*
4399 * Check if typebuf.tb_buf[] contains a terminal key code.
4400 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4401 * + max_offset].
4402 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004403 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 * With a match, the match is removed, the replacement code is inserted in
4405 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4406 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004407 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4408 * "buflen" is then the length of the string in buf[] and is updated for
4409 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 */
4411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004412check_termcode(
4413 int max_offset,
4414 char_u *buf,
4415 int bufsize,
4416 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
4418 char_u *tp;
4419 char_u *p;
4420 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004421 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004423 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 int offset;
4425 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004427 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004428 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 int new_slen;
4430 int extra;
4431 char_u string[MAX_KEY_CODE_LEN + 1];
4432 int i, j;
4433 int idx = 0;
4434#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004435# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4436 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 char_u bytes[6];
4438 int num_bytes;
4439# endif
4440 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 int is_click, is_drag;
4442 int wheel_code = 0;
4443 int current_button;
4444 static int held_button = MOUSE_RELEASE;
4445 static int orig_num_clicks = 1;
4446 static int orig_mouse_code = 0x0;
4447# ifdef CHECK_DOUBLE_CLICK
4448 static int orig_mouse_col = 0;
4449 static int orig_mouse_row = 0;
4450 static struct timeval orig_mouse_time = {0, 0};
4451 /* time of previous mouse click */
4452 struct timeval mouse_time; /* time of current mouse click */
4453 long timediff; /* elapsed time in msec */
4454# endif
4455#endif
4456 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4459
4460 /*
4461 * Speed up the checks for terminal codes by gathering all first bytes
4462 * used in termleader[]. Often this is just a single <Esc>.
4463 */
4464 if (need_gather)
4465 gather_termleader();
4466
4467 /*
4468 * Check at several positions in typebuf.tb_buf[], to catch something like
4469 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4470 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004471 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 * This is used often, KEEP IT FAST!
4473 */
4474 for (offset = 0; offset < max_offset; ++offset)
4475 {
4476 if (buf == NULL)
4477 {
4478 if (offset >= typebuf.tb_len)
4479 break;
4480 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4481 len = typebuf.tb_len - offset; /* length of the input */
4482 }
4483 else
4484 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004485 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004488 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490
4491 /*
4492 * Don't check characters after K_SPECIAL, those are already
4493 * translated terminal chars (avoid translating ~@^Hx).
4494 */
4495 if (*tp == K_SPECIAL)
4496 {
4497 offset += 2; /* there are always 2 extra characters */
4498 continue;
4499 }
4500
4501 /*
4502 * Skip this position if the character does not appear as the first
4503 * character in term_strings. This speeds up a lot, since most
4504 * termcodes start with the same character (ESC or CSI).
4505 */
4506 i = *tp;
4507 for (p = termleader; *p && *p != i; ++p)
4508 ;
4509 if (*p == NUL)
4510 continue;
4511
4512 /*
4513 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4514 * are in Insert mode.
4515 */
4516 if (*tp == ESC && !p_ek && (State & INSERT))
4517 continue;
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004520 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004521 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
4523#ifdef FEAT_GUI
4524 if (gui.in_use)
4525 {
4526 /*
4527 * GUI special key codes are all of the form [CSI xx].
4528 */
4529 if (*tp == CSI) /* Special key from GUI */
4530 {
4531 if (len < 3)
4532 return -1; /* Shouldn't happen */
4533 slen = 3;
4534 key_name[0] = tp[1];
4535 key_name[1] = tp[2];
4536 }
4537 }
4538 else
4539#endif /* FEAT_GUI */
4540 {
4541 for (idx = 0; idx < tc_len; ++idx)
4542 {
4543 /*
4544 * Ignore the entry if we are not at the start of
4545 * typebuf.tb_buf[]
4546 * and there are not enough characters to make a match.
4547 * But only when the 'K' flag is in 'cpoptions'.
4548 */
4549 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004550 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (cpo_koffset && offset && len < slen)
4552 continue;
4553 if (STRNCMP(termcodes[idx].code, tp,
4554 (size_t)(slen > len ? len : slen)) == 0)
4555 {
4556 if (len < slen) /* got a partial sequence */
4557 return -1; /* need to get more chars */
4558
4559 /*
4560 * When found a keypad key, check if there is another key
4561 * that matches and use that one. This makes <Home> to be
4562 * found instead of <kHome> when they produce the same
4563 * key code.
4564 */
4565 if (termcodes[idx].name[0] == 'K'
4566 && VIM_ISDIGIT(termcodes[idx].name[1]))
4567 {
4568 for (j = idx + 1; j < tc_len; ++j)
4569 if (termcodes[j].len == slen &&
4570 STRNCMP(termcodes[idx].code,
4571 termcodes[j].code, slen) == 0)
4572 {
4573 idx = j;
4574 break;
4575 }
4576 }
4577
4578 key_name[0] = termcodes[idx].name[0];
4579 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004582
4583 /*
4584 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004585 * <Esc>[123;*X (modslen == slen - 3)
4586 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4587 * When there is a modifier the * matches a number.
4588 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004589 */
4590 if (termcodes[idx].modlen > 0)
4591 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004592 modslen = termcodes[idx].modlen;
4593 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004594 continue;
4595 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004596 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004597 {
4598 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004599
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004601 return -1; /* need to get more chars */
4602
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004603 if (tp[modslen] == termcodes[idx].code[slen - 1])
4604 slen = modslen + 1; /* no modifiers */
4605 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004606 continue; /* no match */
4607 else
4608 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004609 // Skip over the digits, the final char must
4610 // follow. URXVT can use a negative value, thus
4611 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004612 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004613 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004614 ;
4615 ++j;
4616 if (len < j) /* got a partial sequence */
4617 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004618 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4619 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004620
Bram Moolenaara529ce02017-06-22 22:37:57 +02004621 modifiers_start = tp + slen - 2;
4622
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004623 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004624 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004625 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004626 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004627 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004629 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004631 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004632 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004633
4634 slen = j;
4635 }
4636 key_name[0] = termcodes[idx].name[0];
4637 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004638 break;
4639 }
4640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 }
4643
4644#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004645 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004646 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004647 * detecting the start of these mouse codes they might as well be
4648 * another key code or terminal response. */
4649# ifdef FEAT_MOUSE_DEC
4650 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004651# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004652# ifdef FEAT_MOUSE_PTERM
4653 || key_name[0] == KS_PTERM_MOUSE
4654# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004657 /* Check for some responses from the terminal starting with
4658 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004659 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004660 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004661 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004662 * Also eat other possible responses to t_RV, rxvt returns
4663 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4664 * mrxvt has been reported to have "+" in the version. Assume
4665 * the escape sequence ends with a letter or one of "{|}~".
4666 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004667 * - Cursor position report: <Esc>[{row};{col}R
4668 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004669 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004670 *
4671 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004672 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004673 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004674
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004675 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004676 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004677 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004678 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004680 int col = 0;
4681 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004682 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004685 for (i = 2 + (tp[0] != CSI); i < len
4686 && !(tp[i] >= '{' && tp[i] <= '~')
4687 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004688 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004689 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004690 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004691 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004694 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004695 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004696 return -1;
4697 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004698 if (extra > 0)
4699 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004700
4701 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4702 * u7_status is not "sent", it may be from a previous Vim that
4703 * just exited. But not for <S-F3>, it sends something
4704 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004705 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004706 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004707 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004708 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004709 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004710
Bram Moolenaarb255b902018-04-24 21:40:10 +02004711 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004712 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004713 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004714 if (col == 2)
4715 aw = "single";
4716 else if (col == 3)
4717 aw = "double";
4718 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4719 {
4720 /* Setting the option causes a screen redraw. Do
4721 * that right away if possible, keeping any
4722 * messages. */
4723 set_option_value((char_u *)"ambw", 0L,
4724 (char_u *)aw, 0);
4725# ifdef DEBUG_TERMRESPONSE
4726 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004727 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004728
Bram Moolenaarb255b902018-04-24 21:40:10 +02004729 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004730 }
4731# else
4732 redraw_asap(CLEAR);
4733# endif
4734 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004735 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004736 key_name[0] = (int)KS_EXTRA;
4737 key_name[1] = (int)KE_IGNORE;
4738 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004739# ifdef FEAT_EVAL
4740 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4741# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004744 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4745 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004747 int version = col;
4748
Bram Moolenaarb255b902018-04-24 21:40:10 +02004749 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004750 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004751 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 /* If this code starts with CSI, you can bet that the
4754 * terminal uses 8-bit codes. */
4755 if (tp[0] == CSI)
4756 switch_to_8bit();
4757
4758 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004759 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 * Ignore it for when the user has set 'term' to xterm,
4761 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004762 if (version > 20000)
4763 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004765 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004767 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004768 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004769 int is_mintty = FALSE;
4770
4771 // mintty 2.9.5 sends 77;20905;0c.
4772 // (77 is ASCII 'M' for mintty.)
4773 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4774 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004777 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004779 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 check_for_codes = TRUE;
4781 need_gather = TRUE;
4782 req_codes_from_term();
4783 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004784
4785 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004786 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004787 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004788 {
4789 /* If run from Vim $COLORS is set to the number of
4790 * colors the terminal supports. Otherwise assume
4791 * 256, libvterm supports even more. */
4792 if (mch_getenv((char_u *)"COLORS") == NULL)
4793 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004794 /* Libvterm can handle SGR mouse reporting. */
4795 if (!option_was_set((char_u *)"ttym"))
4796 set_option_value((char_u *)"ttym", 0L,
4797 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004798 }
4799
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004800 if (version == 95)
4801 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004802 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004803 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4804 {
4805 is_not_xterm = TRUE;
4806 is_mac_terminal = TRUE;
4807 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004808 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004809 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4810 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004811 // old iTerm2 sends 0;95;
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004812 else if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004813 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004814 }
4815
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004816 /* Only set 'ttymouse' automatically if it was not set
4817 * by the user already. */
4818 if (!option_was_set((char_u *)"ttym"))
4819 {
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004820 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004821 * Terminal.app, iTerm2 and mintty. */
4822 if (version >= 277 || is_iterm2 || is_mac_terminal
4823 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004824 set_option_value((char_u *)"ttym", 0L,
4825 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004826 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004827 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004828 set_option_value((char_u *)"ttym", 0L,
4829 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004830 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004831
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004832 /* Detect terminals that set $TERM to something like
4833 * "xterm-256colors" but are not fully xterm
4834 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004835
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004836 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004837 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004838 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004839 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004840 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004841 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004842 is_not_xterm = TRUE;
4843
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004844 /* PuTTY sends 0;136;0
4845 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004846 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004847 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004848 is_not_xterm = TRUE;
4849
4850 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004851 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004852 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004853 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004854
Bram Moolenaar668324e2018-06-30 17:09:26 +02004855 // Xterm first responded to this request at patch level
4856 // 95, so assume anything below 95 is not xterm.
4857 if (version < 95)
4858 is_not_xterm = TRUE;
4859
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004860 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004861 * set. Only supported properly by xterm since version
4862 * 279 (otherwise it returns 0x18).
4863 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004864 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004865 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004866 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004867 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004868 && *T_CSH != NUL
4869 && *T_CRS != NUL)
4870 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004871 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004872 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004873 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004874 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004875 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004876
4877 /* Only request the cursor blink mode if t_RC set. Not
4878 * for Gnome terminal, it can't handle t_RC, it
4879 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004880 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004881 && !is_not_xterm
4882 && *T_CRC != NUL)
4883 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004884 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004885 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004886 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004887 need_flush = TRUE;
4888 }
4889
4890 if (need_flush)
4891 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004893 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004895 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 apply_autocmds(EVENT_TERMRESPONSE,
4898 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 key_name[0] = (int)KS_EXTRA;
4900 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004902
Bram Moolenaar4db25542017-08-28 22:43:05 +02004903 /* Check blinking cursor from xterm:
4904 * {lead}?12;1$y set
4905 * {lead}?12;2$y not set
4906 *
4907 * {lead} can be <Esc>[ or CSI
4908 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004909 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004910 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4911 && i == j + 6
4912 && tp[j + 1] == '1'
4913 && tp[j + 2] == '2'
4914 && tp[j + 3] == ';'
4915 && tp[i - 1] == '$'
4916 && tp[i] == 'y')
4917 {
4918 initial_cursor_blink = (tp[j + 4] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004919 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004920 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004921 key_name[0] = (int)KS_EXTRA;
4922 key_name[1] = (int)KE_IGNORE;
4923 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004924# ifdef FEAT_EVAL
4925 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4926# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004927 }
4928
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004929 /*
4930 * Check for a window position response from the terminal:
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004931 * {lead}3;{x};{y}t
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004932 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004933 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004934 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4935 || (len >= 3 && tp[0] == CSI))
4936 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4937 && tp[j + 1] == ';')
4938 {
4939 j += 2;
4940 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4941 ;
4942 if (i < len && tp[i] == ';')
4943 {
4944 winpos_x = atoi((char *)tp + j);
4945 j = i + 1;
4946 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4947 ;
4948 if (i < len && tp[i] == 't')
4949 {
4950 winpos_y = atoi((char *)tp + j);
4951 /* got finished code: consume it */
4952 key_name[0] = (int)KS_EXTRA;
4953 key_name[1] = (int)KE_IGNORE;
4954 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004955
4956 if (--did_request_winpos <= 0)
Bram Moolenaarafd78262019-05-10 23:10:31 +02004957 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004958 }
4959 }
4960 if (i == len)
4961 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004962 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004963 return -1;
4964 }
4965 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004966 }
4967
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004968 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004969 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004970 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004971 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004972 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004973 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004974 * {lead} can be <Esc>] or OSC
4975 * {tail} can be '\007', <Esc>\ or STERM.
4976 *
4977 * Consume any code that starts with "{lead}11;", it's also
4978 * possible that "rgba" is following.
4979 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004980 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004981 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4982 || tp[0] == OSC))
4983 {
4984 j = 1 + (tp[0] == ESC);
4985 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004986 || (argp[1] != '1' && argp[1] != '0')
4987 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004988 i = 0; /* no match */
4989 else
4990 for (i = j; i < len; ++i)
4991 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4992 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004993 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004994 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004995 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4996 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004997
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004998 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004999 && (is_4digit
5000 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005001 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02005002 char_u *tp_r = tp + j + 7;
5003 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5004 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005005# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02005006 int rval, gval, bval;
5007
Bram Moolenaar32e19772019-06-05 22:57:04 +02005008 rval = hexhex2nr(tp_r);
5009 gval = hexhex2nr(tp_b);
5010 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005011# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005012 if (is_bg)
5013 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02005014 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5015 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005016
Bram Moolenaarb255b902018-04-24 21:40:10 +02005017 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005018 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005019# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005020 bg_r = rval;
5021 bg_g = gval;
5022 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005023# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005024 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02005025 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005026 {
5027 /* value differs, apply it */
5028 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02005029 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005030 reset_option_was_set((char_u *)"bg");
5031 redraw_asap(CLEAR);
5032 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005033 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005034# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005035 else
5036 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005037 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005038 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005039 fg_r = rval;
5040 fg_g = gval;
5041 fg_b = bval;
5042 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005043# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005044 }
5045
5046 /* got finished code: consume it */
5047 key_name[0] = (int)KS_EXTRA;
5048 key_name[1] = (int)KE_IGNORE;
5049 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005050# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005051 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5052 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005053# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005054 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005055 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005056 if (i == len)
5057 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005058 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005059 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005063 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005064 * {lead}{flag}+r<hex bytes><{tail}
5065 *
5066 * {lead} can be <Esc>P or DCS
5067 * {flag} can be '0' or '1'
5068 * {tail} can be Esc>\ or STERM
5069 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005070 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01005071 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005072 *
5073 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02005074 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005075 *
5076 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005077 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02005078 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005079 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 || tp[0] == DCS))
5081 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005082 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005083 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02005084 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005085 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005086 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005087 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005088 // key code response
5089 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005091 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5092 || tp[i] == STERM)
5093 {
5094 if (i - j >= 3)
5095 got_code_from_term(tp + j, i);
5096 key_name[0] = (int)KS_EXTRA;
5097 key_name[1] = (int)KE_IGNORE;
5098 slen = i + 1 + (tp[i] == ESC);
5099 break;
5100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005102 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005103 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005104 // Probably the cursor shape response. Make sure that "i"
5105 // is equal to "len" when there are not sufficient
5106 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005107 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005108 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005109 if (i - j == 3 && !isdigit(tp[i]))
5110 break;
5111 if (i - j == 4 && tp[i] != ' ')
5112 break;
5113 if (i - j == 5 && tp[i] != 'q')
5114 break;
5115 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5116 break;
5117 if ((i - j == 6 && tp[i] == STERM)
5118 || (i - j == 7 && tp[i] == '\\'))
5119 {
5120 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005121
Bram Moolenaar66761db2019-06-05 22:07:51 +02005122 // 0, 1 = block blink, 2 = block
5123 // 3 = underline blink, 4 = underline
5124 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005125 number = number == 0 ? 1 : number;
5126 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005127 // The blink flag is actually inverted, compared to
5128 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005129 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005130 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005131 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005132 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005133
Bram Moolenaar590ec872018-03-02 20:58:42 +01005134 key_name[0] = (int)KS_EXTRA;
5135 key_name[1] = (int)KE_IGNORE;
5136 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005137# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005138 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005139# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005140 break;
5141 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005142 }
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005146 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005147 /* These codes arrive many together, each code can be
5148 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005149 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005150 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 }
5154#endif
5155
5156 if (key_name[0] == NUL)
5157 continue; /* No match at this position, try next one */
5158
5159 /* We only get here when we have a complete termcode match */
5160
5161#ifdef FEAT_MOUSE
5162# ifdef FEAT_GUI
5163 /*
5164 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5165 * so that we know which window to scroll later.
5166 */
5167 if (gui.in_use
5168 && key_name[0] == (int)KS_EXTRA
5169 && (key_name[1] == (int)KE_X1MOUSE
5170 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005171 || key_name[1] == (int)KE_MOUSELEFT
5172 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 || key_name[1] == (int)KE_MOUSEDOWN
5174 || key_name[1] == (int)KE_MOUSEUP))
5175 {
5176 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5177 if (num_bytes == -1) /* not enough coordinates */
5178 return -1;
5179 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5180 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5181 slen += num_bytes;
5182 }
5183 else
5184# endif
5185 /*
5186 * If it is a mouse click, get the coordinates.
5187 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005188 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005189# ifdef FEAT_MOUSE_GPM
5190 || key_name[0] == KS_GPM_MOUSE
5191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005193 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194# endif
5195# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005196 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197# endif
5198# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005199 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200# endif
5201# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005202 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005204# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005205 || key_name[0] == KS_URXVT_MOUSE
5206# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005207 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005208 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
5210 is_click = is_drag = FALSE;
5211
Bram Moolenaar864207d2008-06-24 22:14:38 +00005212# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5213 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005214 if (key_name[0] == KS_MOUSE
5215# ifdef FEAT_MOUSE_GPM
5216 || key_name[0] == KS_GPM_MOUSE
5217# endif
5218 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
5220 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005221 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 * s == encoded button state:
5223 * 0x20 = left button down
5224 * 0x21 = middle button down
5225 * 0x22 = right button down
5226 * 0x23 = any button release
5227 * 0x60 = button 4 down (scroll wheel down)
5228 * 0x61 = button 5 down (scroll wheel up)
5229 * add 0x04 for SHIFT
5230 * add 0x08 for ALT
5231 * add 0x10 for CTRL
5232 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005233 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5234 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 * c == column + ' ' + 1 == column + 33
5236 * r == row + ' ' + 1 == row + 33
5237 *
5238 * The coordinates are passed on through global variables.
5239 * Ugly, but this avoids trouble with mouse clicks at an
5240 * unexpected moment and allows for mapping them.
5241 */
5242 for (;;)
5243 {
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005244# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (gui.in_use)
5246 {
5247 /* GUI uses more bits for columns > 223 */
5248 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5249 if (num_bytes == -1) /* not enough coordinates */
5250 return -1;
5251 mouse_code = bytes[0];
5252 mouse_col = 128 * (bytes[1] - ' ' - 1)
5253 + bytes[2] - ' ' - 1;
5254 mouse_row = 128 * (bytes[3] - ' ' - 1)
5255 + bytes[4] - ' ' - 1;
5256 }
5257 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5261 if (num_bytes == -1) /* not enough coordinates */
5262 return -1;
5263 mouse_code = bytes[0];
5264 mouse_col = bytes[1] - ' ' - 1;
5265 mouse_row = bytes[2] - ' ' - 1;
5266 }
5267 slen += num_bytes;
5268
5269 /* If the following bytes is also a mouse code and it has
5270 * the same code, dump this one and get the next. This
5271 * makes dragging a whole lot faster. */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005272# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (gui.in_use)
5274 j = 3;
5275 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 j = termcodes[idx].len;
5278 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5279 && tp[slen + j] == mouse_code
5280 && tp[slen + j + 1] != NUL
5281 && tp[slen + j + 2] != NUL
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005282# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 && (!gui.in_use
5284 || (tp[slen + j + 3] != NUL
5285 && tp[slen + j + 4] != NUL))
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 )
5288 slen += j;
5289 else
5290 break;
5291 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005294 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005295 || key_name[0] == KS_SGR_MOUSE
5296 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005297 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005298 /* URXVT 1015 mouse reporting mode:
5299 * Almost identical to xterm mouse mode, except the values
5300 * are decimal instead of bytes.
5301 *
5302 * \033[%d;%d;%dM
5303 * ^-- row
5304 * ^----- column
5305 * ^-------- code
5306 *
5307 * SGR 1006 mouse reporting mode:
5308 * Almost identical to xterm mouse mode, except the values
5309 * are decimal instead of bytes.
5310 *
5311 * \033[<%d;%d;%dM
5312 * ^-- row
5313 * ^----- column
5314 * ^-------- code
5315 *
5316 * \033[<%d;%d;%dm : mouse release event
5317 * ^-- row
5318 * ^----- column
5319 * ^-------- code
5320 */
5321 p = modifiers_start;
5322 if (p == NULL)
5323 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005324
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005325 mouse_code = getdigits(&p);
5326 if (*p++ != ';')
5327 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005328
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005329 /* when mouse reporting is SGR, add 32 to mouse code */
5330 if (key_name[0] == KS_SGR_MOUSE
5331 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5332 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005333
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005334 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5335 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005336
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005337 mouse_col = getdigits(&p) - 1;
5338 if (*p++ != ';')
5339 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005340
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005341 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005342
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005343 /* The modifiers were the mouse coordinates, not the
5344 * modifier keys (alt/shift/ctrl/meta) state. */
5345 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005346 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005347
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005348 if (key_name[0] == KS_MOUSE
5349# ifdef FEAT_MOUSE_GPM
5350 || key_name[0] == KS_GPM_MOUSE
5351# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005352# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005353 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005354# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005355 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005356 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005357 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005358# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 /*
5360 * Handle mouse events.
5361 * Recognize the xterm mouse wheel, but not in the GUI, the
5362 * Linux console with GPM and the MS-DOS or Win32 console
5363 * (multi-clicks use >= 0x60).
5364 */
5365 if (mouse_code >= MOUSEWHEEL_LOW
5366# ifdef FEAT_GUI
5367 && !gui.in_use
5368# endif
5369# ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005370 && key_name[0] != KS_GPM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371# endif
5372 )
5373 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005374# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5375 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5376 /* mouse-move event, using MOUSE_DRAG works */
5377 mouse_code = MOUSE_DRAG;
5378 else
5379# endif
5380 /* Keep the mouse_code before it's changed, so that we
5381 * remember that it was a mouse wheel click. */
5382 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384# ifdef FEAT_MOUSE_XTERM
5385 else if (held_button == MOUSE_RELEASE
5386# ifdef FEAT_GUI
5387 && !gui.in_use
5388# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005389 && (mouse_code == 0x23 || mouse_code == 0x24
5390 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005392 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5393 * And 0x40 and 0x41 are used by some xterm emulator. */
5394 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5395 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397# endif
5398
5399# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5400 else if (use_xterm_mouse() > 1)
5401 {
5402 if (mouse_code & MOUSE_DRAG_XTERM)
5403 mouse_code |= MOUSE_DRAG;
5404 }
5405# endif
5406# ifdef FEAT_XCLIPBOARD
5407 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5408 {
5409 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5410 stop_xterm_trace();
5411 else
5412 start_xterm_trace(mouse_code);
5413 }
5414# endif
5415# endif
5416 }
5417# endif /* !UNIX || FEAT_MOUSE_XTERM */
5418# ifdef FEAT_MOUSE_NET
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005419 if (key_name[0] == KS_NETTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 int mc, mr;
5422
5423 /* expect a rather limited sequence like: balancing {
5424 * \033}6,45\r
5425 * '6' is the row, 45 is the column
5426 */
5427 p = tp + slen;
5428 mr = getdigits(&p);
5429 if (*p++ != ',')
5430 return -1;
5431 mc = getdigits(&p);
5432 if (*p++ != '\r')
5433 return -1;
5434
5435 mouse_col = mc - 1;
5436 mouse_row = mr - 1;
5437 mouse_code = MOUSE_LEFT;
5438 slen += (int)(p - (tp + slen));
5439 }
5440# endif /* FEAT_MOUSE_NET */
5441# ifdef FEAT_MOUSE_JSB
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005442 if (key_name[0] == KS_JSBTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
5444 int mult, val, iter, button, status;
5445
5446 /* JSBTERM Input Model
5447 * \033[0~zw uniq escape sequence
5448 * (L-x) Left button pressed - not pressed x not reporting
5449 * (M-x) Middle button pressed - not pressed x not reporting
5450 * (R-x) Right button pressed - not pressed x not reporting
5451 * (SDmdu) Single , Double click, m mouse move d button down
5452 * u button up
5453 * ### X cursor position padded to 3 digits
5454 * ### Y cursor position padded to 3 digits
5455 * (s-x) SHIFT key pressed - not pressed x not reporting
5456 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005457 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 */
5459
5460 p = tp + slen;
5461 button = mouse_code = 0;
5462 switch (*p++)
5463 {
5464 case 'L': button = 1; break;
5465 case '-': break;
5466 case 'x': break; /* ignore sequence */
5467 default: return -1; /* Unknown Result */
5468 }
5469 switch (*p++)
5470 {
5471 case 'M': button |= 2; break;
5472 case '-': break;
5473 case 'x': break; /* ignore sequence */
5474 default: return -1; /* Unknown Result */
5475 }
5476 switch (*p++)
5477 {
5478 case 'R': button |= 4; break;
5479 case '-': break;
5480 case 'x': break; /* ignore sequence */
5481 default: return -1; /* Unknown Result */
5482 }
5483 status = *p++;
5484 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5485 mult /= 10, p++)
5486 if (*p >= '0' && *p <= '9')
5487 val += (*p - '0') * mult;
5488 else
5489 return -1;
5490 mouse_col = val;
5491 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5492 mult /= 10, p++)
5493 if (*p >= '0' && *p <= '9')
5494 val += (*p - '0') * mult;
5495 else
5496 return -1;
5497 mouse_row = val;
5498 switch (*p++)
5499 {
5500 case 's': button |= 8; break; /* SHIFT key Pressed */
5501 case '-': break; /* Not Pressed */
5502 case 'x': break; /* Not Reporting */
5503 default: return -1; /* Unknown Result */
5504 }
5505 switch (*p++)
5506 {
5507 case 'c': button |= 16; break; /* CTRL key Pressed */
5508 case '-': break; /* Not Pressed */
5509 case 'x': break; /* Not Reporting */
5510 default: return -1; /* Unknown Result */
5511 }
5512 if (*p++ != '\033')
5513 return -1;
5514 if (*p++ != '\\')
5515 return -1;
5516 switch (status)
5517 {
5518 case 'D': /* Double Click */
5519 case 'S': /* Single Click */
5520 if (button & 1) mouse_code |= MOUSE_LEFT;
5521 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5522 if (button & 4) mouse_code |= MOUSE_RIGHT;
5523 if (button & 8) mouse_code |= MOUSE_SHIFT;
5524 if (button & 16) mouse_code |= MOUSE_CTRL;
5525 break;
5526 case 'm': /* Mouse move */
5527 if (button & 1) mouse_code |= MOUSE_LEFT;
5528 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5529 if (button & 4) mouse_code |= MOUSE_RIGHT;
5530 if (button & 8) mouse_code |= MOUSE_SHIFT;
5531 if (button & 16) mouse_code |= MOUSE_CTRL;
5532 if ((button & 7) != 0)
5533 {
5534 held_button = mouse_code;
5535 mouse_code |= MOUSE_DRAG;
5536 }
5537 is_drag = TRUE;
5538 showmode();
5539 break;
5540 case 'd': /* Button Down */
5541 if (button & 1) mouse_code |= MOUSE_LEFT;
5542 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5543 if (button & 4) mouse_code |= MOUSE_RIGHT;
5544 if (button & 8) mouse_code |= MOUSE_SHIFT;
5545 if (button & 16) mouse_code |= MOUSE_CTRL;
5546 break;
5547 case 'u': /* Button Up */
5548 if (button & 1)
5549 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5550 if (button & 2)
5551 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5552 if (button & 4)
5553 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5554 if (button & 8)
5555 mouse_code |= MOUSE_SHIFT;
5556 if (button & 16)
5557 mouse_code |= MOUSE_CTRL;
5558 break;
5559 default: return -1; /* Unknown Result */
5560 }
5561
5562 slen += (p - (tp + slen));
5563 }
5564# endif /* FEAT_MOUSE_JSB */
5565# ifdef FEAT_MOUSE_DEC
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005566 if (key_name[0] == KS_DEC_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 /* The DEC Locator Input Model
5569 * Netterm delivers the code sequence:
5570 * \033[2;4;24;80&w (left button down)
5571 * \033[3;0;24;80&w (left button up)
5572 * \033[6;1;24;80&w (right button down)
5573 * \033[7;0;24;80&w (right button up)
5574 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5575 * Pe is the event code
5576 * Pb is the button code
5577 * Pr is the row coordinate
5578 * Pc is the column coordinate
5579 * Pp is the third coordinate (page number)
5580 * Pe, the event code indicates what event caused this report
5581 * The following event codes are defined:
5582 * 0 - request, the terminal received an explicit request
5583 * for a locator report, but the locator is unavailable
5584 * 1 - request, the terminal received an explicit request
5585 * for a locator report
5586 * 2 - left button down
5587 * 3 - left button up
5588 * 4 - middle button down
5589 * 5 - middle button up
5590 * 6 - right button down
5591 * 7 - right button up
5592 * 8 - fourth button down
5593 * 9 - fourth button up
5594 * 10 - locator outside filter rectangle
5595 * Pb, the button code, ASCII decimal 0-15 indicating which
5596 * buttons are down if any. The state of the four buttons
5597 * on the locator correspond to the low four bits of the
5598 * decimal value,
5599 * "1" means button depressed
5600 * 0 - no buttons down,
5601 * 1 - right,
5602 * 2 - middle,
5603 * 4 - left,
5604 * 8 - fourth
5605 * Pr is the row coordinate of the locator position in the page,
5606 * encoded as an ASCII decimal value.
5607 * If Pr is omitted, the locator position is undefined
5608 * (outside the terminal window for example).
5609 * Pc is the column coordinate of the locator position in the
5610 * page, encoded as an ASCII decimal value.
5611 * If Pc is omitted, the locator position is undefined
5612 * (outside the terminal window for example).
5613 * Pp is the page coordinate of the locator position
5614 * encoded as an ASCII decimal value.
5615 * The page coordinate may be omitted if the locator is on
5616 * page one (the default). We ignore it anyway.
5617 */
5618 int Pe, Pb, Pr, Pc;
5619
5620 p = tp + slen;
5621
5622 /* get event status */
5623 Pe = getdigits(&p);
5624 if (*p++ != ';')
5625 return -1;
5626
5627 /* get button status */
5628 Pb = getdigits(&p);
5629 if (*p++ != ';')
5630 return -1;
5631
5632 /* get row status */
5633 Pr = getdigits(&p);
5634 if (*p++ != ';')
5635 return -1;
5636
5637 /* get column status */
5638 Pc = getdigits(&p);
5639
5640 /* the page parameter is optional */
5641 if (*p == ';')
5642 {
5643 p++;
5644 (void)getdigits(&p);
5645 }
5646 if (*p++ != '&')
5647 return -1;
5648 if (*p++ != 'w')
5649 return -1;
5650
5651 mouse_code = 0;
5652 switch (Pe)
5653 {
5654 case 0: return -1; /* position request while unavailable */
5655 case 1: /* a response to a locator position request includes
5656 the status of all buttons */
5657 Pb &= 7; /* mask off and ignore fourth button */
5658 if (Pb & 4)
5659 mouse_code = MOUSE_LEFT;
5660 if (Pb & 2)
5661 mouse_code = MOUSE_MIDDLE;
5662 if (Pb & 1)
5663 mouse_code = MOUSE_RIGHT;
5664 if (Pb)
5665 {
5666 held_button = mouse_code;
5667 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005668 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 is_drag = TRUE;
5671 showmode();
5672 break;
5673 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005674 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5677 break;
5678 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005679 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5682 break;
5683 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005684 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5687 break;
5688 case 8: return -1; /* fourth button down */
5689 case 9: return -1; /* fourth button up */
5690 case 10: return -1; /* mouse outside of filter rectangle */
5691 default: return -1; /* should never occur */
5692 }
5693
5694 mouse_col = Pc - 1;
5695 mouse_row = Pr - 1;
5696
5697 slen += (int)(p - (tp + slen));
5698 }
5699# endif /* FEAT_MOUSE_DEC */
5700# ifdef FEAT_MOUSE_PTERM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005701 if (key_name[0] == KS_PTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005703 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
5705 p = tp + slen;
5706
5707 action = getdigits(&p);
5708 if (*p++ != ';')
5709 return -1;
5710
5711 mouse_row = getdigits(&p);
5712 if (*p++ != ';')
5713 return -1;
5714 mouse_col = getdigits(&p);
5715 if (*p++ != ';')
5716 return -1;
5717
5718 button = getdigits(&p);
5719 mouse_code = 0;
5720
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005721 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 case 4: mouse_code = MOUSE_LEFT; break;
5724 case 1: mouse_code = MOUSE_RIGHT; break;
5725 case 2: mouse_code = MOUSE_MIDDLE; break;
5726 default: return -1;
5727 }
5728
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005729 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
5731 case 31: /* Initial press */
5732 if (*p++ != ';')
5733 return -1;
5734
5735 num_clicks = getdigits(&p); /* Not used */
5736 break;
5737
5738 case 32: /* Release */
5739 mouse_code |= MOUSE_RELEASE;
5740 break;
5741
5742 case 33: /* Drag */
5743 held_button = mouse_code;
5744 mouse_code |= MOUSE_DRAG;
5745 break;
5746
5747 default:
5748 return -1;
5749 }
5750
5751 if (*p++ != 't')
5752 return -1;
5753
5754 slen += (p - (tp + slen));
5755 }
5756# endif /* FEAT_MOUSE_PTERM */
5757
5758 /* Interpret the mouse code */
5759 current_button = (mouse_code & MOUSE_CLICK_MASK);
5760 if (current_button == MOUSE_RELEASE
5761# ifdef FEAT_MOUSE_XTERM
5762 && wheel_code == 0
5763# endif
5764 )
5765 {
5766 /*
5767 * If we get a mouse drag or release event when
5768 * there is no mouse button held down (held_button ==
5769 * MOUSE_RELEASE), produce a K_IGNORE below.
5770 * (can happen when you hold down two buttons
5771 * and then let them go, or click in the menu bar, but not
5772 * on a menu, and drag into the text).
5773 */
5774 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5775 is_drag = TRUE;
5776 current_button = held_button;
5777 }
5778 else if (wheel_code == 0)
5779 {
5780# ifdef CHECK_DOUBLE_CLICK
5781# ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 /*
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005783 * Only for Unix, when GUI not active, we handle
5784 * multi-clicks here, but not for GPM mouse events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005786# ifdef FEAT_GUI
5787 if (key_name[0] != KS_GPM_MOUSE && !gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788# else
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005789 if (key_name[0] != KS_GPM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790# endif
5791# else
5792# ifdef FEAT_GUI
5793 if (!gui.in_use)
5794# endif
5795# endif
5796 {
5797 /*
5798 * Compute the time elapsed since the previous mouse click.
5799 */
5800 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005801 if (orig_mouse_time.tv_sec == 0)
5802 {
5803 /*
5804 * Avoid computing the difference between mouse_time
5805 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005806 * difference would be huge and would cause
5807 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005808 */
5809 timediff = p_mouset;
5810 }
5811 else
5812 {
5813 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005814 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005815 if (timediff < 0)
5816 --orig_mouse_time.tv_sec;
5817 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005818 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 orig_mouse_time = mouse_time;
5821 if (mouse_code == orig_mouse_code
5822 && timediff < p_mouset
5823 && orig_num_clicks != 4
5824 && orig_mouse_col == mouse_col
5825 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005826 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005828 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005830 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005831 /* Double click in tab pages line also works
5832 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005834 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 ++orig_num_clicks;
5836 else
5837 orig_num_clicks = 1;
5838 orig_mouse_col = mouse_col;
5839 orig_mouse_row = mouse_row;
5840 orig_topline = curwin->w_topline;
5841#ifdef FEAT_DIFF
5842 orig_topfill = curwin->w_topfill;
5843#endif
5844 }
5845# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5846 else
5847 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5848# endif
5849# else
5850 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5851# endif
5852 is_click = TRUE;
5853 orig_mouse_code = mouse_code;
5854 }
5855 if (!is_drag)
5856 held_button = mouse_code & MOUSE_CLICK_MASK;
5857
5858 /*
5859 * Translate the actual mouse event into a pseudo mouse event.
5860 * First work out what modifiers are to be used.
5861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (orig_mouse_code & MOUSE_SHIFT)
5863 modifiers |= MOD_MASK_SHIFT;
5864 if (orig_mouse_code & MOUSE_CTRL)
5865 modifiers |= MOD_MASK_CTRL;
5866 if (orig_mouse_code & MOUSE_ALT)
5867 modifiers |= MOD_MASK_ALT;
5868 if (orig_num_clicks == 2)
5869 modifiers |= MOD_MASK_2CLICK;
5870 else if (orig_num_clicks == 3)
5871 modifiers |= MOD_MASK_3CLICK;
5872 else if (orig_num_clicks == 4)
5873 modifiers |= MOD_MASK_4CLICK;
5874
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005875 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5876 * added, then it's not mouse up/down. */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005877 key_name[0] = KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005878 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005879 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005880 {
5881 if (wheel_code & MOUSE_CTRL)
5882 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005883 if (wheel_code & MOUSE_ALT)
5884 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 key_name[1] = (wheel_code & 1)
5886 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005887 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 else
5890 key_name[1] = get_pseudo_mouse_code(current_button,
5891 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005892
5893 /* Make sure the mouse position is valid. Some terminals may
5894 * return weird values. */
5895 if (mouse_col >= Columns)
5896 mouse_col = Columns - 1;
5897 if (mouse_row >= Rows)
5898 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
5900#endif /* FEAT_MOUSE */
5901
5902#ifdef FEAT_GUI
5903 /*
5904 * If using the GUI, then we get menu and scrollbar events.
5905 *
5906 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5907 * four bytes which are to be taken as a pointer to the vimmenu_T
5908 * structure.
5909 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005910 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005911 * is one byte with the tab index.
5912 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5914 * by one byte representing the scrollbar number, and then four bytes
5915 * representing a long_u which is the new value of the scrollbar.
5916 *
5917 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5918 * KE_FILLER followed by four bytes representing a long_u which is the
5919 * new value of the scrollbar.
5920 */
5921# ifdef FEAT_MENU
5922 else if (key_name[0] == (int)KS_MENU)
5923 {
5924 long_u val;
5925
5926 num_bytes = get_long_from_buf(tp + slen, &val);
5927 if (num_bytes == -1)
5928 return -1;
5929 current_menu = (vimmenu_T *)val;
5930 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005931
5932 /* The menu may have been deleted right after it was used, check
5933 * for that. */
5934 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5935 {
5936 key_name[0] = KS_EXTRA;
5937 key_name[1] = (int)KE_IGNORE;
5938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
5940# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005941# ifdef FEAT_GUI_TABLINE
5942 else if (key_name[0] == (int)KS_TABLINE)
5943 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005944 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005945 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5946 if (num_bytes == -1)
5947 return -1;
5948 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005949 if (current_tab == 255) /* -1 in a byte gives 255 */
5950 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005951 slen += num_bytes;
5952 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005953 else if (key_name[0] == (int)KS_TABMENU)
5954 {
5955 /* Selecting tabline tab or using its menu. */
5956 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5957 if (num_bytes == -1)
5958 return -1;
5959 current_tab = (int)bytes[0];
5960 current_tabmenu = (int)bytes[1];
5961 slen += num_bytes;
5962 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964# ifndef USE_ON_FLY_SCROLL
5965 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5966 {
5967 long_u val;
5968
5969 /* Get the last scrollbar event in the queue of the same type */
5970 j = 0;
5971 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5972 && tp[j + 2] != NUL; ++i)
5973 {
5974 j += 3;
5975 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5976 if (num_bytes == -1)
5977 break;
5978 if (i == 0)
5979 current_scrollbar = (int)bytes[0];
5980 else if (current_scrollbar != (int)bytes[0])
5981 break;
5982 j += num_bytes;
5983 num_bytes = get_long_from_buf(tp + j, &val);
5984 if (num_bytes == -1)
5985 break;
5986 scrollbar_value = val;
5987 j += num_bytes;
5988 slen = j;
5989 }
5990 if (i == 0) /* not enough characters to make one */
5991 return -1;
5992 }
5993 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5994 {
5995 long_u val;
5996
5997 /* Get the last horiz. scrollbar event in the queue */
5998 j = 0;
5999 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6000 && tp[j + 2] != NUL; ++i)
6001 {
6002 j += 3;
6003 num_bytes = get_long_from_buf(tp + j, &val);
6004 if (num_bytes == -1)
6005 break;
6006 scrollbar_value = val;
6007 j += num_bytes;
6008 slen = j;
6009 }
6010 if (i == 0) /* not enough characters to make one */
6011 return -1;
6012 }
6013# endif /* !USE_ON_FLY_SCROLL */
6014#endif /* FEAT_GUI */
6015
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006016 /*
6017 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6018 */
6019 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6020
6021 /*
6022 * Add any modifier codes to our string.
6023 */
6024 new_slen = 0; /* Length of what will replace the termcode */
6025 if (modifiers != 0)
6026 {
6027 /* Some keys have the modifier included. Need to handle that here
6028 * to make mappings work. */
6029 key = simplify_key(key, &modifiers);
6030 if (modifiers != 0)
6031 {
6032 string[new_slen++] = K_SPECIAL;
6033 string[new_slen++] = (int)KS_MODIFIER;
6034 string[new_slen++] = modifiers;
6035 }
6036 }
6037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006039 key_name[0] = KEY2TERMCAP0(key);
6040 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006042 {
6043 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00006044 if (has_mbyte)
6045 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6046 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006047 string[new_slen++] = key_name[1];
6048 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006049 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6050 && key_name[1] == KE_IGNORE)
6051 {
6052 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6053 * to indicate what happened. */
6054 retval = KEYLEN_REMOVED;
6055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 else
6057 {
6058 string[new_slen++] = K_SPECIAL;
6059 string[new_slen++] = key_name[0];
6060 string[new_slen++] = key_name[1];
6061 }
6062 string[new_slen] = NUL;
6063 extra = new_slen - slen;
6064 if (buf == NULL)
6065 {
6066 if (extra < 0)
6067 /* remove matched chars, taking care of noremap */
6068 del_typebuf(-extra, offset);
6069 else if (extra > 0)
6070 /* insert the extra space we need */
6071 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
6072
6073 /*
6074 * Careful: del_typebuf() and ins_typebuf() may have reallocated
6075 * typebuf.tb_buf[]!
6076 */
6077 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
6078 (size_t)new_slen);
6079 }
6080 else
6081 {
6082 if (extra < 0)
6083 /* remove matched characters */
6084 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006085 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006087 {
6088 /* Insert the extra space we need. If there is insufficient
6089 * space return -1. */
6090 if (*buflen + extra + new_slen >= bufsize)
6091 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006093 (size_t)(*buflen - offset));
6094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006096 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006098 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 }
6100
Bram Moolenaar2951b772013-07-03 12:45:31 +02006101#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006102 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006103#endif
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 return 0; /* no match found */
6106}
6107
Bram Moolenaar9377df32017-10-15 13:22:01 +02006108#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006109/*
6110 * Get the text foreground color, if known.
6111 */
6112 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006113term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006114{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006115 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006116 {
6117 *r = fg_r;
6118 *g = fg_g;
6119 *b = fg_b;
6120 }
6121}
6122
6123/*
6124 * Get the text background color, if known.
6125 */
6126 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006127term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006128{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006129 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006130 {
6131 *r = bg_r;
6132 *g = bg_g;
6133 *b = bg_b;
6134 }
6135}
6136#endif
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138/*
6139 * Replace any terminal code strings in from[] with the equivalent internal
6140 * vim representation. This is used for the "from" and "to" part of a
6141 * mapping, and the "to" part of a menu command.
6142 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6143 * '<'.
6144 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6145 *
6146 * The replacement is done in result[] and finally copied into allocated
6147 * memory. If this all works well *bufp is set to the allocated memory and a
6148 * pointer to it is returned. If something fails *bufp is set to NULL and from
6149 * is returned.
6150 *
6151 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6152 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6153 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6154 * instead of a CTRL-V.
6155 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006156 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006157replace_termcodes(
6158 char_u *from,
6159 char_u **bufp,
6160 int from_part,
6161 int do_lt, /* also translate <lt> */
6162 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 int i;
6165 int slen;
6166 int key;
6167 int dlen = 0;
6168 char_u *src;
6169 int do_backslash; /* backslash is a special character */
6170 int do_special; /* recognize <> key codes */
6171 int do_key_code; /* recognize raw key codes */
6172 char_u *result; /* buffer for resulting string */
6173
6174 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006175 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6177
6178 /*
6179 * Allocate space for the translation. Worst case a single character is
6180 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6181 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02006182 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (result == NULL) /* out of memory */
6184 {
6185 *bufp = NULL;
6186 return from;
6187 }
6188
6189 src = from;
6190
6191 /*
6192 * Check for #n at start only: function key n
6193 */
6194 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6195 {
6196 result[dlen++] = K_SPECIAL;
6197 result[dlen++] = 'k';
6198 if (src[1] == '0')
6199 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6200 else
6201 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6202 src += 2;
6203 }
6204
6205 /*
6206 * Copy each byte from *from to result[dlen]
6207 */
6208 while (*src != NUL)
6209 {
6210 /*
6211 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006212 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
6214 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6215 {
6216#ifdef FEAT_EVAL
6217 /*
6218 * Replace <SID> by K_SNR <script-nr> _.
6219 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6220 */
6221 if (STRNICMP(src, "<SID>", 5) == 0)
6222 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006223 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006224 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 else
6226 {
6227 src += 5;
6228 result[dlen++] = K_SPECIAL;
6229 result[dlen++] = (int)KS_EXTRA;
6230 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006231 sprintf((char *)result + dlen, "%ld",
6232 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 dlen += (int)STRLEN(result + dlen);
6234 result[dlen++] = '_';
6235 continue;
6236 }
6237 }
6238#endif
6239
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006240 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (slen)
6242 {
6243 dlen += slen;
6244 continue;
6245 }
6246 }
6247
6248 /*
6249 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6250 * Note that this is also checked after replacing the <> form.
6251 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6252 * it could be a character in the file.
6253 */
6254 if (do_key_code)
6255 {
6256 i = find_term_bykeys(src);
6257 if (i >= 0)
6258 {
6259 result[dlen++] = K_SPECIAL;
6260 result[dlen++] = termcodes[i].name[0];
6261 result[dlen++] = termcodes[i].name[1];
6262 src += termcodes[i].len;
6263 /* If terminal code matched, continue after it. */
6264 continue;
6265 }
6266 }
6267
6268#ifdef FEAT_EVAL
6269 if (do_special)
6270 {
6271 char_u *p, *s, len;
6272
6273 /*
6274 * Replace <Leader> by the value of "mapleader".
6275 * Replace <LocalLeader> by the value of "maplocalleader".
6276 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6277 */
6278 if (STRNICMP(src, "<Leader>", 8) == 0)
6279 {
6280 len = 8;
6281 p = get_var_value((char_u *)"g:mapleader");
6282 }
6283 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6284 {
6285 len = 13;
6286 p = get_var_value((char_u *)"g:maplocalleader");
6287 }
6288 else
6289 {
6290 len = 0;
6291 p = NULL;
6292 }
6293 if (len != 0)
6294 {
6295 /* Allow up to 8 * 6 characters for "mapleader". */
6296 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6297 s = (char_u *)"\\";
6298 else
6299 s = p;
6300 while (*s != NUL)
6301 result[dlen++] = *s++;
6302 src += len;
6303 continue;
6304 }
6305 }
6306#endif
6307
6308 /*
6309 * Remove CTRL-V and ignore the next character.
6310 * For "from" side the CTRL-V at the end is included, for the "to"
6311 * part it is removed.
6312 * If 'cpoptions' does not contain 'B', also accept a backslash.
6313 */
6314 key = *src;
6315 if (key == Ctrl_V || (do_backslash && key == '\\'))
6316 {
6317 ++src; /* skip CTRL-V or backslash */
6318 if (*src == NUL)
6319 {
6320 if (from_part)
6321 result[dlen++] = key;
6322 break;
6323 }
6324 }
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006327 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 {
6329 /*
6330 * If the character is K_SPECIAL, replace it with K_SPECIAL
6331 * KS_SPECIAL KE_FILLER.
6332 * If compiled with the GUI replace CSI with K_CSI.
6333 */
6334 if (*src == K_SPECIAL)
6335 {
6336 result[dlen++] = K_SPECIAL;
6337 result[dlen++] = KS_SPECIAL;
6338 result[dlen++] = KE_FILLER;
6339 }
6340# ifdef FEAT_GUI
6341 else if (*src == CSI)
6342 {
6343 result[dlen++] = K_SPECIAL;
6344 result[dlen++] = KS_EXTRA;
6345 result[dlen++] = (int)KE_CSI;
6346 }
6347# endif
6348 else
6349 result[dlen++] = *src;
6350 ++src;
6351 }
6352 }
6353 result[dlen] = NUL;
6354
6355 /*
6356 * Copy the new string to allocated memory.
6357 * If this fails, just return from.
6358 */
6359 if ((*bufp = vim_strsave(result)) != NULL)
6360 from = *bufp;
6361 vim_free(result);
6362 return from;
6363}
6364
6365/*
6366 * Find a termcode with keys 'src' (must be NUL terminated).
6367 * Return the index in termcodes[], or -1 if not found.
6368 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006369 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006370find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006373 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 for (i = 0; i < tc_len; ++i)
6376 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006377 if (slen == termcodes[i].len
6378 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 return i;
6380 }
6381 return -1;
6382}
6383
6384/*
6385 * Gather the first characters in the terminal key codes into a string.
6386 * Used to speed up check_termcode().
6387 */
6388 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006389gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 int i;
6392 int len = 0;
6393
6394#ifdef FEAT_GUI
6395 if (gui.in_use)
6396 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6397#endif
6398#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006399 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 termleader[len++] = DCS; /* the termcode response starts with DCS
6401 in 8-bit mode */
6402#endif
6403 termleader[len] = NUL;
6404
6405 for (i = 0; i < tc_len; ++i)
6406 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6407 {
6408 termleader[len++] = termcodes[i].code[0];
6409 termleader[len] = NUL;
6410 }
6411
6412 need_gather = FALSE;
6413}
6414
6415/*
6416 * Show all termcodes (for ":set termcap")
6417 * This code looks a lot like showoptions(), but is different.
6418 */
6419 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006420show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 int col;
6423 int *items;
6424 int item_count;
6425 int run;
6426 int row, rows;
6427 int cols;
6428 int i;
6429 int len;
6430
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006431#define INC3 27 /* try to make three columns */
6432#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433#define GAP 2 /* spaces between columns */
6434
6435 if (tc_len == 0) /* no terminal codes (must be GUI) */
6436 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006437 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (items == NULL)
6439 return;
6440
6441 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006442 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
6444 /*
6445 * do the loop two times:
6446 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006447 * 2. display the medium items (medium length strings)
6448 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006450 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 {
6452 /*
6453 * collect the items in items[]
6454 */
6455 item_count = 0;
6456 for (i = 0; i < tc_len; i++)
6457 {
6458 len = show_one_termcode(termcodes[i].name,
6459 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006460 if (len <= INC3 - GAP ? run == 1
6461 : len <= INC2 - GAP ? run == 2
6462 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 items[item_count++] = i;
6464 }
6465
6466 /*
6467 * display the items
6468 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006469 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006471 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (cols == 0)
6473 cols = 1;
6474 rows = (item_count + cols - 1) / cols;
6475 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006476 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 rows = item_count;
6478 for (row = 0; row < rows && !got_int; ++row)
6479 {
6480 msg_putchar('\n'); /* go to next line */
6481 if (got_int) /* 'q' typed in more */
6482 break;
6483 col = 0;
6484 for (i = row; i < item_count; i += rows)
6485 {
6486 msg_col = col; /* make columns */
6487 show_one_termcode(termcodes[items[i]].name,
6488 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006489 if (run == 2)
6490 col += INC2;
6491 else
6492 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494 out_flush();
6495 ui_breakcheck();
6496 }
6497 }
6498 vim_free(items);
6499}
6500
6501/*
6502 * Show one termcode entry.
6503 * Output goes into IObuff[]
6504 */
6505 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006506show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 char_u *p;
6509 int len;
6510
6511 if (name[0] > '~')
6512 {
6513 IObuff[0] = ' ';
6514 IObuff[1] = ' ';
6515 IObuff[2] = ' ';
6516 IObuff[3] = ' ';
6517 }
6518 else
6519 {
6520 IObuff[0] = 't';
6521 IObuff[1] = '_';
6522 IObuff[2] = name[0];
6523 IObuff[3] = name[1];
6524 }
6525 IObuff[4] = ' ';
6526
6527 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6528 if (p[1] != 't')
6529 STRCPY(IObuff + 5, p);
6530 else
6531 IObuff[5] = NUL;
6532 len = (int)STRLEN(IObuff);
6533 do
6534 IObuff[len++] = ' ';
6535 while (len < 17);
6536 IObuff[len] = NUL;
6537 if (code == NULL)
6538 len += 4;
6539 else
6540 len += vim_strsize(code);
6541
6542 if (printit)
6543 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006544 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006546 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 else
6548 msg_outtrans(code);
6549 }
6550 return len;
6551}
6552
6553#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6554/*
6555 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6556 * termcap codes from the terminal itself.
6557 * We get them one by one to avoid a very long response string.
6558 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006559static int xt_index_in = 0;
6560static int xt_index_out = 0;
6561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006563req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 xt_index_out = 0;
6566 xt_index_in = 0;
6567 req_more_codes_from_term();
6568}
6569
6570 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006571req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 char buf[11];
6574 int old_idx = xt_index_out;
6575
6576 /* Don't do anything when going to exit. */
6577 if (exiting)
6578 return;
6579
6580 /* Send up to 10 more requests out than we received. Avoid sending too
6581 * many, there can be a buffer overflow somewhere. */
6582 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6583 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006584 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006585
Bram Moolenaarb255b902018-04-24 21:40:10 +02006586 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6587 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 out_str_nf((char_u *)buf);
6589 ++xt_index_out;
6590 }
6591
6592 /* Send the codes out right away. */
6593 if (xt_index_out != old_idx)
6594 out_flush();
6595}
6596
6597/*
6598 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6599 * A "0" instead of the "1" indicates a code that isn't supported.
6600 * Both <name> and <string> are encoded in hex.
6601 * "code" points to the "0" or "1".
6602 */
6603 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006604got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606#define XT_LEN 100
6607 char_u name[3];
6608 char_u str[XT_LEN];
6609 int i;
6610 int j = 0;
6611 int c;
6612
6613 /* A '1' means the code is supported, a '0' means it isn't.
6614 * When half the length is > XT_LEN we can't use it.
6615 * Our names are currently all 2 characters. */
6616 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6617 {
6618 /* Get the name from the response and find it in the table. */
6619 name[0] = hexhex2nr(code + 3);
6620 name[1] = hexhex2nr(code + 5);
6621 name[2] = NUL;
6622 for (i = 0; key_names[i] != NULL; ++i)
6623 {
6624 if (STRCMP(key_names[i], name) == 0)
6625 {
6626 xt_index_in = i;
6627 break;
6628 }
6629 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006630
Bram Moolenaarb255b902018-04-24 21:40:10 +02006631 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 if (key_names[i] != NULL)
6634 {
6635 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6636 str[j++] = c;
6637 str[j] = NUL;
6638 if (name[0] == 'C' && name[1] == 'o')
6639 {
6640 /* Color count is not a key code. */
6641 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006642 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644 else
6645 {
6646 /* First delete any existing entry with the same code. */
6647 i = find_term_bykeys(str);
6648 if (i >= 0)
6649 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006650 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 }
6652 }
6653 }
6654
6655 /* May request more codes now that we received one. */
6656 ++xt_index_in;
6657 req_more_codes_from_term();
6658}
6659
6660/*
6661 * Check if there are any unanswered requests and deal with them.
6662 * This is called before starting an external program or getting direct
6663 * keyboard input. We don't want responses to be send to that program or
6664 * handled as typed text.
6665 */
6666 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006667check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 int c;
6670
6671 /* If no codes requested or all are answered, no need to wait. */
6672 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6673 return;
6674
6675 /* Vgetc() will check for and handle any response.
6676 * Keep calling vpeekc() until we don't get any responses. */
6677 ++no_mapping;
6678 ++allow_keys;
6679 for (;;)
6680 {
6681 c = vpeekc();
6682 if (c == NUL) /* nothing available */
6683 break;
6684
6685 /* If a response is recognized it's replaced with K_IGNORE, must read
6686 * it from the input stream. If there is no K_IGNORE we can't do
6687 * anything, break here (there might be some responses further on, but
6688 * we don't want to throw away any typed chars). */
6689 if (c != K_SPECIAL && c != K_IGNORE)
6690 break;
6691 c = vgetc();
6692 if (c != K_IGNORE)
6693 {
6694 vungetc(c);
6695 break;
6696 }
6697 }
6698 --no_mapping;
6699 --allow_keys;
6700}
6701#endif
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703/*
6704 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006705 * corresponding external one recognized by :map/:abbrev commands.
6706 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 *
6708 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006709 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006711 * It uses a growarray to build the translation string since the latter can be
6712 * wider than the original description. The caller has to free the string
6713 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 *
6715 * Returns NULL when there is a problem.
6716 */
6717 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006718translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 garray_T ga;
6721 int c;
6722 int modifiers;
6723 int cpo_bslash;
6724 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 ga_init(&ga);
6727 ga.ga_itemsize = 1;
6728 ga.ga_growsize = 40;
6729
6730 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6731 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 for (; *str; ++str)
6734 {
6735 c = *str;
6736 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6737 {
6738 modifiers = 0;
6739 if (str[1] == KS_MODIFIER)
6740 {
6741 str++;
6742 modifiers = *++str;
6743 c = *++str;
6744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6746 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006747 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {
6749 ga_clear(&ga);
6750 return NULL;
6751 }
6752 c = TO_SPECIAL(str[1], str[2]);
6753 if (c == K_ZERO) /* display <Nul> as ^@ */
6754 c = NUL;
6755 str += 2;
6756 }
6757 if (IS_SPECIAL(c) || modifiers) /* special key */
6758 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006759 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
6761 ga_clear(&ga);
6762 return NULL;
6763 }
6764 ga_concat(&ga, get_special_key_name(c, modifiers));
6765 continue; /* for (str) */
6766 }
6767 }
6768 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6769 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6770 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6771 if (c)
6772 ga_append(&ga, c);
6773 }
6774 ga_append(&ga, NUL);
6775 return (char_u *)(ga.ga_data);
6776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006778#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779static char ksme_str[20];
6780static char ksmr_str[20];
6781static char ksmd_str[20];
6782
6783/*
6784 * For Win32 console: update termcap codes for existing console attributes.
6785 */
6786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006787update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 struct builtin_term *p;
6790
6791 p = find_builtin_term(DEFAULT_TERM);
6792 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6793 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6794 attr | 0x08); /* FOREGROUND_INTENSITY */
6795 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6796 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6797
6798 while (p->bt_string != NULL)
6799 {
6800 if (p->bt_entry == (int)KS_ME)
6801 p->bt_string = &ksme_str[0];
6802 else if (p->bt_entry == (int)KS_MR)
6803 p->bt_string = &ksmr_str[0];
6804 else if (p->bt_entry == (int)KS_MD)
6805 p->bt_string = &ksmd_str[0];
6806 ++p;
6807 }
6808}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006809
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006810# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006811# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006812struct ks_tbl_s
6813{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006814 int code; // value of KS_
6815 char *vtp; // code in vtp mode
6816 char *vtp2; // code in vtp2 mode
6817 char buf[KSSIZE]; // save buffer in non-vtp mode
6818 char vbuf[KSSIZE]; // save buffer in vtp mode
6819 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6820 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006821};
6822
6823static struct ks_tbl_s ks_tbl[] =
6824{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006825 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6826 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6827 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6828 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6829 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6830 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6831 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6832 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6833 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006834# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006835 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6836 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006837 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6838 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006839# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006840 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6841 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006842 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6843 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006844# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006845 {(int)KS_CCO, "256", "256"}, // colors
6846 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006847};
6848
6849 static struct builtin_term *
6850find_first_tcap(
6851 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006852 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006853{
6854 struct builtin_term *p;
6855
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006856 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006857 if (p->bt_entry == code)
6858 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006859 return NULL;
6860}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006861# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006862
6863/*
6864 * For Win32 console: replace the sequence immediately after termguicolors.
6865 */
6866 void
6867swap_tcap(void)
6868{
6869# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006870 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006871 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006872 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006873 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006874 int mode;
6875 enum
6876 {
6877 CMODEINDEX,
6878 CMODE24,
6879 CMODE256
6880 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006881
6882 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006883 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006884 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006885 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006886 {
6887 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006888 if (bt != NULL)
6889 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006890 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6891 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6892 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6893
6894 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6895 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006896 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006897 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006898 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006899 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006900 }
6901
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006902 if (p_tgc)
6903 mode = CMODE24;
6904 else if (t_colors >= 256)
6905 mode = CMODE256;
6906 else
6907 mode = CMODEINDEX;
6908
6909 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006910 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006911 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6912 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006913 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006914 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006915 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006916 case CMODEINDEX:
6917 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6918 break;
6919 case CMODE24:
6920 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6921 break;
6922 default:
6923 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006924 }
6925 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006926 }
6927
6928 if (mode != curr_mode)
6929 {
6930 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006931 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006932 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6933 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006934 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006935 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006936 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006937 case CMODEINDEX:
6938 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6939 break;
6940 case CMODE24:
6941 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6942 break;
6943 default:
6944 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006945 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006946 }
6947 }
6948
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006949 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006950 }
6951# endif
6952}
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006955
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006956#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006957 static int
6958hex_digit(int c)
6959{
6960 if (isdigit(c))
6961 return c - '0';
6962 c = TOLOWER_ASC(c);
6963 if (c >= 'a' && c <= 'f')
6964 return c - 'a' + 10;
6965 return 0x1ffffff;
6966}
6967
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006968# ifdef VIMDLL
6969 static guicolor_T
6970gui_adjust_rgb(guicolor_T c)
6971{
6972 if (gui.in_use)
6973 return c;
6974 else
6975 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6976}
6977# else
6978# define gui_adjust_rgb(c) (c)
6979# endif
6980
Bram Moolenaarab302212016-04-26 20:59:29 +02006981 guicolor_T
6982gui_get_color_cmn(char_u *name)
6983{
Bram Moolenaar28726652017-01-06 18:16:19 +01006984 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6985 * values as used by the MS-Windows GDI api. It should be used only for
6986 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006987# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006988# undef RGB
6989# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006990# ifndef RGB
6991# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6992# endif
6993# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006994 FILE *fd;
6995 char line[LINE_LEN];
6996 char_u *fname;
6997 int r, g, b, i;
6998 guicolor_T color;
6999
7000 struct rgbcolor_table_S {
7001 char_u *color_name;
7002 guicolor_T color;
7003 };
7004
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007005 /* Only non X11 colors (not present in rgb.txt) and colors in
7006 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02007007 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007008 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
7009 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
7010 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
7011 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
7012 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
7013 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
7014 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
7015 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
7016 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
7017 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
7018 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
7019 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
7020 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007021 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
7022 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02007023 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01007024 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007025 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007026 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
7027 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
7028 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
7029 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
7030 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
7031 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
7032 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
7033 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
7034 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007035 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007036 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007037 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
7038 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02007039 };
7040
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007041 static struct rgbcolor_table_S *colornames_table;
7042 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02007043
7044 if (name[0] == '#' && STRLEN(name) == 7)
7045 {
7046 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007047 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02007048 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
7049 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
7050 if (color > 0xffffff)
7051 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007052 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007053 }
7054
7055 /* Check if the name is one of the colors we know */
7056 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
7057 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007058 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007059
7060 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007061 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02007062 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007063 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02007064 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007065 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02007066
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007067 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007068 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
7069 if (fname == NULL)
7070 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007071
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007072 fd = fopen((char *)fname, "rt");
7073 vim_free(fname);
7074 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02007075 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007076 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007077 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007078 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007079 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007080 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007081
7082 for (counting = 1; counting >= 0; --counting)
7083 {
7084 if (!counting)
7085 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007086 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007087 if (colornames_table == NULL)
7088 {
7089 fclose(fd);
7090 return INVALCOLOR;
7091 }
7092 rewind(fd);
7093 }
7094 size = 0;
7095
7096 while (!feof(fd))
7097 {
7098 size_t len;
7099 int pos;
7100
Bram Moolenaar42335f52018-09-13 15:33:43 +02007101 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007102 len = strlen(line);
7103
7104 if (len <= 1 || line[len - 1] != '\n')
7105 continue;
7106
7107 line[len - 1] = '\0';
7108
7109 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7110 if (i != 3)
7111 continue;
7112
7113 if (!counting)
7114 {
7115 char_u *s = vim_strsave((char_u *)line + pos);
7116
7117 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007118 {
7119 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007120 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007121 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007122 colornames_table[size].color_name = s;
7123 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7124 }
7125 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007126
7127 // The distributed rgb.txt has less than 1000 entries. Limit to
7128 // 10000, just in case the file was messed up.
7129 if (size == 10000)
7130 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007131 }
7132 }
7133 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007134 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007135
7136 for (i = 0; i < size; i++)
7137 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007138 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007139
Bram Moolenaarab302212016-04-26 20:59:29 +02007140 return INVALCOLOR;
7141}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007142
7143 guicolor_T
7144gui_get_rgb_color_cmn(int r, int g, int b)
7145{
7146 guicolor_T color = RGB(r, g, b);
7147
7148 if (color > 0xffffff)
7149 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007150 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007151}
Bram Moolenaarab302212016-04-26 20:59:29 +02007152#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007153
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007154#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007155 || defined(PROTO)
7156static int cube_value[] = {
7157 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7158};
7159
7160static int grey_ramp[] = {
7161 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7162 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7163};
7164
7165# ifdef FEAT_TERMINAL
7166# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007167# else
7168# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007169# endif
7170
Bram Moolenaar9894e392018-05-05 14:29:06 +02007171static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007172// R G B idx
7173 { 0, 0, 0, 1}, // black
7174 {224, 0, 0, 2}, // dark red
7175 { 0, 224, 0, 3}, // dark green
7176 {224, 224, 0, 4}, // dark yellow / brown
7177 { 0, 0, 224, 5}, // dark blue
7178 {224, 0, 224, 6}, // dark magenta
7179 { 0, 224, 224, 7}, // dark cyan
7180 {224, 224, 224, 8}, // light grey
7181
7182 {128, 128, 128, 9}, // dark grey
7183 {255, 64, 64, 10}, // light red
7184 { 64, 255, 64, 11}, // light green
7185 {255, 255, 64, 12}, // yellow
7186 { 64, 64, 255, 13}, // light blue
7187 {255, 64, 255, 14}, // light magenta
7188 { 64, 255, 255, 15}, // light cyan
7189 {255, 255, 255, 16}, // white
7190};
7191
7192 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007193cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007194{
7195 int idx;
7196
7197 if (nr < 16)
7198 {
7199 *r = ansi_table[nr][0];
7200 *g = ansi_table[nr][1];
7201 *b = ansi_table[nr][2];
7202 *ansi_idx = ansi_table[nr][3];
7203 }
7204 else if (nr < 232)
7205 {
7206 /* 216 color cube */
7207 idx = nr - 16;
7208 *r = cube_value[idx / 36 % 6];
7209 *g = cube_value[idx / 6 % 6];
7210 *b = cube_value[idx % 6];
7211 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7212 }
7213 else if (nr < 256)
7214 {
7215 /* 24 grey scale ramp */
7216 idx = nr - 232;
7217 *r = grey_ramp[idx];
7218 *g = grey_ramp[idx];
7219 *b = grey_ramp[idx];
7220 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7221 }
7222 else
7223 {
7224 *r = 0;
7225 *g = 0;
7226 *b = 0;
7227 *ansi_idx = 0;
7228 }
7229}
7230#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007231