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