blob: 9eebd28cb11f3c7b21b895b9af22c7c4a1652c8d [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 */
24
25#define tgetstr tgetstr_defined_wrong
26#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
113# define STATUS_GET 1 /* send request when switching to RAW mode */
114# define STATUS_SENT 2 /* did send request, waiting for response */
115# define STATUS_GOT 3 /* received response */
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200118static int crv_status = STATUS_GET;
119
Bram Moolenaar9584b312013-03-13 19:29:28 +0100120/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200121static int u7_status = STATUS_GET;
122
Bram Moolenaar9377df32017-10-15 13:22:01 +0200123# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200124/* Request foreground color report: */
125static int rfg_status = STATUS_GET;
126static int fg_r = 0;
127static int fg_g = 0;
128static int fg_b = 0;
129static int bg_r = 255;
130static int bg_g = 255;
131static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200132# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200133
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200134/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200135static int rbg_status = STATUS_GET;
136
Bram Moolenaar4db25542017-08-28 22:43:05 +0200137/* Request cursor blinking mode report: */
138static int rbm_status = STATUS_GET;
139
140/* Request cursor style report: */
141static int rcs_status = STATUS_GET;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100142
143/* Request windos position report: */
144static int winpos_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
146
147/*
148 * Don't declare these variables if termcap.h contains them.
149 * Autoconf checks if these variables should be declared extern (not all
150 * systems have them).
151 * Some versions define ospeed to be speed_t, but that is incompatible with
152 * BSD, where ospeed is short and speed_t is long.
153 */
154# ifndef HAVE_OSPEED
155# ifdef OSPEED_EXTERN
156extern short ospeed;
157# else
158short ospeed;
159# endif
160# endif
161# ifndef HAVE_UP_BC_PC
162# ifdef UP_BC_PC_EXTERN
163extern char *UP, *BC, PC;
164# else
165char *UP, *BC, PC;
166# endif
167# endif
168
169# define TGETSTR(s, p) vim_tgetstr((s), (p))
170# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif /* HAVE_TGETENT */
173
174static int detected_8bit = FALSE; /* detected 8-bit terminal */
175
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200176#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200177/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200178 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200179static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200180
181/* The blink flag from the style response may be inverted from the actual
182 * blinking state, xterm XORs the flags. */
183static int initial_cursor_shape_blink = FALSE;
184
185/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200186static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200187#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200188
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000189static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
191
192#if defined(FEAT_GUI)
193/*
194 * GUI pseudo term-cap.
195 */
196 {(int)KS_NAME, "gui"},
197 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
198 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
199# ifdef TERMINFO
200 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
201# else
202 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
203# endif
204 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
205# ifdef TERMINFO
206 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
207 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# else
210 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
211 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# endif
214 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
215 /* attributes switched on with 'h', off with * 'H' */
216 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
217 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
218 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
219 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
220 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
221 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
222 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000223 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
224 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200225 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
226 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
228 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
229 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
230 {(int)KS_MS, "y"},
231 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100232 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {(int)KS_LE, "\b"}, /* cursor-left = BS */
234 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
235# ifdef TERMINFO
236 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
237# else
238 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
239# endif
240 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000241 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
243
244#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
247/*
248 * Amiga console window, default for Amiga
249 */
250 {(int)KS_NAME, "amiga"},
251 {(int)KS_CE, "\033[K"},
252 {(int)KS_CD, "\033[J"},
253 {(int)KS_AL, "\033[L"},
254# ifdef TERMINFO
255 {(int)KS_CAL, "\033[%p1%dL"},
256# else
257 {(int)KS_CAL, "\033[%dL"},
258# endif
259 {(int)KS_DL, "\033[M"},
260# ifdef TERMINFO
261 {(int)KS_CDL, "\033[%p1%dM"},
262# else
263 {(int)KS_CDL, "\033[%dM"},
264# endif
265 {(int)KS_CL, "\014"},
266 {(int)KS_VI, "\033[0 p"},
267 {(int)KS_VE, "\033[1 p"},
268 {(int)KS_ME, "\033[0m"},
269 {(int)KS_MR, "\033[7m"},
270 {(int)KS_MD, "\033[1m"},
271 {(int)KS_SE, "\033[0m"},
272 {(int)KS_SO, "\033[33m"},
273 {(int)KS_US, "\033[4m"},
274 {(int)KS_UE, "\033[0m"},
275 {(int)KS_CZH, "\033[3m"},
276 {(int)KS_CZR, "\033[0m"},
277#if defined(__MORPHOS__) || defined(__AROS__)
278 {(int)KS_CCO, "8"}, /* allow 8 colors */
279# ifdef TERMINFO
280 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
281 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
282# else
283 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
284 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
285# endif
286 {(int)KS_OP, "\033[m"}, /* reset colors */
287#endif
288 {(int)KS_MS, "y"},
289 {(int)KS_UT, "y"}, /* guessed */
290 {(int)KS_LE, "\b"},
291# ifdef TERMINFO
292 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
293# else
294 {(int)KS_CM, "\033[%i%d;%dH"},
295# endif
296#if defined(__MORPHOS__)
297 {(int)KS_SR, "\033M"},
298#endif
299# ifdef TERMINFO
300 {(int)KS_CRI, "\033[%p1%dC"},
301# else
302 {(int)KS_CRI, "\033[%dC"},
303# endif
304 {K_UP, "\233A"},
305 {K_DOWN, "\233B"},
306 {K_LEFT, "\233D"},
307 {K_RIGHT, "\233C"},
308 {K_S_UP, "\233T"},
309 {K_S_DOWN, "\233S"},
310 {K_S_LEFT, "\233 A"},
311 {K_S_RIGHT, "\233 @"},
312 {K_S_TAB, "\233Z"},
313 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
314 {K_F2, "\233\061~"},
315 {K_F3, "\233\062~"},
316 {K_F4, "\233\063~"},
317 {K_F5, "\233\064~"},
318 {K_F6, "\233\065~"},
319 {K_F7, "\233\066~"},
320 {K_F8, "\233\067~"},
321 {K_F9, "\233\070~"},
322 {K_F10, "\233\071~"},
323 {K_S_F1, "\233\061\060~"},
324 {K_S_F2, "\233\061\061~"},
325 {K_S_F3, "\233\061\062~"},
326 {K_S_F4, "\233\061\063~"},
327 {K_S_F5, "\233\061\064~"},
328 {K_S_F6, "\233\061\065~"},
329 {K_S_F7, "\233\061\066~"},
330 {K_S_F8, "\233\061\067~"},
331 {K_S_F9, "\233\061\070~"},
332 {K_S_F10, "\233\061\071~"},
333 {K_HELP, "\233?~"},
334 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
335 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
336 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
337 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
338 {K_END, "\233\064\065~"}, /* 101 key keyboard */
339
340 {BT_EXTRA_KEYS, ""},
341 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
342 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
343 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
344# endif
345
346# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
347/*
348 * almost standard ANSI terminal, default for bebox
349 */
350 {(int)KS_NAME, "beos-ansi"},
351 {(int)KS_CE, "\033[K"},
352 {(int)KS_CD, "\033[J"},
353 {(int)KS_AL, "\033[L"},
354# ifdef TERMINFO
355 {(int)KS_CAL, "\033[%p1%dL"},
356# else
357 {(int)KS_CAL, "\033[%dL"},
358# endif
359 {(int)KS_DL, "\033[M"},
360# ifdef TERMINFO
361 {(int)KS_CDL, "\033[%p1%dM"},
362# else
363 {(int)KS_CDL, "\033[%dM"},
364# endif
365#ifdef BEOS_PR_OR_BETTER
366# ifdef TERMINFO
367 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
368# else
369 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
370# endif
371#endif
372 {(int)KS_CL, "\033[H\033[2J"},
373#ifdef notyet
374 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
375 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
376#endif
377 {(int)KS_ME, "\033[m"}, /* normal mode */
378 {(int)KS_MR, "\033[7m"}, /* reverse */
379 {(int)KS_MD, "\033[1m"}, /* bold */
380 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
381 {(int)KS_SE, "\033[m"}, /* standout end */
382 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
383 {(int)KS_CZR, "\033[m"}, /* italic end */
384 {(int)KS_US, "\033[4m"}, /* underscore mode */
385 {(int)KS_UE, "\033[m"}, /* underscore end */
386 {(int)KS_CCO, "8"}, /* allow 8 colors */
387# ifdef TERMINFO
388 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
389 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
390# else
391 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
392 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
393# endif
394 {(int)KS_OP, "\033[m"}, /* reset colors */
395 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
396 {(int)KS_UT, "y"}, /* guessed */
397 {(int)KS_LE, "\b"},
398# ifdef TERMINFO
399 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
400# else
401 {(int)KS_CM, "\033[%i%d;%dH"},
402# endif
403 {(int)KS_SR, "\033M"},
404# ifdef TERMINFO
405 {(int)KS_CRI, "\033[%p1%dC"},
406# else
407 {(int)KS_CRI, "\033[%dC"},
408# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200409# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412
413 {K_UP, "\033[A"},
414 {K_DOWN, "\033[B"},
415 {K_LEFT, "\033[D"},
416 {K_RIGHT, "\033[C"},
417# endif
418
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200419# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420/*
421 * standard ANSI terminal, default for unix
422 */
423 {(int)KS_NAME, "ansi"},
424 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
425 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
426# ifdef TERMINFO
427 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
428# else
429 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
430# endif
431 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
432# ifdef TERMINFO
433 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
434# else
435 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
436# endif
437 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
438 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
439 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
440 {(int)KS_MS, "y"},
441 {(int)KS_UT, "y"}, /* guessed */
442 {(int)KS_LE, "\b"},
443# ifdef TERMINFO
444 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
445# else
446 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
447# endif
448# ifdef TERMINFO
449 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
450# else
451 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
452# endif
453# endif
454
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200455# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456/*
457 * These codes are valid when nansi.sys or equivalent has been installed.
458 * Function keys on a PC are preceded with a NUL. These are converted into
459 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
460 * CTRL-arrow is used instead of SHIFT-arrow.
461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {(int)KS_NAME, "pcansi"},
463 {(int)KS_DL, "\033[M"},
464 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {(int)KS_CE, "\033[K"},
466 {(int)KS_CL, "\033[2J"},
467 {(int)KS_ME, "\033[0m"},
468 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
469 {(int)KS_MD, "\033[1m"}, /* bold: white text */
470 {(int)KS_SE, "\033[0m"}, /* standout end */
471 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
472 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
473 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
474 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
475 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
476 {(int)KS_CCO, "8"}, /* allow 8 colors */
477# ifdef TERMINFO
478 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
479 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
480# else
481 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
482 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
483# endif
484 {(int)KS_OP, "\033[0m"}, /* reset colors */
485 {(int)KS_MS, "y"},
486 {(int)KS_UT, "y"}, /* guessed */
487 {(int)KS_LE, "\b"},
488# ifdef TERMINFO
489 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
490# else
491 {(int)KS_CM, "\033[%i%d;%dH"},
492# endif
493# ifdef TERMINFO
494 {(int)KS_CRI, "\033[%p1%dC"},
495# else
496 {(int)KS_CRI, "\033[%dC"},
497# endif
498 {K_UP, "\316H"},
499 {K_DOWN, "\316P"},
500 {K_LEFT, "\316K"},
501 {K_RIGHT, "\316M"},
502 {K_S_LEFT, "\316s"},
503 {K_S_RIGHT, "\316t"},
504 {K_F1, "\316;"},
505 {K_F2, "\316<"},
506 {K_F3, "\316="},
507 {K_F4, "\316>"},
508 {K_F5, "\316?"},
509 {K_F6, "\316@"},
510 {K_F7, "\316A"},
511 {K_F8, "\316B"},
512 {K_F9, "\316C"},
513 {K_F10, "\316D"},
514 {K_F11, "\316\205"}, /* guessed */
515 {K_F12, "\316\206"}, /* guessed */
516 {K_S_F1, "\316T"},
517 {K_S_F2, "\316U"},
518 {K_S_F3, "\316V"},
519 {K_S_F4, "\316W"},
520 {K_S_F5, "\316X"},
521 {K_S_F6, "\316Y"},
522 {K_S_F7, "\316Z"},
523 {K_S_F8, "\316["},
524 {K_S_F9, "\316\\"},
525 {K_S_F10, "\316]"},
526 {K_S_F11, "\316\207"}, /* guessed */
527 {K_S_F12, "\316\210"}, /* guessed */
528 {K_INS, "\316R"},
529 {K_DEL, "\316S"},
530 {K_HOME, "\316G"},
531 {K_END, "\316O"},
532 {K_PAGEDOWN, "\316Q"},
533 {K_PAGEUP, "\316I"},
534# endif
535
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200536# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537/*
538 * These codes are valid for the Win32 Console . The entries that start with
539 * ESC | are translated into console calls in os_win32.c. The function keys
540 * are also translated in os_win32.c.
541 */
542 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100543 {(int)KS_CE, "\033|K"}, // clear to end of line
544 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100546 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100548 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100550 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100552 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
553 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100555 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
556 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100558 {(int)KS_CL, "\033|J"}, // clear screen
559 {(int)KS_CD, "\033|j"}, // clear to end of display
560 {(int)KS_VI, "\033|v"}, // cursor invisible
561 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar6982f422019-02-16 16:48:01 +0100563 {(int)KS_ME, "\033|0m"}, // normal
564 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
565 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_SO, "\033|31m"}, // standout: white on blue
568 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_SO, "\033|F"}, // standout: high intensity
571 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100573 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
574 {(int)KS_CZR, "\033|0m"}, // italic end
575 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
576 {(int)KS_UE, "\033|0m"}, // underscore end
577 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CAB, "\033|%p1%db"}, // set background color
580 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CAB, "\033|%db"}, // set background color
583 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584# endif
585
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100588 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {(int)KS_LE, "\b"},
590# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100593 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100595 {(int)KS_VB, "\033|B"}, // visual bell
596 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
597 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100599 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100601 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100603# ifdef FEAT_TERMGUICOLORS
604 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
605 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 {K_UP, "\316H"},
609 {K_DOWN, "\316P"},
610 {K_LEFT, "\316K"},
611 {K_RIGHT, "\316M"},
612 {K_S_UP, "\316\304"},
613 {K_S_DOWN, "\316\317"},
614 {K_S_LEFT, "\316\311"},
615 {K_C_LEFT, "\316s"},
616 {K_S_RIGHT, "\316\313"},
617 {K_C_RIGHT, "\316t"},
618 {K_S_TAB, "\316\017"},
619 {K_F1, "\316;"},
620 {K_F2, "\316<"},
621 {K_F3, "\316="},
622 {K_F4, "\316>"},
623 {K_F5, "\316?"},
624 {K_F6, "\316@"},
625 {K_F7, "\316A"},
626 {K_F8, "\316B"},
627 {K_F9, "\316C"},
628 {K_F10, "\316D"},
629 {K_F11, "\316\205"},
630 {K_F12, "\316\206"},
631 {K_S_F1, "\316T"},
632 {K_S_F2, "\316U"},
633 {K_S_F3, "\316V"},
634 {K_S_F4, "\316W"},
635 {K_S_F5, "\316X"},
636 {K_S_F6, "\316Y"},
637 {K_S_F7, "\316Z"},
638 {K_S_F8, "\316["},
639 {K_S_F9, "\316\\"},
640 {K_S_F10, "\316]"},
641 {K_S_F11, "\316\207"},
642 {K_S_F12, "\316\210"},
643 {K_INS, "\316R"},
644 {K_DEL, "\316S"},
645 {K_HOME, "\316G"},
646 {K_S_HOME, "\316\302"},
647 {K_C_HOME, "\316w"},
648 {K_END, "\316O"},
649 {K_S_END, "\316\315"},
650 {K_C_END, "\316u"},
651 {K_PAGEDOWN, "\316Q"},
652 {K_PAGEUP, "\316I"},
653 {K_KPLUS, "\316N"},
654 {K_KMINUS, "\316J"},
655 {K_KMULTIPLY, "\316\067"},
656 {K_K0, "\316\332"},
657 {K_K1, "\316\336"},
658 {K_K2, "\316\342"},
659 {K_K3, "\316\346"},
660 {K_K4, "\316\352"},
661 {K_K5, "\316\356"},
662 {K_K6, "\316\362"},
663 {K_K7, "\316\366"},
664 {K_K8, "\316\372"},
665 {K_K9, "\316\376"},
666# endif
667
668# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
669/*
670 * VT320 is working as an ANSI terminal compatible DEC terminal.
671 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 * TODO:- rewrite ESC[ codes to CSI
673 * - keyboard languages (CSI ? 26 n)
674 */
675 {(int)KS_NAME, "vt320"},
676 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
677 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
678# ifdef TERMINFO
679 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
680# else
681 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
682# endif
683 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
684# ifdef TERMINFO
685 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
686# else
687 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
688# endif
689 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000690 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
691 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
693 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
695 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
696 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
697 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
698 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
699 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
700 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
701 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
702 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
703 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {(int)KS_MS, "y"},
705 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100706 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_LE, "\b"},
708# ifdef TERMINFO
709 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
710 ESC_STR "[%i%p1%d;%p2%dH")},
711# else
712 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
713# endif
714# ifdef TERMINFO
715 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
716# else
717 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
718# endif
719 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
720 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
721 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
722 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200723 // Note: cursor key sequences for application cursor mode are omitted,
724 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
726 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
727 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
728 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
729 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
731 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
732 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
733 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
734 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
737 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
738 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
739 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
740 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
741 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
742 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
743 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
744 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
745 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
746 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
747 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
748 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
749 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
750 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200751 // These sequences starting with <Esc> O may interfere with what the user
752 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
754 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
755 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
756 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
757 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200758 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
759 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
760 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
761 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
762 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
763 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
764 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
765 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
766 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
767 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
769# endif
770
771# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
772/*
773 * Ordinary vt52
774 */
775 {(int)KS_NAME, "vt52"},
776 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
777 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100778# ifdef TERMINFO
779 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
780 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
781# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100785 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
787 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {K_UP, IF_EB("\033A", ESC_STR "A")},
789 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
790 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
791 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100792 {K_F1, IF_EB("\033P", ESC_STR "P")},
793 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
794 {K_F3, IF_EB("\033R", ESC_STR "R")},
795# ifdef __MINT__
796 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
797 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
798 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
799 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
800 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
802 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
803 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
804 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {K_F4, IF_EB("\033S", ESC_STR "S")},
806 {K_F5, IF_EB("\033T", ESC_STR "T")},
807 {K_F6, IF_EB("\033U", ESC_STR "U")},
808 {K_F7, IF_EB("\033V", ESC_STR "V")},
809 {K_F8, IF_EB("\033W", ESC_STR "W")},
810 {K_F9, IF_EB("\033X", ESC_STR "X")},
811 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
812 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
813 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
814 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
815 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
816 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
817 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
818 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
819 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
820 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
821 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
822 {K_INS, IF_EB("\033I", ESC_STR "I")},
823 {K_HOME, IF_EB("\033E", ESC_STR "E")},
824 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
825 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
826# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {(int)KS_MS, "y"},
829# endif
830# endif
831
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200832# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200833 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
835 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
836# ifdef TERMINFO
837 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
838# else
839 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
840# endif
841 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
842# ifdef TERMINFO
843 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
844# else
845 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
846# endif
847# ifdef TERMINFO
848 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
849 ESC_STR "[%i%p1%d;%p2%dr")},
850# else
851 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
852# endif
853 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
854 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
855 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
856 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
857 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
858 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
859 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200860 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
861 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {(int)KS_MS, "y"},
863 {(int)KS_UT, "y"},
864 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200865 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
866 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200867 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200868 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200869# ifdef TERMINFO
870 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
871# else
872 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
873# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200874 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200875 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# ifdef TERMINFO
877 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
878 ESC_STR "[%i%p1%d;%p2%dH")},
879# else
880 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
881# endif
882 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
883# ifdef TERMINFO
884 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
885# else
886 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
887# endif
888 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
889 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
890# ifdef FEAT_XTERM_SAVE
891 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
892 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
893 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
894# endif
895 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
896 {(int)KS_CIE, "\007"},
897 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
898 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200899 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
900 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901# ifdef TERMINFO
902 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
903 ESC_STR "[8;%p1%d;%p2%dt")},
904 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
905 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200906 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907# else
908 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
909 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200910 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911# endif
912 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200913 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200914 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100915 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200916# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200917 /* These are printf strings, not terminal codes. */
918 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
919 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
920# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100921 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
922 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200923 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
924 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
925 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
926 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000927
928 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
929 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
930 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
931 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
932 /* An extra set of cursor keys for vt100 mode */
933 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
934 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
935 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
936 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000938 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
939 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
940 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
941 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000942 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
943 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
944 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
945 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
946 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
947 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
948 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
949 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
950 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
951 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
952 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
953 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000955 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
956 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
957 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
958 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000959 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
960 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000961 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000962 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000963 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000964 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000965 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
966 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000967 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000968 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000969 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000970 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
971 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100972 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
973 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
974 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
975 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
976 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
977 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200978 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
979 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
980 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
981 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
982 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
983 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
984 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
985 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
986 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
987 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +0100988 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
989 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
990 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
994 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
995 /* F14 and F15 are missing, because they send the same codes as the undo
996 * and help key, although they don't work on all keyboards. */
997 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
998 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
999 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1000 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1001 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1002
1003 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1004 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1005 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1006 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1007 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1008 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1009 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1010 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1011 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1012 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1013
1014 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1015 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1016 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1017 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1018 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1019 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1020 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021# endif
1022
1023# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1024/*
1025 * iris-ansi for Silicon Graphics machines.
1026 */
1027 {(int)KS_NAME, "iris-ansi"},
1028 {(int)KS_CE, "\033[K"},
1029 {(int)KS_CD, "\033[J"},
1030 {(int)KS_AL, "\033[L"},
1031# ifdef TERMINFO
1032 {(int)KS_CAL, "\033[%p1%dL"},
1033# else
1034 {(int)KS_CAL, "\033[%dL"},
1035# endif
1036 {(int)KS_DL, "\033[M"},
1037# ifdef TERMINFO
1038 {(int)KS_CDL, "\033[%p1%dM"},
1039# else
1040 {(int)KS_CDL, "\033[%dM"},
1041# endif
1042#if 0 /* The scroll region is not working as Vim expects. */
1043# ifdef TERMINFO
1044 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1045# else
1046 {(int)KS_CS, "\033[%i%d;%dr"},
1047# endif
1048#endif
1049 {(int)KS_CL, "\033[H\033[2J"},
1050 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1051 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1052 {(int)KS_TI, "\033[=6h"},
1053 {(int)KS_TE, "\033[=6l"},
1054 {(int)KS_SE, "\033[21;27m"},
1055 {(int)KS_SO, "\033[1;7m"},
1056 {(int)KS_ME, "\033[m"},
1057 {(int)KS_MR, "\033[7m"},
1058 {(int)KS_MD, "\033[1m"},
1059 {(int)KS_CCO, "8"}, /* allow 8 colors */
1060 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1061 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1062 {(int)KS_US, "\033[4m"}, /* underline on */
1063 {(int)KS_UE, "\033[24m"}, /* underline off */
1064# ifdef TERMINFO
1065 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1066 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1067 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1068 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1069# else
1070 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1071 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1072 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1073 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1074# endif
1075 {(int)KS_MS, "y"}, /* guessed */
1076 {(int)KS_UT, "y"}, /* guessed */
1077 {(int)KS_LE, "\b"},
1078# ifdef TERMINFO
1079 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1080# else
1081 {(int)KS_CM, "\033[%i%d;%dH"},
1082# endif
1083 {(int)KS_SR, "\033M"},
1084# ifdef TERMINFO
1085 {(int)KS_CRI, "\033[%p1%dC"},
1086# else
1087 {(int)KS_CRI, "\033[%dC"},
1088# endif
1089 {(int)KS_CIS, "\033P3.y"},
1090 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1091 {(int)KS_TS, "\033P1.y"},
1092 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1093# ifdef TERMINFO
1094 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1095 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1096# else
1097 {(int)KS_CWS, "\033[203;%d;%d/y"},
1098 {(int)KS_CWP, "\033[205;%d;%d/y"},
1099# endif
1100 {K_UP, "\033[A"},
1101 {K_DOWN, "\033[B"},
1102 {K_LEFT, "\033[D"},
1103 {K_RIGHT, "\033[C"},
1104 {K_S_UP, "\033[161q"},
1105 {K_S_DOWN, "\033[164q"},
1106 {K_S_LEFT, "\033[158q"},
1107 {K_S_RIGHT, "\033[167q"},
1108 {K_F1, "\033[001q"},
1109 {K_F2, "\033[002q"},
1110 {K_F3, "\033[003q"},
1111 {K_F4, "\033[004q"},
1112 {K_F5, "\033[005q"},
1113 {K_F6, "\033[006q"},
1114 {K_F7, "\033[007q"},
1115 {K_F8, "\033[008q"},
1116 {K_F9, "\033[009q"},
1117 {K_F10, "\033[010q"},
1118 {K_F11, "\033[011q"},
1119 {K_F12, "\033[012q"},
1120 {K_S_F1, "\033[013q"},
1121 {K_S_F2, "\033[014q"},
1122 {K_S_F3, "\033[015q"},
1123 {K_S_F4, "\033[016q"},
1124 {K_S_F5, "\033[017q"},
1125 {K_S_F6, "\033[018q"},
1126 {K_S_F7, "\033[019q"},
1127 {K_S_F8, "\033[020q"},
1128 {K_S_F9, "\033[021q"},
1129 {K_S_F10, "\033[022q"},
1130 {K_S_F11, "\033[023q"},
1131 {K_S_F12, "\033[024q"},
1132 {K_INS, "\033[139q"},
1133 {K_HOME, "\033[H"},
1134 {K_END, "\033[146q"},
1135 {K_PAGEUP, "\033[150q"},
1136 {K_PAGEDOWN, "\033[154q"},
1137# endif
1138
1139# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1140/*
1141 * for debugging
1142 */
1143 {(int)KS_NAME, "debug"},
1144 {(int)KS_CE, "[CE]"},
1145 {(int)KS_CD, "[CD]"},
1146 {(int)KS_AL, "[AL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CAL, "[CAL%p1%d]"},
1149# else
1150 {(int)KS_CAL, "[CAL%d]"},
1151# endif
1152 {(int)KS_DL, "[DL]"},
1153# ifdef TERMINFO
1154 {(int)KS_CDL, "[CDL%p1%d]"},
1155# else
1156 {(int)KS_CDL, "[CDL%d]"},
1157# endif
1158# ifdef TERMINFO
1159 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1160# else
1161 {(int)KS_CS, "[%dCS%d]"},
1162# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001165# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# endif
1168# ifdef TERMINFO
1169 {(int)KS_CAB, "[CAB%p1%d]"},
1170 {(int)KS_CAF, "[CAF%p1%d]"},
1171 {(int)KS_CSB, "[CSB%p1%d]"},
1172 {(int)KS_CSF, "[CSF%p1%d]"},
1173# else
1174 {(int)KS_CAB, "[CAB%d]"},
1175 {(int)KS_CAF, "[CAF%d]"},
1176 {(int)KS_CSB, "[CSB%d]"},
1177 {(int)KS_CSF, "[CSF%d]"},
1178# endif
1179 {(int)KS_OP, "[OP]"},
1180 {(int)KS_LE, "[LE]"},
1181 {(int)KS_CL, "[CL]"},
1182 {(int)KS_VI, "[VI]"},
1183 {(int)KS_VE, "[VE]"},
1184 {(int)KS_VS, "[VS]"},
1185 {(int)KS_ME, "[ME]"},
1186 {(int)KS_MR, "[MR]"},
1187 {(int)KS_MB, "[MB]"},
1188 {(int)KS_MD, "[MD]"},
1189 {(int)KS_SE, "[SE]"},
1190 {(int)KS_SO, "[SO]"},
1191 {(int)KS_UE, "[UE]"},
1192 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001193 {(int)KS_UCE, "[UCE]"},
1194 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001195 {(int)KS_STE, "[STE]"},
1196 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {(int)KS_MS, "[MS]"},
1198 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001199 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200# ifdef TERMINFO
1201 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1202# else
1203 {(int)KS_CM, "[%dCM%d]"},
1204# endif
1205 {(int)KS_SR, "[SR]"},
1206# ifdef TERMINFO
1207 {(int)KS_CRI, "[CRI%p1%d]"},
1208# else
1209 {(int)KS_CRI, "[CRI%d]"},
1210# endif
1211 {(int)KS_VB, "[VB]"},
1212 {(int)KS_KS, "[KS]"},
1213 {(int)KS_KE, "[KE]"},
1214 {(int)KS_TI, "[TI]"},
1215 {(int)KS_TE, "[TE]"},
1216 {(int)KS_CIS, "[CIS]"},
1217 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001218 {(int)KS_CSC, "[CSC]"},
1219 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {(int)KS_TS, "[TS]"},
1221 {(int)KS_FS, "[FS]"},
1222# ifdef TERMINFO
1223 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1224 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1225# else
1226 {(int)KS_CWS, "[%dCWS%d]"},
1227 {(int)KS_CWP, "[%dCWP%d]"},
1228# endif
1229 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001230 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001231 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001232 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {K_UP, "[KU]"},
1234 {K_DOWN, "[KD]"},
1235 {K_LEFT, "[KL]"},
1236 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001237 {K_XUP, "[xKU]"},
1238 {K_XDOWN, "[xKD]"},
1239 {K_XLEFT, "[xKL]"},
1240 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {K_S_UP, "[S-KU]"},
1242 {K_S_DOWN, "[S-KD]"},
1243 {K_S_LEFT, "[S-KL]"},
1244 {K_C_LEFT, "[C-KL]"},
1245 {K_S_RIGHT, "[S-KR]"},
1246 {K_C_RIGHT, "[C-KR]"},
1247 {K_F1, "[F1]"},
1248 {K_XF1, "[xF1]"},
1249 {K_F2, "[F2]"},
1250 {K_XF2, "[xF2]"},
1251 {K_F3, "[F3]"},
1252 {K_XF3, "[xF3]"},
1253 {K_F4, "[F4]"},
1254 {K_XF4, "[xF4]"},
1255 {K_F5, "[F5]"},
1256 {K_F6, "[F6]"},
1257 {K_F7, "[F7]"},
1258 {K_F8, "[F8]"},
1259 {K_F9, "[F9]"},
1260 {K_F10, "[F10]"},
1261 {K_F11, "[F11]"},
1262 {K_F12, "[F12]"},
1263 {K_S_F1, "[S-F1]"},
1264 {K_S_XF1, "[S-xF1]"},
1265 {K_S_F2, "[S-F2]"},
1266 {K_S_XF2, "[S-xF2]"},
1267 {K_S_F3, "[S-F3]"},
1268 {K_S_XF3, "[S-xF3]"},
1269 {K_S_F4, "[S-F4]"},
1270 {K_S_XF4, "[S-xF4]"},
1271 {K_S_F5, "[S-F5]"},
1272 {K_S_F6, "[S-F6]"},
1273 {K_S_F7, "[S-F7]"},
1274 {K_S_F8, "[S-F8]"},
1275 {K_S_F9, "[S-F9]"},
1276 {K_S_F10, "[S-F10]"},
1277 {K_S_F11, "[S-F11]"},
1278 {K_S_F12, "[S-F12]"},
1279 {K_HELP, "[HELP]"},
1280 {K_UNDO, "[UNDO]"},
1281 {K_BS, "[BS]"},
1282 {K_INS, "[INS]"},
1283 {K_KINS, "[KINS]"},
1284 {K_DEL, "[DEL]"},
1285 {K_KDEL, "[KDEL]"},
1286 {K_HOME, "[HOME]"},
1287 {K_S_HOME, "[C-HOME]"},
1288 {K_C_HOME, "[C-HOME]"},
1289 {K_KHOME, "[KHOME]"},
1290 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001291 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {K_END, "[END]"},
1293 {K_S_END, "[C-END]"},
1294 {K_C_END, "[C-END]"},
1295 {K_KEND, "[KEND]"},
1296 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001297 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {K_PAGEUP, "[PAGEUP]"},
1299 {K_PAGEDOWN, "[PAGEDOWN]"},
1300 {K_KPAGEUP, "[KPAGEUP]"},
1301 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1302 {K_MOUSE, "[MOUSE]"},
1303 {K_KPLUS, "[KPLUS]"},
1304 {K_KMINUS, "[KMINUS]"},
1305 {K_KDIVIDE, "[KDIVIDE]"},
1306 {K_KMULTIPLY, "[KMULTIPLY]"},
1307 {K_KENTER, "[KENTER]"},
1308 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001309 {K_PS, "[PASTE-START]"},
1310 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {K_K0, "[K0]"},
1312 {K_K1, "[K1]"},
1313 {K_K2, "[K2]"},
1314 {K_K3, "[K3]"},
1315 {K_K4, "[K4]"},
1316 {K_K5, "[K5]"},
1317 {K_K6, "[K6]"},
1318 {K_K7, "[K7]"},
1319 {K_K8, "[K8]"},
1320 {K_K9, "[K9]"},
1321# endif
1322
1323#endif /* NO_BUILTIN_TCAPS */
1324
1325/*
1326 * The most minimal terminal: only clear screen and cursor positioning
1327 * Always included.
1328 */
1329 {(int)KS_NAME, "dumb"},
1330 {(int)KS_CL, "\014"},
1331#ifdef TERMINFO
1332 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1333 ESC_STR "[%i%p1%d;%p2%dH")},
1334#else
1335 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1336#endif
1337
1338/*
1339 * end marker
1340 */
1341 {(int)KS_NAME, NULL}
1342
1343}; /* end of builtin_termcaps */
1344
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001345#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001346 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348{
Bram Moolenaarab302212016-04-26 20:59:29 +02001349 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001350}
1351
1352 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001353termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001354{
1355 guicolor_T t;
1356
1357 if (*name == NUL)
1358 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001359 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001360
1361 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001363 return t;
1364}
1365
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001366 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001367termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001369 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370}
1371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373/*
1374 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef AMIGA
1377# define DEFAULT_TERM (char_u *)"amiga"
1378#endif
1379
1380#ifdef MSWIN
1381# define DEFAULT_TERM (char_u *)"win32"
1382#endif
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#if defined(UNIX) && !defined(__MINT__)
1385# define DEFAULT_TERM (char_u *)"ansi"
1386#endif
1387
1388#ifdef __MINT__
1389# define DEFAULT_TERM (char_u *)"vt52"
1390#endif
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef VMS
1393# define DEFAULT_TERM (char_u *)"vt320"
1394#endif
1395
1396#ifdef __BEOS__
1397# undef DEFAULT_TERM
1398# define DEFAULT_TERM (char_u *)"beos-ansi"
1399#endif
1400
1401#ifndef DEFAULT_TERM
1402# define DEFAULT_TERM (char_u *)"dumb"
1403#endif
1404
1405/*
1406 * Term_strings contains currently used terminal output strings.
1407 * It is initialized with the default values by parse_builtin_tcap().
1408 * The values can be changed by setting the option with the same name.
1409 */
1410char_u *(term_strings[(int)KS_LAST + 1]);
1411
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001412static int need_gather = FALSE; /* need to fill termleader[] */
1413static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001415static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001416static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#endif
1418
1419 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001420find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 struct builtin_term *p;
1423
1424 p = builtin_termcaps;
1425 while (p->bt_string != NULL)
1426 {
1427 if (p->bt_entry == (int)KS_NAME)
1428 {
1429#ifdef UNIX
1430 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1431 return p;
1432 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1433 return p;
1434 else
1435#endif
1436#ifdef VMS
1437 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1438 return p;
1439 else
1440#endif
1441 if (STRCMP(term, p->bt_string) == 0)
1442 return p;
1443 }
1444 ++p;
1445 }
1446 return p;
1447}
1448
1449/*
1450 * Parsing of the builtin termcap entries.
1451 * Caller should check if 'name' is a valid builtin term.
1452 * The terminal's name is not set, as this is already done in termcapinit().
1453 */
1454 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001455parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 struct builtin_term *p;
1458 char_u name[2];
1459 int term_8bit;
1460
1461 p = find_builtin_term(term);
1462 term_8bit = term_is_8bit(term);
1463
1464 /* Do not parse if builtin term not found */
1465 if (p->bt_string == NULL)
1466 return;
1467
1468 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1469 {
1470 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1471 {
1472 /* Only set the value if it wasn't set yet. */
1473 if (term_strings[p->bt_entry] == NULL
1474 || term_strings[p->bt_entry] == empty_option)
1475 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001476#ifdef FEAT_EVAL
1477 int opt_idx = -1;
1478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 /* 8bit terminal: use CSI instead of <Esc>[ */
1480 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1481 {
1482 char_u *s, *t;
1483
1484 s = vim_strsave((char_u *)p->bt_string);
1485 if (s != NULL)
1486 {
1487 for (t = s; *t; ++t)
1488 if (term_7to8bit(t))
1489 {
1490 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001491 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001494#ifdef FEAT_EVAL
1495 opt_idx =
1496#endif
1497 set_term_option_alloced(
1498 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 }
1501 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001504#ifdef FEAT_EVAL
1505 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1506#endif
1507 }
1508#ifdef FEAT_EVAL
1509 set_term_option_sctx_idx(NULL, opt_idx);
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 }
1513 else
1514 {
1515 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1516 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1517 if (find_termcode(name) == NULL)
1518 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1519 }
1520 }
1521}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523/*
1524 * Set number of colors.
1525 * Store it as a number in t_colors.
1526 * Store it as a string in T_CCO (using nr_colors[]).
1527 */
1528 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001529set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 char_u nr_colors[20]; /* string for number of colors */
1532
1533 t_colors = nr;
1534 if (t_colors > 1)
1535 sprintf((char *)nr_colors, "%d", t_colors);
1536 else
1537 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001538 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001540
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001541#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001542/*
1543 * Set the color count to "val" and redraw if it changed.
1544 */
1545 static void
1546may_adjust_color_count(int val)
1547{
1548 if (val != t_colors)
1549 {
1550 /* Nr of colors changed, initialize highlighting and
1551 * redraw everything. This causes a redraw, which usually
1552 * clears the message. Try keeping the message if it
1553 * might work. */
1554 set_keep_msg_from_hist();
1555 set_color_count(val);
1556 init_highlight(TRUE, FALSE);
1557# ifdef DEBUG_TERMRESPONSE
1558 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001559 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001560
Bram Moolenaarb255b902018-04-24 21:40:10 +02001561 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001562 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001563#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001564 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001565#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001566 }
1567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
1569
1570#ifdef HAVE_TGETENT
1571static char *(key_names[]) =
1572{
1573#ifdef FEAT_TERMRESPONSE
1574 /* Do this one first, it may cause a screen redraw. */
1575 "Co",
1576#endif
1577 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 "#2", "#4", "%i", "*7",
1579 "k1", "k2", "k3", "k4", "k5", "k6",
1580 "k7", "k8", "k9", "k;", "F1", "F2",
1581 "%1", "&8", "kb", "kI", "kD", "kh",
1582 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1583 NULL
1584};
1585#endif
1586
Bram Moolenaar9289df52018-05-10 14:40:57 +02001587#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001588 static void
1589get_term_entries(int *height, int *width)
1590{
1591 static struct {
1592 enum SpecialKey dest; /* index in term_strings[] */
1593 char *name; /* termcap name for string */
1594 } string_names[] =
1595 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1596 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1597 {KS_CL, "cl"}, {KS_CD, "cd"},
1598 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1599 {KS_ME, "me"}, {KS_MR, "mr"},
1600 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1601 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1602 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1603 {KS_STE,"Te"}, {KS_STS,"Ts"},
1604 {KS_CM, "cm"}, {KS_SR, "sr"},
1605 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1606 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1607 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1608 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1609 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1610 {KS_VS, "vs"}, {KS_CVS, "VS"},
1611 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1612 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1613 {KS_TS, "ts"}, {KS_FS, "fs"},
1614 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1615 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1616 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1617 {KS_8F, "8f"}, {KS_8B, "8b"},
1618 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1619 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001620 {KS_CST, "ST"}, {KS_CRT, "RT"},
1621 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001622 {(enum SpecialKey)0, NULL}
1623 };
1624 int i;
1625 char_u *p;
1626 static char_u tstrbuf[TBUFSZ];
1627 char_u *tp = tstrbuf;
1628
1629 /*
1630 * get output strings
1631 */
1632 for (i = 0; string_names[i].name != NULL; ++i)
1633 {
1634 if (TERM_STR(string_names[i].dest) == NULL
1635 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001636 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001637 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001638#ifdef FEAT_EVAL
1639 set_term_option_sctx_idx(string_names[i].name, -1);
1640#endif
1641 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001642 }
1643
1644 /* tgetflag() returns 1 if the flag is present, 0 if not and
1645 * possibly -1 if the flag doesn't exist. */
1646 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1647 T_MS = (char_u *)"y";
1648 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1649 T_XS = (char_u *)"y";
1650 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1651 T_XN = (char_u *)"y";
1652 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1653 T_DB = (char_u *)"y";
1654 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1655 T_DA = (char_u *)"y";
1656 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1657 T_UT = (char_u *)"y";
1658
1659 /*
1660 * get key codes
1661 */
1662 for (i = 0; key_names[i] != NULL; ++i)
1663 if (find_termcode((char_u *)key_names[i]) == NULL)
1664 {
1665 p = TGETSTR(key_names[i], &tp);
1666 /* if cursor-left == backspace, ignore it (televideo 925) */
1667 if (p != NULL
1668 && (*p != Ctrl_H
1669 || key_names[i][0] != 'k'
1670 || key_names[i][1] != 'l'))
1671 add_termcode((char_u *)key_names[i], p, FALSE);
1672 }
1673
1674 if (*height == 0)
1675 *height = tgetnum("li");
1676 if (*width == 0)
1677 *width = tgetnum("co");
1678
1679 /*
1680 * Get number of colors (if not done already).
1681 */
1682 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001683 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001684 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001685#ifdef FEAT_EVAL
1686 set_term_option_sctx_idx("Co", -1);
1687#endif
1688 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689
1690# ifndef hpux
1691 BC = (char *)TGETSTR("bc", &tp);
1692 UP = (char *)TGETSTR("up", &tp);
1693 p = TGETSTR("pc", &tp);
1694 if (p)
1695 PC = *p;
1696# endif
1697}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001698#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699
1700 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001701report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001702{
1703 struct builtin_term *termp;
1704
1705 mch_errmsg("\r\n");
1706 if (error_msg != NULL)
1707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001708 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001709 mch_errmsg("\r\n");
1710 }
1711 mch_errmsg("'");
1712 mch_errmsg((char *)term);
1713 mch_errmsg(_("' not known. Available builtin terminals are:"));
1714 mch_errmsg("\r\n");
1715 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1716 {
1717 if (termp->bt_entry == (int)KS_NAME)
1718 {
1719#ifdef HAVE_TGETENT
1720 mch_errmsg(" builtin_");
1721#else
1722 mch_errmsg(" ");
1723#endif
1724 mch_errmsg(termp->bt_string);
1725 mch_errmsg("\r\n");
1726 }
1727 }
1728}
1729
1730 static void
1731report_default_term(char_u *term)
1732{
1733 mch_errmsg(_("defaulting to '"));
1734 mch_errmsg((char *)term);
1735 mch_errmsg("'\r\n");
1736 if (emsg_silent == 0)
1737 {
1738 screen_start(); /* don't know where cursor is now */
1739 out_flush();
1740 if (!is_not_a_term())
1741 ui_delay(2000L, TRUE);
1742 }
1743}
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Set terminal options for terminal "term".
1747 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1748 *
1749 * While doing this, until ttest(), some options may be NULL, be careful.
1750 */
1751 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001752set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 struct builtin_term *termp;
1755#ifdef HAVE_TGETENT
1756 int builtin_first = p_tbi;
1757 int try;
1758 int termcap_cleared = FALSE;
1759#endif
1760 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 char_u *bs_p, *del_p;
1763
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001764 /* In silect mode (ex -s) we don't use the 'term' option. */
1765 if (silent_mode)
1766 return OK;
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 detected_8bit = FALSE; /* reset 8-bit detection */
1769
1770 if (term_is_builtin(term))
1771 {
1772 term += 8;
1773#ifdef HAVE_TGETENT
1774 builtin_first = 1;
1775#endif
1776 }
1777
1778/*
1779 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1780 * If builtin_first is TRUE:
1781 * 0. try builtin termcap
1782 * 1. try external termcap
1783 * 2. if both fail default to a builtin terminal
1784 * If builtin_first is FALSE:
1785 * 1. try external termcap
1786 * 2. try builtin termcap, if both fail default to a builtin terminal
1787 */
1788#ifdef HAVE_TGETENT
1789 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1790 {
1791 /*
1792 * Use external termcap
1793 */
1794 if (try == 1)
1795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 /*
1799 * If the external termcap does not have a matching entry, try the
1800 * builtin ones.
1801 */
1802 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (!termcap_cleared)
1805 {
1806 clear_termoptions(); /* clear old options */
1807 termcap_cleared = TRUE;
1808 }
1809
Bram Moolenaar69e05692018-05-10 14:11:52 +02001810 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 }
1813 else /* try == 0 || try == 2 */
1814#endif /* HAVE_TGETENT */
1815 /*
1816 * Use builtin termcap
1817 */
1818 {
1819#ifdef HAVE_TGETENT
1820 /*
1821 * If builtin termcap was already used, there is no need to search
1822 * for the builtin termcap again, quit now.
1823 */
1824 if (try == 2 && builtin_first && termcap_cleared)
1825 break;
1826#endif
1827 /*
1828 * search for 'term' in builtin_termcaps[]
1829 */
1830 termp = find_builtin_term(term);
1831 if (termp->bt_string == NULL) /* did not find it */
1832 {
1833#ifdef HAVE_TGETENT
1834 /*
1835 * If try == 0, first try the external termcap. If that is not
1836 * found we'll get back here with try == 2.
1837 * If termcap_cleared is set we used the external termcap,
1838 * don't complain about not finding the term in the builtin
1839 * termcap.
1840 */
1841 if (try == 0) /* try external one */
1842 continue;
1843 if (termcap_cleared) /* found in external termcap */
1844 break;
1845#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001846 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 /* when user typed :set term=xxx, quit here */
1849 if (starting != NO_SCREEN)
1850 {
1851 screen_start(); /* don't know where cursor is now */
1852 wait_return(TRUE);
1853 return FAIL;
1854 }
1855 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001856 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001857 set_string_option_direct((char_u *)"term", -1, term,
1858 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 display_errors();
1860 }
1861 out_flush();
1862#ifdef HAVE_TGETENT
1863 if (!termcap_cleared)
1864 {
1865#endif
1866 clear_termoptions(); /* clear old options */
1867#ifdef HAVE_TGETENT
1868 termcap_cleared = TRUE;
1869 }
1870#endif
1871 parse_builtin_tcap(term);
1872#ifdef FEAT_GUI
1873 if (term_is_gui(term))
1874 {
1875 out_flush();
1876 gui_init();
1877 /* If starting the GUI failed, don't do any of the other
1878 * things for this terminal */
1879 if (!gui.in_use)
1880 return FAIL;
1881#ifdef HAVE_TGETENT
1882 break; /* don't try using external termcap */
1883#endif
1884 }
1885#endif /* FEAT_GUI */
1886 }
1887#ifdef HAVE_TGETENT
1888 }
1889#endif
1890
1891/*
1892 * special: There is no info in the termcap about whether the cursor
1893 * positioning is relative to the start of the screen or to the start of the
1894 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1895 * relative.
1896 */
1897 if (STRCMP(term, "pcterm") == 0)
1898 T_CCS = (char_u *)"yes";
1899 else
1900 T_CCS = empty_option;
1901
1902#ifdef UNIX
1903/*
1904 * Any "stty" settings override the default for t_kb from the termcap.
1905 * This is in os_unix.c, because it depends a lot on the version of unix that
1906 * is being used.
1907 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1908 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001909# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 get_stty();
1913#endif
1914
1915/*
1916 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1917 * didn't work, use the default CTRL-H
1918 * The default for t_kD is DEL, unless t_kb is DEL.
1919 * The vim_strsave'd strings are probably lost forever, well it's only two
1920 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1921 * directly.
1922 */
1923#ifdef FEAT_GUI
1924 if (!gui.in_use)
1925#endif
1926 {
1927 bs_p = find_termcode((char_u *)"kb");
1928 del_p = find_termcode((char_u *)"kD");
1929 if (bs_p == NULL || *bs_p == NUL)
1930 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1931 if ((del_p == NULL || *del_p == NUL) &&
1932 (bs_p == NULL || *bs_p != DEL))
1933 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1934 }
1935
1936#if defined(UNIX) || defined(VMS)
1937 term_is_xterm = vim_is_xterm(term);
1938#endif
1939
1940#ifdef FEAT_MOUSE
1941# if defined(UNIX) || defined(VMS)
1942# ifdef FEAT_MOUSE_TTY
1943 /*
1944 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1945 * The termcode for the mouse is added as a side effect in option.c.
1946 */
1947 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001948 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001951 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 if (use_xterm_mouse())
1954 p = NULL; /* keep existing value, might be "xterm2" */
1955 else
1956 p = (char_u *)"xterm";
1957 }
1958# endif
1959 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001962 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1963 * "xterm2" in check_termcode(). */
1964 reset_option_was_set((char_u *)"ttym");
1965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 if (p == NULL
1967# ifdef FEAT_GUI
1968 || gui.in_use
1969# endif
1970 )
1971 check_mouse_termcode(); /* set mouse termcode anyway */
1972 }
1973# endif
1974# else
1975 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1976# endif
1977#endif /* FEAT_MOUSE */
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef USE_TERM_CONSOLE
1980 /* DEFAULT_TERM indicates that it is the machine console. */
1981 if (STRCMP(term, DEFAULT_TERM) != 0)
1982 term_console = FALSE;
1983 else
1984 {
1985 term_console = TRUE;
1986# ifdef AMIGA
1987 win_resize_on(); /* enable window resizing reports */
1988# endif
1989 }
1990#endif
1991
1992#if defined(UNIX) || defined(VMS)
1993 /*
1994 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1995 */
1996 if (vim_is_fastterm(term))
1997 p_tf = TRUE;
1998#endif
1999#ifdef USE_TERM_CONSOLE
2000 /*
2001 * 'ttyfast' is default on consoles
2002 */
2003 if (term_console)
2004 p_tf = TRUE;
2005#endif
2006
2007 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2008
2009 full_screen = TRUE; /* we can use termcap codes from now on */
2010 set_term_defaults(); /* use current values as defaults */
2011#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002012 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002013 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#endif
2015
2016 /*
2017 * Initialize the terminal with the appropriate termcap codes.
2018 * Set the mouse and window title if possible.
2019 * Don't do this when starting, need to parse the .vimrc first, because it
2020 * may redefine t_TI etc.
2021 */
2022 if (starting != NO_SCREEN)
2023 {
2024 starttermcap(); /* may change terminal mode */
2025#ifdef FEAT_MOUSE
2026 setmouse(); /* may start using the mouse */
2027#endif
2028#ifdef FEAT_TITLE
2029 maketitle(); /* may display window title */
2030#endif
2031 }
2032
2033 /* display initial screen after ttest() checking. jw. */
2034 if (width <= 0 || height <= 0)
2035 {
2036 /* termcap failed to report size */
2037 /* set defaults, in case ui_get_shellsize() also fails */
2038 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002039#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 height = 25; /* console is often 25 lines */
2041#else
2042 height = 24; /* most terminals are 24 lines */
2043#endif
2044 }
2045 set_shellsize(width, height, FALSE); /* may change Rows */
2046 if (starting != NO_SCREEN)
2047 {
2048 if (scroll_region)
2049 scroll_region_reset(); /* In case Rows changed */
2050 check_map_keycodes(); /* check mappings for terminal codes used */
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002053 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 /*
2056 * Execute the TermChanged autocommands for each buffer that is
2057 * loaded.
2058 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002059 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002060 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 if (curbuf->b_ml.ml_mfp != NULL)
2063 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2064 curbuf);
2065 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002066 if (bufref_valid(&old_curbuf))
2067 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070
2071#ifdef FEAT_TERMRESPONSE
2072 may_req_termresponse();
2073#endif
2074
2075 return OK;
2076}
2077
2078#if defined(FEAT_MOUSE) || defined(PROTO)
2079
2080# ifdef FEAT_MOUSE_TTY
2081# define HMT_NORMAL 1
2082# define HMT_NETTERM 2
2083# define HMT_DEC 4
2084# define HMT_JSBTERM 8
2085# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002086# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002087# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002088# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089static int has_mouse_termcode = 0;
2090# endif
2091
Bram Moolenaar864207d2008-06-24 22:14:38 +00002092# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002094set_mouse_termcode(
2095 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2096 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 char_u name[2];
2099
2100 name[0] = n;
2101 name[1] = KE_FILLER;
2102 add_termcode(name, s, FALSE);
2103# ifdef FEAT_MOUSE_TTY
2104# ifdef FEAT_MOUSE_JSB
2105 if (n == KS_JSBTERM_MOUSE)
2106 has_mouse_termcode |= HMT_JSBTERM;
2107 else
2108# endif
2109# ifdef FEAT_MOUSE_NET
2110 if (n == KS_NETTERM_MOUSE)
2111 has_mouse_termcode |= HMT_NETTERM;
2112 else
2113# endif
2114# ifdef FEAT_MOUSE_DEC
2115 if (n == KS_DEC_MOUSE)
2116 has_mouse_termcode |= HMT_DEC;
2117 else
2118# endif
2119# ifdef FEAT_MOUSE_PTERM
2120 if (n == KS_PTERM_MOUSE)
2121 has_mouse_termcode |= HMT_PTERM;
2122 else
2123# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002124# ifdef FEAT_MOUSE_URXVT
2125 if (n == KS_URXVT_MOUSE)
2126 has_mouse_termcode |= HMT_URXVT;
2127 else
2128# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002129# ifdef FEAT_MOUSE_SGR
2130 if (n == KS_SGR_MOUSE)
2131 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002132 else if (n == KS_SGR_MOUSE_RELEASE)
2133 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002134 else
2135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 has_mouse_termcode |= HMT_NORMAL;
2137# endif
2138}
2139# endif
2140
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002141# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002142 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002144del_mouse_termcode(
2145 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147 char_u name[2];
2148
2149 name[0] = n;
2150 name[1] = KE_FILLER;
2151 del_termcode(name);
2152# ifdef FEAT_MOUSE_TTY
2153# ifdef FEAT_MOUSE_JSB
2154 if (n == KS_JSBTERM_MOUSE)
2155 has_mouse_termcode &= ~HMT_JSBTERM;
2156 else
2157# endif
2158# ifdef FEAT_MOUSE_NET
2159 if (n == KS_NETTERM_MOUSE)
2160 has_mouse_termcode &= ~HMT_NETTERM;
2161 else
2162# endif
2163# ifdef FEAT_MOUSE_DEC
2164 if (n == KS_DEC_MOUSE)
2165 has_mouse_termcode &= ~HMT_DEC;
2166 else
2167# endif
2168# ifdef FEAT_MOUSE_PTERM
2169 if (n == KS_PTERM_MOUSE)
2170 has_mouse_termcode &= ~HMT_PTERM;
2171 else
2172# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002173# ifdef FEAT_MOUSE_URXVT
2174 if (n == KS_URXVT_MOUSE)
2175 has_mouse_termcode &= ~HMT_URXVT;
2176 else
2177# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002178# ifdef FEAT_MOUSE_SGR
2179 if (n == KS_SGR_MOUSE)
2180 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002181 else if (n == KS_SGR_MOUSE_RELEASE)
2182 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002183 else
2184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 has_mouse_termcode &= ~HMT_NORMAL;
2186# endif
2187}
2188# endif
2189#endif
2190
2191#ifdef HAVE_TGETENT
2192/*
2193 * Call tgetent()
2194 * Return error message if it fails, NULL if it's OK.
2195 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002197tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 int i;
2200
2201 i = TGETENT(tbuf, term);
2202 if (i < 0 /* -1 is always an error */
2203# ifdef TGETENT_ZERO_ERR
2204 || i == 0 /* sometimes zero is also an error */
2205# endif
2206 )
2207 {
2208 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2209 * tgetent() with the always existing "dumb" entry to avoid a crash or
2210 * hang. */
2211 (void)TGETENT(tbuf, "dumb");
2212
2213 if (i < 0)
2214# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002215 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (i == 0)
2217# endif
2218#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002219 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002221 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222#endif
2223 }
2224 return NULL;
2225}
2226
2227/*
2228 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2229 * Fix that here.
2230 */
2231 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002232vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 char *p;
2235
2236 p = tgetstr(s, (char **)pp);
2237 if (p == (char *)-1)
2238 p = NULL;
2239 return (char_u *)p;
2240}
2241#endif /* HAVE_TGETENT */
2242
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002243#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244/*
2245 * Get Columns and Rows from the termcap. Used after a window signal if the
2246 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2247 * and "li" entries never change. But on some systems this works.
2248 * Errors while getting the entries are ignored.
2249 */
2250 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002251getlinecol(
2252 long *cp, /* pointer to columns */
2253 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 char_u tbuf[TBUFSZ];
2256
2257 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2258 {
2259 if (*cp == 0)
2260 *cp = tgetnum("co");
2261 if (*rp == 0)
2262 *rp = tgetnum("li");
2263 }
2264}
2265#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2266
2267/*
2268 * Get a string entry from the termcap and add it to the list of termcodes.
2269 * Used for <t_xx> special keys.
2270 * Give an error message for failure when not sourcing.
2271 * If force given, replace an existing entry.
2272 * Return FAIL if the entry was not found, OK if the entry was added.
2273 */
2274 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002275add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 char_u *term;
2278 int key;
2279 struct builtin_term *termp;
2280#ifdef HAVE_TGETENT
2281 char_u *string;
2282 int i;
2283 int builtin_first;
2284 char_u tbuf[TBUFSZ];
2285 char_u tstrbuf[TBUFSZ];
2286 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288#endif
2289
2290/*
2291 * If the GUI is running or will start in a moment, we only support the keys
2292 * that the GUI can produce.
2293 */
2294#ifdef FEAT_GUI
2295 if (gui.in_use || gui.starting)
2296 return gui_mch_haskey(name);
2297#endif
2298
2299 if (!force && find_termcode(name) != NULL) /* it's already there */
2300 return OK;
2301
2302 term = T_NAME;
2303 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2304 return FAIL;
2305
2306 if (term_is_builtin(term)) /* name starts with "builtin_" */
2307 {
2308 term += 8;
2309#ifdef HAVE_TGETENT
2310 builtin_first = TRUE;
2311#endif
2312 }
2313#ifdef HAVE_TGETENT
2314 else
2315 builtin_first = p_tbi;
2316#endif
2317
2318#ifdef HAVE_TGETENT
2319/*
2320 * We can get the entry from the builtin termcap and from the external one.
2321 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2322 * builtin termcap first.
2323 * If 'ttybuiltin' is off, try external termcap first.
2324 */
2325 for (i = 0; i < 2; ++i)
2326 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002327 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329 /*
2330 * Search in builtin termcap
2331 */
2332 {
2333 termp = find_builtin_term(term);
2334 if (termp->bt_string != NULL) /* found it */
2335 {
2336 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002337 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 while (termp->bt_entry != (int)KS_NAME)
2339 {
2340 if ((int)termp->bt_entry == key)
2341 {
2342 add_termcode(name, (char_u *)termp->bt_string,
2343 term_is_8bit(term));
2344 return OK;
2345 }
2346 ++termp;
2347 }
2348 }
2349 }
2350#ifdef HAVE_TGETENT
2351 else
2352 /*
2353 * Search in external termcap
2354 */
2355 {
2356 error_msg = tgetent_error(tbuf, term);
2357 if (error_msg == NULL)
2358 {
2359 string = TGETSTR((char *)name, &tp);
2360 if (string != NULL && *string != NUL)
2361 {
2362 add_termcode(name, string, FALSE);
2363 return OK;
2364 }
2365 }
2366 }
2367 }
2368#endif
2369
2370 if (sourcing_name == NULL)
2371 {
2372#ifdef HAVE_TGETENT
2373 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002374 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 else
2376#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002377 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 return FAIL;
2380}
2381
2382 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002383term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2386}
2387
2388/*
2389 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2390 * Assume that the terminal is using 8-bit controls when the name contains
2391 * "8bit", like in "xterm-8bit".
2392 */
2393 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002394term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2397}
2398
2399/*
2400 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002401 * <Esc>[ -> CSI <M_C_[>
2402 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 * <Esc>O -> <M-C-O>
2404 */
2405 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002406term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 if (*p == ESC)
2409 {
2410 if (p[1] == '[')
2411 return CSI;
2412 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002413 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (p[1] == 'O')
2415 return 0x8f;
2416 }
2417 return 0;
2418}
2419
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002420#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002422term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2425}
2426#endif
2427
2428#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2429
2430 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002431tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 static char_u buf[16];
2434 char_u *p;
2435
2436 p = buf + 15;
2437 *p = '\0';
2438 do
2439 {
2440 --p;
2441 *p = (char_u) (i % 10 + '0');
2442 i /= 10;
2443 }
2444 while (i > 0 && p > buf);
2445 return p;
2446}
2447#endif
2448
2449#ifndef HAVE_TGETENT
2450
2451/*
2452 * minimal tgoto() implementation.
2453 * no padding and we only parse for %i %d and %+char
2454 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002455 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002456tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 static char buf[30];
2459 char *p, *s, *e;
2460
2461 if (!cm)
2462 return "OOPS";
2463 e = buf + 29;
2464 for (s = buf; s < e && *cm; cm++)
2465 {
2466 if (*cm != '%')
2467 {
2468 *s++ = *cm;
2469 continue;
2470 }
2471 switch (*++cm)
2472 {
2473 case 'd':
2474 p = (char *)tltoa((unsigned long)y);
2475 y = x;
2476 while (*p)
2477 *s++ = *p++;
2478 break;
2479 case 'i':
2480 x++;
2481 y++;
2482 break;
2483 case '+':
2484 *s++ = (char)(*++cm + y);
2485 y = x;
2486 break;
2487 case '%':
2488 *s++ = *cm;
2489 break;
2490 default:
2491 return "OOPS";
2492 }
2493 }
2494 *s = '\0';
2495 return buf;
2496}
2497
2498#endif /* HAVE_TGETENT */
2499
2500/*
2501 * Set the terminal name and initialize the terminal options.
2502 * If "name" is NULL or empty, get the terminal name from the environment.
2503 * If that fails, use the default terminal name.
2504 */
2505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 char_u *term;
2509
2510 if (name != NULL && *name == NUL)
2511 name = NULL; /* empty name is equal to no name */
2512 term = name;
2513
2514#ifdef __BEOS__
2515 /*
2516 * TERM environment variable is normally set to 'ansi' on the Bebox;
2517 * Since the BeBox doesn't quite support full ANSI yet, we use our
2518 * own custom 'ansi-beos' termcap instead, unless the -T option has
2519 * been given on the command line.
2520 */
2521 if (term == NULL
2522 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2523 term = DEFAULT_TERM;
2524#endif
2525#ifndef MSWIN
2526 if (term == NULL)
2527 term = mch_getenv((char_u *)"TERM");
2528#endif
2529 if (term == NULL || *term == NUL)
2530 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002531 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 /* Set the default terminal name. */
2534 set_string_default("term", term);
2535 set_string_default("ttytype", term);
2536
2537 /*
2538 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2539 */
2540 set_termname(T_NAME != NULL ? T_NAME : term);
2541}
2542
2543/*
2544 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2545 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002546#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2548static char_u out_buf[OUT_SIZE + 1];
2549static int out_pos = 0; /* number of chars in out_buf */
2550
2551/*
2552 * out_flush(): flush the output buffer
2553 */
2554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002555out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557 int len;
2558
2559 if (out_pos != 0)
2560 {
2561 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2562 len = out_pos;
2563 out_pos = 0;
2564 ui_write(out_buf, len);
2565 }
2566}
2567
Bram Moolenaara338adc2018-01-31 20:51:47 +01002568/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002569 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2570 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002571 */
2572 void
2573out_flush_cursor(
2574 int force UNUSED, /* when TRUE, update cursor even when not moved */
2575 int clear_selection UNUSED) /* clear selection under cursor */
2576{
2577 mch_disable_flush();
2578 out_flush();
2579 mch_enable_flush();
2580#ifdef FEAT_GUI
2581 if (gui.in_use)
2582 {
2583 gui_update_cursor(force, clear_selection);
2584 gui_may_flush();
2585 }
2586#endif
2587}
2588
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590/*
2591 * Sometimes a byte out of a multi-byte character is written with out_char().
2592 * To avoid flushing half of the character, call this function first.
2593 */
2594 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002595out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2598 out_flush();
2599}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601#ifdef FEAT_GUI
2602/*
2603 * out_trash(): Throw away the contents of the output buffer
2604 */
2605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002606out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 out_pos = 0;
2609}
2610#endif
2611
2612/*
2613 * out_char(c): put a byte into the output buffer.
2614 * Flush it if it becomes full.
2615 * This should not be used for outputting text on the screen (use functions
2616 * like msg_puts() and screen_putchar() for that).
2617 */
2618 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002619out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaard0573012017-10-28 21:11:06 +02002621#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2623 out_char('\r');
2624#endif
2625
2626 out_buf[out_pos++] = c;
2627
2628 /* For testing we flush each time. */
2629 if (out_pos >= OUT_SIZE || p_wd)
2630 out_flush();
2631}
2632
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002633static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635/*
2636 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2637 */
2638 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002639out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaard0573012017-10-28 21:11:06 +02002641#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2643 out_char_nf('\r');
2644#endif
2645
2646 out_buf[out_pos++] = c;
2647
2648 if (out_pos >= OUT_SIZE)
2649 out_flush();
2650}
2651
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002652#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2653 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
2655 * A never-padding out_str.
2656 * use this whenever you don't want to run the string through tputs.
2657 * tputs above is harmless, but tputs from the termcap library
2658 * is likely to strip off leading digits, that it mistakes for padding
2659 * information, and "%i", "%d", etc.
2660 * This should only be used for writing terminal codes, not for outputting
2661 * normal text (use functions like msg_puts() and screen_putchar() for that).
2662 */
2663 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002664out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2667 out_flush();
2668 while (*s)
2669 out_char_nf(*s++);
2670
2671 /* For testing we write one string at a time. */
2672 if (p_wd)
2673 out_flush();
2674}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002678 * A conditional-flushing out_str, mainly for visualbell.
2679 * Handles a delay internally, because termlib may not respect the delay or do
2680 * it at the wrong time.
2681 * Note: Only for terminal strings.
2682 */
2683 void
2684out_str_cf(char_u *s)
2685{
2686 if (s != NULL && *s)
2687 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002688#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002689 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002690#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002691
2692#ifdef FEAT_GUI
2693 /* Don't use tputs() when GUI is used, ncurses crashes. */
2694 if (gui.in_use)
2695 {
2696 out_str_nf(s);
2697 return;
2698 }
2699#endif
2700 if (out_pos > OUT_SIZE - 20)
2701 out_flush();
2702#ifdef HAVE_TGETENT
2703 for (p = s; *s; ++s)
2704 {
2705 /* flush just before delay command */
2706 if (*s == '$' && *(s + 1) == '<')
2707 {
2708 char_u save_c = *s;
2709 int duration = atoi((char *)s + 2);
2710
2711 *s = NUL;
2712 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2713 *s = save_c;
2714 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002715# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002716 /* Only sleep here if we can limit this happening in
2717 * vim_beep(). */
2718 p = vim_strchr(s, '>');
2719 if (p == NULL || duration <= 0)
2720 {
2721 /* can't parse the time, don't sleep here */
2722 p = s;
2723 }
2724 else
2725 {
2726 ++p;
2727 do_sleep(duration);
2728 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002730 /* Rely on the terminal library to sleep. */
2731 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002732# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002733 break;
2734 }
2735 }
2736 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2737#else
2738 while (*s)
2739 out_char_nf(*s++);
2740#endif
2741
2742 /* For testing we write one string at a time. */
2743 if (p_wd)
2744 out_flush();
2745 }
2746}
2747
2748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * out_str(s): Put a character string a byte at a time into the output buffer.
2750 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2751 * This should only be used for writing terminal codes, not for outputting
2752 * normal text (use functions like msg_puts() and screen_putchar() for that).
2753 */
2754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002755out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 if (s != NULL && *s)
2758 {
2759#ifdef FEAT_GUI
2760 /* Don't use tputs() when GUI is used, ncurses crashes. */
2761 if (gui.in_use)
2762 {
2763 out_str_nf(s);
2764 return;
2765 }
2766#endif
2767 /* avoid terminal strings being split up */
2768 if (out_pos > OUT_SIZE - 20)
2769 out_flush();
2770#ifdef HAVE_TGETENT
2771 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2772#else
2773 while (*s)
2774 out_char_nf(*s++);
2775#endif
2776
2777 /* For testing we write one string at a time. */
2778 if (p_wd)
2779 out_flush();
2780 }
2781}
2782
2783/*
2784 * cursor positioning using termcap parser. (jw)
2785 */
2786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002787term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 OUT_STR(tgoto((char *)T_CM, col, row));
2790}
2791
2792 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002793term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 OUT_STR(tgoto((char *)T_CRI, 0, i));
2796}
2797
2798 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002799term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2802}
2803
2804 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002805term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2808}
2809
2810#if defined(HAVE_TGETENT) || defined(PROTO)
2811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002812term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 /* Can't handle a negative value here */
2815 if (x < 0)
2816 x = 0;
2817 if (y < 0)
2818 y = 0;
2819 OUT_STR(tgoto((char *)T_CWP, y, x));
2820}
2821
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002822# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2823/*
2824 * Return TRUE if we can request the terminal for a response.
2825 */
2826 static int
2827can_get_termresponse()
2828{
2829 return cur_tmode == TMODE_RAW
2830 && termcap_active
2831# ifdef UNIX
2832 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2833# endif
2834 && p_ek;
2835}
2836
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002837static int winpos_x = -1;
2838static int winpos_y = -1;
2839static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002840
Bram Moolenaar113e1072019-01-20 15:30:40 +01002841# if (defined(FEAT_EVAL) && defined(HAVE_TGETENT)) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002842/*
2843 * Try getting the Vim window position from the terminal.
2844 * Returns OK or FAIL.
2845 */
2846 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002847term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002848{
2849 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002850 int prev_winpos_x = winpos_x;
2851 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002852
2853 if (*T_CGP == NUL || !can_get_termresponse())
2854 return FAIL;
2855 winpos_x = -1;
2856 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002857 ++did_request_winpos;
2858 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002859 OUT_STR(T_CGP);
2860 out_flush();
2861
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002862 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002863 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002864 {
2865 (void)vpeekc_nomap();
2866 if (winpos_x >= 0 && winpos_y >= 0)
2867 {
2868 *x = winpos_x;
2869 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002870 return OK;
2871 }
2872 ui_delay(10, FALSE);
2873 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002874 /* Do not reset "did_request_winpos", if we timed out the response might
2875 * still come later and we must consume it. */
2876
2877 winpos_x = prev_winpos_x;
2878 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002879 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002880 {
2881 /* Polling: return previous values if we have them. */
2882 *x = winpos_x;
2883 *y = winpos_y;
2884 return OK;
2885 }
2886
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002887 return FALSE;
2888}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002889# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002890# endif
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002893term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002895 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897#endif
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002900term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002903 int i = *s == CSI ? 1 : 2;
2904 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 /* Special handling of 16 colors, because termcap can't handle it */
2907 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2908 /* Also accept CSI instead of <Esc>[ */
2909 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002910 && ((s[0] == ESC && s[1] == '[')
2911#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2912 || (s[0] == ESC && s[1] == '|')
2913#endif
2914 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 && s[i] != NUL
2916 && (STRCMP(s + i + 1, "%p1%dm") == 0
2917 || STRCMP(s + i + 1, "%dm") == 0)
2918 && (s[i] == '3' || s[i] == '4'))
2919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002921 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002923 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002925 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002926#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002927 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002928#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002929 IF_EB("\033[", ESC_STR "[")) : "\233";
2930 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2931 : (n >= 16 ? "48;5;" : "10");
2932
2933 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2935 }
2936 else
2937 OUT_STR(tgoto((char *)s, 0, n));
2938}
2939
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002940 void
2941term_fg_color(int n)
2942{
2943 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2944 if (*T_CAF)
2945 term_color(T_CAF, n);
2946 else if (*T_CSF)
2947 term_color(T_CSF, n);
2948}
2949
2950 void
2951term_bg_color(int n)
2952{
2953 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2954 if (*T_CAB)
2955 term_color(T_CAB, n);
2956 else if (*T_CSB)
2957 term_color(T_CSB, n);
2958}
2959
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002960#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002961
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002962#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2963#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2964#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002965
2966 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002967term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002968{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002969#define MAX_COLOR_STR_LEN 100
2970 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002971
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002972 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002973 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002974 OUT_STR(buf);
2975}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002976
2977 void
2978term_fg_rgb_color(guicolor_T rgb)
2979{
2980 term_rgb_color(T_8F, rgb);
2981}
2982
2983 void
2984term_bg_rgb_color(guicolor_T rgb)
2985{
2986 term_rgb_color(T_8B, rgb);
2987}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002988#endif
2989
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002990#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2991 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
2993 * Generic function to set window title, using t_ts and t_fs.
2994 */
2995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 /* t_ts takes one argument: column in status line */
2999 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
3000 out_str_nf(title);
3001 out_str(T_FS); /* set title end */
3002 out_flush();
3003}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003004
3005/*
3006 * Tell the terminal to push (save) the title and/or icon, so that it can be
3007 * popped (restored) later.
3008 */
3009 void
3010term_push_title(int which)
3011{
3012 if ((which & SAVE_RESTORE_TITLE) && *T_CST != NUL)
3013 {
3014 OUT_STR(T_CST);
3015 out_flush();
3016 }
3017
3018 if ((which & SAVE_RESTORE_ICON) && *T_SSI != NUL)
3019 {
3020 OUT_STR(T_SSI);
3021 out_flush();
3022 }
3023}
3024
3025/*
3026 * Tell the terminal to pop the title and/or icon.
3027 */
3028 void
3029term_pop_title(int which)
3030{
3031 if ((which & SAVE_RESTORE_TITLE) && *T_CRT != NUL)
3032 {
3033 OUT_STR(T_CRT);
3034 out_flush();
3035 }
3036
3037 if ((which & SAVE_RESTORE_ICON) && *T_SRI != NUL)
3038 {
3039 OUT_STR(T_SRI);
3040 out_flush();
3041 }
3042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043#endif
3044
3045/*
3046 * Make sure we have a valid set or terminal options.
3047 * Replace all entries that are NULL by empty_option
3048 */
3049 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003050ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003052 char_u *env_colors;
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 check_options(); /* make sure no options are NULL */
3055
3056 /*
3057 * MUST have "cm": cursor motion.
3058 */
3059 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003060 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /*
3063 * if "cs" defined, use a scroll region, it's faster.
3064 */
3065 if (*T_CS != NUL)
3066 scroll_region = TRUE;
3067 else
3068 scroll_region = FALSE;
3069
3070 if (pairs)
3071 {
3072 /*
3073 * optional pairs
3074 */
3075 /* TP goes to normal mode for TI (invert) and TB (bold) */
3076 if (*T_ME == NUL)
3077 T_ME = T_MR = T_MD = T_MB = empty_option;
3078 if (*T_SO == NUL || *T_SE == NUL)
3079 T_SO = T_SE = empty_option;
3080 if (*T_US == NUL || *T_UE == NUL)
3081 T_US = T_UE = empty_option;
3082 if (*T_CZH == NUL || *T_CZR == NUL)
3083 T_CZH = T_CZR = empty_option;
3084
3085 /* T_VE is needed even though T_VI is not defined */
3086 if (*T_VE == NUL)
3087 T_VI = empty_option;
3088
3089 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3090 if (*T_ME == NUL)
3091 {
3092 T_ME = T_SE;
3093 T_MR = T_SO;
3094 T_MD = T_SO;
3095 }
3096
3097 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3098 if (*T_SO == NUL)
3099 {
3100 T_SE = T_ME;
3101 if (*T_MR == NUL)
3102 T_SO = T_MD;
3103 else
3104 T_SO = T_MR;
3105 }
3106
3107 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3108 if (*T_CZH == NUL)
3109 {
3110 T_CZR = T_ME;
3111 if (*T_MR == NUL)
3112 T_CZH = T_MD;
3113 else
3114 T_CZH = T_MR;
3115 }
3116
3117 /* "Sb" and "Sf" come in pairs */
3118 if (*T_CSB == NUL || *T_CSF == NUL)
3119 {
3120 T_CSB = empty_option;
3121 T_CSF = empty_option;
3122 }
3123
3124 /* "AB" and "AF" come in pairs */
3125 if (*T_CAB == NUL || *T_CAF == NUL)
3126 {
3127 T_CAB = empty_option;
3128 T_CAF = empty_option;
3129 }
3130
3131 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3132 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003133 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /* Set 'weirdinvert' according to value of 't_xs' */
3136 p_wiv = (*T_XS != NUL);
3137 }
3138 need_gather = TRUE;
3139
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003140 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003142 env_colors = mch_getenv((char_u *)"COLORS");
3143 if (env_colors != NULL && isdigit(*env_colors))
3144 {
3145 int colors = atoi((char *)env_colors);
3146
3147 if (colors != t_colors)
3148 set_color_count(colors);
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151
3152#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3153 || defined(PROTO)
3154/*
3155 * Represent the given long_u as individual bytes, with the most significant
3156 * byte first, and store them in dst.
3157 */
3158 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003159add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 int i;
3162 int shift;
3163
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003164 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 shift = 8 * (sizeof(long_u) - i);
3167 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3168 }
3169}
3170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171/*
3172 * Interpret the next string of bytes in buf as a long integer, with the most
3173 * significant byte first. Note that it is assumed that buf has been through
3174 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3175 * Puts result in val, and returns the number of bytes read from buf
3176 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3177 * were present.
3178 */
3179 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003180get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 int len;
3183 char_u bytes[sizeof(long_u)];
3184 int i;
3185 int shift;
3186
3187 *val = 0;
3188 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3189 if (len != -1)
3190 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003191 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 shift = 8 * (sizeof(long_u) - 1 - i);
3194 *val += (long_u)bytes[i] << shift;
3195 }
3196 }
3197 return len;
3198}
3199#endif
3200
3201#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003202 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3203 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204/*
3205 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3206 * that buf has been through inchar(). Returns the actual number of bytes used
3207 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3208 * available.
3209 */
3210 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003211get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
3213 int len = 0;
3214 int i;
3215 char_u c;
3216
3217 for (i = 0; i < num_bytes; i++)
3218 {
3219 if ((c = buf[len++]) == NUL)
3220 return -1;
3221 if (c == K_SPECIAL)
3222 {
3223 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3224 return -1;
3225 if (buf[len++] == (int)KS_ZERO)
3226 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003227 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3228 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3229 if (buf[len++] == (int)KE_CSI)
3230 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003232 else if (c == CSI && buf[len] == KS_EXTRA
3233 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003234 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3235 * the start of a special key, see add_to_input_buf_csi(). */
3236 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 bytes[i] = c;
3238 }
3239 return len;
3240}
3241#endif
3242
3243/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003244 * Check if the new shell size is valid, correct it if it's too small or way
3245 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
3247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003248check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (Rows < min_rows()) /* need room for one window and command line */
3251 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003252 limit_screen_size();
3253}
3254
3255/*
3256 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3257 */
3258 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003259limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003260{
3261 if (Columns < MIN_COLUMNS)
3262 Columns = MIN_COLUMNS;
3263 else if (Columns > 10000)
3264 Columns = 10000;
3265 if (Rows > 1000)
3266 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267}
3268
Bram Moolenaar86b68352004-12-27 21:59:20 +00003269/*
3270 * Invoked just before the screen structures are going to be (re)allocated.
3271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003273win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 static int old_Rows = 0;
3276 static int old_Columns = 0;
3277
3278 if (old_Rows != Rows || old_Columns != Columns)
3279 ui_new_shellsize();
3280 if (old_Rows != Rows)
3281 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003282 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003283 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003284 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 old_Rows = Rows;
3286 shell_new_rows(); /* update window sizes */
3287 }
3288 if (old_Columns != Columns)
3289 {
3290 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293}
3294
3295/*
3296 * Call this function when the Vim shell has been resized in any way.
3297 * Will obtain the current size and redraw (also when size didn't change).
3298 */
3299 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003300shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 set_shellsize(0, 0, FALSE);
3303}
3304
3305/*
3306 * Check if the shell size changed. Handle a resize.
3307 * When the size didn't change, nothing happens.
3308 */
3309 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003310shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 int old_Rows = Rows;
3313 int old_Columns = Columns;
3314
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003315 if (!exiting
3316#ifdef FEAT_GUI
3317 /* Do not get the size when executing a shell command during
3318 * startup. */
3319 && !gui.starting
3320#endif
3321 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003322 {
3323 (void)ui_get_shellsize();
3324 check_shellsize();
3325 if (old_Rows != Rows || old_Columns != Columns)
3326 shell_resized();
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328}
3329
3330/*
3331 * Set size of the Vim shell.
3332 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3333 * window size (this is used for the :win command).
3334 * If 'mustset' is FALSE, we may try to get the real window size and if
3335 * it fails use 'width' and 'height'.
3336 */
3337 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003338set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 static int busy = FALSE;
3341
3342 /*
3343 * Avoid recursiveness, can happen when setting the window size causes
3344 * another window-changed signal.
3345 */
3346 if (busy)
3347 return;
3348
3349 if (width < 0 || height < 0) /* just checking... */
3350 return;
3351
Bram Moolenaara971b822011-09-14 14:43:25 +02003352 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003354 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 State = SETWSIZE;
3356 return;
3357 }
3358
Bram Moolenaara971b822011-09-14 14:43:25 +02003359 /* curwin->w_buffer can be NULL when we are closing a window and the
3360 * buffer has already been closed and removing a scrollbar causes a resize
3361 * event. Don't resize then, it will happen after entering another buffer.
3362 */
3363 if (curwin->w_buffer == NULL)
3364 return;
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 ++busy;
3367
3368#ifdef AMIGA
3369 out_flush(); /* must do this before mch_get_shellsize() for
3370 some obscure reason */
3371#endif
3372
3373 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3374 {
3375 Rows = height;
3376 Columns = width;
3377 check_shellsize();
3378 ui_set_shellsize(mustset);
3379 }
3380 else
3381 check_shellsize();
3382
3383 /* The window layout used to be adjusted here, but it now happens in
3384 * screenalloc() (also invoked from screenclear()). That is because the
3385 * "busy" check above may skip this, but not screenalloc(). */
3386
3387 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3388 screenclear();
3389 else
3390 screen_start(); /* don't know where cursor is now */
3391
3392 if (starting != NO_SCREEN)
3393 {
3394#ifdef FEAT_TITLE
3395 maketitle();
3396#endif
3397 changed_line_abv_curs();
3398 invalidate_botline();
3399
3400 /*
3401 * We only redraw when it's needed:
3402 * - While at the more prompt or executing an external command, don't
3403 * redraw, but position the cursor.
3404 * - While editing the command line, only redraw that.
3405 * - in Ex mode, don't redraw anything.
3406 * - Otherwise, redraw right now, and position the cursor.
3407 * Always need to call update_screen() or screenalloc(), to make
3408 * sure Rows/Columns and the size of ScreenLines[] is correct!
3409 */
3410 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3411 || exmode_active)
3412 {
3413 screenalloc(FALSE);
3414 repeat_message();
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 else
3417 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003418 if (curwin->w_p_scb)
3419 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003420 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003421 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003422 update_screen(NOT_VALID);
3423 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003424 }
3425 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003426 {
3427 update_topline();
3428#if defined(FEAT_INS_EXPAND)
3429 if (pum_visible())
3430 {
3431 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003432 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003433 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003434#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003435 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003436 if (redrawing())
3437 setcursor();
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440 cursor_on(); /* redrawing may have switched it off */
3441 }
3442 out_flush();
3443 --busy;
3444}
3445
3446/*
3447 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3448 * commands and Ex mode).
3449 */
3450 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003451settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453#ifdef FEAT_GUI
3454 /* don't set the term where gvim was started to any mode */
3455 if (gui.in_use)
3456 return;
3457#endif
3458
3459 if (full_screen)
3460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /*
3462 * When returning after calling a shell we want to really set the
3463 * terminal to raw mode, even though we think it already is, because
3464 * the shell program may have reset the terminal mode.
3465 * When we think the terminal is normal, don't try to set it to
3466 * normal again, because that causes problems (logout!) on some
3467 * machines.
3468 */
3469 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3470 {
3471#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003472# ifdef FEAT_GUI
3473 if (!gui.in_use && !gui.starting)
3474# endif
3475 {
3476 /* May need to check for T_CRV response and termcodes, it
3477 * doesn't work in Cooked mode, an external program may get
3478 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003479 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3480 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003481#ifdef FEAT_TERMINAL
3482 || rfg_status == STATUS_SENT
3483#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003484 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003485 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003486 || rcs_status == STATUS_SENT
3487 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003488 (void)vpeekc_nomap();
3489 check_for_codes_from_term();
3490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
3492#ifdef FEAT_MOUSE_TTY
3493 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003494 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003496 if (tmode != TMODE_RAW)
3497 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003499 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 cur_tmode = tmode;
3501#ifdef FEAT_MOUSE
3502 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003503 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003505 if (tmode == TMODE_RAW)
3506 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 out_flush();
3508 }
3509#ifdef FEAT_TERMRESPONSE
3510 may_req_termresponse();
3511#endif
3512 }
3513}
3514
3515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003516starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 if (full_screen && !termcap_active)
3519 {
3520 out_str(T_TI); /* start termcap mode */
3521 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003522 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 out_flush();
3524 termcap_active = TRUE;
3525 screen_start(); /* don't know where cursor is now */
3526#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003527# ifdef FEAT_GUI
3528 if (!gui.in_use && !gui.starting)
3529# endif
3530 {
3531 may_req_termresponse();
3532 /* Immediately check for a response. If t_Co changes, we don't
3533 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003534 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003535 check_for_codes_from_term();
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#endif
3538 }
3539}
3540
3541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003542stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
3544 screen_stop_highlight();
3545 reset_cterm_colors();
3546 if (termcap_active)
3547 {
3548#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003549# ifdef FEAT_GUI
3550 if (!gui.in_use && !gui.starting)
3551# endif
3552 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003553 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003554 if (crv_status == STATUS_SENT
3555 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003556# ifdef FEAT_TERMINAL
3557 || rfg_status == STATUS_SENT
3558# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003559 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003560 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003561 || rcs_status == STATUS_SENT
3562 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003563 {
3564# ifdef UNIX
3565 /* Give the terminal a chance to respond. */
3566 mch_delay(100L, FALSE);
3567# endif
3568# ifdef TCIFLUSH
3569 /* Discard data received but not read. */
3570 if (exiting)
3571 tcflush(fileno(stdin), TCIFLUSH);
3572# endif
3573 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003574 /* Check for termcodes first, otherwise an external program may
3575 * get them. */
3576 check_for_codes_from_term();
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003579 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 out_str(T_KE); /* stop "keypad transmit" mode */
3581 out_flush();
3582 termcap_active = FALSE;
3583 cursor_on(); /* just in case it is still off */
3584 out_str(T_TE); /* stop termcap mode */
3585 screen_start(); /* don't know where cursor is now */
3586 out_flush();
3587 }
3588}
3589
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003590#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591/*
3592 * Request version string (for xterm) when needed.
3593 * Only do this after switching to raw mode, otherwise the result will be
3594 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003595 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003596 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Only do this after termcap mode has been started, otherwise the codes for
3598 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003599 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3600 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003601 * On Unix only do it when both output and input are a tty (avoid writing
3602 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 * The result is caught in check_termcode().
3604 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003606may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003608 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003609 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003610 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 && *T_CRV != NUL)
3612 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003613 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003615 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /* check for the characters now, otherwise they might be eaten by
3617 * get_keystroke() */
3618 out_flush();
3619 (void)vpeekc_nomap();
3620 }
3621}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003622
Bram Moolenaar9584b312013-03-13 19:29:28 +01003623/*
3624 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003625 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003626 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003627 * If the terminal treats \u25bd as single width, the position is (1, 1),
3628 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003629 * This function has the side effect that changes cursor position, so
3630 * it must be called immediately after entering termcap mode.
3631 */
3632 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003633may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003634{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003635 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003636 && can_get_termresponse()
3637 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003638 && *T_U7 != NUL
3639 && !option_was_set((char_u *)"ambiwidth"))
3640 {
3641 char_u buf[16];
3642
Bram Moolenaarb255b902018-04-24 21:40:10 +02003643 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003644 /* Do this in the second row. In the first row the returned sequence
3645 * may be CSI 1;2R, which is the same as <S-F3>. */
3646 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003647 buf[mb_char2bytes(0x25bd, buf)] = 0;
3648 out_str(buf);
3649 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003650 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003651 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003652
3653 /* This overwrites a few characters on the screen, a redraw is needed
3654 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003655 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003656 out_str((char_u *)" ");
3657 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003658
Bram Moolenaar05684312017-12-09 15:11:24 +01003659 /* Need to reset the known cursor position. */
3660 screen_start();
3661
Bram Moolenaar9584b312013-03-13 19:29:28 +01003662 /* check for the characters now, otherwise they might be eaten by
3663 * get_keystroke() */
3664 out_flush();
3665 (void)vpeekc_nomap();
3666 }
3667}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003668
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003669/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003670 * Similar to requesting the version string: Request the terminal background
3671 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003672 */
3673 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003674may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003675{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003676 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003677 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003678 int didit = FALSE;
3679
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003680# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003681 /* Only request foreground if t_RF is set. */
3682 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3683 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003684 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003685 out_str(T_RFG);
3686 rfg_status = STATUS_SENT;
3687 didit = TRUE;
3688 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003689# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003690
3691 /* Only request background if t_RB is set. */
3692 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003693 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003694 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003695 out_str(T_RBG);
3696 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003697 didit = TRUE;
3698 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003699
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003700 if (didit)
3701 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003702 /* check for the characters now, otherwise they might be eaten by
3703 * get_keystroke() */
3704 out_flush();
3705 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003706 }
3707 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003708}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003709
Bram Moolenaar2951b772013-07-03 12:45:31 +02003710# ifdef DEBUG_TERMRESPONSE
3711 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003712log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003713{
3714 static FILE *fd_tr = NULL;
3715 static proftime_T start;
3716 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003717 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003718
3719 if (fd_tr == NULL)
3720 {
3721 fd_tr = fopen("termresponse.log", "w");
3722 profile_start(&start);
3723 }
3724 now = start;
3725 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003726 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3727 must_redraw == NOT_VALID ? "NV"
3728 : must_redraw == CLEAR ? "CL" : " ");
3729 va_start(ap, fmt);
3730 vfprintf(fd_tr, fmt, ap);
3731 va_end(ap);
3732 fputc('\n', fd_tr);
3733 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003734}
3735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#endif
3737
3738/*
3739 * Return TRUE when saving and restoring the screen.
3740 */
3741 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003742swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 return (full_screen && *T_TI != NUL);
3745}
3746
Bram Moolenaarecadf432018-03-20 11:17:04 +01003747#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748/*
3749 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3750 */
3751 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003752setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
3754# ifdef FEAT_MOUSE_TTY
3755 int checkfor;
3756# endif
3757
3758# ifdef FEAT_MOUSESHAPE
3759 update_mouseshape(-1);
3760# endif
3761
3762# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3763# ifdef FEAT_GUI
3764 /* In the GUI the mouse is always enabled. */
3765 if (gui.in_use)
3766 return;
3767# endif
3768 /* be quick when mouse is off */
3769 if (*p_mouse == NUL || has_mouse_termcode == 0)
3770 return;
3771
3772 /* don't switch mouse on when not in raw mode (Ex mode) */
3773 if (cur_tmode != TMODE_RAW)
3774 {
3775 mch_setmouse(FALSE);
3776 return;
3777 }
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (VIsual_active)
3780 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003781 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 checkfor = MOUSE_RETURN;
3783 else if (State & INSERT)
3784 checkfor = MOUSE_INSERT;
3785 else if (State & CMDLINE)
3786 checkfor = MOUSE_COMMAND;
3787 else if (State == CONFIRM || State == EXTERNCMD)
3788 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3789 else
3790 checkfor = MOUSE_NORMAL; /* assume normal mode */
3791
3792 if (mouse_has(checkfor))
3793 mch_setmouse(TRUE);
3794 else
3795 mch_setmouse(FALSE);
3796# endif
3797}
3798
3799/*
3800 * Return TRUE if
3801 * - "c" is in 'mouse', or
3802 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3803 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3804 * normal editing mode (not at hit-return message).
3805 */
3806 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003807mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809 char_u *p;
3810
3811 for (p = p_mouse; *p; ++p)
3812 switch (*p)
3813 {
3814 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3815 return TRUE;
3816 break;
3817 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3818 return TRUE;
3819 break;
3820 default: if (c == *p) return TRUE; break;
3821 }
3822 return FALSE;
3823}
3824
3825/*
3826 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3827 */
3828 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003829mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 return (p_mousem[0] == 'p');
3832}
3833#endif
3834
3835/*
3836 * By outputting the 'cursor very visible' termcap code, for some windowed
3837 * terminals this makes the screen scrolled to the correct position.
3838 * Used when starting Vim or returning from a shell.
3839 */
3840 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003841scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003843 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
3845 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003846 out_str(T_CVS);
3847 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849}
3850
3851static int cursor_is_off = FALSE;
3852
3853/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003854 * Enable the cursor without checking if it's already enabled.
3855 */
3856 void
3857cursor_on_force(void)
3858{
3859 out_str(T_VE);
3860 cursor_is_off = FALSE;
3861}
3862
3863/*
3864 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
3866 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003867cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003870 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871}
3872
3873/*
3874 * Disable the cursor.
3875 */
3876 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003877cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003879 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003881 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 cursor_is_off = TRUE;
3883 }
3884}
3885
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003886#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003888 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003889 */
3890 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003891term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003892{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003893 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003894 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003896 /* Only do something when redrawing the screen and we can restore the
3897 * mode. */
3898 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003899 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003900# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003901 if (forced && initial_cursor_shape > 0)
3902 /* Restore to initial values. */
3903 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003904# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003905 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003906 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003907
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003908 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003909 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003910 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003911 {
3912 if (*T_CSR != NUL)
3913 p = T_CSR; /* Replace mode cursor */
3914 else
3915 p = T_CSI; /* fall back to Insert mode cursor */
3916 if (*p != NUL)
3917 {
3918 out_str(p);
3919 showing_mode = REPLACE;
3920 }
3921 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003922 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003923 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003924 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003925 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003926 {
3927 out_str(T_CSI); /* Insert mode cursor */
3928 showing_mode = INSERT;
3929 }
3930 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003931 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003932 {
3933 out_str(T_CEI); /* non-Insert mode cursor */
3934 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003935 }
3936}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003937
3938# if defined(FEAT_TERMINAL) || defined(PROTO)
3939 void
3940term_cursor_color(char_u *color)
3941{
3942 if (*T_CSC != NUL)
3943 {
3944 out_str(T_CSC); /* set cursor color start */
3945 out_str_nf(color);
3946 out_str(T_CEC); /* set cursor color end */
3947 out_flush();
3948 }
3949}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003950# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003951
Bram Moolenaar4db25542017-08-28 22:43:05 +02003952 int
3953blink_state_is_inverted()
3954{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003955#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003956 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3957 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003958#else
3959 return FALSE;
3960#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003961}
3962
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003963/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003964 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003965 */
3966 void
3967term_cursor_shape(int shape, int blink)
3968{
3969 if (*T_CSH != NUL)
3970 {
3971 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3972 out_flush();
3973 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003974 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003975 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003976 int do_blink = blink;
3977
3978 /* t_SH is empty: try setting just the blink state.
3979 * The blink flags are XORed together, if the initial blinking from
3980 * style and shape differs, we need to invert the flag here. */
3981 if (blink_state_is_inverted())
3982 do_blink = !blink;
3983
3984 if (do_blink && *T_VS != NUL)
3985 {
3986 out_str(T_VS);
3987 out_flush();
3988 }
3989 else if (!do_blink && *T_CVS != NUL)
3990 {
3991 out_str(T_CVS);
3992 out_flush();
3993 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003994 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003995}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003996#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997
3998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Set scrolling region for window 'wp'.
4000 * The region starts 'off' lines from the start of the window.
4001 * Also set the vertical scroll region for a vertically split window. Always
4002 * the full width of the window, excluding the vertical separator.
4003 */
4004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004005scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
4007 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4008 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004010 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4011 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 screen_start(); /* don't know where cursor is now */
4013}
4014
4015/*
4016 * Reset scrolling region to the whole screen.
4017 */
4018 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004019scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (*T_CSV != NUL)
4023 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 screen_start(); /* don't know where cursor is now */
4025}
4026
4027
4028/*
4029 * List of terminal codes that are currently recognized.
4030 */
4031
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004032static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033{
4034 char_u name[2]; /* termcap name of entry */
4035 char_u *code; /* terminal code (in allocated memory) */
4036 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004037 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038} *termcodes = NULL;
4039
4040static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4041static int tc_len = 0; /* current number of entries in termcodes[] */
4042
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004043static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004046clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
4048 while (tc_len > 0)
4049 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004050 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 tc_max_len = 0;
4052
4053#ifdef HAVE_TGETENT
4054 BC = (char *)empty_option;
4055 UP = (char *)empty_option;
4056 PC = NUL; /* set pad character to NUL */
4057 ospeed = 0;
4058#endif
4059
4060 need_gather = TRUE; /* need to fill termleader[] */
4061}
4062
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063#define ATC_FROM_TERM 55
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065/*
4066 * Add a new entry to the list of terminal codes.
4067 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4069 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 */
4071 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004072add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
4074 struct termcode *new_tc;
4075 int i, j;
4076 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004077 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 if (string == NULL || *string == NUL)
4080 {
4081 del_termcode(name);
4082 return;
4083 }
4084
Bram Moolenaar45500912014-07-09 20:51:07 +02004085#if defined(WIN3264) && !defined(FEAT_GUI)
4086 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4087#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (s == NULL)
4091 return;
4092
4093 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004096 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 s[0] = term_7to8bit(string);
4098 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004099
4100#if defined(WIN3264) && !defined(FEAT_GUI)
4101 if (s[0] == K_NUL)
4102 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004103 STRMOVE(s + 1, s);
4104 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004105 }
4106#endif
4107
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004108 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 need_gather = TRUE; /* need to fill termleader[] */
4111
4112 /*
4113 * need to make space for more entries
4114 */
4115 if (tc_len == tc_max_len)
4116 {
4117 tc_max_len += 20;
4118 new_tc = (struct termcode *)alloc(
4119 (unsigned)(tc_max_len * sizeof(struct termcode)));
4120 if (new_tc == NULL)
4121 {
4122 tc_max_len -= 20;
4123 return;
4124 }
4125 for (i = 0; i < tc_len; ++i)
4126 new_tc[i] = termcodes[i];
4127 vim_free(termcodes);
4128 termcodes = new_tc;
4129 }
4130
4131 /*
4132 * Look for existing entry with the same name, it is replaced.
4133 * Look for an existing entry that is alphabetical higher, the new entry
4134 * is inserted in front of it.
4135 */
4136 for (i = 0; i < tc_len; ++i)
4137 {
4138 if (termcodes[i].name[0] < name[0])
4139 continue;
4140 if (termcodes[i].name[0] == name[0])
4141 {
4142 if (termcodes[i].name[1] < name[1])
4143 continue;
4144 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004145 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 */
4147 if (termcodes[i].name[1] == name[1])
4148 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 if (flags == ATC_FROM_TERM && (j = termcode_star(
4150 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004151 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004152 /* Don't replace ESC[123;*X or ESC O*X with another when
4153 * invoked from got_code_from_term(). */
4154 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004155 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004156 && s[len - 1]
4157 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004158 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004159 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004160 vim_free(s);
4161 return;
4162 }
4163 }
4164 else
4165 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004167 vim_free(termcodes[i].code);
4168 --tc_len;
4169 break;
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 }
4173 /*
4174 * Found alphabetical larger entry, move rest to insert new entry
4175 */
4176 for (j = tc_len; j > i; --j)
4177 termcodes[j] = termcodes[j - 1];
4178 break;
4179 }
4180
4181 termcodes[i].name[0] = name[0];
4182 termcodes[i].name[1] = name[1];
4183 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004184 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004185
4186 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4187 * accept modifiers. */
4188 termcodes[i].modlen = 0;
4189 j = termcode_star(s, len);
4190 if (j > 0)
4191 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 ++tc_len;
4193}
4194
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004196 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004198 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004199 */
4200 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004201termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004202{
4203 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4204 if (len >= 3 && code[len - 2] == '*')
4205 {
4206 if (len >= 5 && code[len - 3] == ';')
4207 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004208 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209 return 1;
4210 }
4211 return 0;
4212}
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004215find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 int i;
4218
4219 for (i = 0; i < tc_len; ++i)
4220 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4221 return termcodes[i].code;
4222 return NULL;
4223}
4224
4225#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4226 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004227get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
4229 if (i >= tc_len)
4230 return NULL;
4231 return &termcodes[i].name[0];
4232}
4233#endif
4234
4235 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004236del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 int i;
4239
4240 if (termcodes == NULL) /* nothing there yet */
4241 return;
4242
4243 need_gather = TRUE; /* need to fill termleader[] */
4244
4245 for (i = 0; i < tc_len; ++i)
4246 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4247 {
4248 del_termcode_idx(i);
4249 return;
4250 }
4251 /* not found. Give error message? */
4252}
4253
4254 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004255del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
4257 int i;
4258
4259 vim_free(termcodes[idx].code);
4260 --tc_len;
4261 for (i = idx; i < tc_len; ++i)
4262 termcodes[i] = termcodes[i + 1];
4263}
4264
4265#ifdef FEAT_TERMRESPONSE
4266/*
4267 * Called when detected that the terminal sends 8-bit codes.
4268 * Convert all 7-bit codes to their 8-bit equivalent.
4269 */
4270 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004271switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 int i;
4274 int c;
4275
4276 /* Only need to do something when not already using 8-bit codes. */
4277 if (!term_is_8bit(T_NAME))
4278 {
4279 for (i = 0; i < tc_len; ++i)
4280 {
4281 c = term_7to8bit(termcodes[i].code);
4282 if (c != 0)
4283 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004284 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 termcodes[i].code[0] = c;
4286 }
4287 }
4288 need_gather = TRUE; /* need to fill termleader[] */
4289 }
4290 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004291 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292}
4293#endif
4294
4295#ifdef CHECK_DOUBLE_CLICK
4296static linenr_T orig_topline = 0;
4297# ifdef FEAT_DIFF
4298static int orig_topfill = 0;
4299# endif
4300#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004301#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302/*
4303 * Checking for double clicks ourselves.
4304 * "orig_topline" is used to avoid detecting a double-click when the window
4305 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4306 */
4307/*
4308 * Set orig_topline. Used when jumping to another window, so that a double
4309 * click still works.
4310 */
4311 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004312set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
4314 orig_topline = wp->w_topline;
4315# ifdef FEAT_DIFF
4316 orig_topfill = wp->w_topfill;
4317# endif
4318}
4319#endif
4320
4321/*
4322 * Check if typebuf.tb_buf[] contains a terminal key code.
4323 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4324 * + max_offset].
4325 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004326 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * With a match, the match is removed, the replacement code is inserted in
4328 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4329 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004330 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4331 * "buflen" is then the length of the string in buf[] and is updated for
4332 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 */
4334 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004335check_termcode(
4336 int max_offset,
4337 char_u *buf,
4338 int bufsize,
4339 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 char_u *tp;
4342 char_u *p;
4343 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004346 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 int offset;
4348 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004350 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004351 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 int new_slen;
4353 int extra;
4354 char_u string[MAX_KEY_CODE_LEN + 1];
4355 int i, j;
4356 int idx = 0;
4357#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004358# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4359 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 char_u bytes[6];
4361 int num_bytes;
4362# endif
4363 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 int is_click, is_drag;
4365 int wheel_code = 0;
4366 int current_button;
4367 static int held_button = MOUSE_RELEASE;
4368 static int orig_num_clicks = 1;
4369 static int orig_mouse_code = 0x0;
4370# ifdef CHECK_DOUBLE_CLICK
4371 static int orig_mouse_col = 0;
4372 static int orig_mouse_row = 0;
4373 static struct timeval orig_mouse_time = {0, 0};
4374 /* time of previous mouse click */
4375 struct timeval mouse_time; /* time of current mouse click */
4376 long timediff; /* elapsed time in msec */
4377# endif
4378#endif
4379 int cpo_koffset;
4380#ifdef FEAT_MOUSE_GPM
4381 extern int gpm_flag; /* gpm library variable */
4382#endif
4383
4384 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4385
4386 /*
4387 * Speed up the checks for terminal codes by gathering all first bytes
4388 * used in termleader[]. Often this is just a single <Esc>.
4389 */
4390 if (need_gather)
4391 gather_termleader();
4392
4393 /*
4394 * Check at several positions in typebuf.tb_buf[], to catch something like
4395 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4396 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004397 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 * This is used often, KEEP IT FAST!
4399 */
4400 for (offset = 0; offset < max_offset; ++offset)
4401 {
4402 if (buf == NULL)
4403 {
4404 if (offset >= typebuf.tb_len)
4405 break;
4406 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4407 len = typebuf.tb_len - offset; /* length of the input */
4408 }
4409 else
4410 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004411 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
4413 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004414 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416
4417 /*
4418 * Don't check characters after K_SPECIAL, those are already
4419 * translated terminal chars (avoid translating ~@^Hx).
4420 */
4421 if (*tp == K_SPECIAL)
4422 {
4423 offset += 2; /* there are always 2 extra characters */
4424 continue;
4425 }
4426
4427 /*
4428 * Skip this position if the character does not appear as the first
4429 * character in term_strings. This speeds up a lot, since most
4430 * termcodes start with the same character (ESC or CSI).
4431 */
4432 i = *tp;
4433 for (p = termleader; *p && *p != i; ++p)
4434 ;
4435 if (*p == NUL)
4436 continue;
4437
4438 /*
4439 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4440 * are in Insert mode.
4441 */
4442 if (*tp == ESC && !p_ek && (State & INSERT))
4443 continue;
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004446 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004447 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449#ifdef FEAT_GUI
4450 if (gui.in_use)
4451 {
4452 /*
4453 * GUI special key codes are all of the form [CSI xx].
4454 */
4455 if (*tp == CSI) /* Special key from GUI */
4456 {
4457 if (len < 3)
4458 return -1; /* Shouldn't happen */
4459 slen = 3;
4460 key_name[0] = tp[1];
4461 key_name[1] = tp[2];
4462 }
4463 }
4464 else
4465#endif /* FEAT_GUI */
4466 {
4467 for (idx = 0; idx < tc_len; ++idx)
4468 {
4469 /*
4470 * Ignore the entry if we are not at the start of
4471 * typebuf.tb_buf[]
4472 * and there are not enough characters to make a match.
4473 * But only when the 'K' flag is in 'cpoptions'.
4474 */
4475 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004476 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (cpo_koffset && offset && len < slen)
4478 continue;
4479 if (STRNCMP(termcodes[idx].code, tp,
4480 (size_t)(slen > len ? len : slen)) == 0)
4481 {
4482 if (len < slen) /* got a partial sequence */
4483 return -1; /* need to get more chars */
4484
4485 /*
4486 * When found a keypad key, check if there is another key
4487 * that matches and use that one. This makes <Home> to be
4488 * found instead of <kHome> when they produce the same
4489 * key code.
4490 */
4491 if (termcodes[idx].name[0] == 'K'
4492 && VIM_ISDIGIT(termcodes[idx].name[1]))
4493 {
4494 for (j = idx + 1; j < tc_len; ++j)
4495 if (termcodes[j].len == slen &&
4496 STRNCMP(termcodes[idx].code,
4497 termcodes[j].code, slen) == 0)
4498 {
4499 idx = j;
4500 break;
4501 }
4502 }
4503
4504 key_name[0] = termcodes[idx].name[0];
4505 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 break;
4507 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004508
4509 /*
4510 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004511 * <Esc>[123;*X (modslen == slen - 3)
4512 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4513 * When there is a modifier the * matches a number.
4514 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004515 */
4516 if (termcodes[idx].modlen > 0)
4517 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004518 modslen = termcodes[idx].modlen;
4519 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004520 continue;
4521 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004522 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004523 {
4524 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004525
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004527 return -1; /* need to get more chars */
4528
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004529 if (tp[modslen] == termcodes[idx].code[slen - 1])
4530 slen = modslen + 1; /* no modifiers */
4531 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004532 continue; /* no match */
4533 else
4534 {
4535 /* Skip over the digits, the final char must
4536 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004537 for (j = slen - 2; j < len && (isdigit(tp[j])
4538 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004539 ;
4540 ++j;
4541 if (len < j) /* got a partial sequence */
4542 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004543 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4544 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004545
Bram Moolenaara529ce02017-06-22 22:37:57 +02004546 modifiers_start = tp + slen - 2;
4547
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004548 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004549 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004550 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004552 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004553 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004554 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004555 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004556 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004557 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004558
4559 slen = j;
4560 }
4561 key_name[0] = termcodes[idx].name[0];
4562 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004563 break;
4564 }
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 }
4568
4569#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004570 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004571 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004572 * detecting the start of these mouse codes they might as well be
4573 * another key code or terminal response. */
4574# ifdef FEAT_MOUSE_DEC
4575 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004576# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004577# ifdef FEAT_MOUSE_PTERM
4578 || key_name[0] == KS_PTERM_MOUSE
4579# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004582 /* Check for some responses from the terminal starting with
4583 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004584 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004585 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004586 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004587 * Also eat other possible responses to t_RV, rxvt returns
4588 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4589 * mrxvt has been reported to have "+" in the version. Assume
4590 * the escape sequence ends with a letter or one of "{|}~".
4591 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004592 * - Cursor position report: <Esc>[{row};{col}R
4593 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004594 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004595 *
4596 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004597 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004598 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004599
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004600 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004601 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004602 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004603 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004605 int col = 0;
4606 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004607 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004610 for (i = 2 + (tp[0] != CSI); i < len
4611 && !(tp[i] >= '{' && tp[i] <= '~')
4612 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004613 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004614 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004615 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004616 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004619 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004620 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004621 return -1;
4622 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004623 if (extra > 0)
4624 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004625
4626 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4627 * u7_status is not "sent", it may be from a previous Vim that
4628 * just exited. But not for <S-F3>, it sends something
4629 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004630 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004631 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004632 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004633 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004634 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004635
Bram Moolenaarb255b902018-04-24 21:40:10 +02004636 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004637 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004638 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004639 if (col == 2)
4640 aw = "single";
4641 else if (col == 3)
4642 aw = "double";
4643 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4644 {
4645 /* Setting the option causes a screen redraw. Do
4646 * that right away if possible, keeping any
4647 * messages. */
4648 set_option_value((char_u *)"ambw", 0L,
4649 (char_u *)aw, 0);
4650# ifdef DEBUG_TERMRESPONSE
4651 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004652 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004653
Bram Moolenaarb255b902018-04-24 21:40:10 +02004654 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004655 }
4656# else
4657 redraw_asap(CLEAR);
4658# endif
4659 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004660 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004661 key_name[0] = (int)KS_EXTRA;
4662 key_name[1] = (int)KE_IGNORE;
4663 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004664# ifdef FEAT_EVAL
4665 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4666# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004669 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4670 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004672 int version = col;
4673
Bram Moolenaarb255b902018-04-24 21:40:10 +02004674 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004675 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004676 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /* If this code starts with CSI, you can bet that the
4679 * terminal uses 8-bit codes. */
4680 if (tp[0] == CSI)
4681 switch_to_8bit();
4682
4683 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004684 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 * Ignore it for when the user has set 'term' to xterm,
4686 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004687 if (version > 20000)
4688 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004690 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004692 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004693# ifdef FEAT_MOUSE_SGR
4694 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004695 int is_mintty = FALSE;
4696
4697 // mintty 2.9.5 sends 77;20905;0c.
4698 // (77 is ASCII 'M' for mintty.)
4699 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4700 is_mintty = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004701# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004704 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004706 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 check_for_codes = TRUE;
4708 need_gather = TRUE;
4709 req_codes_from_term();
4710 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004711
4712 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004713 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004714 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004715 {
4716 /* If run from Vim $COLORS is set to the number of
4717 * colors the terminal supports. Otherwise assume
4718 * 256, libvterm supports even more. */
4719 if (mch_getenv((char_u *)"COLORS") == NULL)
4720 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004721# ifdef FEAT_MOUSE_SGR
4722 /* Libvterm can handle SGR mouse reporting. */
4723 if (!option_was_set((char_u *)"ttym"))
4724 set_option_value((char_u *)"ttym", 0L,
4725 (char_u *)"sgr", 0);
4726# endif
4727 }
4728
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004729 if (version == 95)
4730 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004731 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004732 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4733 {
4734 is_not_xterm = TRUE;
4735 is_mac_terminal = TRUE;
4736 }
4737# ifdef FEAT_MOUSE_SGR
Bram Moolenaare330ef42018-07-06 23:11:40 +02004738 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004739 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4740 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004741 else
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004742# endif
Bram Moolenaare330ef42018-07-06 23:11:40 +02004743 // old iTerm2 sends 0;95;
4744 if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
4745 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004746 }
4747
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004748 /* Only set 'ttymouse' automatically if it was not set
4749 * by the user already. */
4750 if (!option_was_set((char_u *)"ttym"))
4751 {
4752# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004753 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004754 * Terminal.app, iTerm2 and mintty. */
4755 if (version >= 277 || is_iterm2 || is_mac_terminal
4756 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004757 set_option_value((char_u *)"ttym", 0L,
4758 (char_u *)"sgr", 0);
4759 else
4760# endif
4761 /* if xterm version >= 95 use mouse dragging */
4762 if (version >= 95)
4763 set_option_value((char_u *)"ttym", 0L,
4764 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004765 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004766
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004767 /* Detect terminals that set $TERM to something like
4768 * "xterm-256colors" but are not fully xterm
4769 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004770
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004771 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004772 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004773 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004774 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004775 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004776 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004777 is_not_xterm = TRUE;
4778
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004779 /* PuTTY sends 0;136;0
4780 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004781 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004782 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004783 is_not_xterm = TRUE;
4784
4785 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004786 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004787 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004788 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004789
Bram Moolenaar668324e2018-06-30 17:09:26 +02004790 // Xterm first responded to this request at patch level
4791 // 95, so assume anything below 95 is not xterm.
4792 if (version < 95)
4793 is_not_xterm = TRUE;
4794
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004795 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004796 * set. Only supported properly by xterm since version
4797 * 279 (otherwise it returns 0x18).
4798 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004799 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004800 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004801 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004802 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004803 && *T_CSH != NUL
4804 && *T_CRS != NUL)
4805 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004806 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004807 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004808 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004809 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004810 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004811
4812 /* Only request the cursor blink mode if t_RC set. Not
4813 * for Gnome terminal, it can't handle t_RC, it
4814 * echoes the characters to the screen. */
4815 if (rbm_status == STATUS_GET
4816 && !is_not_xterm
4817 && *T_CRC != NUL)
4818 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004819 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004820 out_str(T_CRC);
4821 rbm_status = STATUS_SENT;
4822 need_flush = TRUE;
4823 }
4824
4825 if (need_flush)
4826 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004828 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004830 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 apply_autocmds(EVENT_TERMRESPONSE,
4833 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 key_name[0] = (int)KS_EXTRA;
4835 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004837
Bram Moolenaar4db25542017-08-28 22:43:05 +02004838 /* Check blinking cursor from xterm:
4839 * {lead}?12;1$y set
4840 * {lead}?12;2$y not set
4841 *
4842 * {lead} can be <Esc>[ or CSI
4843 */
4844 else if (rbm_status == STATUS_SENT
4845 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4846 && i == j + 6
4847 && tp[j + 1] == '1'
4848 && tp[j + 2] == '2'
4849 && tp[j + 3] == ';'
4850 && tp[i - 1] == '$'
4851 && tp[i] == 'y')
4852 {
4853 initial_cursor_blink = (tp[j + 4] == '1');
4854 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004855 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004856 key_name[0] = (int)KS_EXTRA;
4857 key_name[1] = (int)KE_IGNORE;
4858 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004859# ifdef FEAT_EVAL
4860 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4861# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004862 }
4863
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004864 /*
4865 * Check for a window position response from the terminal:
4866 * {lead}3;{x}:{y}t
4867 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004868 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004869 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4870 || (len >= 3 && tp[0] == CSI))
4871 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4872 && tp[j + 1] == ';')
4873 {
4874 j += 2;
4875 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4876 ;
4877 if (i < len && tp[i] == ';')
4878 {
4879 winpos_x = atoi((char *)tp + j);
4880 j = i + 1;
4881 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4882 ;
4883 if (i < len && tp[i] == 't')
4884 {
4885 winpos_y = atoi((char *)tp + j);
4886 /* got finished code: consume it */
4887 key_name[0] = (int)KS_EXTRA;
4888 key_name[1] = (int)KE_IGNORE;
4889 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004890
4891 if (--did_request_winpos <= 0)
4892 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004893 }
4894 }
4895 if (i == len)
4896 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004897 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004898 return -1;
4899 }
4900 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004901 }
4902
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004903 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004904 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004905 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004906 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004907 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004908 * {lead} can be <Esc>] or OSC
4909 * {tail} can be '\007', <Esc>\ or STERM.
4910 *
4911 * Consume any code that starts with "{lead}11;", it's also
4912 * possible that "rgba" is following.
4913 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004914 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004915 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4916 || tp[0] == OSC))
4917 {
4918 j = 1 + (tp[0] == ESC);
4919 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004920 || (argp[1] != '1' && argp[1] != '0')
4921 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004922 i = 0; /* no match */
4923 else
4924 for (i = j; i < len; ++i)
4925 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4926 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004927 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004928 int is_bg = argp[1] == '1';
4929
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004930 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004931 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004932 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004933#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004934 int rval = hexhex2nr(tp + j + 7);
4935 int gval = hexhex2nr(tp + j + 12);
4936 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004937#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004938 if (is_bg)
4939 {
4940 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004941 + tp[j+17]) ? "light" : "dark";
4942
Bram Moolenaarb255b902018-04-24 21:40:10 +02004943 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004944 rbg_status = STATUS_GOT;
4945#ifdef FEAT_TERMINAL
4946 bg_r = rval;
4947 bg_g = gval;
4948 bg_b = bval;
4949#endif
4950 if (!option_was_set((char_u *)"bg")
4951 && STRCMP(p_bg, newval) != 0)
4952 {
4953 /* value differs, apply it */
4954 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004955 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004956 reset_option_was_set((char_u *)"bg");
4957 redraw_asap(CLEAR);
4958 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004959 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004960#ifdef FEAT_TERMINAL
4961 else
4962 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004963 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004964 rfg_status = STATUS_GOT;
4965 fg_r = rval;
4966 fg_g = gval;
4967 fg_b = bval;
4968 }
4969#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004970 }
4971
4972 /* got finished code: consume it */
4973 key_name[0] = (int)KS_EXTRA;
4974 key_name[1] = (int)KE_IGNORE;
4975 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004976# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004977 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4978 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004979# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004980 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004981 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004982 if (i == len)
4983 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004984 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004985 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004989 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004990 * {lead}{flag}+r<hex bytes><{tail}
4991 *
4992 * {lead} can be <Esc>P or DCS
4993 * {flag} can be '0' or '1'
4994 * {tail} can be Esc>\ or STERM
4995 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004996 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004997 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004998 *
4999 * {lead} can be <Esc>P or DCS
5000 * {tail} can be Esc>\ or STERM
5001 *
5002 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005003 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02005004 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005005 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 || tp[0] == DCS))
5007 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005008 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005009 if (len < j + 3)
5010 i = len; /* need more chars */
5011 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005012 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005013 else if (argp[1] == '+')
5014 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005015 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005016 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005017 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 || tp[i] == STERM)
5019 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005020 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 got_code_from_term(tp + j, i);
5022 key_name[0] = (int)KS_EXTRA;
5023 key_name[1] = (int)KE_IGNORE;
5024 slen = i + 1 + (tp[i] == ESC);
5025 break;
5026 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005027 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005028 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005029 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005030 /* Probably the cursor shape response. Make sure that "i"
5031 * is equal to "len" when there are not sufficient
5032 * characters. */
5033 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005034 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005035 if (i - j == 3 && !isdigit(tp[i]))
5036 break;
5037 if (i - j == 4 && tp[i] != ' ')
5038 break;
5039 if (i - j == 5 && tp[i] != 'q')
5040 break;
5041 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5042 break;
5043 if ((i - j == 6 && tp[i] == STERM)
5044 || (i - j == 7 && tp[i] == '\\'))
5045 {
5046 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005047
Bram Moolenaar590ec872018-03-02 20:58:42 +01005048 /* 0, 1 = block blink, 2 = block
5049 * 3 = underline blink, 4 = underline
5050 * 5 = vertical bar blink, 6 = vertical bar */
5051 number = number == 0 ? 1 : number;
5052 initial_cursor_shape = (number + 1) / 2;
5053 /* The blink flag is actually inverted, compared to
5054 * the value set with T_SH. */
5055 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005056 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01005057 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005058 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005059
Bram Moolenaar590ec872018-03-02 20:58:42 +01005060 key_name[0] = (int)KS_EXTRA;
5061 key_name[1] = (int)KE_IGNORE;
5062 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005063# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005064 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005065# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005066 break;
5067 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005068 }
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005072 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005073 /* These codes arrive many together, each code can be
5074 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005075 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005076 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 }
5080#endif
5081
5082 if (key_name[0] == NUL)
5083 continue; /* No match at this position, try next one */
5084
5085 /* We only get here when we have a complete termcode match */
5086
5087#ifdef FEAT_MOUSE
5088# ifdef FEAT_GUI
5089 /*
5090 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5091 * so that we know which window to scroll later.
5092 */
5093 if (gui.in_use
5094 && key_name[0] == (int)KS_EXTRA
5095 && (key_name[1] == (int)KE_X1MOUSE
5096 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005097 || key_name[1] == (int)KE_MOUSELEFT
5098 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 || key_name[1] == (int)KE_MOUSEDOWN
5100 || key_name[1] == (int)KE_MOUSEUP))
5101 {
5102 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5103 if (num_bytes == -1) /* not enough coordinates */
5104 return -1;
5105 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5106 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5107 slen += num_bytes;
5108 }
5109 else
5110# endif
5111 /*
5112 * If it is a mouse click, get the coordinates.
5113 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005114 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005116 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117# endif
5118# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005119 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120# endif
5121# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005122 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123# endif
5124# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005125 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005127# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005128 || key_name[0] == KS_URXVT_MOUSE
5129# endif
5130# ifdef FEAT_MOUSE_SGR
5131 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005132 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 )
5135 {
5136 is_click = is_drag = FALSE;
5137
Bram Moolenaar864207d2008-06-24 22:14:38 +00005138# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5139 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (key_name[0] == (int)KS_MOUSE)
5141 {
5142 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005143 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 * s == encoded button state:
5145 * 0x20 = left button down
5146 * 0x21 = middle button down
5147 * 0x22 = right button down
5148 * 0x23 = any button release
5149 * 0x60 = button 4 down (scroll wheel down)
5150 * 0x61 = button 5 down (scroll wheel up)
5151 * add 0x04 for SHIFT
5152 * add 0x08 for ALT
5153 * add 0x10 for CTRL
5154 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005155 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5156 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 * c == column + ' ' + 1 == column + 33
5158 * r == row + ' ' + 1 == row + 33
5159 *
5160 * The coordinates are passed on through global variables.
5161 * Ugly, but this avoids trouble with mouse clicks at an
5162 * unexpected moment and allows for mapping them.
5163 */
5164 for (;;)
5165 {
5166#ifdef FEAT_GUI
5167 if (gui.in_use)
5168 {
5169 /* GUI uses more bits for columns > 223 */
5170 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5171 if (num_bytes == -1) /* not enough coordinates */
5172 return -1;
5173 mouse_code = bytes[0];
5174 mouse_col = 128 * (bytes[1] - ' ' - 1)
5175 + bytes[2] - ' ' - 1;
5176 mouse_row = 128 * (bytes[3] - ' ' - 1)
5177 + bytes[4] - ' ' - 1;
5178 }
5179 else
5180#endif
5181 {
5182 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5183 if (num_bytes == -1) /* not enough coordinates */
5184 return -1;
5185 mouse_code = bytes[0];
5186 mouse_col = bytes[1] - ' ' - 1;
5187 mouse_row = bytes[2] - ' ' - 1;
5188 }
5189 slen += num_bytes;
5190
5191 /* If the following bytes is also a mouse code and it has
5192 * the same code, dump this one and get the next. This
5193 * makes dragging a whole lot faster. */
5194#ifdef FEAT_GUI
5195 if (gui.in_use)
5196 j = 3;
5197 else
5198#endif
5199 j = termcodes[idx].len;
5200 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5201 && tp[slen + j] == mouse_code
5202 && tp[slen + j + 1] != NUL
5203 && tp[slen + j + 2] != NUL
5204#ifdef FEAT_GUI
5205 && (!gui.in_use
5206 || (tp[slen + j + 3] != NUL
5207 && tp[slen + j + 4] != NUL))
5208#endif
5209 )
5210 slen += j;
5211 else
5212 break;
5213 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005216# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5217 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005218 || key_name[0] == KS_SGR_MOUSE
5219 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005220 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005221 /* URXVT 1015 mouse reporting mode:
5222 * Almost identical to xterm mouse mode, except the values
5223 * are decimal instead of bytes.
5224 *
5225 * \033[%d;%d;%dM
5226 * ^-- row
5227 * ^----- column
5228 * ^-------- code
5229 *
5230 * SGR 1006 mouse reporting mode:
5231 * Almost identical to xterm mouse mode, except the values
5232 * are decimal instead of bytes.
5233 *
5234 * \033[<%d;%d;%dM
5235 * ^-- row
5236 * ^----- column
5237 * ^-------- code
5238 *
5239 * \033[<%d;%d;%dm : mouse release event
5240 * ^-- row
5241 * ^----- column
5242 * ^-------- code
5243 */
5244 p = modifiers_start;
5245 if (p == NULL)
5246 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005247
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005248 mouse_code = getdigits(&p);
5249 if (*p++ != ';')
5250 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005251
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005252 /* when mouse reporting is SGR, add 32 to mouse code */
5253 if (key_name[0] == KS_SGR_MOUSE
5254 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5255 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005256
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005257 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5258 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005259
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005260 mouse_col = getdigits(&p) - 1;
5261 if (*p++ != ';')
5262 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005263
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005264 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005265
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005266 /* The modifiers were the mouse coordinates, not the
5267 * modifier keys (alt/shift/ctrl/meta) state. */
5268 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005269 }
5270# endif
5271
5272 if (key_name[0] == (int)KS_MOUSE
5273#ifdef FEAT_MOUSE_URXVT
5274 || key_name[0] == (int)KS_URXVT_MOUSE
5275#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005276#ifdef FEAT_MOUSE_SGR
5277 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005278 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005279#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005280 )
5281 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005282# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 /*
5284 * Handle mouse events.
5285 * Recognize the xterm mouse wheel, but not in the GUI, the
5286 * Linux console with GPM and the MS-DOS or Win32 console
5287 * (multi-clicks use >= 0x60).
5288 */
5289 if (mouse_code >= MOUSEWHEEL_LOW
5290# ifdef FEAT_GUI
5291 && !gui.in_use
5292# endif
5293# ifdef FEAT_MOUSE_GPM
5294 && gpm_flag == 0
5295# endif
5296 )
5297 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005298# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5299 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5300 /* mouse-move event, using MOUSE_DRAG works */
5301 mouse_code = MOUSE_DRAG;
5302 else
5303# endif
5304 /* Keep the mouse_code before it's changed, so that we
5305 * remember that it was a mouse wheel click. */
5306 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308# ifdef FEAT_MOUSE_XTERM
5309 else if (held_button == MOUSE_RELEASE
5310# ifdef FEAT_GUI
5311 && !gui.in_use
5312# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005313 && (mouse_code == 0x23 || mouse_code == 0x24
5314 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005316 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5317 * And 0x40 and 0x41 are used by some xterm emulator. */
5318 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5319 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321# endif
5322
5323# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5324 else if (use_xterm_mouse() > 1)
5325 {
5326 if (mouse_code & MOUSE_DRAG_XTERM)
5327 mouse_code |= MOUSE_DRAG;
5328 }
5329# endif
5330# ifdef FEAT_XCLIPBOARD
5331 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5332 {
5333 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5334 stop_xterm_trace();
5335 else
5336 start_xterm_trace(mouse_code);
5337 }
5338# endif
5339# endif
5340 }
5341# endif /* !UNIX || FEAT_MOUSE_XTERM */
5342# ifdef FEAT_MOUSE_NET
5343 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5344 {
5345 int mc, mr;
5346
5347 /* expect a rather limited sequence like: balancing {
5348 * \033}6,45\r
5349 * '6' is the row, 45 is the column
5350 */
5351 p = tp + slen;
5352 mr = getdigits(&p);
5353 if (*p++ != ',')
5354 return -1;
5355 mc = getdigits(&p);
5356 if (*p++ != '\r')
5357 return -1;
5358
5359 mouse_col = mc - 1;
5360 mouse_row = mr - 1;
5361 mouse_code = MOUSE_LEFT;
5362 slen += (int)(p - (tp + slen));
5363 }
5364# endif /* FEAT_MOUSE_NET */
5365# ifdef FEAT_MOUSE_JSB
5366 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5367 {
5368 int mult, val, iter, button, status;
5369
5370 /* JSBTERM Input Model
5371 * \033[0~zw uniq escape sequence
5372 * (L-x) Left button pressed - not pressed x not reporting
5373 * (M-x) Middle button pressed - not pressed x not reporting
5374 * (R-x) Right button pressed - not pressed x not reporting
5375 * (SDmdu) Single , Double click, m mouse move d button down
5376 * u button up
5377 * ### X cursor position padded to 3 digits
5378 * ### Y cursor position padded to 3 digits
5379 * (s-x) SHIFT key pressed - not pressed x not reporting
5380 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005381 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 */
5383
5384 p = tp + slen;
5385 button = mouse_code = 0;
5386 switch (*p++)
5387 {
5388 case 'L': button = 1; break;
5389 case '-': break;
5390 case 'x': break; /* ignore sequence */
5391 default: return -1; /* Unknown Result */
5392 }
5393 switch (*p++)
5394 {
5395 case 'M': button |= 2; break;
5396 case '-': break;
5397 case 'x': break; /* ignore sequence */
5398 default: return -1; /* Unknown Result */
5399 }
5400 switch (*p++)
5401 {
5402 case 'R': button |= 4; break;
5403 case '-': break;
5404 case 'x': break; /* ignore sequence */
5405 default: return -1; /* Unknown Result */
5406 }
5407 status = *p++;
5408 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5409 mult /= 10, p++)
5410 if (*p >= '0' && *p <= '9')
5411 val += (*p - '0') * mult;
5412 else
5413 return -1;
5414 mouse_col = val;
5415 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5416 mult /= 10, p++)
5417 if (*p >= '0' && *p <= '9')
5418 val += (*p - '0') * mult;
5419 else
5420 return -1;
5421 mouse_row = val;
5422 switch (*p++)
5423 {
5424 case 's': button |= 8; break; /* SHIFT key Pressed */
5425 case '-': break; /* Not Pressed */
5426 case 'x': break; /* Not Reporting */
5427 default: return -1; /* Unknown Result */
5428 }
5429 switch (*p++)
5430 {
5431 case 'c': button |= 16; break; /* CTRL key Pressed */
5432 case '-': break; /* Not Pressed */
5433 case 'x': break; /* Not Reporting */
5434 default: return -1; /* Unknown Result */
5435 }
5436 if (*p++ != '\033')
5437 return -1;
5438 if (*p++ != '\\')
5439 return -1;
5440 switch (status)
5441 {
5442 case 'D': /* Double Click */
5443 case 'S': /* Single Click */
5444 if (button & 1) mouse_code |= MOUSE_LEFT;
5445 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5446 if (button & 4) mouse_code |= MOUSE_RIGHT;
5447 if (button & 8) mouse_code |= MOUSE_SHIFT;
5448 if (button & 16) mouse_code |= MOUSE_CTRL;
5449 break;
5450 case 'm': /* Mouse move */
5451 if (button & 1) mouse_code |= MOUSE_LEFT;
5452 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5453 if (button & 4) mouse_code |= MOUSE_RIGHT;
5454 if (button & 8) mouse_code |= MOUSE_SHIFT;
5455 if (button & 16) mouse_code |= MOUSE_CTRL;
5456 if ((button & 7) != 0)
5457 {
5458 held_button = mouse_code;
5459 mouse_code |= MOUSE_DRAG;
5460 }
5461 is_drag = TRUE;
5462 showmode();
5463 break;
5464 case 'd': /* Button Down */
5465 if (button & 1) mouse_code |= MOUSE_LEFT;
5466 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5467 if (button & 4) mouse_code |= MOUSE_RIGHT;
5468 if (button & 8) mouse_code |= MOUSE_SHIFT;
5469 if (button & 16) mouse_code |= MOUSE_CTRL;
5470 break;
5471 case 'u': /* Button Up */
5472 if (button & 1)
5473 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5474 if (button & 2)
5475 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5476 if (button & 4)
5477 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5478 if (button & 8)
5479 mouse_code |= MOUSE_SHIFT;
5480 if (button & 16)
5481 mouse_code |= MOUSE_CTRL;
5482 break;
5483 default: return -1; /* Unknown Result */
5484 }
5485
5486 slen += (p - (tp + slen));
5487 }
5488# endif /* FEAT_MOUSE_JSB */
5489# ifdef FEAT_MOUSE_DEC
5490 if (key_name[0] == (int)KS_DEC_MOUSE)
5491 {
5492 /* The DEC Locator Input Model
5493 * Netterm delivers the code sequence:
5494 * \033[2;4;24;80&w (left button down)
5495 * \033[3;0;24;80&w (left button up)
5496 * \033[6;1;24;80&w (right button down)
5497 * \033[7;0;24;80&w (right button up)
5498 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5499 * Pe is the event code
5500 * Pb is the button code
5501 * Pr is the row coordinate
5502 * Pc is the column coordinate
5503 * Pp is the third coordinate (page number)
5504 * Pe, the event code indicates what event caused this report
5505 * The following event codes are defined:
5506 * 0 - request, the terminal received an explicit request
5507 * for a locator report, but the locator is unavailable
5508 * 1 - request, the terminal received an explicit request
5509 * for a locator report
5510 * 2 - left button down
5511 * 3 - left button up
5512 * 4 - middle button down
5513 * 5 - middle button up
5514 * 6 - right button down
5515 * 7 - right button up
5516 * 8 - fourth button down
5517 * 9 - fourth button up
5518 * 10 - locator outside filter rectangle
5519 * Pb, the button code, ASCII decimal 0-15 indicating which
5520 * buttons are down if any. The state of the four buttons
5521 * on the locator correspond to the low four bits of the
5522 * decimal value,
5523 * "1" means button depressed
5524 * 0 - no buttons down,
5525 * 1 - right,
5526 * 2 - middle,
5527 * 4 - left,
5528 * 8 - fourth
5529 * Pr is the row coordinate of the locator position in the page,
5530 * encoded as an ASCII decimal value.
5531 * If Pr is omitted, the locator position is undefined
5532 * (outside the terminal window for example).
5533 * Pc is the column coordinate of the locator position in the
5534 * page, encoded as an ASCII decimal value.
5535 * If Pc is omitted, the locator position is undefined
5536 * (outside the terminal window for example).
5537 * Pp is the page coordinate of the locator position
5538 * encoded as an ASCII decimal value.
5539 * The page coordinate may be omitted if the locator is on
5540 * page one (the default). We ignore it anyway.
5541 */
5542 int Pe, Pb, Pr, Pc;
5543
5544 p = tp + slen;
5545
5546 /* get event status */
5547 Pe = getdigits(&p);
5548 if (*p++ != ';')
5549 return -1;
5550
5551 /* get button status */
5552 Pb = getdigits(&p);
5553 if (*p++ != ';')
5554 return -1;
5555
5556 /* get row status */
5557 Pr = getdigits(&p);
5558 if (*p++ != ';')
5559 return -1;
5560
5561 /* get column status */
5562 Pc = getdigits(&p);
5563
5564 /* the page parameter is optional */
5565 if (*p == ';')
5566 {
5567 p++;
5568 (void)getdigits(&p);
5569 }
5570 if (*p++ != '&')
5571 return -1;
5572 if (*p++ != 'w')
5573 return -1;
5574
5575 mouse_code = 0;
5576 switch (Pe)
5577 {
5578 case 0: return -1; /* position request while unavailable */
5579 case 1: /* a response to a locator position request includes
5580 the status of all buttons */
5581 Pb &= 7; /* mask off and ignore fourth button */
5582 if (Pb & 4)
5583 mouse_code = MOUSE_LEFT;
5584 if (Pb & 2)
5585 mouse_code = MOUSE_MIDDLE;
5586 if (Pb & 1)
5587 mouse_code = MOUSE_RIGHT;
5588 if (Pb)
5589 {
5590 held_button = mouse_code;
5591 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005592 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594 is_drag = TRUE;
5595 showmode();
5596 break;
5597 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005598 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 break;
5600 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5601 break;
5602 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005603 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 break;
5605 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5606 break;
5607 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005608 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 break;
5610 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5611 break;
5612 case 8: return -1; /* fourth button down */
5613 case 9: return -1; /* fourth button up */
5614 case 10: return -1; /* mouse outside of filter rectangle */
5615 default: return -1; /* should never occur */
5616 }
5617
5618 mouse_col = Pc - 1;
5619 mouse_row = Pr - 1;
5620
5621 slen += (int)(p - (tp + slen));
5622 }
5623# endif /* FEAT_MOUSE_DEC */
5624# ifdef FEAT_MOUSE_PTERM
5625 if (key_name[0] == (int)KS_PTERM_MOUSE)
5626 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005627 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 p = tp + slen;
5630
5631 action = getdigits(&p);
5632 if (*p++ != ';')
5633 return -1;
5634
5635 mouse_row = getdigits(&p);
5636 if (*p++ != ';')
5637 return -1;
5638 mouse_col = getdigits(&p);
5639 if (*p++ != ';')
5640 return -1;
5641
5642 button = getdigits(&p);
5643 mouse_code = 0;
5644
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005645 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
5647 case 4: mouse_code = MOUSE_LEFT; break;
5648 case 1: mouse_code = MOUSE_RIGHT; break;
5649 case 2: mouse_code = MOUSE_MIDDLE; break;
5650 default: return -1;
5651 }
5652
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005653 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 case 31: /* Initial press */
5656 if (*p++ != ';')
5657 return -1;
5658
5659 num_clicks = getdigits(&p); /* Not used */
5660 break;
5661
5662 case 32: /* Release */
5663 mouse_code |= MOUSE_RELEASE;
5664 break;
5665
5666 case 33: /* Drag */
5667 held_button = mouse_code;
5668 mouse_code |= MOUSE_DRAG;
5669 break;
5670
5671 default:
5672 return -1;
5673 }
5674
5675 if (*p++ != 't')
5676 return -1;
5677
5678 slen += (p - (tp + slen));
5679 }
5680# endif /* FEAT_MOUSE_PTERM */
5681
5682 /* Interpret the mouse code */
5683 current_button = (mouse_code & MOUSE_CLICK_MASK);
5684 if (current_button == MOUSE_RELEASE
5685# ifdef FEAT_MOUSE_XTERM
5686 && wheel_code == 0
5687# endif
5688 )
5689 {
5690 /*
5691 * If we get a mouse drag or release event when
5692 * there is no mouse button held down (held_button ==
5693 * MOUSE_RELEASE), produce a K_IGNORE below.
5694 * (can happen when you hold down two buttons
5695 * and then let them go, or click in the menu bar, but not
5696 * on a menu, and drag into the text).
5697 */
5698 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5699 is_drag = TRUE;
5700 current_button = held_button;
5701 }
5702 else if (wheel_code == 0)
5703 {
5704# ifdef CHECK_DOUBLE_CLICK
5705# ifdef FEAT_MOUSE_GPM
5706# ifdef FEAT_GUI
5707 /*
5708 * Only for Unix, when GUI or gpm is not active, we handle
5709 * multi-clicks here.
5710 */
5711 if (gpm_flag == 0 && !gui.in_use)
5712# else
5713 if (gpm_flag == 0)
5714# endif
5715# else
5716# ifdef FEAT_GUI
5717 if (!gui.in_use)
5718# endif
5719# endif
5720 {
5721 /*
5722 * Compute the time elapsed since the previous mouse click.
5723 */
5724 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005725 if (orig_mouse_time.tv_sec == 0)
5726 {
5727 /*
5728 * Avoid computing the difference between mouse_time
5729 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005730 * difference would be huge and would cause
5731 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005732 */
5733 timediff = p_mouset;
5734 }
5735 else
5736 {
5737 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005738 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005739 if (timediff < 0)
5740 --orig_mouse_time.tv_sec;
5741 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005742 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 orig_mouse_time = mouse_time;
5745 if (mouse_code == orig_mouse_code
5746 && timediff < p_mouset
5747 && orig_num_clicks != 4
5748 && orig_mouse_col == mouse_col
5749 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005750 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005752 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005754 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005755 /* Double click in tab pages line also works
5756 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005757 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005758 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ++orig_num_clicks;
5760 else
5761 orig_num_clicks = 1;
5762 orig_mouse_col = mouse_col;
5763 orig_mouse_row = mouse_row;
5764 orig_topline = curwin->w_topline;
5765#ifdef FEAT_DIFF
5766 orig_topfill = curwin->w_topfill;
5767#endif
5768 }
5769# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5770 else
5771 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5772# endif
5773# else
5774 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5775# endif
5776 is_click = TRUE;
5777 orig_mouse_code = mouse_code;
5778 }
5779 if (!is_drag)
5780 held_button = mouse_code & MOUSE_CLICK_MASK;
5781
5782 /*
5783 * Translate the actual mouse event into a pseudo mouse event.
5784 * First work out what modifiers are to be used.
5785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (orig_mouse_code & MOUSE_SHIFT)
5787 modifiers |= MOD_MASK_SHIFT;
5788 if (orig_mouse_code & MOUSE_CTRL)
5789 modifiers |= MOD_MASK_CTRL;
5790 if (orig_mouse_code & MOUSE_ALT)
5791 modifiers |= MOD_MASK_ALT;
5792 if (orig_num_clicks == 2)
5793 modifiers |= MOD_MASK_2CLICK;
5794 else if (orig_num_clicks == 3)
5795 modifiers |= MOD_MASK_3CLICK;
5796 else if (orig_num_clicks == 4)
5797 modifiers |= MOD_MASK_4CLICK;
5798
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005799 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5800 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005802 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005803 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005804 {
5805 if (wheel_code & MOUSE_CTRL)
5806 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005807 if (wheel_code & MOUSE_ALT)
5808 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 key_name[1] = (wheel_code & 1)
5810 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005811 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 else
5814 key_name[1] = get_pseudo_mouse_code(current_button,
5815 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005816
5817 /* Make sure the mouse position is valid. Some terminals may
5818 * return weird values. */
5819 if (mouse_col >= Columns)
5820 mouse_col = Columns - 1;
5821 if (mouse_row >= Rows)
5822 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
5824#endif /* FEAT_MOUSE */
5825
5826#ifdef FEAT_GUI
5827 /*
5828 * If using the GUI, then we get menu and scrollbar events.
5829 *
5830 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5831 * four bytes which are to be taken as a pointer to the vimmenu_T
5832 * structure.
5833 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005834 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005835 * is one byte with the tab index.
5836 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5838 * by one byte representing the scrollbar number, and then four bytes
5839 * representing a long_u which is the new value of the scrollbar.
5840 *
5841 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5842 * KE_FILLER followed by four bytes representing a long_u which is the
5843 * new value of the scrollbar.
5844 */
5845# ifdef FEAT_MENU
5846 else if (key_name[0] == (int)KS_MENU)
5847 {
5848 long_u val;
5849
5850 num_bytes = get_long_from_buf(tp + slen, &val);
5851 if (num_bytes == -1)
5852 return -1;
5853 current_menu = (vimmenu_T *)val;
5854 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005855
5856 /* The menu may have been deleted right after it was used, check
5857 * for that. */
5858 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5859 {
5860 key_name[0] = KS_EXTRA;
5861 key_name[1] = (int)KE_IGNORE;
5862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 }
5864# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005865# ifdef FEAT_GUI_TABLINE
5866 else if (key_name[0] == (int)KS_TABLINE)
5867 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005868 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005869 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5870 if (num_bytes == -1)
5871 return -1;
5872 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005873 if (current_tab == 255) /* -1 in a byte gives 255 */
5874 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005875 slen += num_bytes;
5876 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005877 else if (key_name[0] == (int)KS_TABMENU)
5878 {
5879 /* Selecting tabline tab or using its menu. */
5880 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5881 if (num_bytes == -1)
5882 return -1;
5883 current_tab = (int)bytes[0];
5884 current_tabmenu = (int)bytes[1];
5885 slen += num_bytes;
5886 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888# ifndef USE_ON_FLY_SCROLL
5889 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5890 {
5891 long_u val;
5892
5893 /* Get the last scrollbar event in the queue of the same type */
5894 j = 0;
5895 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5896 && tp[j + 2] != NUL; ++i)
5897 {
5898 j += 3;
5899 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5900 if (num_bytes == -1)
5901 break;
5902 if (i == 0)
5903 current_scrollbar = (int)bytes[0];
5904 else if (current_scrollbar != (int)bytes[0])
5905 break;
5906 j += num_bytes;
5907 num_bytes = get_long_from_buf(tp + j, &val);
5908 if (num_bytes == -1)
5909 break;
5910 scrollbar_value = val;
5911 j += num_bytes;
5912 slen = j;
5913 }
5914 if (i == 0) /* not enough characters to make one */
5915 return -1;
5916 }
5917 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5918 {
5919 long_u val;
5920
5921 /* Get the last horiz. scrollbar event in the queue */
5922 j = 0;
5923 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5924 && tp[j + 2] != NUL; ++i)
5925 {
5926 j += 3;
5927 num_bytes = get_long_from_buf(tp + j, &val);
5928 if (num_bytes == -1)
5929 break;
5930 scrollbar_value = val;
5931 j += num_bytes;
5932 slen = j;
5933 }
5934 if (i == 0) /* not enough characters to make one */
5935 return -1;
5936 }
5937# endif /* !USE_ON_FLY_SCROLL */
5938#endif /* FEAT_GUI */
5939
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005940 /*
5941 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5942 */
5943 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5944
5945 /*
5946 * Add any modifier codes to our string.
5947 */
5948 new_slen = 0; /* Length of what will replace the termcode */
5949 if (modifiers != 0)
5950 {
5951 /* Some keys have the modifier included. Need to handle that here
5952 * to make mappings work. */
5953 key = simplify_key(key, &modifiers);
5954 if (modifiers != 0)
5955 {
5956 string[new_slen++] = K_SPECIAL;
5957 string[new_slen++] = (int)KS_MODIFIER;
5958 string[new_slen++] = modifiers;
5959 }
5960 }
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005963 key_name[0] = KEY2TERMCAP0(key);
5964 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005966 {
5967 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005968 if (has_mbyte)
5969 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5970 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005971 string[new_slen++] = key_name[1];
5972 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005973 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5974 && key_name[1] == KE_IGNORE)
5975 {
5976 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5977 * to indicate what happened. */
5978 retval = KEYLEN_REMOVED;
5979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 else
5981 {
5982 string[new_slen++] = K_SPECIAL;
5983 string[new_slen++] = key_name[0];
5984 string[new_slen++] = key_name[1];
5985 }
5986 string[new_slen] = NUL;
5987 extra = new_slen - slen;
5988 if (buf == NULL)
5989 {
5990 if (extra < 0)
5991 /* remove matched chars, taking care of noremap */
5992 del_typebuf(-extra, offset);
5993 else if (extra > 0)
5994 /* insert the extra space we need */
5995 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5996
5997 /*
5998 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5999 * typebuf.tb_buf[]!
6000 */
6001 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
6002 (size_t)new_slen);
6003 }
6004 else
6005 {
6006 if (extra < 0)
6007 /* remove matched characters */
6008 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006009 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006011 {
6012 /* Insert the extra space we need. If there is insufficient
6013 * space return -1. */
6014 if (*buflen + extra + new_slen >= bufsize)
6015 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006017 (size_t)(*buflen - offset));
6018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006020 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006022 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024
Bram Moolenaar2951b772013-07-03 12:45:31 +02006025#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006026 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006027#endif
6028
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 return 0; /* no match found */
6030}
6031
Bram Moolenaar9377df32017-10-15 13:22:01 +02006032#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006033/*
6034 * Get the text foreground color, if known.
6035 */
6036 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006037term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006038{
6039 if (rfg_status == STATUS_GOT)
6040 {
6041 *r = fg_r;
6042 *g = fg_g;
6043 *b = fg_b;
6044 }
6045}
6046
6047/*
6048 * Get the text background color, if known.
6049 */
6050 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006051term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006052{
6053 if (rbg_status == STATUS_GOT)
6054 {
6055 *r = bg_r;
6056 *g = bg_g;
6057 *b = bg_b;
6058 }
6059}
6060#endif
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062/*
6063 * Replace any terminal code strings in from[] with the equivalent internal
6064 * vim representation. This is used for the "from" and "to" part of a
6065 * mapping, and the "to" part of a menu command.
6066 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6067 * '<'.
6068 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6069 *
6070 * The replacement is done in result[] and finally copied into allocated
6071 * memory. If this all works well *bufp is set to the allocated memory and a
6072 * pointer to it is returned. If something fails *bufp is set to NULL and from
6073 * is returned.
6074 *
6075 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6076 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6077 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6078 * instead of a CTRL-V.
6079 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006080 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006081replace_termcodes(
6082 char_u *from,
6083 char_u **bufp,
6084 int from_part,
6085 int do_lt, /* also translate <lt> */
6086 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 int i;
6089 int slen;
6090 int key;
6091 int dlen = 0;
6092 char_u *src;
6093 int do_backslash; /* backslash is a special character */
6094 int do_special; /* recognize <> key codes */
6095 int do_key_code; /* recognize raw key codes */
6096 char_u *result; /* buffer for resulting string */
6097
6098 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006099 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6101
6102 /*
6103 * Allocate space for the translation. Worst case a single character is
6104 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6105 */
6106 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6107 if (result == NULL) /* out of memory */
6108 {
6109 *bufp = NULL;
6110 return from;
6111 }
6112
6113 src = from;
6114
6115 /*
6116 * Check for #n at start only: function key n
6117 */
6118 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6119 {
6120 result[dlen++] = K_SPECIAL;
6121 result[dlen++] = 'k';
6122 if (src[1] == '0')
6123 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6124 else
6125 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6126 src += 2;
6127 }
6128
6129 /*
6130 * Copy each byte from *from to result[dlen]
6131 */
6132 while (*src != NUL)
6133 {
6134 /*
6135 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006136 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 */
6138 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6139 {
6140#ifdef FEAT_EVAL
6141 /*
6142 * Replace <SID> by K_SNR <script-nr> _.
6143 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6144 */
6145 if (STRNICMP(src, "<SID>", 5) == 0)
6146 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006147 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006148 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 else
6150 {
6151 src += 5;
6152 result[dlen++] = K_SPECIAL;
6153 result[dlen++] = (int)KS_EXTRA;
6154 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006155 sprintf((char *)result + dlen, "%ld",
6156 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 dlen += (int)STRLEN(result + dlen);
6158 result[dlen++] = '_';
6159 continue;
6160 }
6161 }
6162#endif
6163
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006164 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (slen)
6166 {
6167 dlen += slen;
6168 continue;
6169 }
6170 }
6171
6172 /*
6173 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6174 * Note that this is also checked after replacing the <> form.
6175 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6176 * it could be a character in the file.
6177 */
6178 if (do_key_code)
6179 {
6180 i = find_term_bykeys(src);
6181 if (i >= 0)
6182 {
6183 result[dlen++] = K_SPECIAL;
6184 result[dlen++] = termcodes[i].name[0];
6185 result[dlen++] = termcodes[i].name[1];
6186 src += termcodes[i].len;
6187 /* If terminal code matched, continue after it. */
6188 continue;
6189 }
6190 }
6191
6192#ifdef FEAT_EVAL
6193 if (do_special)
6194 {
6195 char_u *p, *s, len;
6196
6197 /*
6198 * Replace <Leader> by the value of "mapleader".
6199 * Replace <LocalLeader> by the value of "maplocalleader".
6200 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6201 */
6202 if (STRNICMP(src, "<Leader>", 8) == 0)
6203 {
6204 len = 8;
6205 p = get_var_value((char_u *)"g:mapleader");
6206 }
6207 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6208 {
6209 len = 13;
6210 p = get_var_value((char_u *)"g:maplocalleader");
6211 }
6212 else
6213 {
6214 len = 0;
6215 p = NULL;
6216 }
6217 if (len != 0)
6218 {
6219 /* Allow up to 8 * 6 characters for "mapleader". */
6220 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6221 s = (char_u *)"\\";
6222 else
6223 s = p;
6224 while (*s != NUL)
6225 result[dlen++] = *s++;
6226 src += len;
6227 continue;
6228 }
6229 }
6230#endif
6231
6232 /*
6233 * Remove CTRL-V and ignore the next character.
6234 * For "from" side the CTRL-V at the end is included, for the "to"
6235 * part it is removed.
6236 * If 'cpoptions' does not contain 'B', also accept a backslash.
6237 */
6238 key = *src;
6239 if (key == Ctrl_V || (do_backslash && key == '\\'))
6240 {
6241 ++src; /* skip CTRL-V or backslash */
6242 if (*src == NUL)
6243 {
6244 if (from_part)
6245 result[dlen++] = key;
6246 break;
6247 }
6248 }
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006251 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 /*
6254 * If the character is K_SPECIAL, replace it with K_SPECIAL
6255 * KS_SPECIAL KE_FILLER.
6256 * If compiled with the GUI replace CSI with K_CSI.
6257 */
6258 if (*src == K_SPECIAL)
6259 {
6260 result[dlen++] = K_SPECIAL;
6261 result[dlen++] = KS_SPECIAL;
6262 result[dlen++] = KE_FILLER;
6263 }
6264# ifdef FEAT_GUI
6265 else if (*src == CSI)
6266 {
6267 result[dlen++] = K_SPECIAL;
6268 result[dlen++] = KS_EXTRA;
6269 result[dlen++] = (int)KE_CSI;
6270 }
6271# endif
6272 else
6273 result[dlen++] = *src;
6274 ++src;
6275 }
6276 }
6277 result[dlen] = NUL;
6278
6279 /*
6280 * Copy the new string to allocated memory.
6281 * If this fails, just return from.
6282 */
6283 if ((*bufp = vim_strsave(result)) != NULL)
6284 from = *bufp;
6285 vim_free(result);
6286 return from;
6287}
6288
6289/*
6290 * Find a termcode with keys 'src' (must be NUL terminated).
6291 * Return the index in termcodes[], or -1 if not found.
6292 */
6293 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006294find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
6296 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006297 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 for (i = 0; i < tc_len; ++i)
6300 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006301 if (slen == termcodes[i].len
6302 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return i;
6304 }
6305 return -1;
6306}
6307
6308/*
6309 * Gather the first characters in the terminal key codes into a string.
6310 * Used to speed up check_termcode().
6311 */
6312 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006313gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315 int i;
6316 int len = 0;
6317
6318#ifdef FEAT_GUI
6319 if (gui.in_use)
6320 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6321#endif
6322#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006323 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 termleader[len++] = DCS; /* the termcode response starts with DCS
6325 in 8-bit mode */
6326#endif
6327 termleader[len] = NUL;
6328
6329 for (i = 0; i < tc_len; ++i)
6330 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6331 {
6332 termleader[len++] = termcodes[i].code[0];
6333 termleader[len] = NUL;
6334 }
6335
6336 need_gather = FALSE;
6337}
6338
6339/*
6340 * Show all termcodes (for ":set termcap")
6341 * This code looks a lot like showoptions(), but is different.
6342 */
6343 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006344show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 int col;
6347 int *items;
6348 int item_count;
6349 int run;
6350 int row, rows;
6351 int cols;
6352 int i;
6353 int len;
6354
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006355#define INC3 27 /* try to make three columns */
6356#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357#define GAP 2 /* spaces between columns */
6358
6359 if (tc_len == 0) /* no terminal codes (must be GUI) */
6360 return;
6361 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6362 if (items == NULL)
6363 return;
6364
6365 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006366 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 /*
6369 * do the loop two times:
6370 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006371 * 2. display the medium items (medium length strings)
6372 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006374 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 /*
6377 * collect the items in items[]
6378 */
6379 item_count = 0;
6380 for (i = 0; i < tc_len; i++)
6381 {
6382 len = show_one_termcode(termcodes[i].name,
6383 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006384 if (len <= INC3 - GAP ? run == 1
6385 : len <= INC2 - GAP ? run == 2
6386 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 items[item_count++] = i;
6388 }
6389
6390 /*
6391 * display the items
6392 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006393 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006395 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (cols == 0)
6397 cols = 1;
6398 rows = (item_count + cols - 1) / cols;
6399 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006400 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 rows = item_count;
6402 for (row = 0; row < rows && !got_int; ++row)
6403 {
6404 msg_putchar('\n'); /* go to next line */
6405 if (got_int) /* 'q' typed in more */
6406 break;
6407 col = 0;
6408 for (i = row; i < item_count; i += rows)
6409 {
6410 msg_col = col; /* make columns */
6411 show_one_termcode(termcodes[items[i]].name,
6412 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006413 if (run == 2)
6414 col += INC2;
6415 else
6416 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418 out_flush();
6419 ui_breakcheck();
6420 }
6421 }
6422 vim_free(items);
6423}
6424
6425/*
6426 * Show one termcode entry.
6427 * Output goes into IObuff[]
6428 */
6429 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006430show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 char_u *p;
6433 int len;
6434
6435 if (name[0] > '~')
6436 {
6437 IObuff[0] = ' ';
6438 IObuff[1] = ' ';
6439 IObuff[2] = ' ';
6440 IObuff[3] = ' ';
6441 }
6442 else
6443 {
6444 IObuff[0] = 't';
6445 IObuff[1] = '_';
6446 IObuff[2] = name[0];
6447 IObuff[3] = name[1];
6448 }
6449 IObuff[4] = ' ';
6450
6451 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6452 if (p[1] != 't')
6453 STRCPY(IObuff + 5, p);
6454 else
6455 IObuff[5] = NUL;
6456 len = (int)STRLEN(IObuff);
6457 do
6458 IObuff[len++] = ' ';
6459 while (len < 17);
6460 IObuff[len] = NUL;
6461 if (code == NULL)
6462 len += 4;
6463 else
6464 len += vim_strsize(code);
6465
6466 if (printit)
6467 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006468 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006470 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 else
6472 msg_outtrans(code);
6473 }
6474 return len;
6475}
6476
6477#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6478/*
6479 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6480 * termcap codes from the terminal itself.
6481 * We get them one by one to avoid a very long response string.
6482 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006483static int xt_index_in = 0;
6484static int xt_index_out = 0;
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006487req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 xt_index_out = 0;
6490 xt_index_in = 0;
6491 req_more_codes_from_term();
6492}
6493
6494 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006495req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 char buf[11];
6498 int old_idx = xt_index_out;
6499
6500 /* Don't do anything when going to exit. */
6501 if (exiting)
6502 return;
6503
6504 /* Send up to 10 more requests out than we received. Avoid sending too
6505 * many, there can be a buffer overflow somewhere. */
6506 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6507 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006508 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006509
Bram Moolenaarb255b902018-04-24 21:40:10 +02006510 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6511 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 out_str_nf((char_u *)buf);
6513 ++xt_index_out;
6514 }
6515
6516 /* Send the codes out right away. */
6517 if (xt_index_out != old_idx)
6518 out_flush();
6519}
6520
6521/*
6522 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6523 * A "0" instead of the "1" indicates a code that isn't supported.
6524 * Both <name> and <string> are encoded in hex.
6525 * "code" points to the "0" or "1".
6526 */
6527 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006528got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530#define XT_LEN 100
6531 char_u name[3];
6532 char_u str[XT_LEN];
6533 int i;
6534 int j = 0;
6535 int c;
6536
6537 /* A '1' means the code is supported, a '0' means it isn't.
6538 * When half the length is > XT_LEN we can't use it.
6539 * Our names are currently all 2 characters. */
6540 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6541 {
6542 /* Get the name from the response and find it in the table. */
6543 name[0] = hexhex2nr(code + 3);
6544 name[1] = hexhex2nr(code + 5);
6545 name[2] = NUL;
6546 for (i = 0; key_names[i] != NULL; ++i)
6547 {
6548 if (STRCMP(key_names[i], name) == 0)
6549 {
6550 xt_index_in = i;
6551 break;
6552 }
6553 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006554
Bram Moolenaarb255b902018-04-24 21:40:10 +02006555 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (key_names[i] != NULL)
6558 {
6559 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6560 str[j++] = c;
6561 str[j] = NUL;
6562 if (name[0] == 'C' && name[1] == 'o')
6563 {
6564 /* Color count is not a key code. */
6565 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006566 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 else
6569 {
6570 /* First delete any existing entry with the same code. */
6571 i = find_term_bykeys(str);
6572 if (i >= 0)
6573 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006574 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576 }
6577 }
6578
6579 /* May request more codes now that we received one. */
6580 ++xt_index_in;
6581 req_more_codes_from_term();
6582}
6583
6584/*
6585 * Check if there are any unanswered requests and deal with them.
6586 * This is called before starting an external program or getting direct
6587 * keyboard input. We don't want responses to be send to that program or
6588 * handled as typed text.
6589 */
6590 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006591check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 int c;
6594
6595 /* If no codes requested or all are answered, no need to wait. */
6596 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6597 return;
6598
6599 /* Vgetc() will check for and handle any response.
6600 * Keep calling vpeekc() until we don't get any responses. */
6601 ++no_mapping;
6602 ++allow_keys;
6603 for (;;)
6604 {
6605 c = vpeekc();
6606 if (c == NUL) /* nothing available */
6607 break;
6608
6609 /* If a response is recognized it's replaced with K_IGNORE, must read
6610 * it from the input stream. If there is no K_IGNORE we can't do
6611 * anything, break here (there might be some responses further on, but
6612 * we don't want to throw away any typed chars). */
6613 if (c != K_SPECIAL && c != K_IGNORE)
6614 break;
6615 c = vgetc();
6616 if (c != K_IGNORE)
6617 {
6618 vungetc(c);
6619 break;
6620 }
6621 }
6622 --no_mapping;
6623 --allow_keys;
6624}
6625#endif
6626
6627#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6628/*
6629 * Translate an internal mapping/abbreviation representation into the
6630 * corresponding external one recognized by :map/:abbrev commands;
6631 * respects the current B/k/< settings of 'cpoption'.
6632 *
6633 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006634 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 *
6636 * It uses a growarray to build the translation string since the
6637 * latter can be wider than the original description. The caller has to
6638 * free the string afterwards.
6639 *
6640 * Returns NULL when there is a problem.
6641 */
6642 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006643translate_mapping(
6644 char_u *str,
6645 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 garray_T ga;
6648 int c;
6649 int modifiers;
6650 int cpo_bslash;
6651 int cpo_special;
6652 int cpo_keycode;
6653
6654 ga_init(&ga);
6655 ga.ga_itemsize = 1;
6656 ga.ga_growsize = 40;
6657
6658 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6659 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6660 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6661
6662 for (; *str; ++str)
6663 {
6664 c = *str;
6665 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6666 {
6667 modifiers = 0;
6668 if (str[1] == KS_MODIFIER)
6669 {
6670 str++;
6671 modifiers = *++str;
6672 c = *++str;
6673 }
6674 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6675 {
6676 int i;
6677
6678 /* try to find special key in termcodes */
6679 for (i = 0; i < tc_len; ++i)
6680 if (termcodes[i].name[0] == str[1]
6681 && termcodes[i].name[1] == str[2])
6682 break;
6683 if (i < tc_len)
6684 {
6685 ga_concat(&ga, termcodes[i].code);
6686 str += 2;
6687 continue; /* for (str) */
6688 }
6689 }
6690 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6691 {
6692 if (expmap && cpo_special)
6693 {
6694 ga_clear(&ga);
6695 return NULL;
6696 }
6697 c = TO_SPECIAL(str[1], str[2]);
6698 if (c == K_ZERO) /* display <Nul> as ^@ */
6699 c = NUL;
6700 str += 2;
6701 }
6702 if (IS_SPECIAL(c) || modifiers) /* special key */
6703 {
6704 if (expmap && cpo_special)
6705 {
6706 ga_clear(&ga);
6707 return NULL;
6708 }
6709 ga_concat(&ga, get_special_key_name(c, modifiers));
6710 continue; /* for (str) */
6711 }
6712 }
6713 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6714 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6715 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6716 if (c)
6717 ga_append(&ga, c);
6718 }
6719 ga_append(&ga, NUL);
6720 return (char_u *)(ga.ga_data);
6721}
6722#endif
6723
6724#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6725static char ksme_str[20];
6726static char ksmr_str[20];
6727static char ksmd_str[20];
6728
6729/*
6730 * For Win32 console: update termcap codes for existing console attributes.
6731 */
6732 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006733update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 struct builtin_term *p;
6736
6737 p = find_builtin_term(DEFAULT_TERM);
6738 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6739 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6740 attr | 0x08); /* FOREGROUND_INTENSITY */
6741 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6742 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6743
6744 while (p->bt_string != NULL)
6745 {
6746 if (p->bt_entry == (int)KS_ME)
6747 p->bt_string = &ksme_str[0];
6748 else if (p->bt_entry == (int)KS_MR)
6749 p->bt_string = &ksmr_str[0];
6750 else if (p->bt_entry == (int)KS_MD)
6751 p->bt_string = &ksmd_str[0];
6752 ++p;
6753 }
6754}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006755
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006756# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006757# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006758struct ks_tbl_s
6759{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006760 int code; // value of KS_
6761 char *vtp; // code in vtp mode
6762 char *vtp2; // code in vtp2 mode
6763 char buf[KSSIZE]; // save buffer in non-vtp mode
6764 char vbuf[KSSIZE]; // save buffer in vtp mode
6765 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6766 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006767};
6768
6769static struct ks_tbl_s ks_tbl[] =
6770{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006771 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6772 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6773 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6774 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6775 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6776 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6777 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6778 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6779 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006780# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006781 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6782 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006783 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6784 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006785# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006786 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6787 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006788 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6789 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006790# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006791 {(int)KS_CCO, "256", "256"}, // colors
6792 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006793};
6794
6795 static struct builtin_term *
6796find_first_tcap(
6797 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006798 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006799{
6800 struct builtin_term *p;
6801
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006802 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006803 if (p->bt_entry == code)
6804 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006805 return NULL;
6806}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006807# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006808
6809/*
6810 * For Win32 console: replace the sequence immediately after termguicolors.
6811 */
6812 void
6813swap_tcap(void)
6814{
6815# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006816 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006817 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006818 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006819 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006820 int mode;
6821 enum
6822 {
6823 CMODEINDEX,
6824 CMODE24,
6825 CMODE256
6826 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006827
6828 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006829 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006830 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006831 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006832 {
6833 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006834 if (bt != NULL)
6835 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006836 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6837 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6838 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6839
6840 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6841 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006842 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006843 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006844 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006845 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006846 }
6847
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006848 if (p_tgc)
6849 mode = CMODE24;
6850 else if (t_colors >= 256)
6851 mode = CMODE256;
6852 else
6853 mode = CMODEINDEX;
6854
6855 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006856 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006857 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6858 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006859 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006860 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006861 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006862 case CMODEINDEX:
6863 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6864 break;
6865 case CMODE24:
6866 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6867 break;
6868 default:
6869 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006870 }
6871 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006872 }
6873
6874 if (mode != curr_mode)
6875 {
6876 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006877 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006878 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6879 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006880 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006881 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006882 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006883 case CMODEINDEX:
6884 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6885 break;
6886 case CMODE24:
6887 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6888 break;
6889 default:
6890 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006891 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006892 }
6893 }
6894
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006895 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006896 }
6897# endif
6898}
6899
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006901
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006902#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006903 static int
6904hex_digit(int c)
6905{
6906 if (isdigit(c))
6907 return c - '0';
6908 c = TOLOWER_ASC(c);
6909 if (c >= 'a' && c <= 'f')
6910 return c - 'a' + 10;
6911 return 0x1ffffff;
6912}
6913
6914 guicolor_T
6915gui_get_color_cmn(char_u *name)
6916{
Bram Moolenaar28726652017-01-06 18:16:19 +01006917 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6918 * values as used by the MS-Windows GDI api. It should be used only for
6919 * MS-Windows GDI builds. */
6920# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6921# undef RGB
6922# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006923# ifndef RGB
6924# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6925# endif
6926# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006927 FILE *fd;
6928 char line[LINE_LEN];
6929 char_u *fname;
6930 int r, g, b, i;
6931 guicolor_T color;
6932
6933 struct rgbcolor_table_S {
6934 char_u *color_name;
6935 guicolor_T color;
6936 };
6937
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006938 /* Only non X11 colors (not present in rgb.txt) and colors in
6939 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006940 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006941 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6942 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6943 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6944 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6945 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6946 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6947 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6948 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6949 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6950 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6951 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6952 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6953 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006954 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6955 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006956 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006957 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006958 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006959 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6960 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6961 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6962 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6963 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6964 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6965 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6966 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6967 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006968 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006969 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006970 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6971 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006972 };
6973
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006974 static struct rgbcolor_table_S *colornames_table;
6975 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006976
6977 if (name[0] == '#' && STRLEN(name) == 7)
6978 {
6979 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006980 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006981 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6982 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6983 if (color > 0xffffff)
6984 return INVALCOLOR;
6985 return color;
6986 }
6987
6988 /* Check if the name is one of the colors we know */
6989 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6990 if (STRICMP(name, rgb_table[i].color_name) == 0)
6991 return rgb_table[i].color;
6992
6993 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006994 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006995 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006996 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006997 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006998 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006999
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007000 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007001 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
7002 if (fname == NULL)
7003 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007004
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007005 fd = fopen((char *)fname, "rt");
7006 vim_free(fname);
7007 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02007008 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007009 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007010 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007011 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007012 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007013 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007014
7015 for (counting = 1; counting >= 0; --counting)
7016 {
7017 if (!counting)
7018 {
7019 colornames_table = (struct rgbcolor_table_S *)alloc(
7020 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
7021 if (colornames_table == NULL)
7022 {
7023 fclose(fd);
7024 return INVALCOLOR;
7025 }
7026 rewind(fd);
7027 }
7028 size = 0;
7029
7030 while (!feof(fd))
7031 {
7032 size_t len;
7033 int pos;
7034
Bram Moolenaar42335f52018-09-13 15:33:43 +02007035 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007036 len = strlen(line);
7037
7038 if (len <= 1 || line[len - 1] != '\n')
7039 continue;
7040
7041 line[len - 1] = '\0';
7042
7043 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7044 if (i != 3)
7045 continue;
7046
7047 if (!counting)
7048 {
7049 char_u *s = vim_strsave((char_u *)line + pos);
7050
7051 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007052 {
7053 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007054 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007055 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007056 colornames_table[size].color_name = s;
7057 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7058 }
7059 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007060
7061 // The distributed rgb.txt has less than 1000 entries. Limit to
7062 // 10000, just in case the file was messed up.
7063 if (size == 10000)
7064 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007065 }
7066 }
7067 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007068 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007069
7070 for (i = 0; i < size; i++)
7071 if (STRICMP(name, colornames_table[i].color_name) == 0)
7072 return colornames_table[i].color;
7073
Bram Moolenaarab302212016-04-26 20:59:29 +02007074 return INVALCOLOR;
7075}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007076
7077 guicolor_T
7078gui_get_rgb_color_cmn(int r, int g, int b)
7079{
7080 guicolor_T color = RGB(r, g, b);
7081
7082 if (color > 0xffffff)
7083 return INVALCOLOR;
7084 return color;
7085}
Bram Moolenaarab302212016-04-26 20:59:29 +02007086#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007087
7088#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
7089 || defined(PROTO)
7090static int cube_value[] = {
7091 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7092};
7093
7094static int grey_ramp[] = {
7095 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7096 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7097};
7098
7099# ifdef FEAT_TERMINAL
7100# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007101# else
7102# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007103# endif
7104
Bram Moolenaar9894e392018-05-05 14:29:06 +02007105static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007106// R G B idx
7107 { 0, 0, 0, 1}, // black
7108 {224, 0, 0, 2}, // dark red
7109 { 0, 224, 0, 3}, // dark green
7110 {224, 224, 0, 4}, // dark yellow / brown
7111 { 0, 0, 224, 5}, // dark blue
7112 {224, 0, 224, 6}, // dark magenta
7113 { 0, 224, 224, 7}, // dark cyan
7114 {224, 224, 224, 8}, // light grey
7115
7116 {128, 128, 128, 9}, // dark grey
7117 {255, 64, 64, 10}, // light red
7118 { 64, 255, 64, 11}, // light green
7119 {255, 255, 64, 12}, // yellow
7120 { 64, 64, 255, 13}, // light blue
7121 {255, 64, 255, 14}, // light magenta
7122 { 64, 255, 255, 15}, // light cyan
7123 {255, 255, 255, 16}, // white
7124};
7125
7126 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007127cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007128{
7129 int idx;
7130
7131 if (nr < 16)
7132 {
7133 *r = ansi_table[nr][0];
7134 *g = ansi_table[nr][1];
7135 *b = ansi_table[nr][2];
7136 *ansi_idx = ansi_table[nr][3];
7137 }
7138 else if (nr < 232)
7139 {
7140 /* 216 color cube */
7141 idx = nr - 16;
7142 *r = cube_value[idx / 36 % 6];
7143 *g = cube_value[idx / 6 % 6];
7144 *b = cube_value[idx % 6];
7145 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7146 }
7147 else if (nr < 256)
7148 {
7149 /* 24 grey scale ramp */
7150 idx = nr - 232;
7151 *r = grey_ramp[idx];
7152 *g = grey_ramp[idx];
7153 *b = grey_ramp[idx];
7154 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7155 }
7156 else
7157 {
7158 *r = 0;
7159 *g = 0;
7160 *b = 0;
7161 *ansi_idx = 0;
7162 }
7163}
7164#endif