blob: 7c2111b0e8e78f469a47f49e38b3f2c1d150c951 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void req_codes_from_term(void);
81static void req_more_codes_from_term(void);
82static void got_code_from_term(char_u *code, int len);
83static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
85#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000086 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
87 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010088static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static void del_termcode_idx(int idx);
91static int term_is_builtin(char_u *name);
92static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010095static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
97/*
98 * Here is our own prototype for tgetstr(), any prototypes from the include
99 * files have been disabled by the define at the start of this file.
100 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
103# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200104 /* Change this to "if 1" to debug what happens with termresponse. */
105# if 0
106# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200107static void log_tr(const char *fmt, ...);
108# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200110# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200111# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200112
Bram Moolenaarafd78262019-05-10 23:10:31 +0200113typedef enum {
114 STATUS_GET, // send request when switching to RAW mode
115 STATUS_SENT, // did send request, checking for response
116 STATUS_GOT, // received response
117 STATUS_FAIL // timed out
118} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200119
Bram Moolenaarafd78262019-05-10 23:10:31 +0200120typedef struct {
121 request_progress_T tr_progress;
122 time_t tr_start; // when request was sent, -1 for never
123} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200124
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125# define TERMREQUEST_INIT {STATUS_GET, -1}
126
127// Request Terminal Version status:
128static termrequest_T crv_status = TERMREQUEST_INIT;
129
130// Request Cursor position report:
131static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200132
Bram Moolenaar9377df32017-10-15 13:22:01 +0200133# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134// Request foreground color report:
135static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200136static int fg_r = 0;
137static int fg_g = 0;
138static int fg_b = 0;
139static int bg_r = 255;
140static int bg_g = 255;
141static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200142# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200143
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200144/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200145static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200146
Bram Moolenaar4db25542017-08-28 22:43:05 +0200147/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200148static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200149
150/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200151static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100152
153/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200154static termrequest_T winpos_status = TERMREQUEST_INIT;
155
156static termrequest_T *all_termrequests[] = {
157 &crv_status,
158 &u7_status,
159# ifdef FEAT_TERMINAL
160 &rfg_status,
161# endif
162 &rbg_status,
163 &rbm_status,
164 &rcs_status,
165 &winpos_status,
166 NULL
167};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# endif
169
170/*
171 * Don't declare these variables if termcap.h contains them.
172 * Autoconf checks if these variables should be declared extern (not all
173 * systems have them).
174 * Some versions define ospeed to be speed_t, but that is incompatible with
175 * BSD, where ospeed is short and speed_t is long.
176 */
177# ifndef HAVE_OSPEED
178# ifdef OSPEED_EXTERN
179extern short ospeed;
180# else
181short ospeed;
182# endif
183# endif
184# ifndef HAVE_UP_BC_PC
185# ifdef UP_BC_PC_EXTERN
186extern char *UP, *BC, PC;
187# else
188char *UP, *BC, PC;
189# endif
190# endif
191
192# define TGETSTR(s, p) vim_tgetstr((s), (p))
193# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif /* HAVE_TGETENT */
196
197static int detected_8bit = FALSE; /* detected 8-bit terminal */
198
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200199#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200200/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200201 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203
204/* The blink flag from the style response may be inverted from the actual
205 * blinking state, xterm XORs the flags. */
206static int initial_cursor_shape_blink = FALSE;
207
208/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200209static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200210#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200211
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000212static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214
215#if defined(FEAT_GUI)
216/*
217 * GUI pseudo term-cap.
218 */
219 {(int)KS_NAME, "gui"},
220 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
221 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
222# ifdef TERMINFO
223 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
224# else
225 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
226# endif
227 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
228# ifdef TERMINFO
229 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
230 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# else
233 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
234 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
237 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
238 /* attributes switched on with 'h', off with * 'H' */
239 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
240 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
241 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
242 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
243 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
244 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
245 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000246 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
247 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200248 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
249 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
251 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
252 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
253 {(int)KS_MS, "y"},
254 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100255 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {(int)KS_LE, "\b"}, /* cursor-left = BS */
257 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
258# ifdef TERMINFO
259 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
260# else
261 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
262# endif
263 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000264 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
266
267#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
270/*
271 * Amiga console window, default for Amiga
272 */
273 {(int)KS_NAME, "amiga"},
274 {(int)KS_CE, "\033[K"},
275 {(int)KS_CD, "\033[J"},
276 {(int)KS_AL, "\033[L"},
277# ifdef TERMINFO
278 {(int)KS_CAL, "\033[%p1%dL"},
279# else
280 {(int)KS_CAL, "\033[%dL"},
281# endif
282 {(int)KS_DL, "\033[M"},
283# ifdef TERMINFO
284 {(int)KS_CDL, "\033[%p1%dM"},
285# else
286 {(int)KS_CDL, "\033[%dM"},
287# endif
288 {(int)KS_CL, "\014"},
289 {(int)KS_VI, "\033[0 p"},
290 {(int)KS_VE, "\033[1 p"},
291 {(int)KS_ME, "\033[0m"},
292 {(int)KS_MR, "\033[7m"},
293 {(int)KS_MD, "\033[1m"},
294 {(int)KS_SE, "\033[0m"},
295 {(int)KS_SO, "\033[33m"},
296 {(int)KS_US, "\033[4m"},
297 {(int)KS_UE, "\033[0m"},
298 {(int)KS_CZH, "\033[3m"},
299 {(int)KS_CZR, "\033[0m"},
300#if defined(__MORPHOS__) || defined(__AROS__)
301 {(int)KS_CCO, "8"}, /* allow 8 colors */
302# ifdef TERMINFO
303 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
304 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
305# else
306 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
307 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
308# endif
309 {(int)KS_OP, "\033[m"}, /* reset colors */
310#endif
311 {(int)KS_MS, "y"},
312 {(int)KS_UT, "y"}, /* guessed */
313 {(int)KS_LE, "\b"},
314# ifdef TERMINFO
315 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
316# else
317 {(int)KS_CM, "\033[%i%d;%dH"},
318# endif
319#if defined(__MORPHOS__)
320 {(int)KS_SR, "\033M"},
321#endif
322# ifdef TERMINFO
323 {(int)KS_CRI, "\033[%p1%dC"},
324# else
325 {(int)KS_CRI, "\033[%dC"},
326# endif
327 {K_UP, "\233A"},
328 {K_DOWN, "\233B"},
329 {K_LEFT, "\233D"},
330 {K_RIGHT, "\233C"},
331 {K_S_UP, "\233T"},
332 {K_S_DOWN, "\233S"},
333 {K_S_LEFT, "\233 A"},
334 {K_S_RIGHT, "\233 @"},
335 {K_S_TAB, "\233Z"},
336 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
337 {K_F2, "\233\061~"},
338 {K_F3, "\233\062~"},
339 {K_F4, "\233\063~"},
340 {K_F5, "\233\064~"},
341 {K_F6, "\233\065~"},
342 {K_F7, "\233\066~"},
343 {K_F8, "\233\067~"},
344 {K_F9, "\233\070~"},
345 {K_F10, "\233\071~"},
346 {K_S_F1, "\233\061\060~"},
347 {K_S_F2, "\233\061\061~"},
348 {K_S_F3, "\233\061\062~"},
349 {K_S_F4, "\233\061\063~"},
350 {K_S_F5, "\233\061\064~"},
351 {K_S_F6, "\233\061\065~"},
352 {K_S_F7, "\233\061\066~"},
353 {K_S_F8, "\233\061\067~"},
354 {K_S_F9, "\233\061\070~"},
355 {K_S_F10, "\233\061\071~"},
356 {K_HELP, "\233?~"},
357 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
358 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
359 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
360 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
361 {K_END, "\233\064\065~"}, /* 101 key keyboard */
362
363 {BT_EXTRA_KEYS, ""},
364 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
365 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
366 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
367# endif
368
369# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
370/*
371 * almost standard ANSI terminal, default for bebox
372 */
373 {(int)KS_NAME, "beos-ansi"},
374 {(int)KS_CE, "\033[K"},
375 {(int)KS_CD, "\033[J"},
376 {(int)KS_AL, "\033[L"},
377# ifdef TERMINFO
378 {(int)KS_CAL, "\033[%p1%dL"},
379# else
380 {(int)KS_CAL, "\033[%dL"},
381# endif
382 {(int)KS_DL, "\033[M"},
383# ifdef TERMINFO
384 {(int)KS_CDL, "\033[%p1%dM"},
385# else
386 {(int)KS_CDL, "\033[%dM"},
387# endif
388#ifdef BEOS_PR_OR_BETTER
389# ifdef TERMINFO
390 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
391# else
392 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
393# endif
394#endif
395 {(int)KS_CL, "\033[H\033[2J"},
396#ifdef notyet
397 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
398 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
399#endif
400 {(int)KS_ME, "\033[m"}, /* normal mode */
401 {(int)KS_MR, "\033[7m"}, /* reverse */
402 {(int)KS_MD, "\033[1m"}, /* bold */
403 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
404 {(int)KS_SE, "\033[m"}, /* standout end */
405 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
406 {(int)KS_CZR, "\033[m"}, /* italic end */
407 {(int)KS_US, "\033[4m"}, /* underscore mode */
408 {(int)KS_UE, "\033[m"}, /* underscore end */
409 {(int)KS_CCO, "8"}, /* allow 8 colors */
410# ifdef TERMINFO
411 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
412 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
413# else
414 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
415 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
416# endif
417 {(int)KS_OP, "\033[m"}, /* reset colors */
418 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
419 {(int)KS_UT, "y"}, /* guessed */
420 {(int)KS_LE, "\b"},
421# ifdef TERMINFO
422 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
423# else
424 {(int)KS_CM, "\033[%i%d;%dH"},
425# endif
426 {(int)KS_SR, "\033M"},
427# ifdef TERMINFO
428 {(int)KS_CRI, "\033[%p1%dC"},
429# else
430 {(int)KS_CRI, "\033[%dC"},
431# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200432# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
436 {K_UP, "\033[A"},
437 {K_DOWN, "\033[B"},
438 {K_LEFT, "\033[D"},
439 {K_RIGHT, "\033[C"},
440# endif
441
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200442# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443/*
444 * standard ANSI terminal, default for unix
445 */
446 {(int)KS_NAME, "ansi"},
447 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
448 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
449# ifdef TERMINFO
450 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
451# else
452 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
453# endif
454 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
455# ifdef TERMINFO
456 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
457# else
458 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
459# endif
460 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
461 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
462 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
463 {(int)KS_MS, "y"},
464 {(int)KS_UT, "y"}, /* guessed */
465 {(int)KS_LE, "\b"},
466# ifdef TERMINFO
467 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
468# else
469 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
470# endif
471# ifdef TERMINFO
472 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
473# else
474 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
475# endif
476# endif
477
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200478# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479/*
480 * These codes are valid when nansi.sys or equivalent has been installed.
481 * Function keys on a PC are preceded with a NUL. These are converted into
482 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
483 * CTRL-arrow is used instead of SHIFT-arrow.
484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {(int)KS_NAME, "pcansi"},
486 {(int)KS_DL, "\033[M"},
487 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {(int)KS_CE, "\033[K"},
489 {(int)KS_CL, "\033[2J"},
490 {(int)KS_ME, "\033[0m"},
491 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
492 {(int)KS_MD, "\033[1m"}, /* bold: white text */
493 {(int)KS_SE, "\033[0m"}, /* standout end */
494 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
495 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
496 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
497 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
498 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
499 {(int)KS_CCO, "8"}, /* allow 8 colors */
500# ifdef TERMINFO
501 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
502 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
503# else
504 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
505 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
506# endif
507 {(int)KS_OP, "\033[0m"}, /* reset colors */
508 {(int)KS_MS, "y"},
509 {(int)KS_UT, "y"}, /* guessed */
510 {(int)KS_LE, "\b"},
511# ifdef TERMINFO
512 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
513# else
514 {(int)KS_CM, "\033[%i%d;%dH"},
515# endif
516# ifdef TERMINFO
517 {(int)KS_CRI, "\033[%p1%dC"},
518# else
519 {(int)KS_CRI, "\033[%dC"},
520# endif
521 {K_UP, "\316H"},
522 {K_DOWN, "\316P"},
523 {K_LEFT, "\316K"},
524 {K_RIGHT, "\316M"},
525 {K_S_LEFT, "\316s"},
526 {K_S_RIGHT, "\316t"},
527 {K_F1, "\316;"},
528 {K_F2, "\316<"},
529 {K_F3, "\316="},
530 {K_F4, "\316>"},
531 {K_F5, "\316?"},
532 {K_F6, "\316@"},
533 {K_F7, "\316A"},
534 {K_F8, "\316B"},
535 {K_F9, "\316C"},
536 {K_F10, "\316D"},
537 {K_F11, "\316\205"}, /* guessed */
538 {K_F12, "\316\206"}, /* guessed */
539 {K_S_F1, "\316T"},
540 {K_S_F2, "\316U"},
541 {K_S_F3, "\316V"},
542 {K_S_F4, "\316W"},
543 {K_S_F5, "\316X"},
544 {K_S_F6, "\316Y"},
545 {K_S_F7, "\316Z"},
546 {K_S_F8, "\316["},
547 {K_S_F9, "\316\\"},
548 {K_S_F10, "\316]"},
549 {K_S_F11, "\316\207"}, /* guessed */
550 {K_S_F12, "\316\210"}, /* guessed */
551 {K_INS, "\316R"},
552 {K_DEL, "\316S"},
553 {K_HOME, "\316G"},
554 {K_END, "\316O"},
555 {K_PAGEDOWN, "\316Q"},
556 {K_PAGEUP, "\316I"},
557# endif
558
Bram Moolenaar4f974752019-02-17 17:44:42 +0100559# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560/*
561 * These codes are valid for the Win32 Console . The entries that start with
562 * ESC | are translated into console calls in os_win32.c. The function keys
563 * are also translated in os_win32.c.
564 */
565 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100566 {(int)KS_CE, "\033|K"}, // clear to end of line
567 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100569 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100573 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
576 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100578 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
579 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100581 {(int)KS_CL, "\033|J"}, // clear screen
582 {(int)KS_CD, "\033|j"}, // clear to end of display
583 {(int)KS_VI, "\033|v"}, // cursor invisible
584 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_ME, "\033|0m"}, // normal
587 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
588 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100590 {(int)KS_SO, "\033|31m"}, // standout: white on blue
591 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100593 {(int)KS_SO, "\033|F"}, // standout: high intensity
594 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100596 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
597 {(int)KS_CZR, "\033|0m"}, // italic end
598 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
599 {(int)KS_UE, "\033|0m"}, // underscore end
600 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100602 {(int)KS_CAB, "\033|%p1%db"}, // set background color
603 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100605 {(int)KS_CAB, "\033|%db"}, // set background color
606 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607# endif
608
Bram Moolenaar6982f422019-02-16 16:48:01 +0100609 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100611 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {(int)KS_LE, "\b"},
613# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100614 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100616 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_VB, "\033|B"}, // visual bell
619 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
620 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100622 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100624 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100626# ifdef FEAT_TERMGUICOLORS
627 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
628 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 {K_UP, "\316H"},
632 {K_DOWN, "\316P"},
633 {K_LEFT, "\316K"},
634 {K_RIGHT, "\316M"},
635 {K_S_UP, "\316\304"},
636 {K_S_DOWN, "\316\317"},
637 {K_S_LEFT, "\316\311"},
638 {K_C_LEFT, "\316s"},
639 {K_S_RIGHT, "\316\313"},
640 {K_C_RIGHT, "\316t"},
641 {K_S_TAB, "\316\017"},
642 {K_F1, "\316;"},
643 {K_F2, "\316<"},
644 {K_F3, "\316="},
645 {K_F4, "\316>"},
646 {K_F5, "\316?"},
647 {K_F6, "\316@"},
648 {K_F7, "\316A"},
649 {K_F8, "\316B"},
650 {K_F9, "\316C"},
651 {K_F10, "\316D"},
652 {K_F11, "\316\205"},
653 {K_F12, "\316\206"},
654 {K_S_F1, "\316T"},
655 {K_S_F2, "\316U"},
656 {K_S_F3, "\316V"},
657 {K_S_F4, "\316W"},
658 {K_S_F5, "\316X"},
659 {K_S_F6, "\316Y"},
660 {K_S_F7, "\316Z"},
661 {K_S_F8, "\316["},
662 {K_S_F9, "\316\\"},
663 {K_S_F10, "\316]"},
664 {K_S_F11, "\316\207"},
665 {K_S_F12, "\316\210"},
666 {K_INS, "\316R"},
667 {K_DEL, "\316S"},
668 {K_HOME, "\316G"},
669 {K_S_HOME, "\316\302"},
670 {K_C_HOME, "\316w"},
671 {K_END, "\316O"},
672 {K_S_END, "\316\315"},
673 {K_C_END, "\316u"},
674 {K_PAGEDOWN, "\316Q"},
675 {K_PAGEUP, "\316I"},
676 {K_KPLUS, "\316N"},
677 {K_KMINUS, "\316J"},
678 {K_KMULTIPLY, "\316\067"},
679 {K_K0, "\316\332"},
680 {K_K1, "\316\336"},
681 {K_K2, "\316\342"},
682 {K_K3, "\316\346"},
683 {K_K4, "\316\352"},
684 {K_K5, "\316\356"},
685 {K_K6, "\316\362"},
686 {K_K7, "\316\366"},
687 {K_K8, "\316\372"},
688 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100689 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
691
692# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
693/*
694 * VT320 is working as an ANSI terminal compatible DEC terminal.
695 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * TODO:- rewrite ESC[ codes to CSI
697 * - keyboard languages (CSI ? 26 n)
698 */
699 {(int)KS_NAME, "vt320"},
700 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
701 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
702# ifdef TERMINFO
703 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
704# else
705 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
706# endif
707 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
708# ifdef TERMINFO
709 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
710# else
711 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
712# endif
713 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000714 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
715 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
717 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000718 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
719 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
720 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
721 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
722 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
723 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
724 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
725 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
726 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
727 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {(int)KS_MS, "y"},
729 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100730 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {(int)KS_LE, "\b"},
732# ifdef TERMINFO
733 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
734 ESC_STR "[%i%p1%d;%p2%dH")},
735# else
736 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
737# endif
738# ifdef TERMINFO
739 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
740# else
741 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
742# endif
743 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
744 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
745 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
746 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200747 // Note: cursor key sequences for application cursor mode are omitted,
748 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000749 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
750 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
751 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
752 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
753 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
755 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
756 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
757 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
758 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
761 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
762 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
763 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
764 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
765 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
766 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
767 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
768 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
769 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
770 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
771 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
772 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
773 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
774 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200775 // These sequences starting with <Esc> O may interfere with what the user
776 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
778 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
779 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
780 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
781 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200782 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
783 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
784 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
785 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
786 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
787 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
788 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
789 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
790 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
791 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
793# endif
794
795# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
796/*
797 * Ordinary vt52
798 */
799 {(int)KS_NAME, "vt52"},
800 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
801 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100802# ifdef TERMINFO
803 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
804 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
805# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100809 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
811 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {K_UP, IF_EB("\033A", ESC_STR "A")},
813 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
814 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
815 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100816 {K_F1, IF_EB("\033P", ESC_STR "P")},
817 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
818 {K_F3, IF_EB("\033R", ESC_STR "R")},
819# ifdef __MINT__
820 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
821 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
822 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
823 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
824 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
826 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
827 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
828 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {K_F4, IF_EB("\033S", ESC_STR "S")},
830 {K_F5, IF_EB("\033T", ESC_STR "T")},
831 {K_F6, IF_EB("\033U", ESC_STR "U")},
832 {K_F7, IF_EB("\033V", ESC_STR "V")},
833 {K_F8, IF_EB("\033W", ESC_STR "W")},
834 {K_F9, IF_EB("\033X", ESC_STR "X")},
835 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
836 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
837 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
838 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
839 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
840 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
841 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
842 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
843 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
844 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
845 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
846 {K_INS, IF_EB("\033I", ESC_STR "I")},
847 {K_HOME, IF_EB("\033E", ESC_STR "E")},
848 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
849 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
850# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_MS, "y"},
853# endif
854# endif
855
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200856# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200857 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
859 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
860# ifdef TERMINFO
861 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
862# else
863 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
864# endif
865 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
866# ifdef TERMINFO
867 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
868# else
869 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
870# endif
871# ifdef TERMINFO
872 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
873 ESC_STR "[%i%p1%d;%p2%dr")},
874# else
875 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
876# endif
877 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
878 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
879 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
880 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
881 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
882 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
883 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200884 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
885 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_MS, "y"},
887 {(int)KS_UT, "y"},
888 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200889 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
890 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200891 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200892 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200893# ifdef TERMINFO
894 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
895# else
896 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
897# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200898 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200899 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900# ifdef TERMINFO
901 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
902 ESC_STR "[%i%p1%d;%p2%dH")},
903# else
904 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
905# endif
906 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
907# ifdef TERMINFO
908 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
909# else
910 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
911# endif
912 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
913 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
914# ifdef FEAT_XTERM_SAVE
915 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
916 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
917 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
918# endif
919 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
920 {(int)KS_CIE, "\007"},
921 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
922 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200923 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
924 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925# ifdef TERMINFO
926 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
927 ESC_STR "[8;%p1%d;%p2%dt")},
928 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
929 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200930 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931# else
932 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
933 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200934 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# endif
936 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200937 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200938 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100939 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200940# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200941 /* These are printf strings, not terminal codes. */
942 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
943 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
944# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100945 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
946 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200947 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
948 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
949 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
950 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000951
952 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
953 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
954 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
955 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
956 /* An extra set of cursor keys for vt100 mode */
957 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
958 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
959 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
960 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000962 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
963 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
964 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
965 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000966 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
967 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
968 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
969 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
970 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
971 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
972 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
973 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
974 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
975 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
976 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
977 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000979 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
980 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
981 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
982 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000983 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
984 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000986 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000988 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000989 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
990 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000991 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000992 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000994 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
995 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100996 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
997 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
998 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
999 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1000 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1001 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +02001002 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
1003 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1004 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1005 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1006 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1007 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1008 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1009 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1010 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1011 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001012 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1013 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1014 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
1016 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001017 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1018 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1019 /* F14 and F15 are missing, because they send the same codes as the undo
1020 * and help key, although they don't work on all keyboards. */
1021 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1022 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1023 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1024 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1025 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1026
1027 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1028 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1029 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1030 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1031 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1032 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1033 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1034 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1035 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1036 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1037
1038 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1039 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1040 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1041 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1042 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1043 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1044 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045# endif
1046
1047# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1048/*
1049 * iris-ansi for Silicon Graphics machines.
1050 */
1051 {(int)KS_NAME, "iris-ansi"},
1052 {(int)KS_CE, "\033[K"},
1053 {(int)KS_CD, "\033[J"},
1054 {(int)KS_AL, "\033[L"},
1055# ifdef TERMINFO
1056 {(int)KS_CAL, "\033[%p1%dL"},
1057# else
1058 {(int)KS_CAL, "\033[%dL"},
1059# endif
1060 {(int)KS_DL, "\033[M"},
1061# ifdef TERMINFO
1062 {(int)KS_CDL, "\033[%p1%dM"},
1063# else
1064 {(int)KS_CDL, "\033[%dM"},
1065# endif
1066#if 0 /* The scroll region is not working as Vim expects. */
1067# ifdef TERMINFO
1068 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1069# else
1070 {(int)KS_CS, "\033[%i%d;%dr"},
1071# endif
1072#endif
1073 {(int)KS_CL, "\033[H\033[2J"},
1074 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1075 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1076 {(int)KS_TI, "\033[=6h"},
1077 {(int)KS_TE, "\033[=6l"},
1078 {(int)KS_SE, "\033[21;27m"},
1079 {(int)KS_SO, "\033[1;7m"},
1080 {(int)KS_ME, "\033[m"},
1081 {(int)KS_MR, "\033[7m"},
1082 {(int)KS_MD, "\033[1m"},
1083 {(int)KS_CCO, "8"}, /* allow 8 colors */
1084 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1085 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1086 {(int)KS_US, "\033[4m"}, /* underline on */
1087 {(int)KS_UE, "\033[24m"}, /* underline off */
1088# ifdef TERMINFO
1089 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1090 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1091 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1092 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1093# else
1094 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1095 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1096 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1097 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1098# endif
1099 {(int)KS_MS, "y"}, /* guessed */
1100 {(int)KS_UT, "y"}, /* guessed */
1101 {(int)KS_LE, "\b"},
1102# ifdef TERMINFO
1103 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1104# else
1105 {(int)KS_CM, "\033[%i%d;%dH"},
1106# endif
1107 {(int)KS_SR, "\033M"},
1108# ifdef TERMINFO
1109 {(int)KS_CRI, "\033[%p1%dC"},
1110# else
1111 {(int)KS_CRI, "\033[%dC"},
1112# endif
1113 {(int)KS_CIS, "\033P3.y"},
1114 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1115 {(int)KS_TS, "\033P1.y"},
1116 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1117# ifdef TERMINFO
1118 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1119 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1120# else
1121 {(int)KS_CWS, "\033[203;%d;%d/y"},
1122 {(int)KS_CWP, "\033[205;%d;%d/y"},
1123# endif
1124 {K_UP, "\033[A"},
1125 {K_DOWN, "\033[B"},
1126 {K_LEFT, "\033[D"},
1127 {K_RIGHT, "\033[C"},
1128 {K_S_UP, "\033[161q"},
1129 {K_S_DOWN, "\033[164q"},
1130 {K_S_LEFT, "\033[158q"},
1131 {K_S_RIGHT, "\033[167q"},
1132 {K_F1, "\033[001q"},
1133 {K_F2, "\033[002q"},
1134 {K_F3, "\033[003q"},
1135 {K_F4, "\033[004q"},
1136 {K_F5, "\033[005q"},
1137 {K_F6, "\033[006q"},
1138 {K_F7, "\033[007q"},
1139 {K_F8, "\033[008q"},
1140 {K_F9, "\033[009q"},
1141 {K_F10, "\033[010q"},
1142 {K_F11, "\033[011q"},
1143 {K_F12, "\033[012q"},
1144 {K_S_F1, "\033[013q"},
1145 {K_S_F2, "\033[014q"},
1146 {K_S_F3, "\033[015q"},
1147 {K_S_F4, "\033[016q"},
1148 {K_S_F5, "\033[017q"},
1149 {K_S_F6, "\033[018q"},
1150 {K_S_F7, "\033[019q"},
1151 {K_S_F8, "\033[020q"},
1152 {K_S_F9, "\033[021q"},
1153 {K_S_F10, "\033[022q"},
1154 {K_S_F11, "\033[023q"},
1155 {K_S_F12, "\033[024q"},
1156 {K_INS, "\033[139q"},
1157 {K_HOME, "\033[H"},
1158 {K_END, "\033[146q"},
1159 {K_PAGEUP, "\033[150q"},
1160 {K_PAGEDOWN, "\033[154q"},
1161# endif
1162
1163# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1164/*
1165 * for debugging
1166 */
1167 {(int)KS_NAME, "debug"},
1168 {(int)KS_CE, "[CE]"},
1169 {(int)KS_CD, "[CD]"},
1170 {(int)KS_AL, "[AL]"},
1171# ifdef TERMINFO
1172 {(int)KS_CAL, "[CAL%p1%d]"},
1173# else
1174 {(int)KS_CAL, "[CAL%d]"},
1175# endif
1176 {(int)KS_DL, "[DL]"},
1177# ifdef TERMINFO
1178 {(int)KS_CDL, "[CDL%p1%d]"},
1179# else
1180 {(int)KS_CDL, "[CDL%d]"},
1181# endif
1182# ifdef TERMINFO
1183 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1184# else
1185 {(int)KS_CS, "[%dCS%d]"},
1186# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001187# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001189# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191# endif
1192# ifdef TERMINFO
1193 {(int)KS_CAB, "[CAB%p1%d]"},
1194 {(int)KS_CAF, "[CAF%p1%d]"},
1195 {(int)KS_CSB, "[CSB%p1%d]"},
1196 {(int)KS_CSF, "[CSF%p1%d]"},
1197# else
1198 {(int)KS_CAB, "[CAB%d]"},
1199 {(int)KS_CAF, "[CAF%d]"},
1200 {(int)KS_CSB, "[CSB%d]"},
1201 {(int)KS_CSF, "[CSF%d]"},
1202# endif
1203 {(int)KS_OP, "[OP]"},
1204 {(int)KS_LE, "[LE]"},
1205 {(int)KS_CL, "[CL]"},
1206 {(int)KS_VI, "[VI]"},
1207 {(int)KS_VE, "[VE]"},
1208 {(int)KS_VS, "[VS]"},
1209 {(int)KS_ME, "[ME]"},
1210 {(int)KS_MR, "[MR]"},
1211 {(int)KS_MB, "[MB]"},
1212 {(int)KS_MD, "[MD]"},
1213 {(int)KS_SE, "[SE]"},
1214 {(int)KS_SO, "[SO]"},
1215 {(int)KS_UE, "[UE]"},
1216 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001217 {(int)KS_UCE, "[UCE]"},
1218 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001219 {(int)KS_STE, "[STE]"},
1220 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {(int)KS_MS, "[MS]"},
1222 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001223 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224# ifdef TERMINFO
1225 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1226# else
1227 {(int)KS_CM, "[%dCM%d]"},
1228# endif
1229 {(int)KS_SR, "[SR]"},
1230# ifdef TERMINFO
1231 {(int)KS_CRI, "[CRI%p1%d]"},
1232# else
1233 {(int)KS_CRI, "[CRI%d]"},
1234# endif
1235 {(int)KS_VB, "[VB]"},
1236 {(int)KS_KS, "[KS]"},
1237 {(int)KS_KE, "[KE]"},
1238 {(int)KS_TI, "[TI]"},
1239 {(int)KS_TE, "[TE]"},
1240 {(int)KS_CIS, "[CIS]"},
1241 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001242 {(int)KS_CSC, "[CSC]"},
1243 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {(int)KS_TS, "[TS]"},
1245 {(int)KS_FS, "[FS]"},
1246# ifdef TERMINFO
1247 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1248 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1249# else
1250 {(int)KS_CWS, "[%dCWS%d]"},
1251 {(int)KS_CWP, "[%dCWP%d]"},
1252# endif
1253 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001254 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001255 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001256 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {K_UP, "[KU]"},
1258 {K_DOWN, "[KD]"},
1259 {K_LEFT, "[KL]"},
1260 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001261 {K_XUP, "[xKU]"},
1262 {K_XDOWN, "[xKD]"},
1263 {K_XLEFT, "[xKL]"},
1264 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {K_S_UP, "[S-KU]"},
1266 {K_S_DOWN, "[S-KD]"},
1267 {K_S_LEFT, "[S-KL]"},
1268 {K_C_LEFT, "[C-KL]"},
1269 {K_S_RIGHT, "[S-KR]"},
1270 {K_C_RIGHT, "[C-KR]"},
1271 {K_F1, "[F1]"},
1272 {K_XF1, "[xF1]"},
1273 {K_F2, "[F2]"},
1274 {K_XF2, "[xF2]"},
1275 {K_F3, "[F3]"},
1276 {K_XF3, "[xF3]"},
1277 {K_F4, "[F4]"},
1278 {K_XF4, "[xF4]"},
1279 {K_F5, "[F5]"},
1280 {K_F6, "[F6]"},
1281 {K_F7, "[F7]"},
1282 {K_F8, "[F8]"},
1283 {K_F9, "[F9]"},
1284 {K_F10, "[F10]"},
1285 {K_F11, "[F11]"},
1286 {K_F12, "[F12]"},
1287 {K_S_F1, "[S-F1]"},
1288 {K_S_XF1, "[S-xF1]"},
1289 {K_S_F2, "[S-F2]"},
1290 {K_S_XF2, "[S-xF2]"},
1291 {K_S_F3, "[S-F3]"},
1292 {K_S_XF3, "[S-xF3]"},
1293 {K_S_F4, "[S-F4]"},
1294 {K_S_XF4, "[S-xF4]"},
1295 {K_S_F5, "[S-F5]"},
1296 {K_S_F6, "[S-F6]"},
1297 {K_S_F7, "[S-F7]"},
1298 {K_S_F8, "[S-F8]"},
1299 {K_S_F9, "[S-F9]"},
1300 {K_S_F10, "[S-F10]"},
1301 {K_S_F11, "[S-F11]"},
1302 {K_S_F12, "[S-F12]"},
1303 {K_HELP, "[HELP]"},
1304 {K_UNDO, "[UNDO]"},
1305 {K_BS, "[BS]"},
1306 {K_INS, "[INS]"},
1307 {K_KINS, "[KINS]"},
1308 {K_DEL, "[DEL]"},
1309 {K_KDEL, "[KDEL]"},
1310 {K_HOME, "[HOME]"},
1311 {K_S_HOME, "[C-HOME]"},
1312 {K_C_HOME, "[C-HOME]"},
1313 {K_KHOME, "[KHOME]"},
1314 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_END, "[END]"},
1317 {K_S_END, "[C-END]"},
1318 {K_C_END, "[C-END]"},
1319 {K_KEND, "[KEND]"},
1320 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001321 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {K_PAGEUP, "[PAGEUP]"},
1323 {K_PAGEDOWN, "[PAGEDOWN]"},
1324 {K_KPAGEUP, "[KPAGEUP]"},
1325 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1326 {K_MOUSE, "[MOUSE]"},
1327 {K_KPLUS, "[KPLUS]"},
1328 {K_KMINUS, "[KMINUS]"},
1329 {K_KDIVIDE, "[KDIVIDE]"},
1330 {K_KMULTIPLY, "[KMULTIPLY]"},
1331 {K_KENTER, "[KENTER]"},
1332 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001333 {K_PS, "[PASTE-START]"},
1334 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {K_K0, "[K0]"},
1336 {K_K1, "[K1]"},
1337 {K_K2, "[K2]"},
1338 {K_K3, "[K3]"},
1339 {K_K4, "[K4]"},
1340 {K_K5, "[K5]"},
1341 {K_K6, "[K6]"},
1342 {K_K7, "[K7]"},
1343 {K_K8, "[K8]"},
1344 {K_K9, "[K9]"},
1345# endif
1346
1347#endif /* NO_BUILTIN_TCAPS */
1348
1349/*
1350 * The most minimal terminal: only clear screen and cursor positioning
1351 * Always included.
1352 */
1353 {(int)KS_NAME, "dumb"},
1354 {(int)KS_CL, "\014"},
1355#ifdef TERMINFO
1356 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1357 ESC_STR "[%i%p1%d;%p2%dH")},
1358#else
1359 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1360#endif
1361
1362/*
1363 * end marker
1364 */
1365 {(int)KS_NAME, NULL}
1366
1367}; /* end of builtin_termcaps */
1368
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001369#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001371termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001372{
Bram Moolenaarab302212016-04-26 20:59:29 +02001373 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374}
1375
1376 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001377termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001378{
1379 guicolor_T t;
1380
1381 if (*name == NUL)
1382 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001383 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001384
1385 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001386 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001387 return t;
1388}
1389
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001390 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001391termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001392{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001393 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001394}
1395#endif
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/*
1398 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef AMIGA
1401# define DEFAULT_TERM (char_u *)"amiga"
1402#endif
1403
1404#ifdef MSWIN
1405# define DEFAULT_TERM (char_u *)"win32"
1406#endif
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#if defined(UNIX) && !defined(__MINT__)
1409# define DEFAULT_TERM (char_u *)"ansi"
1410#endif
1411
1412#ifdef __MINT__
1413# define DEFAULT_TERM (char_u *)"vt52"
1414#endif
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#ifdef VMS
1417# define DEFAULT_TERM (char_u *)"vt320"
1418#endif
1419
1420#ifdef __BEOS__
1421# undef DEFAULT_TERM
1422# define DEFAULT_TERM (char_u *)"beos-ansi"
1423#endif
1424
1425#ifndef DEFAULT_TERM
1426# define DEFAULT_TERM (char_u *)"dumb"
1427#endif
1428
1429/*
1430 * Term_strings contains currently used terminal output strings.
1431 * It is initialized with the default values by parse_builtin_tcap().
1432 * The values can be changed by setting the option with the same name.
1433 */
1434char_u *(term_strings[(int)KS_LAST + 1]);
1435
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001436static int need_gather = FALSE; /* need to fill termleader[] */
1437static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001439static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001440static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#endif
1442
1443 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001444find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 struct builtin_term *p;
1447
1448 p = builtin_termcaps;
1449 while (p->bt_string != NULL)
1450 {
1451 if (p->bt_entry == (int)KS_NAME)
1452 {
1453#ifdef UNIX
1454 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1455 return p;
1456 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1457 return p;
1458 else
1459#endif
1460#ifdef VMS
1461 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1462 return p;
1463 else
1464#endif
1465 if (STRCMP(term, p->bt_string) == 0)
1466 return p;
1467 }
1468 ++p;
1469 }
1470 return p;
1471}
1472
1473/*
1474 * Parsing of the builtin termcap entries.
1475 * Caller should check if 'name' is a valid builtin term.
1476 * The terminal's name is not set, as this is already done in termcapinit().
1477 */
1478 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001479parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 struct builtin_term *p;
1482 char_u name[2];
1483 int term_8bit;
1484
1485 p = find_builtin_term(term);
1486 term_8bit = term_is_8bit(term);
1487
1488 /* Do not parse if builtin term not found */
1489 if (p->bt_string == NULL)
1490 return;
1491
1492 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1493 {
1494 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1495 {
1496 /* Only set the value if it wasn't set yet. */
1497 if (term_strings[p->bt_entry] == NULL
1498 || term_strings[p->bt_entry] == empty_option)
1499 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001500#ifdef FEAT_EVAL
1501 int opt_idx = -1;
1502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 /* 8bit terminal: use CSI instead of <Esc>[ */
1504 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1505 {
1506 char_u *s, *t;
1507
1508 s = vim_strsave((char_u *)p->bt_string);
1509 if (s != NULL)
1510 {
1511 for (t = s; *t; ++t)
1512 if (term_7to8bit(t))
1513 {
1514 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001515 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001518#ifdef FEAT_EVAL
1519 opt_idx =
1520#endif
1521 set_term_option_alloced(
1522 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 }
1525 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001528#ifdef FEAT_EVAL
1529 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1530#endif
1531 }
1532#ifdef FEAT_EVAL
1533 set_term_option_sctx_idx(NULL, opt_idx);
1534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 }
1537 else
1538 {
1539 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1540 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1541 if (find_termcode(name) == NULL)
1542 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1543 }
1544 }
1545}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547/*
1548 * Set number of colors.
1549 * Store it as a number in t_colors.
1550 * Store it as a string in T_CCO (using nr_colors[]).
1551 */
1552 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001553set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 char_u nr_colors[20]; /* string for number of colors */
1556
1557 t_colors = nr;
1558 if (t_colors > 1)
1559 sprintf((char *)nr_colors, "%d", t_colors);
1560 else
1561 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001562 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001564
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001565#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001566/*
1567 * Set the color count to "val" and redraw if it changed.
1568 */
1569 static void
1570may_adjust_color_count(int val)
1571{
1572 if (val != t_colors)
1573 {
1574 /* Nr of colors changed, initialize highlighting and
1575 * redraw everything. This causes a redraw, which usually
1576 * clears the message. Try keeping the message if it
1577 * might work. */
1578 set_keep_msg_from_hist();
1579 set_color_count(val);
1580 init_highlight(TRUE, FALSE);
1581# ifdef DEBUG_TERMRESPONSE
1582 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001583 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001584
Bram Moolenaarb255b902018-04-24 21:40:10 +02001585 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001586 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001587#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001588 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001589#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001590 }
1591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#endif
1593
1594#ifdef HAVE_TGETENT
1595static char *(key_names[]) =
1596{
1597#ifdef FEAT_TERMRESPONSE
1598 /* Do this one first, it may cause a screen redraw. */
1599 "Co",
1600#endif
1601 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 "#2", "#4", "%i", "*7",
1603 "k1", "k2", "k3", "k4", "k5", "k6",
1604 "k7", "k8", "k9", "k;", "F1", "F2",
1605 "%1", "&8", "kb", "kI", "kD", "kh",
1606 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1607 NULL
1608};
1609#endif
1610
Bram Moolenaar9289df52018-05-10 14:40:57 +02001611#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001612 static void
1613get_term_entries(int *height, int *width)
1614{
1615 static struct {
1616 enum SpecialKey dest; /* index in term_strings[] */
1617 char *name; /* termcap name for string */
1618 } string_names[] =
1619 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1620 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1621 {KS_CL, "cl"}, {KS_CD, "cd"},
1622 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1623 {KS_ME, "me"}, {KS_MR, "mr"},
1624 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1625 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1626 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1627 {KS_STE,"Te"}, {KS_STS,"Ts"},
1628 {KS_CM, "cm"}, {KS_SR, "sr"},
1629 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1630 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1631 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1632 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1633 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1634 {KS_VS, "vs"}, {KS_CVS, "VS"},
1635 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1636 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1637 {KS_TS, "ts"}, {KS_FS, "fs"},
1638 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1639 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1640 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1641 {KS_8F, "8f"}, {KS_8B, "8b"},
1642 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1643 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001644 {KS_CST, "ST"}, {KS_CRT, "RT"},
1645 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001646 {(enum SpecialKey)0, NULL}
1647 };
1648 int i;
1649 char_u *p;
1650 static char_u tstrbuf[TBUFSZ];
1651 char_u *tp = tstrbuf;
1652
1653 /*
1654 * get output strings
1655 */
1656 for (i = 0; string_names[i].name != NULL; ++i)
1657 {
1658 if (TERM_STR(string_names[i].dest) == NULL
1659 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001660 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001661 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001662#ifdef FEAT_EVAL
1663 set_term_option_sctx_idx(string_names[i].name, -1);
1664#endif
1665 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001666 }
1667
1668 /* tgetflag() returns 1 if the flag is present, 0 if not and
1669 * possibly -1 if the flag doesn't exist. */
1670 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1671 T_MS = (char_u *)"y";
1672 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1673 T_XS = (char_u *)"y";
1674 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1675 T_XN = (char_u *)"y";
1676 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1677 T_DB = (char_u *)"y";
1678 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1679 T_DA = (char_u *)"y";
1680 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1681 T_UT = (char_u *)"y";
1682
1683 /*
1684 * get key codes
1685 */
1686 for (i = 0; key_names[i] != NULL; ++i)
1687 if (find_termcode((char_u *)key_names[i]) == NULL)
1688 {
1689 p = TGETSTR(key_names[i], &tp);
1690 /* if cursor-left == backspace, ignore it (televideo 925) */
1691 if (p != NULL
1692 && (*p != Ctrl_H
1693 || key_names[i][0] != 'k'
1694 || key_names[i][1] != 'l'))
1695 add_termcode((char_u *)key_names[i], p, FALSE);
1696 }
1697
1698 if (*height == 0)
1699 *height = tgetnum("li");
1700 if (*width == 0)
1701 *width = tgetnum("co");
1702
1703 /*
1704 * Get number of colors (if not done already).
1705 */
1706 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001707 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709#ifdef FEAT_EVAL
1710 set_term_option_sctx_idx("Co", -1);
1711#endif
1712 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713
1714# ifndef hpux
1715 BC = (char *)TGETSTR("bc", &tp);
1716 UP = (char *)TGETSTR("up", &tp);
1717 p = TGETSTR("pc", &tp);
1718 if (p)
1719 PC = *p;
1720# endif
1721}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001722#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723
1724 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001725report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001726{
1727 struct builtin_term *termp;
1728
1729 mch_errmsg("\r\n");
1730 if (error_msg != NULL)
1731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001732 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 mch_errmsg("\r\n");
1734 }
1735 mch_errmsg("'");
1736 mch_errmsg((char *)term);
1737 mch_errmsg(_("' not known. Available builtin terminals are:"));
1738 mch_errmsg("\r\n");
1739 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1740 {
1741 if (termp->bt_entry == (int)KS_NAME)
1742 {
1743#ifdef HAVE_TGETENT
1744 mch_errmsg(" builtin_");
1745#else
1746 mch_errmsg(" ");
1747#endif
1748 mch_errmsg(termp->bt_string);
1749 mch_errmsg("\r\n");
1750 }
1751 }
1752}
1753
1754 static void
1755report_default_term(char_u *term)
1756{
1757 mch_errmsg(_("defaulting to '"));
1758 mch_errmsg((char *)term);
1759 mch_errmsg("'\r\n");
1760 if (emsg_silent == 0)
1761 {
1762 screen_start(); /* don't know where cursor is now */
1763 out_flush();
1764 if (!is_not_a_term())
1765 ui_delay(2000L, TRUE);
1766 }
1767}
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
1770 * Set terminal options for terminal "term".
1771 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1772 *
1773 * While doing this, until ttest(), some options may be NULL, be careful.
1774 */
1775 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001776set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 struct builtin_term *termp;
1779#ifdef HAVE_TGETENT
1780 int builtin_first = p_tbi;
1781 int try;
1782 int termcap_cleared = FALSE;
1783#endif
1784 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 char_u *bs_p, *del_p;
1787
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001788 /* In silect mode (ex -s) we don't use the 'term' option. */
1789 if (silent_mode)
1790 return OK;
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 detected_8bit = FALSE; /* reset 8-bit detection */
1793
1794 if (term_is_builtin(term))
1795 {
1796 term += 8;
1797#ifdef HAVE_TGETENT
1798 builtin_first = 1;
1799#endif
1800 }
1801
1802/*
1803 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1804 * If builtin_first is TRUE:
1805 * 0. try builtin termcap
1806 * 1. try external termcap
1807 * 2. if both fail default to a builtin terminal
1808 * If builtin_first is FALSE:
1809 * 1. try external termcap
1810 * 2. try builtin termcap, if both fail default to a builtin terminal
1811 */
1812#ifdef HAVE_TGETENT
1813 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1814 {
1815 /*
1816 * Use external termcap
1817 */
1818 if (try == 1)
1819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 /*
1823 * If the external termcap does not have a matching entry, try the
1824 * builtin ones.
1825 */
1826 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!termcap_cleared)
1829 {
1830 clear_termoptions(); /* clear old options */
1831 termcap_cleared = TRUE;
1832 }
1833
Bram Moolenaar69e05692018-05-10 14:11:52 +02001834 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836 }
1837 else /* try == 0 || try == 2 */
1838#endif /* HAVE_TGETENT */
1839 /*
1840 * Use builtin termcap
1841 */
1842 {
1843#ifdef HAVE_TGETENT
1844 /*
1845 * If builtin termcap was already used, there is no need to search
1846 * for the builtin termcap again, quit now.
1847 */
1848 if (try == 2 && builtin_first && termcap_cleared)
1849 break;
1850#endif
1851 /*
1852 * search for 'term' in builtin_termcaps[]
1853 */
1854 termp = find_builtin_term(term);
1855 if (termp->bt_string == NULL) /* did not find it */
1856 {
1857#ifdef HAVE_TGETENT
1858 /*
1859 * If try == 0, first try the external termcap. If that is not
1860 * found we'll get back here with try == 2.
1861 * If termcap_cleared is set we used the external termcap,
1862 * don't complain about not finding the term in the builtin
1863 * termcap.
1864 */
1865 if (try == 0) /* try external one */
1866 continue;
1867 if (termcap_cleared) /* found in external termcap */
1868 break;
1869#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001870 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 /* when user typed :set term=xxx, quit here */
1873 if (starting != NO_SCREEN)
1874 {
1875 screen_start(); /* don't know where cursor is now */
1876 wait_return(TRUE);
1877 return FAIL;
1878 }
1879 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001880 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001881 set_string_option_direct((char_u *)"term", -1, term,
1882 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 display_errors();
1884 }
1885 out_flush();
1886#ifdef HAVE_TGETENT
1887 if (!termcap_cleared)
1888 {
1889#endif
1890 clear_termoptions(); /* clear old options */
1891#ifdef HAVE_TGETENT
1892 termcap_cleared = TRUE;
1893 }
1894#endif
1895 parse_builtin_tcap(term);
1896#ifdef FEAT_GUI
1897 if (term_is_gui(term))
1898 {
1899 out_flush();
1900 gui_init();
1901 /* If starting the GUI failed, don't do any of the other
1902 * things for this terminal */
1903 if (!gui.in_use)
1904 return FAIL;
1905#ifdef HAVE_TGETENT
1906 break; /* don't try using external termcap */
1907#endif
1908 }
1909#endif /* FEAT_GUI */
1910 }
1911#ifdef HAVE_TGETENT
1912 }
1913#endif
1914
1915/*
1916 * special: There is no info in the termcap about whether the cursor
1917 * positioning is relative to the start of the screen or to the start of the
1918 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1919 * relative.
1920 */
1921 if (STRCMP(term, "pcterm") == 0)
1922 T_CCS = (char_u *)"yes";
1923 else
1924 T_CCS = empty_option;
1925
1926#ifdef UNIX
1927/*
1928 * Any "stty" settings override the default for t_kb from the termcap.
1929 * This is in os_unix.c, because it depends a lot on the version of unix that
1930 * is being used.
1931 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1932 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001933# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 get_stty();
1937#endif
1938
1939/*
1940 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1941 * didn't work, use the default CTRL-H
1942 * The default for t_kD is DEL, unless t_kb is DEL.
1943 * The vim_strsave'd strings are probably lost forever, well it's only two
1944 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1945 * directly.
1946 */
1947#ifdef FEAT_GUI
1948 if (!gui.in_use)
1949#endif
1950 {
1951 bs_p = find_termcode((char_u *)"kb");
1952 del_p = find_termcode((char_u *)"kD");
1953 if (bs_p == NULL || *bs_p == NUL)
1954 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1955 if ((del_p == NULL || *del_p == NUL) &&
1956 (bs_p == NULL || *bs_p != DEL))
1957 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1958 }
1959
1960#if defined(UNIX) || defined(VMS)
1961 term_is_xterm = vim_is_xterm(term);
1962#endif
1963
1964#ifdef FEAT_MOUSE
1965# if defined(UNIX) || defined(VMS)
1966# ifdef FEAT_MOUSE_TTY
1967 /*
1968 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1969 * The termcode for the mouse is added as a side effect in option.c.
1970 */
1971 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001972 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001975 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 if (use_xterm_mouse())
1978 p = NULL; /* keep existing value, might be "xterm2" */
1979 else
1980 p = (char_u *)"xterm";
1981 }
1982# endif
1983 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001986 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1987 * "xterm2" in check_termcode(). */
1988 reset_option_was_set((char_u *)"ttym");
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (p == NULL
1991# ifdef FEAT_GUI
1992 || gui.in_use
1993# endif
1994 )
1995 check_mouse_termcode(); /* set mouse termcode anyway */
1996 }
1997# endif
1998# else
1999 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
2000# endif
2001#endif /* FEAT_MOUSE */
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#ifdef USE_TERM_CONSOLE
2004 /* DEFAULT_TERM indicates that it is the machine console. */
2005 if (STRCMP(term, DEFAULT_TERM) != 0)
2006 term_console = FALSE;
2007 else
2008 {
2009 term_console = TRUE;
2010# ifdef AMIGA
2011 win_resize_on(); /* enable window resizing reports */
2012# endif
2013 }
2014#endif
2015
2016#if defined(UNIX) || defined(VMS)
2017 /*
2018 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2019 */
2020 if (vim_is_fastterm(term))
2021 p_tf = TRUE;
2022#endif
2023#ifdef USE_TERM_CONSOLE
2024 /*
2025 * 'ttyfast' is default on consoles
2026 */
2027 if (term_console)
2028 p_tf = TRUE;
2029#endif
2030
2031 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2032
2033 full_screen = TRUE; /* we can use termcap codes from now on */
2034 set_term_defaults(); /* use current values as defaults */
2035#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002036 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002037 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#endif
2039
2040 /*
2041 * Initialize the terminal with the appropriate termcap codes.
2042 * Set the mouse and window title if possible.
2043 * Don't do this when starting, need to parse the .vimrc first, because it
2044 * may redefine t_TI etc.
2045 */
2046 if (starting != NO_SCREEN)
2047 {
2048 starttermcap(); /* may change terminal mode */
2049#ifdef FEAT_MOUSE
2050 setmouse(); /* may start using the mouse */
2051#endif
2052#ifdef FEAT_TITLE
2053 maketitle(); /* may display window title */
2054#endif
2055 }
2056
2057 /* display initial screen after ttest() checking. jw. */
2058 if (width <= 0 || height <= 0)
2059 {
2060 /* termcap failed to report size */
2061 /* set defaults, in case ui_get_shellsize() also fails */
2062 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002063#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 height = 25; /* console is often 25 lines */
2065#else
2066 height = 24; /* most terminals are 24 lines */
2067#endif
2068 }
2069 set_shellsize(width, height, FALSE); /* may change Rows */
2070 if (starting != NO_SCREEN)
2071 {
2072 if (scroll_region)
2073 scroll_region_reset(); /* In case Rows changed */
2074 check_map_keycodes(); /* check mappings for terminal codes used */
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002077 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 /*
2080 * Execute the TermChanged autocommands for each buffer that is
2081 * loaded.
2082 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002083 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002084 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 if (curbuf->b_ml.ml_mfp != NULL)
2087 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2088 curbuf);
2089 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002090 if (bufref_valid(&old_curbuf))
2091 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094
2095#ifdef FEAT_TERMRESPONSE
2096 may_req_termresponse();
2097#endif
2098
2099 return OK;
2100}
2101
2102#if defined(FEAT_MOUSE) || defined(PROTO)
2103
2104# ifdef FEAT_MOUSE_TTY
2105# define HMT_NORMAL 1
2106# define HMT_NETTERM 2
2107# define HMT_DEC 4
2108# define HMT_JSBTERM 8
2109# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002110# define HMT_URXVT 32
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002111# define HMT_GPM 64
2112# define HMT_SGR 128
2113# define HMT_SGR_REL 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114static int has_mouse_termcode = 0;
2115# endif
2116
Bram Moolenaar864207d2008-06-24 22:14:38 +00002117# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002119set_mouse_termcode(
2120 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2121 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 char_u name[2];
2124
2125 name[0] = n;
2126 name[1] = KE_FILLER;
2127 add_termcode(name, s, FALSE);
2128# ifdef FEAT_MOUSE_TTY
2129# ifdef FEAT_MOUSE_JSB
2130 if (n == KS_JSBTERM_MOUSE)
2131 has_mouse_termcode |= HMT_JSBTERM;
2132 else
2133# endif
2134# ifdef FEAT_MOUSE_NET
2135 if (n == KS_NETTERM_MOUSE)
2136 has_mouse_termcode |= HMT_NETTERM;
2137 else
2138# endif
2139# ifdef FEAT_MOUSE_DEC
2140 if (n == KS_DEC_MOUSE)
2141 has_mouse_termcode |= HMT_DEC;
2142 else
2143# endif
2144# ifdef FEAT_MOUSE_PTERM
2145 if (n == KS_PTERM_MOUSE)
2146 has_mouse_termcode |= HMT_PTERM;
2147 else
2148# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002149# ifdef FEAT_MOUSE_URXVT
2150 if (n == KS_URXVT_MOUSE)
2151 has_mouse_termcode |= HMT_URXVT;
2152 else
2153# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002154# ifdef FEAT_MOUSE_GPM
2155 if (n == KS_GPM_MOUSE)
2156 has_mouse_termcode |= HMT_GPM;
2157 else
2158# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002159 if (n == KS_SGR_MOUSE)
2160 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002161 else if (n == KS_SGR_MOUSE_RELEASE)
2162 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 has_mouse_termcode |= HMT_NORMAL;
2165# endif
2166}
2167# endif
2168
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002169# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002170 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002172del_mouse_termcode(
2173 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 char_u name[2];
2176
2177 name[0] = n;
2178 name[1] = KE_FILLER;
2179 del_termcode(name);
2180# ifdef FEAT_MOUSE_TTY
2181# ifdef FEAT_MOUSE_JSB
2182 if (n == KS_JSBTERM_MOUSE)
2183 has_mouse_termcode &= ~HMT_JSBTERM;
2184 else
2185# endif
2186# ifdef FEAT_MOUSE_NET
2187 if (n == KS_NETTERM_MOUSE)
2188 has_mouse_termcode &= ~HMT_NETTERM;
2189 else
2190# endif
2191# ifdef FEAT_MOUSE_DEC
2192 if (n == KS_DEC_MOUSE)
2193 has_mouse_termcode &= ~HMT_DEC;
2194 else
2195# endif
2196# ifdef FEAT_MOUSE_PTERM
2197 if (n == KS_PTERM_MOUSE)
2198 has_mouse_termcode &= ~HMT_PTERM;
2199 else
2200# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002201# ifdef FEAT_MOUSE_URXVT
2202 if (n == KS_URXVT_MOUSE)
2203 has_mouse_termcode &= ~HMT_URXVT;
2204 else
2205# endif
Bram Moolenaar5d0183b2019-05-11 21:38:58 +02002206# ifdef FEAT_MOUSE_GPM
2207 if (n == KS_GPM_MOUSE)
2208 has_mouse_termcode &= ~HMT_GPM;
2209 else
2210# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002211 if (n == KS_SGR_MOUSE)
2212 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002213 else if (n == KS_SGR_MOUSE_RELEASE)
2214 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 has_mouse_termcode &= ~HMT_NORMAL;
2217# endif
2218}
2219# endif
2220#endif
2221
2222#ifdef HAVE_TGETENT
2223/*
2224 * Call tgetent()
2225 * Return error message if it fails, NULL if it's OK.
2226 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002227 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002228tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 int i;
2231
2232 i = TGETENT(tbuf, term);
2233 if (i < 0 /* -1 is always an error */
2234# ifdef TGETENT_ZERO_ERR
2235 || i == 0 /* sometimes zero is also an error */
2236# endif
2237 )
2238 {
2239 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2240 * tgetent() with the always existing "dumb" entry to avoid a crash or
2241 * hang. */
2242 (void)TGETENT(tbuf, "dumb");
2243
2244 if (i < 0)
2245# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002246 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (i == 0)
2248# endif
2249#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002252 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#endif
2254 }
2255 return NULL;
2256}
2257
2258/*
2259 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2260 * Fix that here.
2261 */
2262 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 char *p;
2266
2267 p = tgetstr(s, (char **)pp);
2268 if (p == (char *)-1)
2269 p = NULL;
2270 return (char_u *)p;
2271}
2272#endif /* HAVE_TGETENT */
2273
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002274#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275/*
2276 * Get Columns and Rows from the termcap. Used after a window signal if the
2277 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2278 * and "li" entries never change. But on some systems this works.
2279 * Errors while getting the entries are ignored.
2280 */
2281 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002282getlinecol(
2283 long *cp, /* pointer to columns */
2284 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 char_u tbuf[TBUFSZ];
2287
2288 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2289 {
2290 if (*cp == 0)
2291 *cp = tgetnum("co");
2292 if (*rp == 0)
2293 *rp = tgetnum("li");
2294 }
2295}
2296#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2297
2298/*
2299 * Get a string entry from the termcap and add it to the list of termcodes.
2300 * Used for <t_xx> special keys.
2301 * Give an error message for failure when not sourcing.
2302 * If force given, replace an existing entry.
2303 * Return FAIL if the entry was not found, OK if the entry was added.
2304 */
2305 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002306add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 char_u *term;
2309 int key;
2310 struct builtin_term *termp;
2311#ifdef HAVE_TGETENT
2312 char_u *string;
2313 int i;
2314 int builtin_first;
2315 char_u tbuf[TBUFSZ];
2316 char_u tstrbuf[TBUFSZ];
2317 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002318 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
2320
2321/*
2322 * If the GUI is running or will start in a moment, we only support the keys
2323 * that the GUI can produce.
2324 */
2325#ifdef FEAT_GUI
2326 if (gui.in_use || gui.starting)
2327 return gui_mch_haskey(name);
2328#endif
2329
2330 if (!force && find_termcode(name) != NULL) /* it's already there */
2331 return OK;
2332
2333 term = T_NAME;
2334 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2335 return FAIL;
2336
2337 if (term_is_builtin(term)) /* name starts with "builtin_" */
2338 {
2339 term += 8;
2340#ifdef HAVE_TGETENT
2341 builtin_first = TRUE;
2342#endif
2343 }
2344#ifdef HAVE_TGETENT
2345 else
2346 builtin_first = p_tbi;
2347#endif
2348
2349#ifdef HAVE_TGETENT
2350/*
2351 * We can get the entry from the builtin termcap and from the external one.
2352 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2353 * builtin termcap first.
2354 * If 'ttybuiltin' is off, try external termcap first.
2355 */
2356 for (i = 0; i < 2; ++i)
2357 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002358 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360 /*
2361 * Search in builtin termcap
2362 */
2363 {
2364 termp = find_builtin_term(term);
2365 if (termp->bt_string != NULL) /* found it */
2366 {
2367 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002368 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 while (termp->bt_entry != (int)KS_NAME)
2370 {
2371 if ((int)termp->bt_entry == key)
2372 {
2373 add_termcode(name, (char_u *)termp->bt_string,
2374 term_is_8bit(term));
2375 return OK;
2376 }
2377 ++termp;
2378 }
2379 }
2380 }
2381#ifdef HAVE_TGETENT
2382 else
2383 /*
2384 * Search in external termcap
2385 */
2386 {
2387 error_msg = tgetent_error(tbuf, term);
2388 if (error_msg == NULL)
2389 {
2390 string = TGETSTR((char *)name, &tp);
2391 if (string != NULL && *string != NUL)
2392 {
2393 add_termcode(name, string, FALSE);
2394 return OK;
2395 }
2396 }
2397 }
2398 }
2399#endif
2400
2401 if (sourcing_name == NULL)
2402 {
2403#ifdef HAVE_TGETENT
2404 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
2407#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 return FAIL;
2411}
2412
2413 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002414term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2417}
2418
2419/*
2420 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2421 * Assume that the terminal is using 8-bit controls when the name contains
2422 * "8bit", like in "xterm-8bit".
2423 */
2424 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002425term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2428}
2429
2430/*
2431 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002432 * <Esc>[ -> CSI <M_C_[>
2433 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 * <Esc>O -> <M-C-O>
2435 */
2436 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002437term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (*p == ESC)
2440 {
2441 if (p[1] == '[')
2442 return CSI;
2443 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002444 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (p[1] == 'O')
2446 return 0x8f;
2447 }
2448 return 0;
2449}
2450
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002451#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002453term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2456}
2457#endif
2458
2459#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2460
2461 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002462tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 static char_u buf[16];
2465 char_u *p;
2466
2467 p = buf + 15;
2468 *p = '\0';
2469 do
2470 {
2471 --p;
2472 *p = (char_u) (i % 10 + '0');
2473 i /= 10;
2474 }
2475 while (i > 0 && p > buf);
2476 return p;
2477}
2478#endif
2479
2480#ifndef HAVE_TGETENT
2481
2482/*
2483 * minimal tgoto() implementation.
2484 * no padding and we only parse for %i %d and %+char
2485 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002486 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002487tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 static char buf[30];
2490 char *p, *s, *e;
2491
2492 if (!cm)
2493 return "OOPS";
2494 e = buf + 29;
2495 for (s = buf; s < e && *cm; cm++)
2496 {
2497 if (*cm != '%')
2498 {
2499 *s++ = *cm;
2500 continue;
2501 }
2502 switch (*++cm)
2503 {
2504 case 'd':
2505 p = (char *)tltoa((unsigned long)y);
2506 y = x;
2507 while (*p)
2508 *s++ = *p++;
2509 break;
2510 case 'i':
2511 x++;
2512 y++;
2513 break;
2514 case '+':
2515 *s++ = (char)(*++cm + y);
2516 y = x;
2517 break;
2518 case '%':
2519 *s++ = *cm;
2520 break;
2521 default:
2522 return "OOPS";
2523 }
2524 }
2525 *s = '\0';
2526 return buf;
2527}
2528
2529#endif /* HAVE_TGETENT */
2530
2531/*
2532 * Set the terminal name and initialize the terminal options.
2533 * If "name" is NULL or empty, get the terminal name from the environment.
2534 * If that fails, use the default terminal name.
2535 */
2536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002537termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 char_u *term;
2540
2541 if (name != NULL && *name == NUL)
2542 name = NULL; /* empty name is equal to no name */
2543 term = name;
2544
2545#ifdef __BEOS__
2546 /*
2547 * TERM environment variable is normally set to 'ansi' on the Bebox;
2548 * Since the BeBox doesn't quite support full ANSI yet, we use our
2549 * own custom 'ansi-beos' termcap instead, unless the -T option has
2550 * been given on the command line.
2551 */
2552 if (term == NULL
2553 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2554 term = DEFAULT_TERM;
2555#endif
2556#ifndef MSWIN
2557 if (term == NULL)
2558 term = mch_getenv((char_u *)"TERM");
2559#endif
2560 if (term == NULL || *term == NUL)
2561 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002562 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 /* Set the default terminal name. */
2565 set_string_default("term", term);
2566 set_string_default("ttytype", term);
2567
2568 /*
2569 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2570 */
2571 set_termname(T_NAME != NULL ? T_NAME : term);
2572}
2573
2574/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002575 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002577#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002578
2579// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002581
2582static int out_pos = 0; // number of chars in out_buf
2583
2584// Since the maximum number of SGR parameters shown as a normal value range is
2585// 16, the escape sequence length can be 4 * 16 + lead + tail.
2586#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588/*
2589 * out_flush(): flush the output buffer
2590 */
2591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 int len;
2595
2596 if (out_pos != 0)
2597 {
2598 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2599 len = out_pos;
2600 out_pos = 0;
2601 ui_write(out_buf, len);
2602 }
2603}
2604
Bram Moolenaara338adc2018-01-31 20:51:47 +01002605/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002606 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2607 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002608 */
2609 void
2610out_flush_cursor(
2611 int force UNUSED, /* when TRUE, update cursor even when not moved */
2612 int clear_selection UNUSED) /* clear selection under cursor */
2613{
2614 mch_disable_flush();
2615 out_flush();
2616 mch_enable_flush();
2617#ifdef FEAT_GUI
2618 if (gui.in_use)
2619 {
2620 gui_update_cursor(force, clear_selection);
2621 gui_may_flush();
2622 }
2623#endif
2624}
2625
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627/*
2628 * Sometimes a byte out of a multi-byte character is written with out_char().
2629 * To avoid flushing half of the character, call this function first.
2630 */
2631 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002632out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2635 out_flush();
2636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638#ifdef FEAT_GUI
2639/*
2640 * out_trash(): Throw away the contents of the output buffer
2641 */
2642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002643out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 out_pos = 0;
2646}
2647#endif
2648
2649/*
2650 * out_char(c): put a byte into the output buffer.
2651 * Flush it if it becomes full.
2652 * This should not be used for outputting text on the screen (use functions
2653 * like msg_puts() and screen_putchar() for that).
2654 */
2655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002656out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
Bram Moolenaard0573012017-10-28 21:11:06 +02002658#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2660 out_char('\r');
2661#endif
2662
2663 out_buf[out_pos++] = c;
2664
2665 /* For testing we flush each time. */
2666 if (out_pos >= OUT_SIZE || p_wd)
2667 out_flush();
2668}
2669
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002670static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672/*
2673 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2674 */
2675 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaard0573012017-10-28 21:11:06 +02002678#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2680 out_char_nf('\r');
2681#endif
2682
2683 out_buf[out_pos++] = c;
2684
2685 if (out_pos >= OUT_SIZE)
2686 out_flush();
2687}
2688
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002689#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2690 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691/*
2692 * A never-padding out_str.
2693 * use this whenever you don't want to run the string through tputs.
2694 * tputs above is harmless, but tputs from the termcap library
2695 * is likely to strip off leading digits, that it mistakes for padding
2696 * information, and "%i", "%d", etc.
2697 * This should only be used for writing terminal codes, not for outputting
2698 * normal text (use functions like msg_puts() and screen_putchar() for that).
2699 */
2700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002703 // avoid terminal strings being split up
2704 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 while (*s)
2708 out_char_nf(*s++);
2709
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002710 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (p_wd)
2712 out_flush();
2713}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002717 * A conditional-flushing out_str, mainly for visualbell.
2718 * Handles a delay internally, because termlib may not respect the delay or do
2719 * it at the wrong time.
2720 * Note: Only for terminal strings.
2721 */
2722 void
2723out_str_cf(char_u *s)
2724{
2725 if (s != NULL && *s)
2726 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002727#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002728 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002730
2731#ifdef FEAT_GUI
2732 /* Don't use tputs() when GUI is used, ncurses crashes. */
2733 if (gui.in_use)
2734 {
2735 out_str_nf(s);
2736 return;
2737 }
2738#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002739 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002740 out_flush();
2741#ifdef HAVE_TGETENT
2742 for (p = s; *s; ++s)
2743 {
2744 /* flush just before delay command */
2745 if (*s == '$' && *(s + 1) == '<')
2746 {
2747 char_u save_c = *s;
2748 int duration = atoi((char *)s + 2);
2749
2750 *s = NUL;
2751 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2752 *s = save_c;
2753 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002754# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002755 /* Only sleep here if we can limit this happening in
2756 * vim_beep(). */
2757 p = vim_strchr(s, '>');
2758 if (p == NULL || duration <= 0)
2759 {
2760 /* can't parse the time, don't sleep here */
2761 p = s;
2762 }
2763 else
2764 {
2765 ++p;
2766 do_sleep(duration);
2767 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002768# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002769 /* Rely on the terminal library to sleep. */
2770 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002771# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002772 break;
2773 }
2774 }
2775 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2776#else
2777 while (*s)
2778 out_char_nf(*s++);
2779#endif
2780
2781 /* For testing we write one string at a time. */
2782 if (p_wd)
2783 out_flush();
2784 }
2785}
2786
2787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * out_str(s): Put a character string a byte at a time into the output buffer.
2789 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2790 * This should only be used for writing terminal codes, not for outputting
2791 * normal text (use functions like msg_puts() and screen_putchar() for that).
2792 */
2793 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002794out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 if (s != NULL && *s)
2797 {
2798#ifdef FEAT_GUI
2799 /* Don't use tputs() when GUI is used, ncurses crashes. */
2800 if (gui.in_use)
2801 {
2802 out_str_nf(s);
2803 return;
2804 }
2805#endif
2806 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002807 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 out_flush();
2809#ifdef HAVE_TGETENT
2810 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2811#else
2812 while (*s)
2813 out_char_nf(*s++);
2814#endif
2815
2816 /* For testing we write one string at a time. */
2817 if (p_wd)
2818 out_flush();
2819 }
2820}
2821
2822/*
2823 * cursor positioning using termcap parser. (jw)
2824 */
2825 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002826term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 OUT_STR(tgoto((char *)T_CM, col, row));
2829}
2830
2831 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002832term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
2834 OUT_STR(tgoto((char *)T_CRI, 0, i));
2835}
2836
2837 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002838term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
2840 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2841}
2842
2843 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002844term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2847}
2848
2849#if defined(HAVE_TGETENT) || defined(PROTO)
2850 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002851term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 /* Can't handle a negative value here */
2854 if (x < 0)
2855 x = 0;
2856 if (y < 0)
2857 y = 0;
2858 OUT_STR(tgoto((char *)T_CWP, y, x));
2859}
2860
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002861# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2862/*
2863 * Return TRUE if we can request the terminal for a response.
2864 */
2865 static int
2866can_get_termresponse()
2867{
2868 return cur_tmode == TMODE_RAW
2869 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002870# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002871 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002872# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002873 && p_ek;
2874}
2875
Bram Moolenaarafd78262019-05-10 23:10:31 +02002876/*
2877 * Set "status" to STATUS_SENT.
2878 */
2879 static void
2880termrequest_sent(termrequest_T *status)
2881{
2882 status->tr_progress = STATUS_SENT;
2883 status->tr_start = time(NULL);
2884}
2885
2886/*
2887 * Return TRUE if any of the requests are in STATUS_SENT.
2888 */
2889 static int
2890termrequest_any_pending()
2891{
2892 int i;
2893 time_t now = time(NULL);
2894
2895 for (i = 0; all_termrequests[i] != NULL; ++i)
2896 {
2897 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2898 {
2899 if (all_termrequests[i]->tr_start > 0 && now > 0
2900 && all_termrequests[i]->tr_start + 2 < now)
2901 // Sent the request more than 2 seconds ago and didn't get a
2902 // response, assume it failed.
2903 all_termrequests[i]->tr_progress = STATUS_FAIL;
2904 else
2905 return TRUE;
2906 }
2907 }
2908 return FALSE;
2909}
2910
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002911static int winpos_x = -1;
2912static int winpos_y = -1;
2913static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002914
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002915# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002916/*
2917 * Try getting the Vim window position from the terminal.
2918 * Returns OK or FAIL.
2919 */
2920 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002921term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002922{
2923 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002924 int prev_winpos_x = winpos_x;
2925 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002926
2927 if (*T_CGP == NUL || !can_get_termresponse())
2928 return FAIL;
2929 winpos_x = -1;
2930 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002931 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002932 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002933 OUT_STR(T_CGP);
2934 out_flush();
2935
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002936 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002937 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002938 {
2939 (void)vpeekc_nomap();
2940 if (winpos_x >= 0 && winpos_y >= 0)
2941 {
2942 *x = winpos_x;
2943 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002944 return OK;
2945 }
2946 ui_delay(10, FALSE);
2947 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002948 /* Do not reset "did_request_winpos", if we timed out the response might
2949 * still come later and we must consume it. */
2950
2951 winpos_x = prev_winpos_x;
2952 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002953 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002954 {
2955 /* Polling: return previous values if we have them. */
2956 *x = winpos_x;
2957 *y = winpos_y;
2958 return OK;
2959 }
2960
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002961 return FALSE;
2962}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002963# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002964# endif
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002967term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002969 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970}
2971#endif
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002974term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002977 int i = *s == CSI ? 1 : 2;
2978 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /* Special handling of 16 colors, because termcap can't handle it */
2981 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2982 /* Also accept CSI instead of <Esc>[ */
2983 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002984 && ((s[0] == ESC && s[1] == '[')
2985#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2986 || (s[0] == ESC && s[1] == '|')
2987#endif
2988 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 && s[i] != NUL
2990 && (STRCMP(s + i + 1, "%p1%dm") == 0
2991 || STRCMP(s + i + 1, "%dm") == 0)
2992 && (s[i] == '3' || s[i] == '4'))
2993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002995 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002997 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002999 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003000#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02003001 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003002#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003003 IF_EB("\033[", ESC_STR "[")) : "\233";
3004 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3005 : (n >= 16 ? "48;5;" : "10");
3006
3007 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3009 }
3010 else
3011 OUT_STR(tgoto((char *)s, 0, n));
3012}
3013
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003014 void
3015term_fg_color(int n)
3016{
3017 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
3018 if (*T_CAF)
3019 term_color(T_CAF, n);
3020 else if (*T_CSF)
3021 term_color(T_CSF, n);
3022}
3023
3024 void
3025term_bg_color(int n)
3026{
3027 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
3028 if (*T_CAB)
3029 term_color(T_CAB, n);
3030 else if (*T_CSB)
3031 term_color(T_CSB, n);
3032}
3033
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003034#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003035
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003036#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3037#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3038#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003039
3040 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003041term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003042{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003043#define MAX_COLOR_STR_LEN 100
3044 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003045
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003046 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003047 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003048 OUT_STR(buf);
3049}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003050
3051 void
3052term_fg_rgb_color(guicolor_T rgb)
3053{
3054 term_rgb_color(T_8F, rgb);
3055}
3056
3057 void
3058term_bg_rgb_color(guicolor_T rgb)
3059{
3060 term_rgb_color(T_8B, rgb);
3061}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003062#endif
3063
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003064#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3065 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066/*
3067 * Generic function to set window title, using t_ts and t_fs.
3068 */
3069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003070term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 /* t_ts takes one argument: column in status line */
3073 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
3074 out_str_nf(title);
3075 out_str(T_FS); /* set title end */
3076 out_flush();
3077}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003078
3079/*
3080 * Tell the terminal to push (save) the title and/or icon, so that it can be
3081 * popped (restored) later.
3082 */
3083 void
3084term_push_title(int which)
3085{
Bram Moolenaar27821262019-05-08 16:41:09 +02003086 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003087 {
3088 OUT_STR(T_CST);
3089 out_flush();
3090 }
3091
Bram Moolenaar27821262019-05-08 16:41:09 +02003092 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003093 {
3094 OUT_STR(T_SSI);
3095 out_flush();
3096 }
3097}
3098
3099/*
3100 * Tell the terminal to pop the title and/or icon.
3101 */
3102 void
3103term_pop_title(int which)
3104{
Bram Moolenaar27821262019-05-08 16:41:09 +02003105 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003106 {
3107 OUT_STR(T_CRT);
3108 out_flush();
3109 }
3110
Bram Moolenaar27821262019-05-08 16:41:09 +02003111 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003112 {
3113 OUT_STR(T_SRI);
3114 out_flush();
3115 }
3116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
3118
3119/*
3120 * Make sure we have a valid set or terminal options.
3121 * Replace all entries that are NULL by empty_option
3122 */
3123 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003124ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003126 char_u *env_colors;
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 check_options(); /* make sure no options are NULL */
3129
3130 /*
3131 * MUST have "cm": cursor motion.
3132 */
3133 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003134 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * if "cs" defined, use a scroll region, it's faster.
3138 */
3139 if (*T_CS != NUL)
3140 scroll_region = TRUE;
3141 else
3142 scroll_region = FALSE;
3143
3144 if (pairs)
3145 {
3146 /*
3147 * optional pairs
3148 */
3149 /* TP goes to normal mode for TI (invert) and TB (bold) */
3150 if (*T_ME == NUL)
3151 T_ME = T_MR = T_MD = T_MB = empty_option;
3152 if (*T_SO == NUL || *T_SE == NUL)
3153 T_SO = T_SE = empty_option;
3154 if (*T_US == NUL || *T_UE == NUL)
3155 T_US = T_UE = empty_option;
3156 if (*T_CZH == NUL || *T_CZR == NUL)
3157 T_CZH = T_CZR = empty_option;
3158
3159 /* T_VE is needed even though T_VI is not defined */
3160 if (*T_VE == NUL)
3161 T_VI = empty_option;
3162
3163 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3164 if (*T_ME == NUL)
3165 {
3166 T_ME = T_SE;
3167 T_MR = T_SO;
3168 T_MD = T_SO;
3169 }
3170
3171 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3172 if (*T_SO == NUL)
3173 {
3174 T_SE = T_ME;
3175 if (*T_MR == NUL)
3176 T_SO = T_MD;
3177 else
3178 T_SO = T_MR;
3179 }
3180
3181 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3182 if (*T_CZH == NUL)
3183 {
3184 T_CZR = T_ME;
3185 if (*T_MR == NUL)
3186 T_CZH = T_MD;
3187 else
3188 T_CZH = T_MR;
3189 }
3190
3191 /* "Sb" and "Sf" come in pairs */
3192 if (*T_CSB == NUL || *T_CSF == NUL)
3193 {
3194 T_CSB = empty_option;
3195 T_CSF = empty_option;
3196 }
3197
3198 /* "AB" and "AF" come in pairs */
3199 if (*T_CAB == NUL || *T_CAF == NUL)
3200 {
3201 T_CAB = empty_option;
3202 T_CAF = empty_option;
3203 }
3204
3205 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3206 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003207 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /* Set 'weirdinvert' according to value of 't_xs' */
3210 p_wiv = (*T_XS != NUL);
3211 }
3212 need_gather = TRUE;
3213
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003214 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003216 env_colors = mch_getenv((char_u *)"COLORS");
3217 if (env_colors != NULL && isdigit(*env_colors))
3218 {
3219 int colors = atoi((char *)env_colors);
3220
3221 if (colors != t_colors)
3222 set_color_count(colors);
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3227 || defined(PROTO)
3228/*
3229 * Represent the given long_u as individual bytes, with the most significant
3230 * byte first, and store them in dst.
3231 */
3232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 int i;
3236 int shift;
3237
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003238 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 shift = 8 * (sizeof(long_u) - i);
3241 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3242 }
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245/*
3246 * Interpret the next string of bytes in buf as a long integer, with the most
3247 * significant byte first. Note that it is assumed that buf has been through
3248 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3249 * Puts result in val, and returns the number of bytes read from buf
3250 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3251 * were present.
3252 */
3253 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 int len;
3257 char_u bytes[sizeof(long_u)];
3258 int i;
3259 int shift;
3260
3261 *val = 0;
3262 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3263 if (len != -1)
3264 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003265 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 shift = 8 * (sizeof(long_u) - 1 - i);
3268 *val += (long_u)bytes[i] << shift;
3269 }
3270 }
3271 return len;
3272}
3273#endif
3274
3275#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003276 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3277 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
3279 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3280 * that buf has been through inchar(). Returns the actual number of bytes used
3281 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3282 * available.
3283 */
3284 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003285get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 int len = 0;
3288 int i;
3289 char_u c;
3290
3291 for (i = 0; i < num_bytes; i++)
3292 {
3293 if ((c = buf[len++]) == NUL)
3294 return -1;
3295 if (c == K_SPECIAL)
3296 {
3297 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3298 return -1;
3299 if (buf[len++] == (int)KS_ZERO)
3300 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003301 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3302 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3303 if (buf[len++] == (int)KE_CSI)
3304 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003306 else if (c == CSI && buf[len] == KS_EXTRA
3307 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3309 * the start of a special key, see add_to_input_buf_csi(). */
3310 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 bytes[i] = c;
3312 }
3313 return len;
3314}
3315#endif
3316
3317/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003318 * Check if the new shell size is valid, correct it if it's too small or way
3319 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (Rows < min_rows()) /* need room for one window and command line */
3325 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003326 limit_screen_size();
3327}
3328
3329/*
3330 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3331 */
3332 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003333limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003334{
3335 if (Columns < MIN_COLUMNS)
3336 Columns = MIN_COLUMNS;
3337 else if (Columns > 10000)
3338 Columns = 10000;
3339 if (Rows > 1000)
3340 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
Bram Moolenaar86b68352004-12-27 21:59:20 +00003343/*
3344 * Invoked just before the screen structures are going to be (re)allocated.
3345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003347win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 static int old_Rows = 0;
3350 static int old_Columns = 0;
3351
3352 if (old_Rows != Rows || old_Columns != Columns)
3353 ui_new_shellsize();
3354 if (old_Rows != Rows)
3355 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003356 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003357 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003358 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 old_Rows = Rows;
3360 shell_new_rows(); /* update window sizes */
3361 }
3362 if (old_Columns != Columns)
3363 {
3364 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367}
3368
3369/*
3370 * Call this function when the Vim shell has been resized in any way.
3371 * Will obtain the current size and redraw (also when size didn't change).
3372 */
3373 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 set_shellsize(0, 0, FALSE);
3377}
3378
3379/*
3380 * Check if the shell size changed. Handle a resize.
3381 * When the size didn't change, nothing happens.
3382 */
3383 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003384shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 int old_Rows = Rows;
3387 int old_Columns = Columns;
3388
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003389 if (!exiting
3390#ifdef FEAT_GUI
3391 /* Do not get the size when executing a shell command during
3392 * startup. */
3393 && !gui.starting
3394#endif
3395 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003396 {
3397 (void)ui_get_shellsize();
3398 check_shellsize();
3399 if (old_Rows != Rows || old_Columns != Columns)
3400 shell_resized();
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
3404/*
3405 * Set size of the Vim shell.
3406 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3407 * window size (this is used for the :win command).
3408 * If 'mustset' is FALSE, we may try to get the real window size and if
3409 * it fails use 'width' and 'height'.
3410 */
3411 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003412set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 static int busy = FALSE;
3415
3416 /*
3417 * Avoid recursiveness, can happen when setting the window size causes
3418 * another window-changed signal.
3419 */
3420 if (busy)
3421 return;
3422
3423 if (width < 0 || height < 0) /* just checking... */
3424 return;
3425
Bram Moolenaara971b822011-09-14 14:43:25 +02003426 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003428 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 State = SETWSIZE;
3430 return;
3431 }
3432
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003433 if (updating_screen)
3434 // resizing while in update_screen() may cause a crash
3435 return;
3436
Bram Moolenaara971b822011-09-14 14:43:25 +02003437 /* curwin->w_buffer can be NULL when we are closing a window and the
3438 * buffer has already been closed and removing a scrollbar causes a resize
3439 * event. Don't resize then, it will happen after entering another buffer.
3440 */
3441 if (curwin->w_buffer == NULL)
3442 return;
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 ++busy;
3445
3446#ifdef AMIGA
3447 out_flush(); /* must do this before mch_get_shellsize() for
3448 some obscure reason */
3449#endif
3450
3451 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3452 {
3453 Rows = height;
3454 Columns = width;
3455 check_shellsize();
3456 ui_set_shellsize(mustset);
3457 }
3458 else
3459 check_shellsize();
3460
3461 /* The window layout used to be adjusted here, but it now happens in
3462 * screenalloc() (also invoked from screenclear()). That is because the
3463 * "busy" check above may skip this, but not screenalloc(). */
3464
3465 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3466 screenclear();
3467 else
3468 screen_start(); /* don't know where cursor is now */
3469
3470 if (starting != NO_SCREEN)
3471 {
3472#ifdef FEAT_TITLE
3473 maketitle();
3474#endif
3475 changed_line_abv_curs();
3476 invalidate_botline();
3477
3478 /*
3479 * We only redraw when it's needed:
3480 * - While at the more prompt or executing an external command, don't
3481 * redraw, but position the cursor.
3482 * - While editing the command line, only redraw that.
3483 * - in Ex mode, don't redraw anything.
3484 * - Otherwise, redraw right now, and position the cursor.
3485 * Always need to call update_screen() or screenalloc(), to make
3486 * sure Rows/Columns and the size of ScreenLines[] is correct!
3487 */
3488 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3489 || exmode_active)
3490 {
3491 screenalloc(FALSE);
3492 repeat_message();
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 else
3495 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003496 if (curwin->w_p_scb)
3497 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003498 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003499 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003500 update_screen(NOT_VALID);
3501 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003502 }
3503 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003504 {
3505 update_topline();
3506#if defined(FEAT_INS_EXPAND)
3507 if (pum_visible())
3508 {
3509 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003510 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003511 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003512#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003513 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003514 if (redrawing())
3515 setcursor();
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518 cursor_on(); /* redrawing may have switched it off */
3519 }
3520 out_flush();
3521 --busy;
3522}
3523
3524/*
3525 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3526 * commands and Ex mode).
3527 */
3528 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003529settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531#ifdef FEAT_GUI
3532 /* don't set the term where gvim was started to any mode */
3533 if (gui.in_use)
3534 return;
3535#endif
3536
3537 if (full_screen)
3538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 /*
3540 * When returning after calling a shell we want to really set the
3541 * terminal to raw mode, even though we think it already is, because
3542 * the shell program may have reset the terminal mode.
3543 * When we think the terminal is normal, don't try to set it to
3544 * normal again, because that causes problems (logout!) on some
3545 * machines.
3546 */
3547 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3548 {
3549#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003550# ifdef FEAT_GUI
3551 if (!gui.in_use && !gui.starting)
3552# endif
3553 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003554 // May need to check for T_CRV response and termcodes, it
3555 // doesn't work in Cooked mode, an external program may get
3556 // them.
3557 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003558 (void)vpeekc_nomap();
3559 check_for_codes_from_term();
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
3562#ifdef FEAT_MOUSE_TTY
3563 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003564 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003566 if (termcap_active)
3567 {
3568 if (tmode != TMODE_RAW)
3569 out_str(T_BD); // disable bracketed paste mode
3570 else
3571 out_str(T_BE); // enable bracketed paste mode (should
3572 // be before mch_settmode().
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003575 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 cur_tmode = tmode;
3577#ifdef FEAT_MOUSE
3578 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003579 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#endif
3581 out_flush();
3582 }
3583#ifdef FEAT_TERMRESPONSE
3584 may_req_termresponse();
3585#endif
3586 }
3587}
3588
3589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 if (full_screen && !termcap_active)
3593 {
3594 out_str(T_TI); /* start termcap mode */
3595 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003596 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 out_flush();
3598 termcap_active = TRUE;
3599 screen_start(); /* don't know where cursor is now */
3600#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003601# ifdef FEAT_GUI
3602 if (!gui.in_use && !gui.starting)
3603# endif
3604 {
3605 may_req_termresponse();
3606 /* Immediately check for a response. If t_Co changes, we don't
3607 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003608 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003609 check_for_codes_from_term();
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611#endif
3612 }
3613}
3614
3615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003616stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 screen_stop_highlight();
3619 reset_cterm_colors();
3620 if (termcap_active)
3621 {
3622#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003623# ifdef FEAT_GUI
3624 if (!gui.in_use && !gui.starting)
3625# endif
3626 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003627 // May need to discard T_CRV, T_U7 or T_RBG response.
3628 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003629 {
3630# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003631 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003632 mch_delay(100L, FALSE);
3633# endif
3634# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003635 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003636 if (exiting)
3637 tcflush(fileno(stdin), TCIFLUSH);
3638# endif
3639 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003640 /* Check for termcodes first, otherwise an external program may
3641 * get them. */
3642 check_for_codes_from_term();
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003645 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 out_str(T_KE); /* stop "keypad transmit" mode */
3647 out_flush();
3648 termcap_active = FALSE;
3649 cursor_on(); /* just in case it is still off */
3650 out_str(T_TE); /* stop termcap mode */
3651 screen_start(); /* don't know where cursor is now */
3652 out_flush();
3653 }
3654}
3655
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003656#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657/*
3658 * Request version string (for xterm) when needed.
3659 * Only do this after switching to raw mode, otherwise the result will be
3660 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003661 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003662 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * Only do this after termcap mode has been started, otherwise the codes for
3664 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003665 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3666 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003667 * On Unix only do it when both output and input are a tty (avoid writing
3668 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 * The result is caught in check_termcode().
3670 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003671 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003672may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003674 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003675 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003676 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 && *T_CRV != NUL)
3678 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003679 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003681 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 /* check for the characters now, otherwise they might be eaten by
3683 * get_keystroke() */
3684 out_flush();
3685 (void)vpeekc_nomap();
3686 }
3687}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003688
Bram Moolenaar9584b312013-03-13 19:29:28 +01003689/*
3690 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003691 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003692 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003693 * If the terminal treats \u25bd as single width, the position is (1, 1),
3694 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003695 * This function has the side effect that changes cursor position, so
3696 * it must be called immediately after entering termcap mode.
3697 */
3698 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003699may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003700{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003701 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003702 && can_get_termresponse()
3703 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003704 && *T_U7 != NUL
3705 && !option_was_set((char_u *)"ambiwidth"))
3706 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003707 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003708
Bram Moolenaarafd78262019-05-10 23:10:31 +02003709 LOG_TR(("Sending U7 request"));
3710 /* Do this in the second row. In the first row the returned sequence
3711 * may be CSI 1;2R, which is the same as <S-F3>. */
3712 term_windgoto(1, 0);
3713 buf[mb_char2bytes(0x25bd, buf)] = 0;
3714 out_str(buf);
3715 out_str(T_U7);
3716 termrequest_sent(&u7_status);
3717 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003718
Bram Moolenaarafd78262019-05-10 23:10:31 +02003719 /* This overwrites a few characters on the screen, a redraw is needed
3720 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003721 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003722 term_windgoto(1, 0);
3723 out_str((char_u *)" ");
3724 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003725
Bram Moolenaarafd78262019-05-10 23:10:31 +02003726 /* Need to reset the known cursor position. */
3727 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003728
Bram Moolenaarafd78262019-05-10 23:10:31 +02003729 /* check for the characters now, otherwise they might be eaten by
3730 * get_keystroke() */
3731 out_flush();
3732 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003733 }
3734}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003735
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003736/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003737 * Similar to requesting the version string: Request the terminal background
3738 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003739 */
3740 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003741may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003742{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003743 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003744 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003745 int didit = FALSE;
3746
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003747# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003748 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003749 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003750 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003751 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003752 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003753 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003754 didit = TRUE;
3755 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003756# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003757
3758 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003759 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003760 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003761 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003762 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003763 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003764 didit = TRUE;
3765 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003766
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003767 if (didit)
3768 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003769 /* check for the characters now, otherwise they might be eaten by
3770 * get_keystroke() */
3771 out_flush();
3772 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003773 }
3774 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003775}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003776
Bram Moolenaar2951b772013-07-03 12:45:31 +02003777# ifdef DEBUG_TERMRESPONSE
3778 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003779log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003780{
3781 static FILE *fd_tr = NULL;
3782 static proftime_T start;
3783 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003784 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003785
3786 if (fd_tr == NULL)
3787 {
3788 fd_tr = fopen("termresponse.log", "w");
3789 profile_start(&start);
3790 }
3791 now = start;
3792 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003793 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3794 must_redraw == NOT_VALID ? "NV"
3795 : must_redraw == CLEAR ? "CL" : " ");
3796 va_start(ap, fmt);
3797 vfprintf(fd_tr, fmt, ap);
3798 va_end(ap);
3799 fputc('\n', fd_tr);
3800 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003801}
3802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#endif
3804
3805/*
3806 * Return TRUE when saving and restoring the screen.
3807 */
3808 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003809swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 return (full_screen && *T_TI != NUL);
3812}
3813
Bram Moolenaarecadf432018-03-20 11:17:04 +01003814#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815/*
3816 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3817 */
3818 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003819setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821# ifdef FEAT_MOUSE_TTY
3822 int checkfor;
3823# endif
3824
3825# ifdef FEAT_MOUSESHAPE
3826 update_mouseshape(-1);
3827# endif
3828
3829# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3830# ifdef FEAT_GUI
3831 /* In the GUI the mouse is always enabled. */
3832 if (gui.in_use)
3833 return;
3834# endif
3835 /* be quick when mouse is off */
3836 if (*p_mouse == NUL || has_mouse_termcode == 0)
3837 return;
3838
3839 /* don't switch mouse on when not in raw mode (Ex mode) */
3840 if (cur_tmode != TMODE_RAW)
3841 {
3842 mch_setmouse(FALSE);
3843 return;
3844 }
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (VIsual_active)
3847 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003848 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 checkfor = MOUSE_RETURN;
3850 else if (State & INSERT)
3851 checkfor = MOUSE_INSERT;
3852 else if (State & CMDLINE)
3853 checkfor = MOUSE_COMMAND;
3854 else if (State == CONFIRM || State == EXTERNCMD)
3855 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3856 else
3857 checkfor = MOUSE_NORMAL; /* assume normal mode */
3858
3859 if (mouse_has(checkfor))
3860 mch_setmouse(TRUE);
3861 else
3862 mch_setmouse(FALSE);
3863# endif
3864}
3865
3866/*
3867 * Return TRUE if
3868 * - "c" is in 'mouse', or
3869 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3870 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3871 * normal editing mode (not at hit-return message).
3872 */
3873 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 char_u *p;
3877
3878 for (p = p_mouse; *p; ++p)
3879 switch (*p)
3880 {
3881 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3882 return TRUE;
3883 break;
3884 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3885 return TRUE;
3886 break;
3887 default: if (c == *p) return TRUE; break;
3888 }
3889 return FALSE;
3890}
3891
3892/*
3893 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3894 */
3895 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003896mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 return (p_mousem[0] == 'p');
3899}
3900#endif
3901
3902/*
3903 * By outputting the 'cursor very visible' termcap code, for some windowed
3904 * terminals this makes the screen scrolled to the correct position.
3905 * Used when starting Vim or returning from a shell.
3906 */
3907 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003908scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003910 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
3912 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003913 out_str(T_CVS);
3914 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916}
3917
3918static int cursor_is_off = FALSE;
3919
3920/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003921 * Enable the cursor without checking if it's already enabled.
3922 */
3923 void
3924cursor_on_force(void)
3925{
3926 out_str(T_VE);
3927 cursor_is_off = FALSE;
3928}
3929
3930/*
3931 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 */
3933 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003934cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003937 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938}
3939
3940/*
3941 * Disable the cursor.
3942 */
3943 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003944cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003946 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003948 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 cursor_is_off = TRUE;
3950 }
3951}
3952
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003953#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003955 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003956 */
3957 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003958term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003959{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003960 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003961 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003962
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003963 /* Only do something when redrawing the screen and we can restore the
3964 * mode. */
3965 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003966 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003967# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003968 if (forced && initial_cursor_shape > 0)
3969 /* Restore to initial values. */
3970 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003971# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003973 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003975 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003976 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003977 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003978 {
3979 if (*T_CSR != NUL)
3980 p = T_CSR; /* Replace mode cursor */
3981 else
3982 p = T_CSI; /* fall back to Insert mode cursor */
3983 if (*p != NUL)
3984 {
3985 out_str(p);
3986 showing_mode = REPLACE;
3987 }
3988 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003989 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003990 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003991 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003992 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003993 {
3994 out_str(T_CSI); /* Insert mode cursor */
3995 showing_mode = INSERT;
3996 }
3997 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003998 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003999 {
4000 out_str(T_CEI); /* non-Insert mode cursor */
4001 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004002 }
4003}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004004
4005# if defined(FEAT_TERMINAL) || defined(PROTO)
4006 void
4007term_cursor_color(char_u *color)
4008{
4009 if (*T_CSC != NUL)
4010 {
4011 out_str(T_CSC); /* set cursor color start */
4012 out_str_nf(color);
4013 out_str(T_CEC); /* set cursor color end */
4014 out_flush();
4015 }
4016}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004017# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004018
Bram Moolenaar4db25542017-08-28 22:43:05 +02004019 int
4020blink_state_is_inverted()
4021{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004022#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004023 return rbm_status.tr_progress == STATUS_GOT
4024 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004025 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004026#else
4027 return FALSE;
4028#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004029}
4030
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004031/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004032 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004033 */
4034 void
4035term_cursor_shape(int shape, int blink)
4036{
4037 if (*T_CSH != NUL)
4038 {
4039 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4040 out_flush();
4041 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004042 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004043 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004044 int do_blink = blink;
4045
4046 /* t_SH is empty: try setting just the blink state.
4047 * The blink flags are XORed together, if the initial blinking from
4048 * style and shape differs, we need to invert the flag here. */
4049 if (blink_state_is_inverted())
4050 do_blink = !blink;
4051
4052 if (do_blink && *T_VS != NUL)
4053 {
4054 out_str(T_VS);
4055 out_flush();
4056 }
4057 else if (!do_blink && *T_CVS != NUL)
4058 {
4059 out_str(T_CVS);
4060 out_flush();
4061 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004062 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004063}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004064#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Set scrolling region for window 'wp'.
4068 * The region starts 'off' lines from the start of the window.
4069 * Also set the vertical scroll region for a vertically split window. Always
4070 * the full width of the window, excluding the vertical separator.
4071 */
4072 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004073scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4076 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004078 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4079 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 screen_start(); /* don't know where cursor is now */
4081}
4082
4083/*
4084 * Reset scrolling region to the whole screen.
4085 */
4086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004087scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (*T_CSV != NUL)
4091 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 screen_start(); /* don't know where cursor is now */
4093}
4094
4095
4096/*
4097 * List of terminal codes that are currently recognized.
4098 */
4099
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004100static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 char_u name[2]; /* termcap name of entry */
4103 char_u *code; /* terminal code (in allocated memory) */
4104 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004105 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106} *termcodes = NULL;
4107
4108static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4109static int tc_len = 0; /* current number of entries in termcodes[] */
4110
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004111static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004114clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 while (tc_len > 0)
4117 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004118 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 tc_max_len = 0;
4120
4121#ifdef HAVE_TGETENT
4122 BC = (char *)empty_option;
4123 UP = (char *)empty_option;
4124 PC = NUL; /* set pad character to NUL */
4125 ospeed = 0;
4126#endif
4127
4128 need_gather = TRUE; /* need to fill termleader[] */
4129}
4130
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004131#define ATC_FROM_TERM 55
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133/*
4134 * Add a new entry to the list of terminal codes.
4135 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004136 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4137 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
4139 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004140add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 struct termcode *new_tc;
4143 int i, j;
4144 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004145 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 if (string == NULL || *string == NUL)
4148 {
4149 del_termcode(name);
4150 return;
4151 }
4152
Bram Moolenaar4f974752019-02-17 17:44:42 +01004153#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02004154 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4155#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004156# ifdef VIMDLL
4157 if (!gui.in_use)
4158 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4159 else
4160# endif
4161 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (s == NULL)
4164 return;
4165
4166 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004167 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004169 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 s[0] = term_7to8bit(string);
4171 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004172
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004173#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4174# ifdef VIMDLL
4175 if (!gui.in_use)
4176# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004177 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004178 if (s[0] == K_NUL)
4179 {
4180 STRMOVE(s + 1, s);
4181 s[1] = 3;
4182 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004183 }
4184#endif
4185
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 need_gather = TRUE; /* need to fill termleader[] */
4189
4190 /*
4191 * need to make space for more entries
4192 */
4193 if (tc_len == tc_max_len)
4194 {
4195 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004196 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (new_tc == NULL)
4198 {
4199 tc_max_len -= 20;
4200 return;
4201 }
4202 for (i = 0; i < tc_len; ++i)
4203 new_tc[i] = termcodes[i];
4204 vim_free(termcodes);
4205 termcodes = new_tc;
4206 }
4207
4208 /*
4209 * Look for existing entry with the same name, it is replaced.
4210 * Look for an existing entry that is alphabetical higher, the new entry
4211 * is inserted in front of it.
4212 */
4213 for (i = 0; i < tc_len; ++i)
4214 {
4215 if (termcodes[i].name[0] < name[0])
4216 continue;
4217 if (termcodes[i].name[0] == name[0])
4218 {
4219 if (termcodes[i].name[1] < name[1])
4220 continue;
4221 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 */
4224 if (termcodes[i].name[1] == name[1])
4225 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004226 if (flags == ATC_FROM_TERM && (j = termcode_star(
4227 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004228 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004229 /* Don't replace ESC[123;*X or ESC O*X with another when
4230 * invoked from got_code_from_term(). */
4231 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004232 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 && s[len - 1]
4234 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004235 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004236 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 vim_free(s);
4238 return;
4239 }
4240 }
4241 else
4242 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004243 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004244 vim_free(termcodes[i].code);
4245 --tc_len;
4246 break;
4247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 }
4250 /*
4251 * Found alphabetical larger entry, move rest to insert new entry
4252 */
4253 for (j = tc_len; j > i; --j)
4254 termcodes[j] = termcodes[j - 1];
4255 break;
4256 }
4257
4258 termcodes[i].name[0] = name[0];
4259 termcodes[i].name[1] = name[1];
4260 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004261 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004262
4263 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4264 * accept modifiers. */
4265 termcodes[i].modlen = 0;
4266 j = termcode_star(s, len);
4267 if (j > 0)
4268 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 ++tc_len;
4270}
4271
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004272/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004273 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004275 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 */
4277 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004278termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004279{
4280 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4281 if (len >= 3 && code[len - 2] == '*')
4282 {
4283 if (len >= 5 && code[len - 3] == ';')
4284 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004285 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004286 return 1;
4287 }
4288 return 0;
4289}
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004292find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293{
4294 int i;
4295
4296 for (i = 0; i < tc_len; ++i)
4297 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4298 return termcodes[i].code;
4299 return NULL;
4300}
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004303get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
4305 if (i >= tc_len)
4306 return NULL;
4307 return &termcodes[i].name[0];
4308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004311del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
4313 int i;
4314
4315 if (termcodes == NULL) /* nothing there yet */
4316 return;
4317
4318 need_gather = TRUE; /* need to fill termleader[] */
4319
4320 for (i = 0; i < tc_len; ++i)
4321 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4322 {
4323 del_termcode_idx(i);
4324 return;
4325 }
4326 /* not found. Give error message? */
4327}
4328
4329 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004330del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 int i;
4333
4334 vim_free(termcodes[idx].code);
4335 --tc_len;
4336 for (i = idx; i < tc_len; ++i)
4337 termcodes[i] = termcodes[i + 1];
4338}
4339
4340#ifdef FEAT_TERMRESPONSE
4341/*
4342 * Called when detected that the terminal sends 8-bit codes.
4343 * Convert all 7-bit codes to their 8-bit equivalent.
4344 */
4345 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004346switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
4348 int i;
4349 int c;
4350
4351 /* Only need to do something when not already using 8-bit codes. */
4352 if (!term_is_8bit(T_NAME))
4353 {
4354 for (i = 0; i < tc_len; ++i)
4355 {
4356 c = term_7to8bit(termcodes[i].code);
4357 if (c != 0)
4358 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004359 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 termcodes[i].code[0] = c;
4361 }
4362 }
4363 need_gather = TRUE; /* need to fill termleader[] */
4364 }
4365 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004366 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367}
4368#endif
4369
4370#ifdef CHECK_DOUBLE_CLICK
4371static linenr_T orig_topline = 0;
4372# ifdef FEAT_DIFF
4373static int orig_topfill = 0;
4374# endif
4375#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004376#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377/*
4378 * Checking for double clicks ourselves.
4379 * "orig_topline" is used to avoid detecting a double-click when the window
4380 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4381 */
4382/*
4383 * Set orig_topline. Used when jumping to another window, so that a double
4384 * click still works.
4385 */
4386 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004387set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 orig_topline = wp->w_topline;
4390# ifdef FEAT_DIFF
4391 orig_topfill = wp->w_topfill;
4392# endif
4393}
4394#endif
4395
4396/*
4397 * Check if typebuf.tb_buf[] contains a terminal key code.
4398 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4399 * + max_offset].
4400 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004401 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * With a match, the match is removed, the replacement code is inserted in
4403 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4404 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004405 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4406 * "buflen" is then the length of the string in buf[] and is updated for
4407 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
4409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004410check_termcode(
4411 int max_offset,
4412 char_u *buf,
4413 int bufsize,
4414 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 char_u *tp;
4417 char_u *p;
4418 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004419 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004421 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 int offset;
4423 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004424 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004425 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 int new_slen;
4428 int extra;
4429 char_u string[MAX_KEY_CODE_LEN + 1];
4430 int i, j;
4431 int idx = 0;
4432#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004433# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4434 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 char_u bytes[6];
4436 int num_bytes;
4437# endif
4438 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 int is_click, is_drag;
4440 int wheel_code = 0;
4441 int current_button;
4442 static int held_button = MOUSE_RELEASE;
4443 static int orig_num_clicks = 1;
4444 static int orig_mouse_code = 0x0;
4445# ifdef CHECK_DOUBLE_CLICK
4446 static int orig_mouse_col = 0;
4447 static int orig_mouse_row = 0;
4448 static struct timeval orig_mouse_time = {0, 0};
4449 /* time of previous mouse click */
4450 struct timeval mouse_time; /* time of current mouse click */
4451 long timediff; /* elapsed time in msec */
4452# endif
4453#endif
4454 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4457
4458 /*
4459 * Speed up the checks for terminal codes by gathering all first bytes
4460 * used in termleader[]. Often this is just a single <Esc>.
4461 */
4462 if (need_gather)
4463 gather_termleader();
4464
4465 /*
4466 * Check at several positions in typebuf.tb_buf[], to catch something like
4467 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4468 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004469 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 * This is used often, KEEP IT FAST!
4471 */
4472 for (offset = 0; offset < max_offset; ++offset)
4473 {
4474 if (buf == NULL)
4475 {
4476 if (offset >= typebuf.tb_len)
4477 break;
4478 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4479 len = typebuf.tb_len - offset; /* length of the input */
4480 }
4481 else
4482 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004483 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004486 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
4489 /*
4490 * Don't check characters after K_SPECIAL, those are already
4491 * translated terminal chars (avoid translating ~@^Hx).
4492 */
4493 if (*tp == K_SPECIAL)
4494 {
4495 offset += 2; /* there are always 2 extra characters */
4496 continue;
4497 }
4498
4499 /*
4500 * Skip this position if the character does not appear as the first
4501 * character in term_strings. This speeds up a lot, since most
4502 * termcodes start with the same character (ESC or CSI).
4503 */
4504 i = *tp;
4505 for (p = termleader; *p && *p != i; ++p)
4506 ;
4507 if (*p == NUL)
4508 continue;
4509
4510 /*
4511 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4512 * are in Insert mode.
4513 */
4514 if (*tp == ESC && !p_ek && (State & INSERT))
4515 continue;
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004518 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521#ifdef FEAT_GUI
4522 if (gui.in_use)
4523 {
4524 /*
4525 * GUI special key codes are all of the form [CSI xx].
4526 */
4527 if (*tp == CSI) /* Special key from GUI */
4528 {
4529 if (len < 3)
4530 return -1; /* Shouldn't happen */
4531 slen = 3;
4532 key_name[0] = tp[1];
4533 key_name[1] = tp[2];
4534 }
4535 }
4536 else
4537#endif /* FEAT_GUI */
4538 {
4539 for (idx = 0; idx < tc_len; ++idx)
4540 {
4541 /*
4542 * Ignore the entry if we are not at the start of
4543 * typebuf.tb_buf[]
4544 * and there are not enough characters to make a match.
4545 * But only when the 'K' flag is in 'cpoptions'.
4546 */
4547 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004548 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (cpo_koffset && offset && len < slen)
4550 continue;
4551 if (STRNCMP(termcodes[idx].code, tp,
4552 (size_t)(slen > len ? len : slen)) == 0)
4553 {
4554 if (len < slen) /* got a partial sequence */
4555 return -1; /* need to get more chars */
4556
4557 /*
4558 * When found a keypad key, check if there is another key
4559 * that matches and use that one. This makes <Home> to be
4560 * found instead of <kHome> when they produce the same
4561 * key code.
4562 */
4563 if (termcodes[idx].name[0] == 'K'
4564 && VIM_ISDIGIT(termcodes[idx].name[1]))
4565 {
4566 for (j = idx + 1; j < tc_len; ++j)
4567 if (termcodes[j].len == slen &&
4568 STRNCMP(termcodes[idx].code,
4569 termcodes[j].code, slen) == 0)
4570 {
4571 idx = j;
4572 break;
4573 }
4574 }
4575
4576 key_name[0] = termcodes[idx].name[0];
4577 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004580
4581 /*
4582 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004583 * <Esc>[123;*X (modslen == slen - 3)
4584 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4585 * When there is a modifier the * matches a number.
4586 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004587 */
4588 if (termcodes[idx].modlen > 0)
4589 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 modslen = termcodes[idx].modlen;
4591 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004592 continue;
4593 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004594 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004595 {
4596 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004597
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004599 return -1; /* need to get more chars */
4600
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004601 if (tp[modslen] == termcodes[idx].code[slen - 1])
4602 slen = modslen + 1; /* no modifiers */
4603 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004604 continue; /* no match */
4605 else
4606 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004607 // Skip over the digits, the final char must
4608 // follow. URXVT can use a negative value, thus
4609 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004610 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004611 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004612 ;
4613 ++j;
4614 if (len < j) /* got a partial sequence */
4615 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004616 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4617 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004618
Bram Moolenaara529ce02017-06-22 22:37:57 +02004619 modifiers_start = tp + slen - 2;
4620
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004621 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004622 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004623 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004625 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004626 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004627 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004629 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004631
4632 slen = j;
4633 }
4634 key_name[0] = termcodes[idx].name[0];
4635 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004636 break;
4637 }
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 }
4641
4642#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004643 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004644 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004645 * detecting the start of these mouse codes they might as well be
4646 * another key code or terminal response. */
4647# ifdef FEAT_MOUSE_DEC
4648 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004649# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004650# ifdef FEAT_MOUSE_PTERM
4651 || key_name[0] == KS_PTERM_MOUSE
4652# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004653 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004655 /* Check for some responses from the terminal starting with
4656 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004657 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004658 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004659 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004660 * Also eat other possible responses to t_RV, rxvt returns
4661 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4662 * mrxvt has been reported to have "+" in the version. Assume
4663 * the escape sequence ends with a letter or one of "{|}~".
4664 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004665 * - Cursor position report: <Esc>[{row};{col}R
4666 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004667 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004668 *
4669 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004670 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004671 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004672
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004673 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004674 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004675 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004676 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004678 int col = 0;
4679 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004680 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004683 for (i = 2 + (tp[0] != CSI); i < len
4684 && !(tp[i] >= '{' && tp[i] <= '~')
4685 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004686 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004687 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004688 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004689 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004692 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004693 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004694 return -1;
4695 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004696 if (extra > 0)
4697 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004698
4699 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4700 * u7_status is not "sent", it may be from a previous Vim that
4701 * just exited. But not for <S-F3>, it sends something
4702 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004703 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004704 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004705 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004706 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004707 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004708
Bram Moolenaarb255b902018-04-24 21:40:10 +02004709 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004710 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004711 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004712 if (col == 2)
4713 aw = "single";
4714 else if (col == 3)
4715 aw = "double";
4716 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4717 {
4718 /* Setting the option causes a screen redraw. Do
4719 * that right away if possible, keeping any
4720 * messages. */
4721 set_option_value((char_u *)"ambw", 0L,
4722 (char_u *)aw, 0);
4723# ifdef DEBUG_TERMRESPONSE
4724 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004725 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004726
Bram Moolenaarb255b902018-04-24 21:40:10 +02004727 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004728 }
4729# else
4730 redraw_asap(CLEAR);
4731# endif
4732 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004733 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004734 key_name[0] = (int)KS_EXTRA;
4735 key_name[1] = (int)KE_IGNORE;
4736 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004737# ifdef FEAT_EVAL
4738 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4739# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004742 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4743 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004745 int version = col;
4746
Bram Moolenaarb255b902018-04-24 21:40:10 +02004747 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004748 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004749 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 /* If this code starts with CSI, you can bet that the
4752 * terminal uses 8-bit codes. */
4753 if (tp[0] == CSI)
4754 switch_to_8bit();
4755
4756 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004757 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 * Ignore it for when the user has set 'term' to xterm,
4759 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004760 if (version > 20000)
4761 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004763 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004765 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004766 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004767 int is_mintty = FALSE;
4768
4769 // mintty 2.9.5 sends 77;20905;0c.
4770 // (77 is ASCII 'M' for mintty.)
4771 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4772 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004775 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004777 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 check_for_codes = TRUE;
4779 need_gather = TRUE;
4780 req_codes_from_term();
4781 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004782
4783 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004784 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004785 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004786 {
4787 /* If run from Vim $COLORS is set to the number of
4788 * colors the terminal supports. Otherwise assume
4789 * 256, libvterm supports even more. */
4790 if (mch_getenv((char_u *)"COLORS") == NULL)
4791 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004792 /* Libvterm can handle SGR mouse reporting. */
4793 if (!option_was_set((char_u *)"ttym"))
4794 set_option_value((char_u *)"ttym", 0L,
4795 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004796 }
4797
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004798 if (version == 95)
4799 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004800 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004801 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4802 {
4803 is_not_xterm = TRUE;
4804 is_mac_terminal = TRUE;
4805 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004806 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004807 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4808 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004809 // old iTerm2 sends 0;95;
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004810 else if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004811 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004812 }
4813
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004814 /* Only set 'ttymouse' automatically if it was not set
4815 * by the user already. */
4816 if (!option_was_set((char_u *)"ttym"))
4817 {
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004818 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004819 * Terminal.app, iTerm2 and mintty. */
4820 if (version >= 277 || is_iterm2 || is_mac_terminal
4821 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004822 set_option_value((char_u *)"ttym", 0L,
4823 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004824 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004825 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004826 set_option_value((char_u *)"ttym", 0L,
4827 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004828 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004829
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004830 /* Detect terminals that set $TERM to something like
4831 * "xterm-256colors" but are not fully xterm
4832 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004833
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004834 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004835 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004836 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004837 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004838 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004839 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004840 is_not_xterm = TRUE;
4841
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004842 /* PuTTY sends 0;136;0
4843 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004844 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004845 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004846 is_not_xterm = TRUE;
4847
4848 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004849 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004850 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004851 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004852
Bram Moolenaar668324e2018-06-30 17:09:26 +02004853 // Xterm first responded to this request at patch level
4854 // 95, so assume anything below 95 is not xterm.
4855 if (version < 95)
4856 is_not_xterm = TRUE;
4857
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004858 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004859 * set. Only supported properly by xterm since version
4860 * 279 (otherwise it returns 0x18).
4861 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004862 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004863 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004864 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004865 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004866 && *T_CSH != NUL
4867 && *T_CRS != NUL)
4868 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004869 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004870 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004871 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004872 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004873 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004874
4875 /* Only request the cursor blink mode if t_RC set. Not
4876 * for Gnome terminal, it can't handle t_RC, it
4877 * echoes the characters to the screen. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004878 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004879 && !is_not_xterm
4880 && *T_CRC != NUL)
4881 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004882 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004883 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004884 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004885 need_flush = TRUE;
4886 }
4887
4888 if (need_flush)
4889 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004891 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004893 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 apply_autocmds(EVENT_TERMRESPONSE,
4896 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 key_name[0] = (int)KS_EXTRA;
4898 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004900
Bram Moolenaar4db25542017-08-28 22:43:05 +02004901 /* Check blinking cursor from xterm:
4902 * {lead}?12;1$y set
4903 * {lead}?12;2$y not set
4904 *
4905 * {lead} can be <Esc>[ or CSI
4906 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004907 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004908 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4909 && i == j + 6
4910 && tp[j + 1] == '1'
4911 && tp[j + 2] == '2'
4912 && tp[j + 3] == ';'
4913 && tp[i - 1] == '$'
4914 && tp[i] == 'y')
4915 {
4916 initial_cursor_blink = (tp[j + 4] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004917 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004918 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004919 key_name[0] = (int)KS_EXTRA;
4920 key_name[1] = (int)KE_IGNORE;
4921 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004922# ifdef FEAT_EVAL
4923 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4924# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004925 }
4926
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004927 /*
4928 * Check for a window position response from the terminal:
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004929 * {lead}3;{x};{y}t
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004930 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004931 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004932 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4933 || (len >= 3 && tp[0] == CSI))
4934 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4935 && tp[j + 1] == ';')
4936 {
4937 j += 2;
4938 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4939 ;
4940 if (i < len && tp[i] == ';')
4941 {
4942 winpos_x = atoi((char *)tp + j);
4943 j = i + 1;
4944 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4945 ;
4946 if (i < len && tp[i] == 't')
4947 {
4948 winpos_y = atoi((char *)tp + j);
4949 /* got finished code: consume it */
4950 key_name[0] = (int)KS_EXTRA;
4951 key_name[1] = (int)KE_IGNORE;
4952 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004953
4954 if (--did_request_winpos <= 0)
Bram Moolenaarafd78262019-05-10 23:10:31 +02004955 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004956 }
4957 }
4958 if (i == len)
4959 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004960 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004961 return -1;
4962 }
4963 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004964 }
4965
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004966 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004967 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004968 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004969 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004970 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004971 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004972 * {lead} can be <Esc>] or OSC
4973 * {tail} can be '\007', <Esc>\ or STERM.
4974 *
4975 * Consume any code that starts with "{lead}11;", it's also
4976 * possible that "rgba" is following.
4977 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004978 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004979 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4980 || tp[0] == OSC))
4981 {
4982 j = 1 + (tp[0] == ESC);
4983 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004984 || (argp[1] != '1' && argp[1] != '0')
4985 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004986 i = 0; /* no match */
4987 else
4988 for (i = j; i < len; ++i)
4989 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4990 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004991 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004992 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004993 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4994 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004995
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004996 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004997 && (is_4digit
4998 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004999 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02005000 char_u *tp_r = tp + j + 7;
5001 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5002 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005003# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02005004 int rval, gval, bval;
5005
Bram Moolenaar32e19772019-06-05 22:57:04 +02005006 rval = hexhex2nr(tp_r);
5007 gval = hexhex2nr(tp_b);
5008 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005009# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005010 if (is_bg)
5011 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02005012 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5013 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005014
Bram Moolenaarb255b902018-04-24 21:40:10 +02005015 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005016 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005017# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005018 bg_r = rval;
5019 bg_g = gval;
5020 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005021# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005022 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02005023 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005024 {
5025 /* value differs, apply it */
5026 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02005027 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005028 reset_option_was_set((char_u *)"bg");
5029 redraw_asap(CLEAR);
5030 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005031 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005032# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005033 else
5034 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005035 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005036 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005037 fg_r = rval;
5038 fg_g = gval;
5039 fg_b = bval;
5040 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005041# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005042 }
5043
5044 /* got finished code: consume it */
5045 key_name[0] = (int)KS_EXTRA;
5046 key_name[1] = (int)KE_IGNORE;
5047 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005048# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005049 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5050 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005051# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005052 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005053 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005054 if (i == len)
5055 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005056 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005057 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005061 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005062 * {lead}{flag}+r<hex bytes><{tail}
5063 *
5064 * {lead} can be <Esc>P or DCS
5065 * {flag} can be '0' or '1'
5066 * {tail} can be Esc>\ or STERM
5067 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005068 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01005069 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005070 *
5071 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02005072 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005073 *
5074 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005075 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02005076 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005077 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 || tp[0] == DCS))
5079 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005080 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005081 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02005082 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005083 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005084 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005085 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005086 // key code response
5087 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005089 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5090 || tp[i] == STERM)
5091 {
5092 if (i - j >= 3)
5093 got_code_from_term(tp + j, i);
5094 key_name[0] = (int)KS_EXTRA;
5095 key_name[1] = (int)KE_IGNORE;
5096 slen = i + 1 + (tp[i] == ESC);
5097 break;
5098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005100 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005101 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005102 // Probably the cursor shape response. Make sure that "i"
5103 // is equal to "len" when there are not sufficient
5104 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005105 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005106 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005107 if (i - j == 3 && !isdigit(tp[i]))
5108 break;
5109 if (i - j == 4 && tp[i] != ' ')
5110 break;
5111 if (i - j == 5 && tp[i] != 'q')
5112 break;
5113 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5114 break;
5115 if ((i - j == 6 && tp[i] == STERM)
5116 || (i - j == 7 && tp[i] == '\\'))
5117 {
5118 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005119
Bram Moolenaar66761db2019-06-05 22:07:51 +02005120 // 0, 1 = block blink, 2 = block
5121 // 3 = underline blink, 4 = underline
5122 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005123 number = number == 0 ? 1 : number;
5124 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005125 // The blink flag is actually inverted, compared to
5126 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005127 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005128 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005129 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005130 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005131
Bram Moolenaar590ec872018-03-02 20:58:42 +01005132 key_name[0] = (int)KS_EXTRA;
5133 key_name[1] = (int)KE_IGNORE;
5134 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005135# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005136 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005137# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005138 break;
5139 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005140 }
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005144 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005145 /* These codes arrive many together, each code can be
5146 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005147 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005148 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 }
5152#endif
5153
5154 if (key_name[0] == NUL)
5155 continue; /* No match at this position, try next one */
5156
5157 /* We only get here when we have a complete termcode match */
5158
5159#ifdef FEAT_MOUSE
5160# ifdef FEAT_GUI
5161 /*
5162 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5163 * so that we know which window to scroll later.
5164 */
5165 if (gui.in_use
5166 && key_name[0] == (int)KS_EXTRA
5167 && (key_name[1] == (int)KE_X1MOUSE
5168 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005169 || key_name[1] == (int)KE_MOUSELEFT
5170 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 || key_name[1] == (int)KE_MOUSEDOWN
5172 || key_name[1] == (int)KE_MOUSEUP))
5173 {
5174 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5175 if (num_bytes == -1) /* not enough coordinates */
5176 return -1;
5177 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5178 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5179 slen += num_bytes;
5180 }
5181 else
5182# endif
5183 /*
5184 * If it is a mouse click, get the coordinates.
5185 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005186 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005187# ifdef FEAT_MOUSE_GPM
5188 || key_name[0] == KS_GPM_MOUSE
5189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005191 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192# endif
5193# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005194 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195# endif
5196# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005197 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198# endif
5199# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005200 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005202# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005203 || key_name[0] == KS_URXVT_MOUSE
5204# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005205 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005206 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 is_click = is_drag = FALSE;
5209
Bram Moolenaar864207d2008-06-24 22:14:38 +00005210# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5211 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005212 if (key_name[0] == KS_MOUSE
5213# ifdef FEAT_MOUSE_GPM
5214 || key_name[0] == KS_GPM_MOUSE
5215# endif
5216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005219 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 * s == encoded button state:
5221 * 0x20 = left button down
5222 * 0x21 = middle button down
5223 * 0x22 = right button down
5224 * 0x23 = any button release
5225 * 0x60 = button 4 down (scroll wheel down)
5226 * 0x61 = button 5 down (scroll wheel up)
5227 * add 0x04 for SHIFT
5228 * add 0x08 for ALT
5229 * add 0x10 for CTRL
5230 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005231 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5232 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * c == column + ' ' + 1 == column + 33
5234 * r == row + ' ' + 1 == row + 33
5235 *
5236 * The coordinates are passed on through global variables.
5237 * Ugly, but this avoids trouble with mouse clicks at an
5238 * unexpected moment and allows for mapping them.
5239 */
5240 for (;;)
5241 {
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005242# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (gui.in_use)
5244 {
5245 /* GUI uses more bits for columns > 223 */
5246 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5247 if (num_bytes == -1) /* not enough coordinates */
5248 return -1;
5249 mouse_code = bytes[0];
5250 mouse_col = 128 * (bytes[1] - ' ' - 1)
5251 + bytes[2] - ' ' - 1;
5252 mouse_row = 128 * (bytes[3] - ' ' - 1)
5253 + bytes[4] - ' ' - 1;
5254 }
5255 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5259 if (num_bytes == -1) /* not enough coordinates */
5260 return -1;
5261 mouse_code = bytes[0];
5262 mouse_col = bytes[1] - ' ' - 1;
5263 mouse_row = bytes[2] - ' ' - 1;
5264 }
5265 slen += num_bytes;
5266
5267 /* If the following bytes is also a mouse code and it has
5268 * the same code, dump this one and get the next. This
5269 * makes dragging a whole lot faster. */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005270# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (gui.in_use)
5272 j = 3;
5273 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 j = termcodes[idx].len;
5276 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5277 && tp[slen + j] == mouse_code
5278 && tp[slen + j + 1] != NUL
5279 && tp[slen + j + 2] != NUL
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005280# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 && (!gui.in_use
5282 || (tp[slen + j + 3] != NUL
5283 && tp[slen + j + 4] != NUL))
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 )
5286 slen += j;
5287 else
5288 break;
5289 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005292 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005293 || key_name[0] == KS_SGR_MOUSE
5294 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005295 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005296 /* URXVT 1015 mouse reporting mode:
5297 * Almost identical to xterm mouse mode, except the values
5298 * are decimal instead of bytes.
5299 *
5300 * \033[%d;%d;%dM
5301 * ^-- row
5302 * ^----- column
5303 * ^-------- code
5304 *
5305 * SGR 1006 mouse reporting mode:
5306 * Almost identical to xterm mouse mode, except the values
5307 * are decimal instead of bytes.
5308 *
5309 * \033[<%d;%d;%dM
5310 * ^-- row
5311 * ^----- column
5312 * ^-------- code
5313 *
5314 * \033[<%d;%d;%dm : mouse release event
5315 * ^-- row
5316 * ^----- column
5317 * ^-------- code
5318 */
5319 p = modifiers_start;
5320 if (p == NULL)
5321 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005322
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005323 mouse_code = getdigits(&p);
5324 if (*p++ != ';')
5325 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005326
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005327 /* when mouse reporting is SGR, add 32 to mouse code */
5328 if (key_name[0] == KS_SGR_MOUSE
5329 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5330 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005331
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005332 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5333 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005334
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005335 mouse_col = getdigits(&p) - 1;
5336 if (*p++ != ';')
5337 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005338
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005339 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005340
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005341 /* The modifiers were the mouse coordinates, not the
5342 * modifier keys (alt/shift/ctrl/meta) state. */
5343 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005344 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005345
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005346 if (key_name[0] == KS_MOUSE
5347# ifdef FEAT_MOUSE_GPM
5348 || key_name[0] == KS_GPM_MOUSE
5349# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005350# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005351 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005352# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005353 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005354 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005355 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005356# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 /*
5358 * Handle mouse events.
5359 * Recognize the xterm mouse wheel, but not in the GUI, the
5360 * Linux console with GPM and the MS-DOS or Win32 console
5361 * (multi-clicks use >= 0x60).
5362 */
5363 if (mouse_code >= MOUSEWHEEL_LOW
5364# ifdef FEAT_GUI
5365 && !gui.in_use
5366# endif
5367# ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005368 && key_name[0] != KS_GPM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369# endif
5370 )
5371 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005372# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5373 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5374 /* mouse-move event, using MOUSE_DRAG works */
5375 mouse_code = MOUSE_DRAG;
5376 else
5377# endif
5378 /* Keep the mouse_code before it's changed, so that we
5379 * remember that it was a mouse wheel click. */
5380 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382# ifdef FEAT_MOUSE_XTERM
5383 else if (held_button == MOUSE_RELEASE
5384# ifdef FEAT_GUI
5385 && !gui.in_use
5386# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005387 && (mouse_code == 0x23 || mouse_code == 0x24
5388 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005390 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5391 * And 0x40 and 0x41 are used by some xterm emulator. */
5392 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5393 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395# endif
5396
5397# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5398 else if (use_xterm_mouse() > 1)
5399 {
5400 if (mouse_code & MOUSE_DRAG_XTERM)
5401 mouse_code |= MOUSE_DRAG;
5402 }
5403# endif
5404# ifdef FEAT_XCLIPBOARD
5405 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5406 {
5407 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5408 stop_xterm_trace();
5409 else
5410 start_xterm_trace(mouse_code);
5411 }
5412# endif
5413# endif
5414 }
5415# endif /* !UNIX || FEAT_MOUSE_XTERM */
5416# ifdef FEAT_MOUSE_NET
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005417 if (key_name[0] == KS_NETTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 int mc, mr;
5420
5421 /* expect a rather limited sequence like: balancing {
5422 * \033}6,45\r
5423 * '6' is the row, 45 is the column
5424 */
5425 p = tp + slen;
5426 mr = getdigits(&p);
5427 if (*p++ != ',')
5428 return -1;
5429 mc = getdigits(&p);
5430 if (*p++ != '\r')
5431 return -1;
5432
5433 mouse_col = mc - 1;
5434 mouse_row = mr - 1;
5435 mouse_code = MOUSE_LEFT;
5436 slen += (int)(p - (tp + slen));
5437 }
5438# endif /* FEAT_MOUSE_NET */
5439# ifdef FEAT_MOUSE_JSB
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005440 if (key_name[0] == KS_JSBTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
5442 int mult, val, iter, button, status;
5443
5444 /* JSBTERM Input Model
5445 * \033[0~zw uniq escape sequence
5446 * (L-x) Left button pressed - not pressed x not reporting
5447 * (M-x) Middle button pressed - not pressed x not reporting
5448 * (R-x) Right button pressed - not pressed x not reporting
5449 * (SDmdu) Single , Double click, m mouse move d button down
5450 * u button up
5451 * ### X cursor position padded to 3 digits
5452 * ### Y cursor position padded to 3 digits
5453 * (s-x) SHIFT key pressed - not pressed x not reporting
5454 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005455 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 */
5457
5458 p = tp + slen;
5459 button = mouse_code = 0;
5460 switch (*p++)
5461 {
5462 case 'L': button = 1; break;
5463 case '-': break;
5464 case 'x': break; /* ignore sequence */
5465 default: return -1; /* Unknown Result */
5466 }
5467 switch (*p++)
5468 {
5469 case 'M': button |= 2; break;
5470 case '-': break;
5471 case 'x': break; /* ignore sequence */
5472 default: return -1; /* Unknown Result */
5473 }
5474 switch (*p++)
5475 {
5476 case 'R': button |= 4; break;
5477 case '-': break;
5478 case 'x': break; /* ignore sequence */
5479 default: return -1; /* Unknown Result */
5480 }
5481 status = *p++;
5482 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5483 mult /= 10, p++)
5484 if (*p >= '0' && *p <= '9')
5485 val += (*p - '0') * mult;
5486 else
5487 return -1;
5488 mouse_col = val;
5489 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5490 mult /= 10, p++)
5491 if (*p >= '0' && *p <= '9')
5492 val += (*p - '0') * mult;
5493 else
5494 return -1;
5495 mouse_row = val;
5496 switch (*p++)
5497 {
5498 case 's': button |= 8; break; /* SHIFT key Pressed */
5499 case '-': break; /* Not Pressed */
5500 case 'x': break; /* Not Reporting */
5501 default: return -1; /* Unknown Result */
5502 }
5503 switch (*p++)
5504 {
5505 case 'c': button |= 16; break; /* CTRL key Pressed */
5506 case '-': break; /* Not Pressed */
5507 case 'x': break; /* Not Reporting */
5508 default: return -1; /* Unknown Result */
5509 }
5510 if (*p++ != '\033')
5511 return -1;
5512 if (*p++ != '\\')
5513 return -1;
5514 switch (status)
5515 {
5516 case 'D': /* Double Click */
5517 case 'S': /* Single Click */
5518 if (button & 1) mouse_code |= MOUSE_LEFT;
5519 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5520 if (button & 4) mouse_code |= MOUSE_RIGHT;
5521 if (button & 8) mouse_code |= MOUSE_SHIFT;
5522 if (button & 16) mouse_code |= MOUSE_CTRL;
5523 break;
5524 case 'm': /* Mouse move */
5525 if (button & 1) mouse_code |= MOUSE_LEFT;
5526 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5527 if (button & 4) mouse_code |= MOUSE_RIGHT;
5528 if (button & 8) mouse_code |= MOUSE_SHIFT;
5529 if (button & 16) mouse_code |= MOUSE_CTRL;
5530 if ((button & 7) != 0)
5531 {
5532 held_button = mouse_code;
5533 mouse_code |= MOUSE_DRAG;
5534 }
5535 is_drag = TRUE;
5536 showmode();
5537 break;
5538 case 'd': /* Button Down */
5539 if (button & 1) mouse_code |= MOUSE_LEFT;
5540 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5541 if (button & 4) mouse_code |= MOUSE_RIGHT;
5542 if (button & 8) mouse_code |= MOUSE_SHIFT;
5543 if (button & 16) mouse_code |= MOUSE_CTRL;
5544 break;
5545 case 'u': /* Button Up */
5546 if (button & 1)
5547 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5548 if (button & 2)
5549 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5550 if (button & 4)
5551 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5552 if (button & 8)
5553 mouse_code |= MOUSE_SHIFT;
5554 if (button & 16)
5555 mouse_code |= MOUSE_CTRL;
5556 break;
5557 default: return -1; /* Unknown Result */
5558 }
5559
5560 slen += (p - (tp + slen));
5561 }
5562# endif /* FEAT_MOUSE_JSB */
5563# ifdef FEAT_MOUSE_DEC
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005564 if (key_name[0] == KS_DEC_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 /* The DEC Locator Input Model
5567 * Netterm delivers the code sequence:
5568 * \033[2;4;24;80&w (left button down)
5569 * \033[3;0;24;80&w (left button up)
5570 * \033[6;1;24;80&w (right button down)
5571 * \033[7;0;24;80&w (right button up)
5572 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5573 * Pe is the event code
5574 * Pb is the button code
5575 * Pr is the row coordinate
5576 * Pc is the column coordinate
5577 * Pp is the third coordinate (page number)
5578 * Pe, the event code indicates what event caused this report
5579 * The following event codes are defined:
5580 * 0 - request, the terminal received an explicit request
5581 * for a locator report, but the locator is unavailable
5582 * 1 - request, the terminal received an explicit request
5583 * for a locator report
5584 * 2 - left button down
5585 * 3 - left button up
5586 * 4 - middle button down
5587 * 5 - middle button up
5588 * 6 - right button down
5589 * 7 - right button up
5590 * 8 - fourth button down
5591 * 9 - fourth button up
5592 * 10 - locator outside filter rectangle
5593 * Pb, the button code, ASCII decimal 0-15 indicating which
5594 * buttons are down if any. The state of the four buttons
5595 * on the locator correspond to the low four bits of the
5596 * decimal value,
5597 * "1" means button depressed
5598 * 0 - no buttons down,
5599 * 1 - right,
5600 * 2 - middle,
5601 * 4 - left,
5602 * 8 - fourth
5603 * Pr is the row coordinate of the locator position in the page,
5604 * encoded as an ASCII decimal value.
5605 * If Pr is omitted, the locator position is undefined
5606 * (outside the terminal window for example).
5607 * Pc is the column coordinate of the locator position in the
5608 * page, encoded as an ASCII decimal value.
5609 * If Pc is omitted, the locator position is undefined
5610 * (outside the terminal window for example).
5611 * Pp is the page coordinate of the locator position
5612 * encoded as an ASCII decimal value.
5613 * The page coordinate may be omitted if the locator is on
5614 * page one (the default). We ignore it anyway.
5615 */
5616 int Pe, Pb, Pr, Pc;
5617
5618 p = tp + slen;
5619
5620 /* get event status */
5621 Pe = getdigits(&p);
5622 if (*p++ != ';')
5623 return -1;
5624
5625 /* get button status */
5626 Pb = getdigits(&p);
5627 if (*p++ != ';')
5628 return -1;
5629
5630 /* get row status */
5631 Pr = getdigits(&p);
5632 if (*p++ != ';')
5633 return -1;
5634
5635 /* get column status */
5636 Pc = getdigits(&p);
5637
5638 /* the page parameter is optional */
5639 if (*p == ';')
5640 {
5641 p++;
5642 (void)getdigits(&p);
5643 }
5644 if (*p++ != '&')
5645 return -1;
5646 if (*p++ != 'w')
5647 return -1;
5648
5649 mouse_code = 0;
5650 switch (Pe)
5651 {
5652 case 0: return -1; /* position request while unavailable */
5653 case 1: /* a response to a locator position request includes
5654 the status of all buttons */
5655 Pb &= 7; /* mask off and ignore fourth button */
5656 if (Pb & 4)
5657 mouse_code = MOUSE_LEFT;
5658 if (Pb & 2)
5659 mouse_code = MOUSE_MIDDLE;
5660 if (Pb & 1)
5661 mouse_code = MOUSE_RIGHT;
5662 if (Pb)
5663 {
5664 held_button = mouse_code;
5665 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005666 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 is_drag = TRUE;
5669 showmode();
5670 break;
5671 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005672 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 break;
5674 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5675 break;
5676 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005677 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5680 break;
5681 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005682 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 break;
5684 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5685 break;
5686 case 8: return -1; /* fourth button down */
5687 case 9: return -1; /* fourth button up */
5688 case 10: return -1; /* mouse outside of filter rectangle */
5689 default: return -1; /* should never occur */
5690 }
5691
5692 mouse_col = Pc - 1;
5693 mouse_row = Pr - 1;
5694
5695 slen += (int)(p - (tp + slen));
5696 }
5697# endif /* FEAT_MOUSE_DEC */
5698# ifdef FEAT_MOUSE_PTERM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005699 if (key_name[0] == KS_PTERM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005701 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 p = tp + slen;
5704
5705 action = getdigits(&p);
5706 if (*p++ != ';')
5707 return -1;
5708
5709 mouse_row = getdigits(&p);
5710 if (*p++ != ';')
5711 return -1;
5712 mouse_col = getdigits(&p);
5713 if (*p++ != ';')
5714 return -1;
5715
5716 button = getdigits(&p);
5717 mouse_code = 0;
5718
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005719 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 case 4: mouse_code = MOUSE_LEFT; break;
5722 case 1: mouse_code = MOUSE_RIGHT; break;
5723 case 2: mouse_code = MOUSE_MIDDLE; break;
5724 default: return -1;
5725 }
5726
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005727 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 case 31: /* Initial press */
5730 if (*p++ != ';')
5731 return -1;
5732
5733 num_clicks = getdigits(&p); /* Not used */
5734 break;
5735
5736 case 32: /* Release */
5737 mouse_code |= MOUSE_RELEASE;
5738 break;
5739
5740 case 33: /* Drag */
5741 held_button = mouse_code;
5742 mouse_code |= MOUSE_DRAG;
5743 break;
5744
5745 default:
5746 return -1;
5747 }
5748
5749 if (*p++ != 't')
5750 return -1;
5751
5752 slen += (p - (tp + slen));
5753 }
5754# endif /* FEAT_MOUSE_PTERM */
5755
5756 /* Interpret the mouse code */
5757 current_button = (mouse_code & MOUSE_CLICK_MASK);
5758 if (current_button == MOUSE_RELEASE
5759# ifdef FEAT_MOUSE_XTERM
5760 && wheel_code == 0
5761# endif
5762 )
5763 {
5764 /*
5765 * If we get a mouse drag or release event when
5766 * there is no mouse button held down (held_button ==
5767 * MOUSE_RELEASE), produce a K_IGNORE below.
5768 * (can happen when you hold down two buttons
5769 * and then let them go, or click in the menu bar, but not
5770 * on a menu, and drag into the text).
5771 */
5772 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5773 is_drag = TRUE;
5774 current_button = held_button;
5775 }
5776 else if (wheel_code == 0)
5777 {
5778# ifdef CHECK_DOUBLE_CLICK
5779# ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 /*
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005781 * Only for Unix, when GUI not active, we handle
5782 * multi-clicks here, but not for GPM mouse events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005784# ifdef FEAT_GUI
5785 if (key_name[0] != KS_GPM_MOUSE && !gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786# else
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005787 if (key_name[0] != KS_GPM_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788# endif
5789# else
5790# ifdef FEAT_GUI
5791 if (!gui.in_use)
5792# endif
5793# endif
5794 {
5795 /*
5796 * Compute the time elapsed since the previous mouse click.
5797 */
5798 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005799 if (orig_mouse_time.tv_sec == 0)
5800 {
5801 /*
5802 * Avoid computing the difference between mouse_time
5803 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005804 * difference would be huge and would cause
5805 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005806 */
5807 timediff = p_mouset;
5808 }
5809 else
5810 {
5811 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005812 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005813 if (timediff < 0)
5814 --orig_mouse_time.tv_sec;
5815 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005816 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 orig_mouse_time = mouse_time;
5819 if (mouse_code == orig_mouse_code
5820 && timediff < p_mouset
5821 && orig_num_clicks != 4
5822 && orig_mouse_col == mouse_col
5823 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005824 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005826 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005828 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005829 /* Double click in tab pages line also works
5830 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005831 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 ++orig_num_clicks;
5834 else
5835 orig_num_clicks = 1;
5836 orig_mouse_col = mouse_col;
5837 orig_mouse_row = mouse_row;
5838 orig_topline = curwin->w_topline;
5839#ifdef FEAT_DIFF
5840 orig_topfill = curwin->w_topfill;
5841#endif
5842 }
5843# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5844 else
5845 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5846# endif
5847# else
5848 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5849# endif
5850 is_click = TRUE;
5851 orig_mouse_code = mouse_code;
5852 }
5853 if (!is_drag)
5854 held_button = mouse_code & MOUSE_CLICK_MASK;
5855
5856 /*
5857 * Translate the actual mouse event into a pseudo mouse event.
5858 * First work out what modifiers are to be used.
5859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (orig_mouse_code & MOUSE_SHIFT)
5861 modifiers |= MOD_MASK_SHIFT;
5862 if (orig_mouse_code & MOUSE_CTRL)
5863 modifiers |= MOD_MASK_CTRL;
5864 if (orig_mouse_code & MOUSE_ALT)
5865 modifiers |= MOD_MASK_ALT;
5866 if (orig_num_clicks == 2)
5867 modifiers |= MOD_MASK_2CLICK;
5868 else if (orig_num_clicks == 3)
5869 modifiers |= MOD_MASK_3CLICK;
5870 else if (orig_num_clicks == 4)
5871 modifiers |= MOD_MASK_4CLICK;
5872
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005873 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5874 * added, then it's not mouse up/down. */
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005875 key_name[0] = KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005876 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005877 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005878 {
5879 if (wheel_code & MOUSE_CTRL)
5880 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005881 if (wheel_code & MOUSE_ALT)
5882 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 key_name[1] = (wheel_code & 1)
5884 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005885 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 else
5888 key_name[1] = get_pseudo_mouse_code(current_button,
5889 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005890
5891 /* Make sure the mouse position is valid. Some terminals may
5892 * return weird values. */
5893 if (mouse_col >= Columns)
5894 mouse_col = Columns - 1;
5895 if (mouse_row >= Rows)
5896 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898#endif /* FEAT_MOUSE */
5899
5900#ifdef FEAT_GUI
5901 /*
5902 * If using the GUI, then we get menu and scrollbar events.
5903 *
5904 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5905 * four bytes which are to be taken as a pointer to the vimmenu_T
5906 * structure.
5907 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005908 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005909 * is one byte with the tab index.
5910 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5912 * by one byte representing the scrollbar number, and then four bytes
5913 * representing a long_u which is the new value of the scrollbar.
5914 *
5915 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5916 * KE_FILLER followed by four bytes representing a long_u which is the
5917 * new value of the scrollbar.
5918 */
5919# ifdef FEAT_MENU
5920 else if (key_name[0] == (int)KS_MENU)
5921 {
5922 long_u val;
5923
5924 num_bytes = get_long_from_buf(tp + slen, &val);
5925 if (num_bytes == -1)
5926 return -1;
5927 current_menu = (vimmenu_T *)val;
5928 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005929
5930 /* The menu may have been deleted right after it was used, check
5931 * for that. */
5932 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5933 {
5934 key_name[0] = KS_EXTRA;
5935 key_name[1] = (int)KE_IGNORE;
5936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005939# ifdef FEAT_GUI_TABLINE
5940 else if (key_name[0] == (int)KS_TABLINE)
5941 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005942 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005943 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5944 if (num_bytes == -1)
5945 return -1;
5946 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005947 if (current_tab == 255) /* -1 in a byte gives 255 */
5948 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005949 slen += num_bytes;
5950 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005951 else if (key_name[0] == (int)KS_TABMENU)
5952 {
5953 /* Selecting tabline tab or using its menu. */
5954 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5955 if (num_bytes == -1)
5956 return -1;
5957 current_tab = (int)bytes[0];
5958 current_tabmenu = (int)bytes[1];
5959 slen += num_bytes;
5960 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962# ifndef USE_ON_FLY_SCROLL
5963 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5964 {
5965 long_u val;
5966
5967 /* Get the last scrollbar event in the queue of the same type */
5968 j = 0;
5969 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5970 && tp[j + 2] != NUL; ++i)
5971 {
5972 j += 3;
5973 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5974 if (num_bytes == -1)
5975 break;
5976 if (i == 0)
5977 current_scrollbar = (int)bytes[0];
5978 else if (current_scrollbar != (int)bytes[0])
5979 break;
5980 j += num_bytes;
5981 num_bytes = get_long_from_buf(tp + j, &val);
5982 if (num_bytes == -1)
5983 break;
5984 scrollbar_value = val;
5985 j += num_bytes;
5986 slen = j;
5987 }
5988 if (i == 0) /* not enough characters to make one */
5989 return -1;
5990 }
5991 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5992 {
5993 long_u val;
5994
5995 /* Get the last horiz. scrollbar event in the queue */
5996 j = 0;
5997 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5998 && tp[j + 2] != NUL; ++i)
5999 {
6000 j += 3;
6001 num_bytes = get_long_from_buf(tp + j, &val);
6002 if (num_bytes == -1)
6003 break;
6004 scrollbar_value = val;
6005 j += num_bytes;
6006 slen = j;
6007 }
6008 if (i == 0) /* not enough characters to make one */
6009 return -1;
6010 }
6011# endif /* !USE_ON_FLY_SCROLL */
6012#endif /* FEAT_GUI */
6013
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006014 /*
6015 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6016 */
6017 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6018
6019 /*
6020 * Add any modifier codes to our string.
6021 */
6022 new_slen = 0; /* Length of what will replace the termcode */
6023 if (modifiers != 0)
6024 {
6025 /* Some keys have the modifier included. Need to handle that here
6026 * to make mappings work. */
6027 key = simplify_key(key, &modifiers);
6028 if (modifiers != 0)
6029 {
6030 string[new_slen++] = K_SPECIAL;
6031 string[new_slen++] = (int)KS_MODIFIER;
6032 string[new_slen++] = modifiers;
6033 }
6034 }
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006037 key_name[0] = KEY2TERMCAP0(key);
6038 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006040 {
6041 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00006042 if (has_mbyte)
6043 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6044 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006045 string[new_slen++] = key_name[1];
6046 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006047 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6048 && key_name[1] == KE_IGNORE)
6049 {
6050 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6051 * to indicate what happened. */
6052 retval = KEYLEN_REMOVED;
6053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 else
6055 {
6056 string[new_slen++] = K_SPECIAL;
6057 string[new_slen++] = key_name[0];
6058 string[new_slen++] = key_name[1];
6059 }
6060 string[new_slen] = NUL;
6061 extra = new_slen - slen;
6062 if (buf == NULL)
6063 {
6064 if (extra < 0)
6065 /* remove matched chars, taking care of noremap */
6066 del_typebuf(-extra, offset);
6067 else if (extra > 0)
6068 /* insert the extra space we need */
6069 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
6070
6071 /*
6072 * Careful: del_typebuf() and ins_typebuf() may have reallocated
6073 * typebuf.tb_buf[]!
6074 */
6075 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
6076 (size_t)new_slen);
6077 }
6078 else
6079 {
6080 if (extra < 0)
6081 /* remove matched characters */
6082 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006083 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006085 {
6086 /* Insert the extra space we need. If there is insufficient
6087 * space return -1. */
6088 if (*buflen + extra + new_slen >= bufsize)
6089 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006091 (size_t)(*buflen - offset));
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006094 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006096 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
6098
Bram Moolenaar2951b772013-07-03 12:45:31 +02006099#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006100 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006101#endif
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 return 0; /* no match found */
6104}
6105
Bram Moolenaar9377df32017-10-15 13:22:01 +02006106#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006107/*
6108 * Get the text foreground color, if known.
6109 */
6110 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006111term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006112{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006113 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006114 {
6115 *r = fg_r;
6116 *g = fg_g;
6117 *b = fg_b;
6118 }
6119}
6120
6121/*
6122 * Get the text background color, if known.
6123 */
6124 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006125term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006126{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006127 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006128 {
6129 *r = bg_r;
6130 *g = bg_g;
6131 *b = bg_b;
6132 }
6133}
6134#endif
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136/*
6137 * Replace any terminal code strings in from[] with the equivalent internal
6138 * vim representation. This is used for the "from" and "to" part of a
6139 * mapping, and the "to" part of a menu command.
6140 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6141 * '<'.
6142 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6143 *
6144 * The replacement is done in result[] and finally copied into allocated
6145 * memory. If this all works well *bufp is set to the allocated memory and a
6146 * pointer to it is returned. If something fails *bufp is set to NULL and from
6147 * is returned.
6148 *
6149 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6150 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6151 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6152 * instead of a CTRL-V.
6153 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006154 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006155replace_termcodes(
6156 char_u *from,
6157 char_u **bufp,
6158 int from_part,
6159 int do_lt, /* also translate <lt> */
6160 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 int i;
6163 int slen;
6164 int key;
6165 int dlen = 0;
6166 char_u *src;
6167 int do_backslash; /* backslash is a special character */
6168 int do_special; /* recognize <> key codes */
6169 int do_key_code; /* recognize raw key codes */
6170 char_u *result; /* buffer for resulting string */
6171
6172 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006173 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6175
6176 /*
6177 * Allocate space for the translation. Worst case a single character is
6178 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6179 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02006180 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (result == NULL) /* out of memory */
6182 {
6183 *bufp = NULL;
6184 return from;
6185 }
6186
6187 src = from;
6188
6189 /*
6190 * Check for #n at start only: function key n
6191 */
6192 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6193 {
6194 result[dlen++] = K_SPECIAL;
6195 result[dlen++] = 'k';
6196 if (src[1] == '0')
6197 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6198 else
6199 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6200 src += 2;
6201 }
6202
6203 /*
6204 * Copy each byte from *from to result[dlen]
6205 */
6206 while (*src != NUL)
6207 {
6208 /*
6209 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006210 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 */
6212 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6213 {
6214#ifdef FEAT_EVAL
6215 /*
6216 * Replace <SID> by K_SNR <script-nr> _.
6217 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6218 */
6219 if (STRNICMP(src, "<SID>", 5) == 0)
6220 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006221 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006222 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 else
6224 {
6225 src += 5;
6226 result[dlen++] = K_SPECIAL;
6227 result[dlen++] = (int)KS_EXTRA;
6228 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006229 sprintf((char *)result + dlen, "%ld",
6230 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 dlen += (int)STRLEN(result + dlen);
6232 result[dlen++] = '_';
6233 continue;
6234 }
6235 }
6236#endif
6237
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006238 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (slen)
6240 {
6241 dlen += slen;
6242 continue;
6243 }
6244 }
6245
6246 /*
6247 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6248 * Note that this is also checked after replacing the <> form.
6249 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6250 * it could be a character in the file.
6251 */
6252 if (do_key_code)
6253 {
6254 i = find_term_bykeys(src);
6255 if (i >= 0)
6256 {
6257 result[dlen++] = K_SPECIAL;
6258 result[dlen++] = termcodes[i].name[0];
6259 result[dlen++] = termcodes[i].name[1];
6260 src += termcodes[i].len;
6261 /* If terminal code matched, continue after it. */
6262 continue;
6263 }
6264 }
6265
6266#ifdef FEAT_EVAL
6267 if (do_special)
6268 {
6269 char_u *p, *s, len;
6270
6271 /*
6272 * Replace <Leader> by the value of "mapleader".
6273 * Replace <LocalLeader> by the value of "maplocalleader".
6274 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6275 */
6276 if (STRNICMP(src, "<Leader>", 8) == 0)
6277 {
6278 len = 8;
6279 p = get_var_value((char_u *)"g:mapleader");
6280 }
6281 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6282 {
6283 len = 13;
6284 p = get_var_value((char_u *)"g:maplocalleader");
6285 }
6286 else
6287 {
6288 len = 0;
6289 p = NULL;
6290 }
6291 if (len != 0)
6292 {
6293 /* Allow up to 8 * 6 characters for "mapleader". */
6294 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6295 s = (char_u *)"\\";
6296 else
6297 s = p;
6298 while (*s != NUL)
6299 result[dlen++] = *s++;
6300 src += len;
6301 continue;
6302 }
6303 }
6304#endif
6305
6306 /*
6307 * Remove CTRL-V and ignore the next character.
6308 * For "from" side the CTRL-V at the end is included, for the "to"
6309 * part it is removed.
6310 * If 'cpoptions' does not contain 'B', also accept a backslash.
6311 */
6312 key = *src;
6313 if (key == Ctrl_V || (do_backslash && key == '\\'))
6314 {
6315 ++src; /* skip CTRL-V or backslash */
6316 if (*src == NUL)
6317 {
6318 if (from_part)
6319 result[dlen++] = key;
6320 break;
6321 }
6322 }
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006325 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
6327 /*
6328 * If the character is K_SPECIAL, replace it with K_SPECIAL
6329 * KS_SPECIAL KE_FILLER.
6330 * If compiled with the GUI replace CSI with K_CSI.
6331 */
6332 if (*src == K_SPECIAL)
6333 {
6334 result[dlen++] = K_SPECIAL;
6335 result[dlen++] = KS_SPECIAL;
6336 result[dlen++] = KE_FILLER;
6337 }
6338# ifdef FEAT_GUI
6339 else if (*src == CSI)
6340 {
6341 result[dlen++] = K_SPECIAL;
6342 result[dlen++] = KS_EXTRA;
6343 result[dlen++] = (int)KE_CSI;
6344 }
6345# endif
6346 else
6347 result[dlen++] = *src;
6348 ++src;
6349 }
6350 }
6351 result[dlen] = NUL;
6352
6353 /*
6354 * Copy the new string to allocated memory.
6355 * If this fails, just return from.
6356 */
6357 if ((*bufp = vim_strsave(result)) != NULL)
6358 from = *bufp;
6359 vim_free(result);
6360 return from;
6361}
6362
6363/*
6364 * Find a termcode with keys 'src' (must be NUL terminated).
6365 * Return the index in termcodes[], or -1 if not found.
6366 */
6367 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006368find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006371 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373 for (i = 0; i < tc_len; ++i)
6374 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006375 if (slen == termcodes[i].len
6376 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 return i;
6378 }
6379 return -1;
6380}
6381
6382/*
6383 * Gather the first characters in the terminal key codes into a string.
6384 * Used to speed up check_termcode().
6385 */
6386 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006387gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int i;
6390 int len = 0;
6391
6392#ifdef FEAT_GUI
6393 if (gui.in_use)
6394 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6395#endif
6396#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006397 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 termleader[len++] = DCS; /* the termcode response starts with DCS
6399 in 8-bit mode */
6400#endif
6401 termleader[len] = NUL;
6402
6403 for (i = 0; i < tc_len; ++i)
6404 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6405 {
6406 termleader[len++] = termcodes[i].code[0];
6407 termleader[len] = NUL;
6408 }
6409
6410 need_gather = FALSE;
6411}
6412
6413/*
6414 * Show all termcodes (for ":set termcap")
6415 * This code looks a lot like showoptions(), but is different.
6416 */
6417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006418show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 int col;
6421 int *items;
6422 int item_count;
6423 int run;
6424 int row, rows;
6425 int cols;
6426 int i;
6427 int len;
6428
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006429#define INC3 27 /* try to make three columns */
6430#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431#define GAP 2 /* spaces between columns */
6432
6433 if (tc_len == 0) /* no terminal codes (must be GUI) */
6434 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006435 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (items == NULL)
6437 return;
6438
6439 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006440 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
6442 /*
6443 * do the loop two times:
6444 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006445 * 2. display the medium items (medium length strings)
6446 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006448 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
6450 /*
6451 * collect the items in items[]
6452 */
6453 item_count = 0;
6454 for (i = 0; i < tc_len; i++)
6455 {
6456 len = show_one_termcode(termcodes[i].name,
6457 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006458 if (len <= INC3 - GAP ? run == 1
6459 : len <= INC2 - GAP ? run == 2
6460 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 items[item_count++] = i;
6462 }
6463
6464 /*
6465 * display the items
6466 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006467 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006469 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (cols == 0)
6471 cols = 1;
6472 rows = (item_count + cols - 1) / cols;
6473 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006474 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 rows = item_count;
6476 for (row = 0; row < rows && !got_int; ++row)
6477 {
6478 msg_putchar('\n'); /* go to next line */
6479 if (got_int) /* 'q' typed in more */
6480 break;
6481 col = 0;
6482 for (i = row; i < item_count; i += rows)
6483 {
6484 msg_col = col; /* make columns */
6485 show_one_termcode(termcodes[items[i]].name,
6486 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006487 if (run == 2)
6488 col += INC2;
6489 else
6490 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 out_flush();
6493 ui_breakcheck();
6494 }
6495 }
6496 vim_free(items);
6497}
6498
6499/*
6500 * Show one termcode entry.
6501 * Output goes into IObuff[]
6502 */
6503 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006504show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 char_u *p;
6507 int len;
6508
6509 if (name[0] > '~')
6510 {
6511 IObuff[0] = ' ';
6512 IObuff[1] = ' ';
6513 IObuff[2] = ' ';
6514 IObuff[3] = ' ';
6515 }
6516 else
6517 {
6518 IObuff[0] = 't';
6519 IObuff[1] = '_';
6520 IObuff[2] = name[0];
6521 IObuff[3] = name[1];
6522 }
6523 IObuff[4] = ' ';
6524
6525 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6526 if (p[1] != 't')
6527 STRCPY(IObuff + 5, p);
6528 else
6529 IObuff[5] = NUL;
6530 len = (int)STRLEN(IObuff);
6531 do
6532 IObuff[len++] = ' ';
6533 while (len < 17);
6534 IObuff[len] = NUL;
6535 if (code == NULL)
6536 len += 4;
6537 else
6538 len += vim_strsize(code);
6539
6540 if (printit)
6541 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006542 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006544 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 else
6546 msg_outtrans(code);
6547 }
6548 return len;
6549}
6550
6551#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6552/*
6553 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6554 * termcap codes from the terminal itself.
6555 * We get them one by one to avoid a very long response string.
6556 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006557static int xt_index_in = 0;
6558static int xt_index_out = 0;
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006561req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
6563 xt_index_out = 0;
6564 xt_index_in = 0;
6565 req_more_codes_from_term();
6566}
6567
6568 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006569req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 char buf[11];
6572 int old_idx = xt_index_out;
6573
6574 /* Don't do anything when going to exit. */
6575 if (exiting)
6576 return;
6577
6578 /* Send up to 10 more requests out than we received. Avoid sending too
6579 * many, there can be a buffer overflow somewhere. */
6580 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6581 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006582 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006583
Bram Moolenaarb255b902018-04-24 21:40:10 +02006584 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6585 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 out_str_nf((char_u *)buf);
6587 ++xt_index_out;
6588 }
6589
6590 /* Send the codes out right away. */
6591 if (xt_index_out != old_idx)
6592 out_flush();
6593}
6594
6595/*
6596 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6597 * A "0" instead of the "1" indicates a code that isn't supported.
6598 * Both <name> and <string> are encoded in hex.
6599 * "code" points to the "0" or "1".
6600 */
6601 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006602got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604#define XT_LEN 100
6605 char_u name[3];
6606 char_u str[XT_LEN];
6607 int i;
6608 int j = 0;
6609 int c;
6610
6611 /* A '1' means the code is supported, a '0' means it isn't.
6612 * When half the length is > XT_LEN we can't use it.
6613 * Our names are currently all 2 characters. */
6614 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6615 {
6616 /* Get the name from the response and find it in the table. */
6617 name[0] = hexhex2nr(code + 3);
6618 name[1] = hexhex2nr(code + 5);
6619 name[2] = NUL;
6620 for (i = 0; key_names[i] != NULL; ++i)
6621 {
6622 if (STRCMP(key_names[i], name) == 0)
6623 {
6624 xt_index_in = i;
6625 break;
6626 }
6627 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006628
Bram Moolenaarb255b902018-04-24 21:40:10 +02006629 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (key_names[i] != NULL)
6632 {
6633 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6634 str[j++] = c;
6635 str[j] = NUL;
6636 if (name[0] == 'C' && name[1] == 'o')
6637 {
6638 /* Color count is not a key code. */
6639 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006640 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
6642 else
6643 {
6644 /* First delete any existing entry with the same code. */
6645 i = find_term_bykeys(str);
6646 if (i >= 0)
6647 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006648 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
6650 }
6651 }
6652
6653 /* May request more codes now that we received one. */
6654 ++xt_index_in;
6655 req_more_codes_from_term();
6656}
6657
6658/*
6659 * Check if there are any unanswered requests and deal with them.
6660 * This is called before starting an external program or getting direct
6661 * keyboard input. We don't want responses to be send to that program or
6662 * handled as typed text.
6663 */
6664 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006665check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 int c;
6668
6669 /* If no codes requested or all are answered, no need to wait. */
6670 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6671 return;
6672
6673 /* Vgetc() will check for and handle any response.
6674 * Keep calling vpeekc() until we don't get any responses. */
6675 ++no_mapping;
6676 ++allow_keys;
6677 for (;;)
6678 {
6679 c = vpeekc();
6680 if (c == NUL) /* nothing available */
6681 break;
6682
6683 /* If a response is recognized it's replaced with K_IGNORE, must read
6684 * it from the input stream. If there is no K_IGNORE we can't do
6685 * anything, break here (there might be some responses further on, but
6686 * we don't want to throw away any typed chars). */
6687 if (c != K_SPECIAL && c != K_IGNORE)
6688 break;
6689 c = vgetc();
6690 if (c != K_IGNORE)
6691 {
6692 vungetc(c);
6693 break;
6694 }
6695 }
6696 --no_mapping;
6697 --allow_keys;
6698}
6699#endif
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701/*
6702 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006703 * corresponding external one recognized by :map/:abbrev commands.
6704 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 *
6706 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006707 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006709 * It uses a growarray to build the translation string since the latter can be
6710 * wider than the original description. The caller has to free the string
6711 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 *
6713 * Returns NULL when there is a problem.
6714 */
6715 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006716translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 garray_T ga;
6719 int c;
6720 int modifiers;
6721 int cpo_bslash;
6722 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724 ga_init(&ga);
6725 ga.ga_itemsize = 1;
6726 ga.ga_growsize = 40;
6727
6728 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6729 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 for (; *str; ++str)
6732 {
6733 c = *str;
6734 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6735 {
6736 modifiers = 0;
6737 if (str[1] == KS_MODIFIER)
6738 {
6739 str++;
6740 modifiers = *++str;
6741 c = *++str;
6742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6744 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006745 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {
6747 ga_clear(&ga);
6748 return NULL;
6749 }
6750 c = TO_SPECIAL(str[1], str[2]);
6751 if (c == K_ZERO) /* display <Nul> as ^@ */
6752 c = NUL;
6753 str += 2;
6754 }
6755 if (IS_SPECIAL(c) || modifiers) /* special key */
6756 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02006757 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 ga_clear(&ga);
6760 return NULL;
6761 }
6762 ga_concat(&ga, get_special_key_name(c, modifiers));
6763 continue; /* for (str) */
6764 }
6765 }
6766 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6767 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6768 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6769 if (c)
6770 ga_append(&ga, c);
6771 }
6772 ga_append(&ga, NUL);
6773 return (char_u *)(ga.ga_data);
6774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006776#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777static char ksme_str[20];
6778static char ksmr_str[20];
6779static char ksmd_str[20];
6780
6781/*
6782 * For Win32 console: update termcap codes for existing console attributes.
6783 */
6784 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006785update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 struct builtin_term *p;
6788
6789 p = find_builtin_term(DEFAULT_TERM);
6790 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6791 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6792 attr | 0x08); /* FOREGROUND_INTENSITY */
6793 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6794 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6795
6796 while (p->bt_string != NULL)
6797 {
6798 if (p->bt_entry == (int)KS_ME)
6799 p->bt_string = &ksme_str[0];
6800 else if (p->bt_entry == (int)KS_MR)
6801 p->bt_string = &ksmr_str[0];
6802 else if (p->bt_entry == (int)KS_MD)
6803 p->bt_string = &ksmd_str[0];
6804 ++p;
6805 }
6806}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006807
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006808# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006809# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006810struct ks_tbl_s
6811{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006812 int code; // value of KS_
6813 char *vtp; // code in vtp mode
6814 char *vtp2; // code in vtp2 mode
6815 char buf[KSSIZE]; // save buffer in non-vtp mode
6816 char vbuf[KSSIZE]; // save buffer in vtp mode
6817 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6818 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006819};
6820
6821static struct ks_tbl_s ks_tbl[] =
6822{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006823 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6824 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6825 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6826 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6827 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6828 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6829 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6830 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6831 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006832# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006833 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6834 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006835 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6836 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006837# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006838 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6839 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006840 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6841 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006842# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006843 {(int)KS_CCO, "256", "256"}, // colors
6844 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006845};
6846
6847 static struct builtin_term *
6848find_first_tcap(
6849 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006850 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006851{
6852 struct builtin_term *p;
6853
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006854 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006855 if (p->bt_entry == code)
6856 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006857 return NULL;
6858}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006859# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006860
6861/*
6862 * For Win32 console: replace the sequence immediately after termguicolors.
6863 */
6864 void
6865swap_tcap(void)
6866{
6867# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006868 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006869 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006870 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006871 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006872 int mode;
6873 enum
6874 {
6875 CMODEINDEX,
6876 CMODE24,
6877 CMODE256
6878 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006879
6880 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006881 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006882 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006883 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006884 {
6885 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006886 if (bt != NULL)
6887 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006888 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6889 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6890 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6891
6892 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6893 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006894 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006895 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006896 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006897 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006898 }
6899
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006900 if (p_tgc)
6901 mode = CMODE24;
6902 else if (t_colors >= 256)
6903 mode = CMODE256;
6904 else
6905 mode = CMODEINDEX;
6906
6907 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006908 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006909 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6910 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006911 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006912 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006913 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006914 case CMODEINDEX:
6915 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6916 break;
6917 case CMODE24:
6918 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6919 break;
6920 default:
6921 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006922 }
6923 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006924 }
6925
6926 if (mode != curr_mode)
6927 {
6928 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006929 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006930 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6931 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006932 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006933 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006934 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006935 case CMODEINDEX:
6936 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6937 break;
6938 case CMODE24:
6939 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6940 break;
6941 default:
6942 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006943 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006944 }
6945 }
6946
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006947 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006948 }
6949# endif
6950}
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006953
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006954#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006955 static int
6956hex_digit(int c)
6957{
6958 if (isdigit(c))
6959 return c - '0';
6960 c = TOLOWER_ASC(c);
6961 if (c >= 'a' && c <= 'f')
6962 return c - 'a' + 10;
6963 return 0x1ffffff;
6964}
6965
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006966# ifdef VIMDLL
6967 static guicolor_T
6968gui_adjust_rgb(guicolor_T c)
6969{
6970 if (gui.in_use)
6971 return c;
6972 else
6973 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6974}
6975# else
6976# define gui_adjust_rgb(c) (c)
6977# endif
6978
Bram Moolenaarab302212016-04-26 20:59:29 +02006979 guicolor_T
6980gui_get_color_cmn(char_u *name)
6981{
Bram Moolenaar28726652017-01-06 18:16:19 +01006982 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6983 * values as used by the MS-Windows GDI api. It should be used only for
6984 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006985# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006986# undef RGB
6987# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006988# ifndef RGB
6989# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6990# endif
6991# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006992 FILE *fd;
6993 char line[LINE_LEN];
6994 char_u *fname;
6995 int r, g, b, i;
6996 guicolor_T color;
6997
6998 struct rgbcolor_table_S {
6999 char_u *color_name;
7000 guicolor_T color;
7001 };
7002
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007003 /* Only non X11 colors (not present in rgb.txt) and colors in
7004 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02007005 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007006 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
7007 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
7008 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
7009 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
7010 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
7011 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
7012 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
7013 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
7014 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
7015 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
7016 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
7017 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
7018 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007019 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
7020 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02007021 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01007022 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007023 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007024 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
7025 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
7026 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
7027 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
7028 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
7029 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
7030 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
7031 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
7032 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007033 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02007034 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007035 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
7036 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02007037 };
7038
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007039 static struct rgbcolor_table_S *colornames_table;
7040 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02007041
7042 if (name[0] == '#' && STRLEN(name) == 7)
7043 {
7044 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02007045 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02007046 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
7047 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
7048 if (color > 0xffffff)
7049 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007050 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007051 }
7052
7053 /* Check if the name is one of the colors we know */
7054 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
7055 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007056 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02007057
7058 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007059 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02007060 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007061 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02007062 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007063 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02007064
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007065 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007066 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
7067 if (fname == NULL)
7068 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007069
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007070 fd = fopen((char *)fname, "rt");
7071 vim_free(fname);
7072 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02007073 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007074 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007075 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007076 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007077 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007078 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007079
7080 for (counting = 1; counting >= 0; --counting)
7081 {
7082 if (!counting)
7083 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007084 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007085 if (colornames_table == NULL)
7086 {
7087 fclose(fd);
7088 return INVALCOLOR;
7089 }
7090 rewind(fd);
7091 }
7092 size = 0;
7093
7094 while (!feof(fd))
7095 {
7096 size_t len;
7097 int pos;
7098
Bram Moolenaar42335f52018-09-13 15:33:43 +02007099 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007100 len = strlen(line);
7101
7102 if (len <= 1 || line[len - 1] != '\n')
7103 continue;
7104
7105 line[len - 1] = '\0';
7106
7107 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7108 if (i != 3)
7109 continue;
7110
7111 if (!counting)
7112 {
7113 char_u *s = vim_strsave((char_u *)line + pos);
7114
7115 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007116 {
7117 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007118 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007119 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007120 colornames_table[size].color_name = s;
7121 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7122 }
7123 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007124
7125 // The distributed rgb.txt has less than 1000 entries. Limit to
7126 // 10000, just in case the file was messed up.
7127 if (size == 10000)
7128 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007129 }
7130 }
7131 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007132 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007133
7134 for (i = 0; i < size; i++)
7135 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007136 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007137
Bram Moolenaarab302212016-04-26 20:59:29 +02007138 return INVALCOLOR;
7139}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007140
7141 guicolor_T
7142gui_get_rgb_color_cmn(int r, int g, int b)
7143{
7144 guicolor_T color = RGB(r, g, b);
7145
7146 if (color > 0xffffff)
7147 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007148 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007149}
Bram Moolenaarab302212016-04-26 20:59:29 +02007150#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007151
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007152#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007153 || defined(PROTO)
7154static int cube_value[] = {
7155 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7156};
7157
7158static int grey_ramp[] = {
7159 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7160 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7161};
7162
7163# ifdef FEAT_TERMINAL
7164# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007165# else
7166# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007167# endif
7168
Bram Moolenaar9894e392018-05-05 14:29:06 +02007169static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007170// R G B idx
7171 { 0, 0, 0, 1}, // black
7172 {224, 0, 0, 2}, // dark red
7173 { 0, 224, 0, 3}, // dark green
7174 {224, 224, 0, 4}, // dark yellow / brown
7175 { 0, 0, 224, 5}, // dark blue
7176 {224, 0, 224, 6}, // dark magenta
7177 { 0, 224, 224, 7}, // dark cyan
7178 {224, 224, 224, 8}, // light grey
7179
7180 {128, 128, 128, 9}, // dark grey
7181 {255, 64, 64, 10}, // light red
7182 { 64, 255, 64, 11}, // light green
7183 {255, 255, 64, 12}, // yellow
7184 { 64, 64, 255, 13}, // light blue
7185 {255, 64, 255, 14}, // light magenta
7186 { 64, 255, 255, 15}, // light cyan
7187 {255, 255, 255, 16}, // white
7188};
7189
7190 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007191cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007192{
7193 int idx;
7194
7195 if (nr < 16)
7196 {
7197 *r = ansi_table[nr][0];
7198 *g = ansi_table[nr][1];
7199 *b = ansi_table[nr][2];
7200 *ansi_idx = ansi_table[nr][3];
7201 }
7202 else if (nr < 232)
7203 {
7204 /* 216 color cube */
7205 idx = nr - 16;
7206 *r = cube_value[idx / 36 % 6];
7207 *g = cube_value[idx / 6 % 6];
7208 *b = cube_value[idx % 6];
7209 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7210 }
7211 else if (nr < 256)
7212 {
7213 /* 24 grey scale ramp */
7214 idx = nr - 232;
7215 *r = grey_ramp[idx];
7216 *g = grey_ramp[idx];
7217 *b = grey_ramp[idx];
7218 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7219 }
7220 else
7221 {
7222 *r = 0;
7223 *g = 0;
7224 *b = 0;
7225 *ansi_idx = 0;
7226 }
7227}
7228#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007229