blob: 47d2bdafb11568132a31282d6654a004bf542754 [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"},
543 {(int)KS_CE, "\033|K"}, /* clear to end of line */
544 {(int)KS_AL, "\033|L"}, /* add new blank line */
545# ifdef TERMINFO
546 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
547# else
548 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
549# endif
550 {(int)KS_DL, "\033|M"}, /* delete line */
551# ifdef TERMINFO
552 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
553# else
554 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
555# endif
556 {(int)KS_CL, "\033|J"}, /* clear screen */
557 {(int)KS_CD, "\033|j"}, /* clear to end of display */
558 {(int)KS_VI, "\033|v"}, /* cursor invisible */
559 {(int)KS_VE, "\033|V"}, /* cursor visible */
560
561 {(int)KS_ME, "\033|0m"}, /* normal */
562 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
563 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
564#if 1
565 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
566 {(int)KS_SE, "\033|0m"}, /* standout end */
567#else
568 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
569 {(int)KS_SE, "\033|f"}, /* standout end */
570#endif
571 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
572 {(int)KS_CZR, "\033|0m"}, /* italic end */
573 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
574 {(int)KS_UE, "\033|0m"}, /* underscore end */
575 {(int)KS_CCO, "16"}, /* allow 16 colors */
576# ifdef TERMINFO
577 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
578 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
579# else
580 {(int)KS_CAB, "\033|%db"}, /* set background color */
581 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
582# endif
583
584 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
585 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100586 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {(int)KS_LE, "\b"},
588# ifdef TERMINFO
589 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
590# else
591 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
592# endif
593 {(int)KS_VB, "\033|B"}, /* visual bell */
594 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
595 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
596# ifdef TERMINFO
597 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
598# else
599 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
600# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100601# ifdef FEAT_TERMGUICOLORS
602 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
603 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 {K_UP, "\316H"},
607 {K_DOWN, "\316P"},
608 {K_LEFT, "\316K"},
609 {K_RIGHT, "\316M"},
610 {K_S_UP, "\316\304"},
611 {K_S_DOWN, "\316\317"},
612 {K_S_LEFT, "\316\311"},
613 {K_C_LEFT, "\316s"},
614 {K_S_RIGHT, "\316\313"},
615 {K_C_RIGHT, "\316t"},
616 {K_S_TAB, "\316\017"},
617 {K_F1, "\316;"},
618 {K_F2, "\316<"},
619 {K_F3, "\316="},
620 {K_F4, "\316>"},
621 {K_F5, "\316?"},
622 {K_F6, "\316@"},
623 {K_F7, "\316A"},
624 {K_F8, "\316B"},
625 {K_F9, "\316C"},
626 {K_F10, "\316D"},
627 {K_F11, "\316\205"},
628 {K_F12, "\316\206"},
629 {K_S_F1, "\316T"},
630 {K_S_F2, "\316U"},
631 {K_S_F3, "\316V"},
632 {K_S_F4, "\316W"},
633 {K_S_F5, "\316X"},
634 {K_S_F6, "\316Y"},
635 {K_S_F7, "\316Z"},
636 {K_S_F8, "\316["},
637 {K_S_F9, "\316\\"},
638 {K_S_F10, "\316]"},
639 {K_S_F11, "\316\207"},
640 {K_S_F12, "\316\210"},
641 {K_INS, "\316R"},
642 {K_DEL, "\316S"},
643 {K_HOME, "\316G"},
644 {K_S_HOME, "\316\302"},
645 {K_C_HOME, "\316w"},
646 {K_END, "\316O"},
647 {K_S_END, "\316\315"},
648 {K_C_END, "\316u"},
649 {K_PAGEDOWN, "\316Q"},
650 {K_PAGEUP, "\316I"},
651 {K_KPLUS, "\316N"},
652 {K_KMINUS, "\316J"},
653 {K_KMULTIPLY, "\316\067"},
654 {K_K0, "\316\332"},
655 {K_K1, "\316\336"},
656 {K_K2, "\316\342"},
657 {K_K3, "\316\346"},
658 {K_K4, "\316\352"},
659 {K_K5, "\316\356"},
660 {K_K6, "\316\362"},
661 {K_K7, "\316\366"},
662 {K_K8, "\316\372"},
663 {K_K9, "\316\376"},
664# endif
665
666# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
667/*
668 * VT320 is working as an ANSI terminal compatible DEC terminal.
669 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 * TODO:- rewrite ESC[ codes to CSI
671 * - keyboard languages (CSI ? 26 n)
672 */
673 {(int)KS_NAME, "vt320"},
674 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
675 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
676# ifdef TERMINFO
677 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
678# else
679 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
680# endif
681 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
682# ifdef TERMINFO
683 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
684# else
685 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
686# endif
687 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000688 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
689 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
691 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
693 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
694 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
695 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
696 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
697 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
698 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
699 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
700 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
701 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {(int)KS_MS, "y"},
703 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100704 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {(int)KS_LE, "\b"},
706# ifdef TERMINFO
707 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
708 ESC_STR "[%i%p1%d;%p2%dH")},
709# else
710 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
711# endif
712# ifdef TERMINFO
713 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
714# else
715 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
716# endif
717 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
718 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
719 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
720 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200721 // Note: cursor key sequences for application cursor mode are omitted,
722 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000723 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
724 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
725 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
726 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
727 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
729 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
730 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
731 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
732 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000733 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
735 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
736 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
737 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
738 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
739 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
740 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
741 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
742 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
743 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
744 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
745 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
746 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
747 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
748 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200749 // These sequences starting with <Esc> O may interfere with what the user
750 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
752 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
753 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
754 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
755 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200756 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
757 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
758 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
759 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
760 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
761 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
762 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
763 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
764 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
765 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
767# endif
768
769# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
770/*
771 * Ordinary vt52
772 */
773 {(int)KS_NAME, "vt52"},
774 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
775 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100776# ifdef TERMINFO
777 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
778 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
779# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100783 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
785 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {K_UP, IF_EB("\033A", ESC_STR "A")},
787 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
788 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
789 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100790 {K_F1, IF_EB("\033P", ESC_STR "P")},
791 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
792 {K_F3, IF_EB("\033R", ESC_STR "R")},
793# ifdef __MINT__
794 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
795 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
796 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
797 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
798 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
800 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
801 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
802 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {K_F4, IF_EB("\033S", ESC_STR "S")},
804 {K_F5, IF_EB("\033T", ESC_STR "T")},
805 {K_F6, IF_EB("\033U", ESC_STR "U")},
806 {K_F7, IF_EB("\033V", ESC_STR "V")},
807 {K_F8, IF_EB("\033W", ESC_STR "W")},
808 {K_F9, IF_EB("\033X", ESC_STR "X")},
809 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
810 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
811 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
812 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
813 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
814 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
815 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
816 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
817 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
818 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
819 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
820 {K_INS, IF_EB("\033I", ESC_STR "I")},
821 {K_HOME, IF_EB("\033E", ESC_STR "E")},
822 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
823 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
824# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {(int)KS_MS, "y"},
827# endif
828# endif
829
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200830# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200831 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
833 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
834# ifdef TERMINFO
835 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
836# else
837 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
838# endif
839 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
840# ifdef TERMINFO
841 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
842# else
843 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
844# endif
845# ifdef TERMINFO
846 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
847 ESC_STR "[%i%p1%d;%p2%dr")},
848# else
849 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
850# endif
851 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
852 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
853 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
854 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
855 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
856 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
857 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200858 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
859 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {(int)KS_MS, "y"},
861 {(int)KS_UT, "y"},
862 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200863 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
864 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200865 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200866 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200867# ifdef TERMINFO
868 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
869# else
870 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
871# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200872 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200873 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# ifdef TERMINFO
875 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
876 ESC_STR "[%i%p1%d;%p2%dH")},
877# else
878 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
879# endif
880 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
881# ifdef TERMINFO
882 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
883# else
884 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
885# endif
886 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
887 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
888# ifdef FEAT_XTERM_SAVE
889 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
890 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
891 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
892# endif
893 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
894 {(int)KS_CIE, "\007"},
895 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
896 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200897 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
898 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899# ifdef TERMINFO
900 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
901 ESC_STR "[8;%p1%d;%p2%dt")},
902 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
903 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200904 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905# else
906 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
907 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200908 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909# endif
910 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200911 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200912 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100913 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200914# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200915 /* These are printf strings, not terminal codes. */
916 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
917 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
918# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100919 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
920 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200921 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
922 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
923 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
924 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000925
926 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
927 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
928 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
929 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
930 /* An extra set of cursor keys for vt100 mode */
931 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
932 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
933 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
934 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000936 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
937 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
938 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
939 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000940 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
941 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
942 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
943 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
944 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
945 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
946 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
947 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
948 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
949 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
950 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
951 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000953 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
954 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
955 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
956 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000957 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
958 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000959 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000960 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000961 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000962 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000963 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
964 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000965 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000966 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000967 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
969 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100970 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
971 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
972 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
973 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
974 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
975 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200976 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
977 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
978 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
979 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
980 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
981 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
982 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
983 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
984 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
985 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +0100986 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
987 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
988 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000991 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
992 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
993 /* F14 and F15 are missing, because they send the same codes as the undo
994 * and help key, although they don't work on all keyboards. */
995 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
996 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
997 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
998 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
999 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1000
1001 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1002 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1003 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1004 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1005 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1006 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1007 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1008 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1009 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1010 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1011
1012 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1013 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1014 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1015 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1016 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1017 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1018 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019# endif
1020
1021# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1022/*
1023 * iris-ansi for Silicon Graphics machines.
1024 */
1025 {(int)KS_NAME, "iris-ansi"},
1026 {(int)KS_CE, "\033[K"},
1027 {(int)KS_CD, "\033[J"},
1028 {(int)KS_AL, "\033[L"},
1029# ifdef TERMINFO
1030 {(int)KS_CAL, "\033[%p1%dL"},
1031# else
1032 {(int)KS_CAL, "\033[%dL"},
1033# endif
1034 {(int)KS_DL, "\033[M"},
1035# ifdef TERMINFO
1036 {(int)KS_CDL, "\033[%p1%dM"},
1037# else
1038 {(int)KS_CDL, "\033[%dM"},
1039# endif
1040#if 0 /* The scroll region is not working as Vim expects. */
1041# ifdef TERMINFO
1042 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1043# else
1044 {(int)KS_CS, "\033[%i%d;%dr"},
1045# endif
1046#endif
1047 {(int)KS_CL, "\033[H\033[2J"},
1048 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1049 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1050 {(int)KS_TI, "\033[=6h"},
1051 {(int)KS_TE, "\033[=6l"},
1052 {(int)KS_SE, "\033[21;27m"},
1053 {(int)KS_SO, "\033[1;7m"},
1054 {(int)KS_ME, "\033[m"},
1055 {(int)KS_MR, "\033[7m"},
1056 {(int)KS_MD, "\033[1m"},
1057 {(int)KS_CCO, "8"}, /* allow 8 colors */
1058 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1059 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1060 {(int)KS_US, "\033[4m"}, /* underline on */
1061 {(int)KS_UE, "\033[24m"}, /* underline off */
1062# ifdef TERMINFO
1063 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1064 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1065 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1066 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1067# else
1068 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1069 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1070 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1071 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1072# endif
1073 {(int)KS_MS, "y"}, /* guessed */
1074 {(int)KS_UT, "y"}, /* guessed */
1075 {(int)KS_LE, "\b"},
1076# ifdef TERMINFO
1077 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1078# else
1079 {(int)KS_CM, "\033[%i%d;%dH"},
1080# endif
1081 {(int)KS_SR, "\033M"},
1082# ifdef TERMINFO
1083 {(int)KS_CRI, "\033[%p1%dC"},
1084# else
1085 {(int)KS_CRI, "\033[%dC"},
1086# endif
1087 {(int)KS_CIS, "\033P3.y"},
1088 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1089 {(int)KS_TS, "\033P1.y"},
1090 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1091# ifdef TERMINFO
1092 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1093 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1094# else
1095 {(int)KS_CWS, "\033[203;%d;%d/y"},
1096 {(int)KS_CWP, "\033[205;%d;%d/y"},
1097# endif
1098 {K_UP, "\033[A"},
1099 {K_DOWN, "\033[B"},
1100 {K_LEFT, "\033[D"},
1101 {K_RIGHT, "\033[C"},
1102 {K_S_UP, "\033[161q"},
1103 {K_S_DOWN, "\033[164q"},
1104 {K_S_LEFT, "\033[158q"},
1105 {K_S_RIGHT, "\033[167q"},
1106 {K_F1, "\033[001q"},
1107 {K_F2, "\033[002q"},
1108 {K_F3, "\033[003q"},
1109 {K_F4, "\033[004q"},
1110 {K_F5, "\033[005q"},
1111 {K_F6, "\033[006q"},
1112 {K_F7, "\033[007q"},
1113 {K_F8, "\033[008q"},
1114 {K_F9, "\033[009q"},
1115 {K_F10, "\033[010q"},
1116 {K_F11, "\033[011q"},
1117 {K_F12, "\033[012q"},
1118 {K_S_F1, "\033[013q"},
1119 {K_S_F2, "\033[014q"},
1120 {K_S_F3, "\033[015q"},
1121 {K_S_F4, "\033[016q"},
1122 {K_S_F5, "\033[017q"},
1123 {K_S_F6, "\033[018q"},
1124 {K_S_F7, "\033[019q"},
1125 {K_S_F8, "\033[020q"},
1126 {K_S_F9, "\033[021q"},
1127 {K_S_F10, "\033[022q"},
1128 {K_S_F11, "\033[023q"},
1129 {K_S_F12, "\033[024q"},
1130 {K_INS, "\033[139q"},
1131 {K_HOME, "\033[H"},
1132 {K_END, "\033[146q"},
1133 {K_PAGEUP, "\033[150q"},
1134 {K_PAGEDOWN, "\033[154q"},
1135# endif
1136
1137# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1138/*
1139 * for debugging
1140 */
1141 {(int)KS_NAME, "debug"},
1142 {(int)KS_CE, "[CE]"},
1143 {(int)KS_CD, "[CD]"},
1144 {(int)KS_AL, "[AL]"},
1145# ifdef TERMINFO
1146 {(int)KS_CAL, "[CAL%p1%d]"},
1147# else
1148 {(int)KS_CAL, "[CAL%d]"},
1149# endif
1150 {(int)KS_DL, "[DL]"},
1151# ifdef TERMINFO
1152 {(int)KS_CDL, "[CDL%p1%d]"},
1153# else
1154 {(int)KS_CDL, "[CDL%d]"},
1155# endif
1156# ifdef TERMINFO
1157 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1158# else
1159 {(int)KS_CS, "[%dCS%d]"},
1160# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001161# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# endif
1166# ifdef TERMINFO
1167 {(int)KS_CAB, "[CAB%p1%d]"},
1168 {(int)KS_CAF, "[CAF%p1%d]"},
1169 {(int)KS_CSB, "[CSB%p1%d]"},
1170 {(int)KS_CSF, "[CSF%p1%d]"},
1171# else
1172 {(int)KS_CAB, "[CAB%d]"},
1173 {(int)KS_CAF, "[CAF%d]"},
1174 {(int)KS_CSB, "[CSB%d]"},
1175 {(int)KS_CSF, "[CSF%d]"},
1176# endif
1177 {(int)KS_OP, "[OP]"},
1178 {(int)KS_LE, "[LE]"},
1179 {(int)KS_CL, "[CL]"},
1180 {(int)KS_VI, "[VI]"},
1181 {(int)KS_VE, "[VE]"},
1182 {(int)KS_VS, "[VS]"},
1183 {(int)KS_ME, "[ME]"},
1184 {(int)KS_MR, "[MR]"},
1185 {(int)KS_MB, "[MB]"},
1186 {(int)KS_MD, "[MD]"},
1187 {(int)KS_SE, "[SE]"},
1188 {(int)KS_SO, "[SO]"},
1189 {(int)KS_UE, "[UE]"},
1190 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001191 {(int)KS_UCE, "[UCE]"},
1192 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001193 {(int)KS_STE, "[STE]"},
1194 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_MS, "[MS]"},
1196 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001197 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# ifdef TERMINFO
1199 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1200# else
1201 {(int)KS_CM, "[%dCM%d]"},
1202# endif
1203 {(int)KS_SR, "[SR]"},
1204# ifdef TERMINFO
1205 {(int)KS_CRI, "[CRI%p1%d]"},
1206# else
1207 {(int)KS_CRI, "[CRI%d]"},
1208# endif
1209 {(int)KS_VB, "[VB]"},
1210 {(int)KS_KS, "[KS]"},
1211 {(int)KS_KE, "[KE]"},
1212 {(int)KS_TI, "[TI]"},
1213 {(int)KS_TE, "[TE]"},
1214 {(int)KS_CIS, "[CIS]"},
1215 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001216 {(int)KS_CSC, "[CSC]"},
1217 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {(int)KS_TS, "[TS]"},
1219 {(int)KS_FS, "[FS]"},
1220# ifdef TERMINFO
1221 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1222 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1223# else
1224 {(int)KS_CWS, "[%dCWS%d]"},
1225 {(int)KS_CWP, "[%dCWP%d]"},
1226# endif
1227 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001228 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001229 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001230 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {K_UP, "[KU]"},
1232 {K_DOWN, "[KD]"},
1233 {K_LEFT, "[KL]"},
1234 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 {K_XUP, "[xKU]"},
1236 {K_XDOWN, "[xKD]"},
1237 {K_XLEFT, "[xKL]"},
1238 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_S_UP, "[S-KU]"},
1240 {K_S_DOWN, "[S-KD]"},
1241 {K_S_LEFT, "[S-KL]"},
1242 {K_C_LEFT, "[C-KL]"},
1243 {K_S_RIGHT, "[S-KR]"},
1244 {K_C_RIGHT, "[C-KR]"},
1245 {K_F1, "[F1]"},
1246 {K_XF1, "[xF1]"},
1247 {K_F2, "[F2]"},
1248 {K_XF2, "[xF2]"},
1249 {K_F3, "[F3]"},
1250 {K_XF3, "[xF3]"},
1251 {K_F4, "[F4]"},
1252 {K_XF4, "[xF4]"},
1253 {K_F5, "[F5]"},
1254 {K_F6, "[F6]"},
1255 {K_F7, "[F7]"},
1256 {K_F8, "[F8]"},
1257 {K_F9, "[F9]"},
1258 {K_F10, "[F10]"},
1259 {K_F11, "[F11]"},
1260 {K_F12, "[F12]"},
1261 {K_S_F1, "[S-F1]"},
1262 {K_S_XF1, "[S-xF1]"},
1263 {K_S_F2, "[S-F2]"},
1264 {K_S_XF2, "[S-xF2]"},
1265 {K_S_F3, "[S-F3]"},
1266 {K_S_XF3, "[S-xF3]"},
1267 {K_S_F4, "[S-F4]"},
1268 {K_S_XF4, "[S-xF4]"},
1269 {K_S_F5, "[S-F5]"},
1270 {K_S_F6, "[S-F6]"},
1271 {K_S_F7, "[S-F7]"},
1272 {K_S_F8, "[S-F8]"},
1273 {K_S_F9, "[S-F9]"},
1274 {K_S_F10, "[S-F10]"},
1275 {K_S_F11, "[S-F11]"},
1276 {K_S_F12, "[S-F12]"},
1277 {K_HELP, "[HELP]"},
1278 {K_UNDO, "[UNDO]"},
1279 {K_BS, "[BS]"},
1280 {K_INS, "[INS]"},
1281 {K_KINS, "[KINS]"},
1282 {K_DEL, "[DEL]"},
1283 {K_KDEL, "[KDEL]"},
1284 {K_HOME, "[HOME]"},
1285 {K_S_HOME, "[C-HOME]"},
1286 {K_C_HOME, "[C-HOME]"},
1287 {K_KHOME, "[KHOME]"},
1288 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001289 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {K_END, "[END]"},
1291 {K_S_END, "[C-END]"},
1292 {K_C_END, "[C-END]"},
1293 {K_KEND, "[KEND]"},
1294 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_PAGEUP, "[PAGEUP]"},
1297 {K_PAGEDOWN, "[PAGEDOWN]"},
1298 {K_KPAGEUP, "[KPAGEUP]"},
1299 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1300 {K_MOUSE, "[MOUSE]"},
1301 {K_KPLUS, "[KPLUS]"},
1302 {K_KMINUS, "[KMINUS]"},
1303 {K_KDIVIDE, "[KDIVIDE]"},
1304 {K_KMULTIPLY, "[KMULTIPLY]"},
1305 {K_KENTER, "[KENTER]"},
1306 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001307 {K_PS, "[PASTE-START]"},
1308 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_K0, "[K0]"},
1310 {K_K1, "[K1]"},
1311 {K_K2, "[K2]"},
1312 {K_K3, "[K3]"},
1313 {K_K4, "[K4]"},
1314 {K_K5, "[K5]"},
1315 {K_K6, "[K6]"},
1316 {K_K7, "[K7]"},
1317 {K_K8, "[K8]"},
1318 {K_K9, "[K9]"},
1319# endif
1320
1321#endif /* NO_BUILTIN_TCAPS */
1322
1323/*
1324 * The most minimal terminal: only clear screen and cursor positioning
1325 * Always included.
1326 */
1327 {(int)KS_NAME, "dumb"},
1328 {(int)KS_CL, "\014"},
1329#ifdef TERMINFO
1330 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1331 ESC_STR "[%i%p1%d;%p2%dH")},
1332#else
1333 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1334#endif
1335
1336/*
1337 * end marker
1338 */
1339 {(int)KS_NAME, NULL}
1340
1341}; /* end of builtin_termcaps */
1342
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001343#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001345termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001346{
Bram Moolenaarab302212016-04-26 20:59:29 +02001347 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348}
1349
1350 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001351termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001352{
1353 guicolor_T t;
1354
1355 if (*name == NUL)
1356 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001357 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001358
1359 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001360 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361 return t;
1362}
1363
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001364 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001365termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001366{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001367 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368}
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374#ifdef AMIGA
1375# define DEFAULT_TERM (char_u *)"amiga"
1376#endif
1377
1378#ifdef MSWIN
1379# define DEFAULT_TERM (char_u *)"win32"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#if defined(UNIX) && !defined(__MINT__)
1383# define DEFAULT_TERM (char_u *)"ansi"
1384#endif
1385
1386#ifdef __MINT__
1387# define DEFAULT_TERM (char_u *)"vt52"
1388#endif
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef VMS
1391# define DEFAULT_TERM (char_u *)"vt320"
1392#endif
1393
1394#ifdef __BEOS__
1395# undef DEFAULT_TERM
1396# define DEFAULT_TERM (char_u *)"beos-ansi"
1397#endif
1398
1399#ifndef DEFAULT_TERM
1400# define DEFAULT_TERM (char_u *)"dumb"
1401#endif
1402
1403/*
1404 * Term_strings contains currently used terminal output strings.
1405 * It is initialized with the default values by parse_builtin_tcap().
1406 * The values can be changed by setting the option with the same name.
1407 */
1408char_u *(term_strings[(int)KS_LAST + 1]);
1409
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001410static int need_gather = FALSE; /* need to fill termleader[] */
1411static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001413static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001414static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#endif
1416
1417 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001418find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 struct builtin_term *p;
1421
1422 p = builtin_termcaps;
1423 while (p->bt_string != NULL)
1424 {
1425 if (p->bt_entry == (int)KS_NAME)
1426 {
1427#ifdef UNIX
1428 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1429 return p;
1430 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1431 return p;
1432 else
1433#endif
1434#ifdef VMS
1435 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1436 return p;
1437 else
1438#endif
1439 if (STRCMP(term, p->bt_string) == 0)
1440 return p;
1441 }
1442 ++p;
1443 }
1444 return p;
1445}
1446
1447/*
1448 * Parsing of the builtin termcap entries.
1449 * Caller should check if 'name' is a valid builtin term.
1450 * The terminal's name is not set, as this is already done in termcapinit().
1451 */
1452 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001453parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 struct builtin_term *p;
1456 char_u name[2];
1457 int term_8bit;
1458
1459 p = find_builtin_term(term);
1460 term_8bit = term_is_8bit(term);
1461
1462 /* Do not parse if builtin term not found */
1463 if (p->bt_string == NULL)
1464 return;
1465
1466 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1467 {
1468 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1469 {
1470 /* Only set the value if it wasn't set yet. */
1471 if (term_strings[p->bt_entry] == NULL
1472 || term_strings[p->bt_entry] == empty_option)
1473 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001474#ifdef FEAT_EVAL
1475 int opt_idx = -1;
1476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /* 8bit terminal: use CSI instead of <Esc>[ */
1478 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1479 {
1480 char_u *s, *t;
1481
1482 s = vim_strsave((char_u *)p->bt_string);
1483 if (s != NULL)
1484 {
1485 for (t = s; *t; ++t)
1486 if (term_7to8bit(t))
1487 {
1488 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001489 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001492#ifdef FEAT_EVAL
1493 opt_idx =
1494#endif
1495 set_term_option_alloced(
1496 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 }
1499 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001502#ifdef FEAT_EVAL
1503 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1504#endif
1505 }
1506#ifdef FEAT_EVAL
1507 set_term_option_sctx_idx(NULL, opt_idx);
1508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510 }
1511 else
1512 {
1513 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1514 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1515 if (find_termcode(name) == NULL)
1516 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1517 }
1518 }
1519}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521/*
1522 * Set number of colors.
1523 * Store it as a number in t_colors.
1524 * Store it as a string in T_CCO (using nr_colors[]).
1525 */
1526 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001527set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 char_u nr_colors[20]; /* string for number of colors */
1530
1531 t_colors = nr;
1532 if (t_colors > 1)
1533 sprintf((char *)nr_colors, "%d", t_colors);
1534 else
1535 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001536 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001538
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001539#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001540/*
1541 * Set the color count to "val" and redraw if it changed.
1542 */
1543 static void
1544may_adjust_color_count(int val)
1545{
1546 if (val != t_colors)
1547 {
1548 /* Nr of colors changed, initialize highlighting and
1549 * redraw everything. This causes a redraw, which usually
1550 * clears the message. Try keeping the message if it
1551 * might work. */
1552 set_keep_msg_from_hist();
1553 set_color_count(val);
1554 init_highlight(TRUE, FALSE);
1555# ifdef DEBUG_TERMRESPONSE
1556 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001557 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001558
Bram Moolenaarb255b902018-04-24 21:40:10 +02001559 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001560 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001561#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001562 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001563#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001564 }
1565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#endif
1567
1568#ifdef HAVE_TGETENT
1569static char *(key_names[]) =
1570{
1571#ifdef FEAT_TERMRESPONSE
1572 /* Do this one first, it may cause a screen redraw. */
1573 "Co",
1574#endif
1575 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 "#2", "#4", "%i", "*7",
1577 "k1", "k2", "k3", "k4", "k5", "k6",
1578 "k7", "k8", "k9", "k;", "F1", "F2",
1579 "%1", "&8", "kb", "kI", "kD", "kh",
1580 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1581 NULL
1582};
1583#endif
1584
Bram Moolenaar9289df52018-05-10 14:40:57 +02001585#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001586 static void
1587get_term_entries(int *height, int *width)
1588{
1589 static struct {
1590 enum SpecialKey dest; /* index in term_strings[] */
1591 char *name; /* termcap name for string */
1592 } string_names[] =
1593 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1594 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1595 {KS_CL, "cl"}, {KS_CD, "cd"},
1596 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1597 {KS_ME, "me"}, {KS_MR, "mr"},
1598 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1599 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1600 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1601 {KS_STE,"Te"}, {KS_STS,"Ts"},
1602 {KS_CM, "cm"}, {KS_SR, "sr"},
1603 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1604 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1605 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1606 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1607 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1608 {KS_VS, "vs"}, {KS_CVS, "VS"},
1609 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1610 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1611 {KS_TS, "ts"}, {KS_FS, "fs"},
1612 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1613 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1614 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1615 {KS_8F, "8f"}, {KS_8B, "8b"},
1616 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1617 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001618 {KS_CST, "ST"}, {KS_CRT, "RT"},
1619 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001620 {(enum SpecialKey)0, NULL}
1621 };
1622 int i;
1623 char_u *p;
1624 static char_u tstrbuf[TBUFSZ];
1625 char_u *tp = tstrbuf;
1626
1627 /*
1628 * get output strings
1629 */
1630 for (i = 0; string_names[i].name != NULL; ++i)
1631 {
1632 if (TERM_STR(string_names[i].dest) == NULL
1633 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001634 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001635 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001636#ifdef FEAT_EVAL
1637 set_term_option_sctx_idx(string_names[i].name, -1);
1638#endif
1639 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001640 }
1641
1642 /* tgetflag() returns 1 if the flag is present, 0 if not and
1643 * possibly -1 if the flag doesn't exist. */
1644 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1645 T_MS = (char_u *)"y";
1646 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1647 T_XS = (char_u *)"y";
1648 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1649 T_XN = (char_u *)"y";
1650 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1651 T_DB = (char_u *)"y";
1652 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1653 T_DA = (char_u *)"y";
1654 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1655 T_UT = (char_u *)"y";
1656
1657 /*
1658 * get key codes
1659 */
1660 for (i = 0; key_names[i] != NULL; ++i)
1661 if (find_termcode((char_u *)key_names[i]) == NULL)
1662 {
1663 p = TGETSTR(key_names[i], &tp);
1664 /* if cursor-left == backspace, ignore it (televideo 925) */
1665 if (p != NULL
1666 && (*p != Ctrl_H
1667 || key_names[i][0] != 'k'
1668 || key_names[i][1] != 'l'))
1669 add_termcode((char_u *)key_names[i], p, FALSE);
1670 }
1671
1672 if (*height == 0)
1673 *height = tgetnum("li");
1674 if (*width == 0)
1675 *width = tgetnum("co");
1676
1677 /*
1678 * Get number of colors (if not done already).
1679 */
1680 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001681 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001682 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001683#ifdef FEAT_EVAL
1684 set_term_option_sctx_idx("Co", -1);
1685#endif
1686 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001687
1688# ifndef hpux
1689 BC = (char *)TGETSTR("bc", &tp);
1690 UP = (char *)TGETSTR("up", &tp);
1691 p = TGETSTR("pc", &tp);
1692 if (p)
1693 PC = *p;
1694# endif
1695}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001696#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001697
1698 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001699report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700{
1701 struct builtin_term *termp;
1702
1703 mch_errmsg("\r\n");
1704 if (error_msg != NULL)
1705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001706 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001707 mch_errmsg("\r\n");
1708 }
1709 mch_errmsg("'");
1710 mch_errmsg((char *)term);
1711 mch_errmsg(_("' not known. Available builtin terminals are:"));
1712 mch_errmsg("\r\n");
1713 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1714 {
1715 if (termp->bt_entry == (int)KS_NAME)
1716 {
1717#ifdef HAVE_TGETENT
1718 mch_errmsg(" builtin_");
1719#else
1720 mch_errmsg(" ");
1721#endif
1722 mch_errmsg(termp->bt_string);
1723 mch_errmsg("\r\n");
1724 }
1725 }
1726}
1727
1728 static void
1729report_default_term(char_u *term)
1730{
1731 mch_errmsg(_("defaulting to '"));
1732 mch_errmsg((char *)term);
1733 mch_errmsg("'\r\n");
1734 if (emsg_silent == 0)
1735 {
1736 screen_start(); /* don't know where cursor is now */
1737 out_flush();
1738 if (!is_not_a_term())
1739 ui_delay(2000L, TRUE);
1740 }
1741}
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743/*
1744 * Set terminal options for terminal "term".
1745 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1746 *
1747 * While doing this, until ttest(), some options may be NULL, be careful.
1748 */
1749 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001750set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 struct builtin_term *termp;
1753#ifdef HAVE_TGETENT
1754 int builtin_first = p_tbi;
1755 int try;
1756 int termcap_cleared = FALSE;
1757#endif
1758 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001759 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 char_u *bs_p, *del_p;
1761
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001762 /* In silect mode (ex -s) we don't use the 'term' option. */
1763 if (silent_mode)
1764 return OK;
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 detected_8bit = FALSE; /* reset 8-bit detection */
1767
1768 if (term_is_builtin(term))
1769 {
1770 term += 8;
1771#ifdef HAVE_TGETENT
1772 builtin_first = 1;
1773#endif
1774 }
1775
1776/*
1777 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1778 * If builtin_first is TRUE:
1779 * 0. try builtin termcap
1780 * 1. try external termcap
1781 * 2. if both fail default to a builtin terminal
1782 * If builtin_first is FALSE:
1783 * 1. try external termcap
1784 * 2. try builtin termcap, if both fail default to a builtin terminal
1785 */
1786#ifdef HAVE_TGETENT
1787 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1788 {
1789 /*
1790 * Use external termcap
1791 */
1792 if (try == 1)
1793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 /*
1797 * If the external termcap does not have a matching entry, try the
1798 * builtin ones.
1799 */
1800 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!termcap_cleared)
1803 {
1804 clear_termoptions(); /* clear old options */
1805 termcap_cleared = TRUE;
1806 }
1807
Bram Moolenaar69e05692018-05-10 14:11:52 +02001808 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810 }
1811 else /* try == 0 || try == 2 */
1812#endif /* HAVE_TGETENT */
1813 /*
1814 * Use builtin termcap
1815 */
1816 {
1817#ifdef HAVE_TGETENT
1818 /*
1819 * If builtin termcap was already used, there is no need to search
1820 * for the builtin termcap again, quit now.
1821 */
1822 if (try == 2 && builtin_first && termcap_cleared)
1823 break;
1824#endif
1825 /*
1826 * search for 'term' in builtin_termcaps[]
1827 */
1828 termp = find_builtin_term(term);
1829 if (termp->bt_string == NULL) /* did not find it */
1830 {
1831#ifdef HAVE_TGETENT
1832 /*
1833 * If try == 0, first try the external termcap. If that is not
1834 * found we'll get back here with try == 2.
1835 * If termcap_cleared is set we used the external termcap,
1836 * don't complain about not finding the term in the builtin
1837 * termcap.
1838 */
1839 if (try == 0) /* try external one */
1840 continue;
1841 if (termcap_cleared) /* found in external termcap */
1842 break;
1843#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001844 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 /* when user typed :set term=xxx, quit here */
1847 if (starting != NO_SCREEN)
1848 {
1849 screen_start(); /* don't know where cursor is now */
1850 wait_return(TRUE);
1851 return FAIL;
1852 }
1853 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001854 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001855 set_string_option_direct((char_u *)"term", -1, term,
1856 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 display_errors();
1858 }
1859 out_flush();
1860#ifdef HAVE_TGETENT
1861 if (!termcap_cleared)
1862 {
1863#endif
1864 clear_termoptions(); /* clear old options */
1865#ifdef HAVE_TGETENT
1866 termcap_cleared = TRUE;
1867 }
1868#endif
1869 parse_builtin_tcap(term);
1870#ifdef FEAT_GUI
1871 if (term_is_gui(term))
1872 {
1873 out_flush();
1874 gui_init();
1875 /* If starting the GUI failed, don't do any of the other
1876 * things for this terminal */
1877 if (!gui.in_use)
1878 return FAIL;
1879#ifdef HAVE_TGETENT
1880 break; /* don't try using external termcap */
1881#endif
1882 }
1883#endif /* FEAT_GUI */
1884 }
1885#ifdef HAVE_TGETENT
1886 }
1887#endif
1888
1889/*
1890 * special: There is no info in the termcap about whether the cursor
1891 * positioning is relative to the start of the screen or to the start of the
1892 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1893 * relative.
1894 */
1895 if (STRCMP(term, "pcterm") == 0)
1896 T_CCS = (char_u *)"yes";
1897 else
1898 T_CCS = empty_option;
1899
1900#ifdef UNIX
1901/*
1902 * Any "stty" settings override the default for t_kb from the termcap.
1903 * This is in os_unix.c, because it depends a lot on the version of unix that
1904 * is being used.
1905 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1906 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001907# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 get_stty();
1911#endif
1912
1913/*
1914 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1915 * didn't work, use the default CTRL-H
1916 * The default for t_kD is DEL, unless t_kb is DEL.
1917 * The vim_strsave'd strings are probably lost forever, well it's only two
1918 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1919 * directly.
1920 */
1921#ifdef FEAT_GUI
1922 if (!gui.in_use)
1923#endif
1924 {
1925 bs_p = find_termcode((char_u *)"kb");
1926 del_p = find_termcode((char_u *)"kD");
1927 if (bs_p == NULL || *bs_p == NUL)
1928 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1929 if ((del_p == NULL || *del_p == NUL) &&
1930 (bs_p == NULL || *bs_p != DEL))
1931 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1932 }
1933
1934#if defined(UNIX) || defined(VMS)
1935 term_is_xterm = vim_is_xterm(term);
1936#endif
1937
1938#ifdef FEAT_MOUSE
1939# if defined(UNIX) || defined(VMS)
1940# ifdef FEAT_MOUSE_TTY
1941 /*
1942 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1943 * The termcode for the mouse is added as a side effect in option.c.
1944 */
1945 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001946 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001949 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 if (use_xterm_mouse())
1952 p = NULL; /* keep existing value, might be "xterm2" */
1953 else
1954 p = (char_u *)"xterm";
1955 }
1956# endif
1957 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001960 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1961 * "xterm2" in check_termcode(). */
1962 reset_option_was_set((char_u *)"ttym");
1963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (p == NULL
1965# ifdef FEAT_GUI
1966 || gui.in_use
1967# endif
1968 )
1969 check_mouse_termcode(); /* set mouse termcode anyway */
1970 }
1971# endif
1972# else
1973 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1974# endif
1975#endif /* FEAT_MOUSE */
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#ifdef USE_TERM_CONSOLE
1978 /* DEFAULT_TERM indicates that it is the machine console. */
1979 if (STRCMP(term, DEFAULT_TERM) != 0)
1980 term_console = FALSE;
1981 else
1982 {
1983 term_console = TRUE;
1984# ifdef AMIGA
1985 win_resize_on(); /* enable window resizing reports */
1986# endif
1987 }
1988#endif
1989
1990#if defined(UNIX) || defined(VMS)
1991 /*
1992 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1993 */
1994 if (vim_is_fastterm(term))
1995 p_tf = TRUE;
1996#endif
1997#ifdef USE_TERM_CONSOLE
1998 /*
1999 * 'ttyfast' is default on consoles
2000 */
2001 if (term_console)
2002 p_tf = TRUE;
2003#endif
2004
2005 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2006
2007 full_screen = TRUE; /* we can use termcap codes from now on */
2008 set_term_defaults(); /* use current values as defaults */
2009#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002010 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002011 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#endif
2013
2014 /*
2015 * Initialize the terminal with the appropriate termcap codes.
2016 * Set the mouse and window title if possible.
2017 * Don't do this when starting, need to parse the .vimrc first, because it
2018 * may redefine t_TI etc.
2019 */
2020 if (starting != NO_SCREEN)
2021 {
2022 starttermcap(); /* may change terminal mode */
2023#ifdef FEAT_MOUSE
2024 setmouse(); /* may start using the mouse */
2025#endif
2026#ifdef FEAT_TITLE
2027 maketitle(); /* may display window title */
2028#endif
2029 }
2030
2031 /* display initial screen after ttest() checking. jw. */
2032 if (width <= 0 || height <= 0)
2033 {
2034 /* termcap failed to report size */
2035 /* set defaults, in case ui_get_shellsize() also fails */
2036 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002037#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 height = 25; /* console is often 25 lines */
2039#else
2040 height = 24; /* most terminals are 24 lines */
2041#endif
2042 }
2043 set_shellsize(width, height, FALSE); /* may change Rows */
2044 if (starting != NO_SCREEN)
2045 {
2046 if (scroll_region)
2047 scroll_region_reset(); /* In case Rows changed */
2048 check_map_keycodes(); /* check mappings for terminal codes used */
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002051 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053 /*
2054 * Execute the TermChanged autocommands for each buffer that is
2055 * loaded.
2056 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002057 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002058 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060 if (curbuf->b_ml.ml_mfp != NULL)
2061 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2062 curbuf);
2063 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002064 if (bufref_valid(&old_curbuf))
2065 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069#ifdef FEAT_TERMRESPONSE
2070 may_req_termresponse();
2071#endif
2072
2073 return OK;
2074}
2075
2076#if defined(FEAT_MOUSE) || defined(PROTO)
2077
2078# ifdef FEAT_MOUSE_TTY
2079# define HMT_NORMAL 1
2080# define HMT_NETTERM 2
2081# define HMT_DEC 4
2082# define HMT_JSBTERM 8
2083# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002084# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002085# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002086# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087static int has_mouse_termcode = 0;
2088# endif
2089
Bram Moolenaar864207d2008-06-24 22:14:38 +00002090# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002092set_mouse_termcode(
2093 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2094 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
2096 char_u name[2];
2097
2098 name[0] = n;
2099 name[1] = KE_FILLER;
2100 add_termcode(name, s, FALSE);
2101# ifdef FEAT_MOUSE_TTY
2102# ifdef FEAT_MOUSE_JSB
2103 if (n == KS_JSBTERM_MOUSE)
2104 has_mouse_termcode |= HMT_JSBTERM;
2105 else
2106# endif
2107# ifdef FEAT_MOUSE_NET
2108 if (n == KS_NETTERM_MOUSE)
2109 has_mouse_termcode |= HMT_NETTERM;
2110 else
2111# endif
2112# ifdef FEAT_MOUSE_DEC
2113 if (n == KS_DEC_MOUSE)
2114 has_mouse_termcode |= HMT_DEC;
2115 else
2116# endif
2117# ifdef FEAT_MOUSE_PTERM
2118 if (n == KS_PTERM_MOUSE)
2119 has_mouse_termcode |= HMT_PTERM;
2120 else
2121# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002122# ifdef FEAT_MOUSE_URXVT
2123 if (n == KS_URXVT_MOUSE)
2124 has_mouse_termcode |= HMT_URXVT;
2125 else
2126# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002127# ifdef FEAT_MOUSE_SGR
2128 if (n == KS_SGR_MOUSE)
2129 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002130 else if (n == KS_SGR_MOUSE_RELEASE)
2131 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002132 else
2133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 has_mouse_termcode |= HMT_NORMAL;
2135# endif
2136}
2137# endif
2138
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002139# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002140 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002142del_mouse_termcode(
2143 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 char_u name[2];
2146
2147 name[0] = n;
2148 name[1] = KE_FILLER;
2149 del_termcode(name);
2150# ifdef FEAT_MOUSE_TTY
2151# ifdef FEAT_MOUSE_JSB
2152 if (n == KS_JSBTERM_MOUSE)
2153 has_mouse_termcode &= ~HMT_JSBTERM;
2154 else
2155# endif
2156# ifdef FEAT_MOUSE_NET
2157 if (n == KS_NETTERM_MOUSE)
2158 has_mouse_termcode &= ~HMT_NETTERM;
2159 else
2160# endif
2161# ifdef FEAT_MOUSE_DEC
2162 if (n == KS_DEC_MOUSE)
2163 has_mouse_termcode &= ~HMT_DEC;
2164 else
2165# endif
2166# ifdef FEAT_MOUSE_PTERM
2167 if (n == KS_PTERM_MOUSE)
2168 has_mouse_termcode &= ~HMT_PTERM;
2169 else
2170# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002171# ifdef FEAT_MOUSE_URXVT
2172 if (n == KS_URXVT_MOUSE)
2173 has_mouse_termcode &= ~HMT_URXVT;
2174 else
2175# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002176# ifdef FEAT_MOUSE_SGR
2177 if (n == KS_SGR_MOUSE)
2178 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002179 else if (n == KS_SGR_MOUSE_RELEASE)
2180 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002181 else
2182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 has_mouse_termcode &= ~HMT_NORMAL;
2184# endif
2185}
2186# endif
2187#endif
2188
2189#ifdef HAVE_TGETENT
2190/*
2191 * Call tgetent()
2192 * Return error message if it fails, NULL if it's OK.
2193 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002194 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002195tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
2197 int i;
2198
2199 i = TGETENT(tbuf, term);
2200 if (i < 0 /* -1 is always an error */
2201# ifdef TGETENT_ZERO_ERR
2202 || i == 0 /* sometimes zero is also an error */
2203# endif
2204 )
2205 {
2206 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2207 * tgetent() with the always existing "dumb" entry to avoid a crash or
2208 * hang. */
2209 (void)TGETENT(tbuf, "dumb");
2210
2211 if (i < 0)
2212# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002213 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (i == 0)
2215# endif
2216#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002219 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
2221 }
2222 return NULL;
2223}
2224
2225/*
2226 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2227 * Fix that here.
2228 */
2229 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002230vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 char *p;
2233
2234 p = tgetstr(s, (char **)pp);
2235 if (p == (char *)-1)
2236 p = NULL;
2237 return (char_u *)p;
2238}
2239#endif /* HAVE_TGETENT */
2240
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002241#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242/*
2243 * Get Columns and Rows from the termcap. Used after a window signal if the
2244 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2245 * and "li" entries never change. But on some systems this works.
2246 * Errors while getting the entries are ignored.
2247 */
2248 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002249getlinecol(
2250 long *cp, /* pointer to columns */
2251 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 char_u tbuf[TBUFSZ];
2254
2255 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2256 {
2257 if (*cp == 0)
2258 *cp = tgetnum("co");
2259 if (*rp == 0)
2260 *rp = tgetnum("li");
2261 }
2262}
2263#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2264
2265/*
2266 * Get a string entry from the termcap and add it to the list of termcodes.
2267 * Used for <t_xx> special keys.
2268 * Give an error message for failure when not sourcing.
2269 * If force given, replace an existing entry.
2270 * Return FAIL if the entry was not found, OK if the entry was added.
2271 */
2272 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002273add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 char_u *term;
2276 int key;
2277 struct builtin_term *termp;
2278#ifdef HAVE_TGETENT
2279 char_u *string;
2280 int i;
2281 int builtin_first;
2282 char_u tbuf[TBUFSZ];
2283 char_u tstrbuf[TBUFSZ];
2284 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002285 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#endif
2287
2288/*
2289 * If the GUI is running or will start in a moment, we only support the keys
2290 * that the GUI can produce.
2291 */
2292#ifdef FEAT_GUI
2293 if (gui.in_use || gui.starting)
2294 return gui_mch_haskey(name);
2295#endif
2296
2297 if (!force && find_termcode(name) != NULL) /* it's already there */
2298 return OK;
2299
2300 term = T_NAME;
2301 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2302 return FAIL;
2303
2304 if (term_is_builtin(term)) /* name starts with "builtin_" */
2305 {
2306 term += 8;
2307#ifdef HAVE_TGETENT
2308 builtin_first = TRUE;
2309#endif
2310 }
2311#ifdef HAVE_TGETENT
2312 else
2313 builtin_first = p_tbi;
2314#endif
2315
2316#ifdef HAVE_TGETENT
2317/*
2318 * We can get the entry from the builtin termcap and from the external one.
2319 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2320 * builtin termcap first.
2321 * If 'ttybuiltin' is off, try external termcap first.
2322 */
2323 for (i = 0; i < 2; ++i)
2324 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002325 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#endif
2327 /*
2328 * Search in builtin termcap
2329 */
2330 {
2331 termp = find_builtin_term(term);
2332 if (termp->bt_string != NULL) /* found it */
2333 {
2334 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002335 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 while (termp->bt_entry != (int)KS_NAME)
2337 {
2338 if ((int)termp->bt_entry == key)
2339 {
2340 add_termcode(name, (char_u *)termp->bt_string,
2341 term_is_8bit(term));
2342 return OK;
2343 }
2344 ++termp;
2345 }
2346 }
2347 }
2348#ifdef HAVE_TGETENT
2349 else
2350 /*
2351 * Search in external termcap
2352 */
2353 {
2354 error_msg = tgetent_error(tbuf, term);
2355 if (error_msg == NULL)
2356 {
2357 string = TGETSTR((char *)name, &tp);
2358 if (string != NULL && *string != NUL)
2359 {
2360 add_termcode(name, string, FALSE);
2361 return OK;
2362 }
2363 }
2364 }
2365 }
2366#endif
2367
2368 if (sourcing_name == NULL)
2369 {
2370#ifdef HAVE_TGETENT
2371 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002372 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 else
2374#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002375 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 return FAIL;
2378}
2379
2380 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002381term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2384}
2385
2386/*
2387 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2388 * Assume that the terminal is using 8-bit controls when the name contains
2389 * "8bit", like in "xterm-8bit".
2390 */
2391 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002392term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2395}
2396
2397/*
2398 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002399 * <Esc>[ -> CSI <M_C_[>
2400 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 * <Esc>O -> <M-C-O>
2402 */
2403 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002404term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 if (*p == ESC)
2407 {
2408 if (p[1] == '[')
2409 return CSI;
2410 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002411 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (p[1] == 'O')
2413 return 0x8f;
2414 }
2415 return 0;
2416}
2417
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002418#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002420term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2423}
2424#endif
2425
2426#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2427
2428 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002429tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 static char_u buf[16];
2432 char_u *p;
2433
2434 p = buf + 15;
2435 *p = '\0';
2436 do
2437 {
2438 --p;
2439 *p = (char_u) (i % 10 + '0');
2440 i /= 10;
2441 }
2442 while (i > 0 && p > buf);
2443 return p;
2444}
2445#endif
2446
2447#ifndef HAVE_TGETENT
2448
2449/*
2450 * minimal tgoto() implementation.
2451 * no padding and we only parse for %i %d and %+char
2452 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002453 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002454tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456 static char buf[30];
2457 char *p, *s, *e;
2458
2459 if (!cm)
2460 return "OOPS";
2461 e = buf + 29;
2462 for (s = buf; s < e && *cm; cm++)
2463 {
2464 if (*cm != '%')
2465 {
2466 *s++ = *cm;
2467 continue;
2468 }
2469 switch (*++cm)
2470 {
2471 case 'd':
2472 p = (char *)tltoa((unsigned long)y);
2473 y = x;
2474 while (*p)
2475 *s++ = *p++;
2476 break;
2477 case 'i':
2478 x++;
2479 y++;
2480 break;
2481 case '+':
2482 *s++ = (char)(*++cm + y);
2483 y = x;
2484 break;
2485 case '%':
2486 *s++ = *cm;
2487 break;
2488 default:
2489 return "OOPS";
2490 }
2491 }
2492 *s = '\0';
2493 return buf;
2494}
2495
2496#endif /* HAVE_TGETENT */
2497
2498/*
2499 * Set the terminal name and initialize the terminal options.
2500 * If "name" is NULL or empty, get the terminal name from the environment.
2501 * If that fails, use the default terminal name.
2502 */
2503 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002504termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 char_u *term;
2507
2508 if (name != NULL && *name == NUL)
2509 name = NULL; /* empty name is equal to no name */
2510 term = name;
2511
2512#ifdef __BEOS__
2513 /*
2514 * TERM environment variable is normally set to 'ansi' on the Bebox;
2515 * Since the BeBox doesn't quite support full ANSI yet, we use our
2516 * own custom 'ansi-beos' termcap instead, unless the -T option has
2517 * been given on the command line.
2518 */
2519 if (term == NULL
2520 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2521 term = DEFAULT_TERM;
2522#endif
2523#ifndef MSWIN
2524 if (term == NULL)
2525 term = mch_getenv((char_u *)"TERM");
2526#endif
2527 if (term == NULL || *term == NUL)
2528 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 /* Set the default terminal name. */
2532 set_string_default("term", term);
2533 set_string_default("ttytype", term);
2534
2535 /*
2536 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2537 */
2538 set_termname(T_NAME != NULL ? T_NAME : term);
2539}
2540
2541/*
2542 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2543 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002544#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2546static char_u out_buf[OUT_SIZE + 1];
2547static int out_pos = 0; /* number of chars in out_buf */
2548
2549/*
2550 * out_flush(): flush the output buffer
2551 */
2552 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002553out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int len;
2556
2557 if (out_pos != 0)
2558 {
2559 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2560 len = out_pos;
2561 out_pos = 0;
2562 ui_write(out_buf, len);
2563 }
2564}
2565
Bram Moolenaara338adc2018-01-31 20:51:47 +01002566/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002567 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2568 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002569 */
2570 void
2571out_flush_cursor(
2572 int force UNUSED, /* when TRUE, update cursor even when not moved */
2573 int clear_selection UNUSED) /* clear selection under cursor */
2574{
2575 mch_disable_flush();
2576 out_flush();
2577 mch_enable_flush();
2578#ifdef FEAT_GUI
2579 if (gui.in_use)
2580 {
2581 gui_update_cursor(force, clear_selection);
2582 gui_may_flush();
2583 }
2584#endif
2585}
2586
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Sometimes a byte out of a multi-byte character is written with out_char().
2590 * To avoid flushing half of the character, call this function first.
2591 */
2592 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2596 out_flush();
2597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599#ifdef FEAT_GUI
2600/*
2601 * out_trash(): Throw away the contents of the output buffer
2602 */
2603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002604out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 out_pos = 0;
2607}
2608#endif
2609
2610/*
2611 * out_char(c): put a byte into the output buffer.
2612 * Flush it if it becomes full.
2613 * This should not be used for outputting text on the screen (use functions
2614 * like msg_puts() and screen_putchar() for that).
2615 */
2616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002617out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaard0573012017-10-28 21:11:06 +02002619#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2621 out_char('\r');
2622#endif
2623
2624 out_buf[out_pos++] = c;
2625
2626 /* For testing we flush each time. */
2627 if (out_pos >= OUT_SIZE || p_wd)
2628 out_flush();
2629}
2630
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002631static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633/*
2634 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2635 */
2636 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002637out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
Bram Moolenaard0573012017-10-28 21:11:06 +02002639#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2641 out_char_nf('\r');
2642#endif
2643
2644 out_buf[out_pos++] = c;
2645
2646 if (out_pos >= OUT_SIZE)
2647 out_flush();
2648}
2649
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002650#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2651 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
2653 * A never-padding out_str.
2654 * use this whenever you don't want to run the string through tputs.
2655 * tputs above is harmless, but tputs from the termcap library
2656 * is likely to strip off leading digits, that it mistakes for padding
2657 * information, and "%i", "%d", etc.
2658 * This should only be used for writing terminal codes, not for outputting
2659 * normal text (use functions like msg_puts() and screen_putchar() for that).
2660 */
2661 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002662out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2665 out_flush();
2666 while (*s)
2667 out_char_nf(*s++);
2668
2669 /* For testing we write one string at a time. */
2670 if (p_wd)
2671 out_flush();
2672}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002676 * A conditional-flushing out_str, mainly for visualbell.
2677 * Handles a delay internally, because termlib may not respect the delay or do
2678 * it at the wrong time.
2679 * Note: Only for terminal strings.
2680 */
2681 void
2682out_str_cf(char_u *s)
2683{
2684 if (s != NULL && *s)
2685 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002686#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002687 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002688#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002689
2690#ifdef FEAT_GUI
2691 /* Don't use tputs() when GUI is used, ncurses crashes. */
2692 if (gui.in_use)
2693 {
2694 out_str_nf(s);
2695 return;
2696 }
2697#endif
2698 if (out_pos > OUT_SIZE - 20)
2699 out_flush();
2700#ifdef HAVE_TGETENT
2701 for (p = s; *s; ++s)
2702 {
2703 /* flush just before delay command */
2704 if (*s == '$' && *(s + 1) == '<')
2705 {
2706 char_u save_c = *s;
2707 int duration = atoi((char *)s + 2);
2708
2709 *s = NUL;
2710 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2711 *s = save_c;
2712 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002713# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002714 /* Only sleep here if we can limit this happening in
2715 * vim_beep(). */
2716 p = vim_strchr(s, '>');
2717 if (p == NULL || duration <= 0)
2718 {
2719 /* can't parse the time, don't sleep here */
2720 p = s;
2721 }
2722 else
2723 {
2724 ++p;
2725 do_sleep(duration);
2726 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002727# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002728 /* Rely on the terminal library to sleep. */
2729 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002730# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002731 break;
2732 }
2733 }
2734 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2735#else
2736 while (*s)
2737 out_char_nf(*s++);
2738#endif
2739
2740 /* For testing we write one string at a time. */
2741 if (p_wd)
2742 out_flush();
2743 }
2744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * out_str(s): Put a character string a byte at a time into the output buffer.
2748 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2749 * This should only be used for writing terminal codes, not for outputting
2750 * normal text (use functions like msg_puts() and screen_putchar() for that).
2751 */
2752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002753out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
2755 if (s != NULL && *s)
2756 {
2757#ifdef FEAT_GUI
2758 /* Don't use tputs() when GUI is used, ncurses crashes. */
2759 if (gui.in_use)
2760 {
2761 out_str_nf(s);
2762 return;
2763 }
2764#endif
2765 /* avoid terminal strings being split up */
2766 if (out_pos > OUT_SIZE - 20)
2767 out_flush();
2768#ifdef HAVE_TGETENT
2769 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2770#else
2771 while (*s)
2772 out_char_nf(*s++);
2773#endif
2774
2775 /* For testing we write one string at a time. */
2776 if (p_wd)
2777 out_flush();
2778 }
2779}
2780
2781/*
2782 * cursor positioning using termcap parser. (jw)
2783 */
2784 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002785term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 OUT_STR(tgoto((char *)T_CM, col, row));
2788}
2789
2790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002791term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 OUT_STR(tgoto((char *)T_CRI, 0, i));
2794}
2795
2796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002797term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2800}
2801
2802 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002803term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2806}
2807
2808#if defined(HAVE_TGETENT) || defined(PROTO)
2809 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002810term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812 /* Can't handle a negative value here */
2813 if (x < 0)
2814 x = 0;
2815 if (y < 0)
2816 y = 0;
2817 OUT_STR(tgoto((char *)T_CWP, y, x));
2818}
2819
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002820# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2821/*
2822 * Return TRUE if we can request the terminal for a response.
2823 */
2824 static int
2825can_get_termresponse()
2826{
2827 return cur_tmode == TMODE_RAW
2828 && termcap_active
2829# ifdef UNIX
2830 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2831# endif
2832 && p_ek;
2833}
2834
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002835static int winpos_x = -1;
2836static int winpos_y = -1;
2837static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838
Bram Moolenaar113e1072019-01-20 15:30:40 +01002839# if (defined(FEAT_EVAL) && defined(HAVE_TGETENT)) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002840/*
2841 * Try getting the Vim window position from the terminal.
2842 * Returns OK or FAIL.
2843 */
2844 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002845term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002846{
2847 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002848 int prev_winpos_x = winpos_x;
2849 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002850
2851 if (*T_CGP == NUL || !can_get_termresponse())
2852 return FAIL;
2853 winpos_x = -1;
2854 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002855 ++did_request_winpos;
2856 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002857 OUT_STR(T_CGP);
2858 out_flush();
2859
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002860 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002861 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002862 {
2863 (void)vpeekc_nomap();
2864 if (winpos_x >= 0 && winpos_y >= 0)
2865 {
2866 *x = winpos_x;
2867 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002868 return OK;
2869 }
2870 ui_delay(10, FALSE);
2871 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002872 /* Do not reset "did_request_winpos", if we timed out the response might
2873 * still come later and we must consume it. */
2874
2875 winpos_x = prev_winpos_x;
2876 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002877 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002878 {
2879 /* Polling: return previous values if we have them. */
2880 *x = winpos_x;
2881 *y = winpos_y;
2882 return OK;
2883 }
2884
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002885 return FALSE;
2886}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002887# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002888# endif
2889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002891term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002893 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895#endif
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002898term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002901 int i = *s == CSI ? 1 : 2;
2902 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 /* Special handling of 16 colors, because termcap can't handle it */
2905 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2906 /* Also accept CSI instead of <Esc>[ */
2907 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002908 && ((s[0] == ESC && s[1] == '[')
2909#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2910 || (s[0] == ESC && s[1] == '|')
2911#endif
2912 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 && s[i] != NUL
2914 && (STRCMP(s + i + 1, "%p1%dm") == 0
2915 || STRCMP(s + i + 1, "%dm") == 0)
2916 && (s[i] == '3' || s[i] == '4'))
2917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002919 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002921 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002923 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002924#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002925 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002926#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002927 IF_EB("\033[", ESC_STR "[")) : "\233";
2928 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2929 : (n >= 16 ? "48;5;" : "10");
2930
2931 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2933 }
2934 else
2935 OUT_STR(tgoto((char *)s, 0, n));
2936}
2937
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002938 void
2939term_fg_color(int n)
2940{
2941 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2942 if (*T_CAF)
2943 term_color(T_CAF, n);
2944 else if (*T_CSF)
2945 term_color(T_CSF, n);
2946}
2947
2948 void
2949term_bg_color(int n)
2950{
2951 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2952 if (*T_CAB)
2953 term_color(T_CAB, n);
2954 else if (*T_CSB)
2955 term_color(T_CSB, n);
2956}
2957
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002958#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002959
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002960#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2961#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2962#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002963
2964 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002965term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002966{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002967#define MAX_COLOR_STR_LEN 100
2968 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002969
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002970 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002971 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002972 OUT_STR(buf);
2973}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002974
2975 void
2976term_fg_rgb_color(guicolor_T rgb)
2977{
2978 term_rgb_color(T_8F, rgb);
2979}
2980
2981 void
2982term_bg_rgb_color(guicolor_T rgb)
2983{
2984 term_rgb_color(T_8B, rgb);
2985}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002986#endif
2987
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002988#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2989 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
2991 * Generic function to set window title, using t_ts and t_fs.
2992 */
2993 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002994term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 /* t_ts takes one argument: column in status line */
2997 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2998 out_str_nf(title);
2999 out_str(T_FS); /* set title end */
3000 out_flush();
3001}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003002
3003/*
3004 * Tell the terminal to push (save) the title and/or icon, so that it can be
3005 * popped (restored) later.
3006 */
3007 void
3008term_push_title(int which)
3009{
3010 if ((which & SAVE_RESTORE_TITLE) && *T_CST != NUL)
3011 {
3012 OUT_STR(T_CST);
3013 out_flush();
3014 }
3015
3016 if ((which & SAVE_RESTORE_ICON) && *T_SSI != NUL)
3017 {
3018 OUT_STR(T_SSI);
3019 out_flush();
3020 }
3021}
3022
3023/*
3024 * Tell the terminal to pop the title and/or icon.
3025 */
3026 void
3027term_pop_title(int which)
3028{
3029 if ((which & SAVE_RESTORE_TITLE) && *T_CRT != NUL)
3030 {
3031 OUT_STR(T_CRT);
3032 out_flush();
3033 }
3034
3035 if ((which & SAVE_RESTORE_ICON) && *T_SRI != NUL)
3036 {
3037 OUT_STR(T_SRI);
3038 out_flush();
3039 }
3040}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041#endif
3042
3043/*
3044 * Make sure we have a valid set or terminal options.
3045 * Replace all entries that are NULL by empty_option
3046 */
3047 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003048ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003050 char_u *env_colors;
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 check_options(); /* make sure no options are NULL */
3053
3054 /*
3055 * MUST have "cm": cursor motion.
3056 */
3057 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003058 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 /*
3061 * if "cs" defined, use a scroll region, it's faster.
3062 */
3063 if (*T_CS != NUL)
3064 scroll_region = TRUE;
3065 else
3066 scroll_region = FALSE;
3067
3068 if (pairs)
3069 {
3070 /*
3071 * optional pairs
3072 */
3073 /* TP goes to normal mode for TI (invert) and TB (bold) */
3074 if (*T_ME == NUL)
3075 T_ME = T_MR = T_MD = T_MB = empty_option;
3076 if (*T_SO == NUL || *T_SE == NUL)
3077 T_SO = T_SE = empty_option;
3078 if (*T_US == NUL || *T_UE == NUL)
3079 T_US = T_UE = empty_option;
3080 if (*T_CZH == NUL || *T_CZR == NUL)
3081 T_CZH = T_CZR = empty_option;
3082
3083 /* T_VE is needed even though T_VI is not defined */
3084 if (*T_VE == NUL)
3085 T_VI = empty_option;
3086
3087 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3088 if (*T_ME == NUL)
3089 {
3090 T_ME = T_SE;
3091 T_MR = T_SO;
3092 T_MD = T_SO;
3093 }
3094
3095 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3096 if (*T_SO == NUL)
3097 {
3098 T_SE = T_ME;
3099 if (*T_MR == NUL)
3100 T_SO = T_MD;
3101 else
3102 T_SO = T_MR;
3103 }
3104
3105 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3106 if (*T_CZH == NUL)
3107 {
3108 T_CZR = T_ME;
3109 if (*T_MR == NUL)
3110 T_CZH = T_MD;
3111 else
3112 T_CZH = T_MR;
3113 }
3114
3115 /* "Sb" and "Sf" come in pairs */
3116 if (*T_CSB == NUL || *T_CSF == NUL)
3117 {
3118 T_CSB = empty_option;
3119 T_CSF = empty_option;
3120 }
3121
3122 /* "AB" and "AF" come in pairs */
3123 if (*T_CAB == NUL || *T_CAF == NUL)
3124 {
3125 T_CAB = empty_option;
3126 T_CAF = empty_option;
3127 }
3128
3129 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3130 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003131 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133 /* Set 'weirdinvert' according to value of 't_xs' */
3134 p_wiv = (*T_XS != NUL);
3135 }
3136 need_gather = TRUE;
3137
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003138 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003140 env_colors = mch_getenv((char_u *)"COLORS");
3141 if (env_colors != NULL && isdigit(*env_colors))
3142 {
3143 int colors = atoi((char *)env_colors);
3144
3145 if (colors != t_colors)
3146 set_color_count(colors);
3147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3151 || defined(PROTO)
3152/*
3153 * Represent the given long_u as individual bytes, with the most significant
3154 * byte first, and store them in dst.
3155 */
3156 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003157add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
3159 int i;
3160 int shift;
3161
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003162 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 shift = 8 * (sizeof(long_u) - i);
3165 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3166 }
3167}
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169/*
3170 * Interpret the next string of bytes in buf as a long integer, with the most
3171 * significant byte first. Note that it is assumed that buf has been through
3172 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3173 * Puts result in val, and returns the number of bytes read from buf
3174 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3175 * were present.
3176 */
3177 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003178get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179{
3180 int len;
3181 char_u bytes[sizeof(long_u)];
3182 int i;
3183 int shift;
3184
3185 *val = 0;
3186 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3187 if (len != -1)
3188 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003189 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 shift = 8 * (sizeof(long_u) - 1 - i);
3192 *val += (long_u)bytes[i] << shift;
3193 }
3194 }
3195 return len;
3196}
3197#endif
3198
3199#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003200 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3201 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202/*
3203 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3204 * that buf has been through inchar(). Returns the actual number of bytes used
3205 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3206 * available.
3207 */
3208 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003209get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int len = 0;
3212 int i;
3213 char_u c;
3214
3215 for (i = 0; i < num_bytes; i++)
3216 {
3217 if ((c = buf[len++]) == NUL)
3218 return -1;
3219 if (c == K_SPECIAL)
3220 {
3221 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3222 return -1;
3223 if (buf[len++] == (int)KS_ZERO)
3224 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003225 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3226 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3227 if (buf[len++] == (int)KE_CSI)
3228 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003230 else if (c == CSI && buf[len] == KS_EXTRA
3231 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003232 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3233 * the start of a special key, see add_to_input_buf_csi(). */
3234 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 bytes[i] = c;
3236 }
3237 return len;
3238}
3239#endif
3240
3241/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003242 * Check if the new shell size is valid, correct it if it's too small or way
3243 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
3245 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003246check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (Rows < min_rows()) /* need room for one window and command line */
3249 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003250 limit_screen_size();
3251}
3252
3253/*
3254 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3255 */
3256 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003257limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003258{
3259 if (Columns < MIN_COLUMNS)
3260 Columns = MIN_COLUMNS;
3261 else if (Columns > 10000)
3262 Columns = 10000;
3263 if (Rows > 1000)
3264 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265}
3266
Bram Moolenaar86b68352004-12-27 21:59:20 +00003267/*
3268 * Invoked just before the screen structures are going to be (re)allocated.
3269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003271win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 static int old_Rows = 0;
3274 static int old_Columns = 0;
3275
3276 if (old_Rows != Rows || old_Columns != Columns)
3277 ui_new_shellsize();
3278 if (old_Rows != Rows)
3279 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003280 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003281 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003282 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 old_Rows = Rows;
3284 shell_new_rows(); /* update window sizes */
3285 }
3286 if (old_Columns != Columns)
3287 {
3288 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291}
3292
3293/*
3294 * Call this function when the Vim shell has been resized in any way.
3295 * Will obtain the current size and redraw (also when size didn't change).
3296 */
3297 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003298shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 set_shellsize(0, 0, FALSE);
3301}
3302
3303/*
3304 * Check if the shell size changed. Handle a resize.
3305 * When the size didn't change, nothing happens.
3306 */
3307 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003308shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int old_Rows = Rows;
3311 int old_Columns = Columns;
3312
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003313 if (!exiting
3314#ifdef FEAT_GUI
3315 /* Do not get the size when executing a shell command during
3316 * startup. */
3317 && !gui.starting
3318#endif
3319 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003320 {
3321 (void)ui_get_shellsize();
3322 check_shellsize();
3323 if (old_Rows != Rows || old_Columns != Columns)
3324 shell_resized();
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
3329 * Set size of the Vim shell.
3330 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3331 * window size (this is used for the :win command).
3332 * If 'mustset' is FALSE, we may try to get the real window size and if
3333 * it fails use 'width' and 'height'.
3334 */
3335 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003336set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 static int busy = FALSE;
3339
3340 /*
3341 * Avoid recursiveness, can happen when setting the window size causes
3342 * another window-changed signal.
3343 */
3344 if (busy)
3345 return;
3346
3347 if (width < 0 || height < 0) /* just checking... */
3348 return;
3349
Bram Moolenaara971b822011-09-14 14:43:25 +02003350 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003352 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 State = SETWSIZE;
3354 return;
3355 }
3356
Bram Moolenaara971b822011-09-14 14:43:25 +02003357 /* curwin->w_buffer can be NULL when we are closing a window and the
3358 * buffer has already been closed and removing a scrollbar causes a resize
3359 * event. Don't resize then, it will happen after entering another buffer.
3360 */
3361 if (curwin->w_buffer == NULL)
3362 return;
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 ++busy;
3365
3366#ifdef AMIGA
3367 out_flush(); /* must do this before mch_get_shellsize() for
3368 some obscure reason */
3369#endif
3370
3371 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3372 {
3373 Rows = height;
3374 Columns = width;
3375 check_shellsize();
3376 ui_set_shellsize(mustset);
3377 }
3378 else
3379 check_shellsize();
3380
3381 /* The window layout used to be adjusted here, but it now happens in
3382 * screenalloc() (also invoked from screenclear()). That is because the
3383 * "busy" check above may skip this, but not screenalloc(). */
3384
3385 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3386 screenclear();
3387 else
3388 screen_start(); /* don't know where cursor is now */
3389
3390 if (starting != NO_SCREEN)
3391 {
3392#ifdef FEAT_TITLE
3393 maketitle();
3394#endif
3395 changed_line_abv_curs();
3396 invalidate_botline();
3397
3398 /*
3399 * We only redraw when it's needed:
3400 * - While at the more prompt or executing an external command, don't
3401 * redraw, but position the cursor.
3402 * - While editing the command line, only redraw that.
3403 * - in Ex mode, don't redraw anything.
3404 * - Otherwise, redraw right now, and position the cursor.
3405 * Always need to call update_screen() or screenalloc(), to make
3406 * sure Rows/Columns and the size of ScreenLines[] is correct!
3407 */
3408 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3409 || exmode_active)
3410 {
3411 screenalloc(FALSE);
3412 repeat_message();
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
3415 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003416 if (curwin->w_p_scb)
3417 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003418 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003419 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003420 update_screen(NOT_VALID);
3421 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003422 }
3423 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003424 {
3425 update_topline();
3426#if defined(FEAT_INS_EXPAND)
3427 if (pum_visible())
3428 {
3429 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003430 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003431 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003432#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003433 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003434 if (redrawing())
3435 setcursor();
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 cursor_on(); /* redrawing may have switched it off */
3439 }
3440 out_flush();
3441 --busy;
3442}
3443
3444/*
3445 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3446 * commands and Ex mode).
3447 */
3448 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003449settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451#ifdef FEAT_GUI
3452 /* don't set the term where gvim was started to any mode */
3453 if (gui.in_use)
3454 return;
3455#endif
3456
3457 if (full_screen)
3458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 /*
3460 * When returning after calling a shell we want to really set the
3461 * terminal to raw mode, even though we think it already is, because
3462 * the shell program may have reset the terminal mode.
3463 * When we think the terminal is normal, don't try to set it to
3464 * normal again, because that causes problems (logout!) on some
3465 * machines.
3466 */
3467 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3468 {
3469#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003470# ifdef FEAT_GUI
3471 if (!gui.in_use && !gui.starting)
3472# endif
3473 {
3474 /* May need to check for T_CRV response and termcodes, it
3475 * doesn't work in Cooked mode, an external program may get
3476 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003477 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3478 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003479#ifdef FEAT_TERMINAL
3480 || rfg_status == STATUS_SENT
3481#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003482 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003483 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003484 || rcs_status == STATUS_SENT
3485 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003486 (void)vpeekc_nomap();
3487 check_for_codes_from_term();
3488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
3490#ifdef FEAT_MOUSE_TTY
3491 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003492 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003494 if (tmode != TMODE_RAW)
3495 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003497 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 cur_tmode = tmode;
3499#ifdef FEAT_MOUSE
3500 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003501 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003503 if (tmode == TMODE_RAW)
3504 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 out_flush();
3506 }
3507#ifdef FEAT_TERMRESPONSE
3508 may_req_termresponse();
3509#endif
3510 }
3511}
3512
3513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003514starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 if (full_screen && !termcap_active)
3517 {
3518 out_str(T_TI); /* start termcap mode */
3519 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003520 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 out_flush();
3522 termcap_active = TRUE;
3523 screen_start(); /* don't know where cursor is now */
3524#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003525# ifdef FEAT_GUI
3526 if (!gui.in_use && !gui.starting)
3527# endif
3528 {
3529 may_req_termresponse();
3530 /* Immediately check for a response. If t_Co changes, we don't
3531 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003532 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003533 check_for_codes_from_term();
3534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif
3536 }
3537}
3538
3539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 screen_stop_highlight();
3543 reset_cterm_colors();
3544 if (termcap_active)
3545 {
3546#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003547# ifdef FEAT_GUI
3548 if (!gui.in_use && !gui.starting)
3549# endif
3550 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003551 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003552 if (crv_status == STATUS_SENT
3553 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003554# ifdef FEAT_TERMINAL
3555 || rfg_status == STATUS_SENT
3556# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003557 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003558 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003559 || rcs_status == STATUS_SENT
3560 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003561 {
3562# ifdef UNIX
3563 /* Give the terminal a chance to respond. */
3564 mch_delay(100L, FALSE);
3565# endif
3566# ifdef TCIFLUSH
3567 /* Discard data received but not read. */
3568 if (exiting)
3569 tcflush(fileno(stdin), TCIFLUSH);
3570# endif
3571 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003572 /* Check for termcodes first, otherwise an external program may
3573 * get them. */
3574 check_for_codes_from_term();
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003577 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 out_str(T_KE); /* stop "keypad transmit" mode */
3579 out_flush();
3580 termcap_active = FALSE;
3581 cursor_on(); /* just in case it is still off */
3582 out_str(T_TE); /* stop termcap mode */
3583 screen_start(); /* don't know where cursor is now */
3584 out_flush();
3585 }
3586}
3587
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003588#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589/*
3590 * Request version string (for xterm) when needed.
3591 * Only do this after switching to raw mode, otherwise the result will be
3592 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003593 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003594 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * Only do this after termcap mode has been started, otherwise the codes for
3596 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003597 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3598 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003599 * On Unix only do it when both output and input are a tty (avoid writing
3600 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 * The result is caught in check_termcode().
3602 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003604may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003606 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003607 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003608 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 && *T_CRV != NUL)
3610 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003611 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003613 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* check for the characters now, otherwise they might be eaten by
3615 * get_keystroke() */
3616 out_flush();
3617 (void)vpeekc_nomap();
3618 }
3619}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003620
Bram Moolenaar9584b312013-03-13 19:29:28 +01003621/*
3622 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003623 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003624 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003625 * If the terminal treats \u25bd as single width, the position is (1, 1),
3626 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003627 * This function has the side effect that changes cursor position, so
3628 * it must be called immediately after entering termcap mode.
3629 */
3630 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003631may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003632{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003633 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003634 && can_get_termresponse()
3635 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003636 && *T_U7 != NUL
3637 && !option_was_set((char_u *)"ambiwidth"))
3638 {
3639 char_u buf[16];
3640
Bram Moolenaarb255b902018-04-24 21:40:10 +02003641 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003642 /* Do this in the second row. In the first row the returned sequence
3643 * may be CSI 1;2R, which is the same as <S-F3>. */
3644 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003645 buf[mb_char2bytes(0x25bd, buf)] = 0;
3646 out_str(buf);
3647 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003648 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003649 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003650
3651 /* This overwrites a few characters on the screen, a redraw is needed
3652 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003653 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003654 out_str((char_u *)" ");
3655 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003656
Bram Moolenaar05684312017-12-09 15:11:24 +01003657 /* Need to reset the known cursor position. */
3658 screen_start();
3659
Bram Moolenaar9584b312013-03-13 19:29:28 +01003660 /* check for the characters now, otherwise they might be eaten by
3661 * get_keystroke() */
3662 out_flush();
3663 (void)vpeekc_nomap();
3664 }
3665}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003666
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003667/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003668 * Similar to requesting the version string: Request the terminal background
3669 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003670 */
3671 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003672may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003673{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003674 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003675 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003676 int didit = FALSE;
3677
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003678# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003679 /* Only request foreground if t_RF is set. */
3680 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3681 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003682 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003683 out_str(T_RFG);
3684 rfg_status = STATUS_SENT;
3685 didit = TRUE;
3686 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003687# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003688
3689 /* Only request background if t_RB is set. */
3690 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003691 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003692 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003693 out_str(T_RBG);
3694 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003695 didit = TRUE;
3696 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003697
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003698 if (didit)
3699 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003700 /* check for the characters now, otherwise they might be eaten by
3701 * get_keystroke() */
3702 out_flush();
3703 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003704 }
3705 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003706}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003707
Bram Moolenaar2951b772013-07-03 12:45:31 +02003708# ifdef DEBUG_TERMRESPONSE
3709 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003710log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003711{
3712 static FILE *fd_tr = NULL;
3713 static proftime_T start;
3714 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003715 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003716
3717 if (fd_tr == NULL)
3718 {
3719 fd_tr = fopen("termresponse.log", "w");
3720 profile_start(&start);
3721 }
3722 now = start;
3723 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003724 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3725 must_redraw == NOT_VALID ? "NV"
3726 : must_redraw == CLEAR ? "CL" : " ");
3727 va_start(ap, fmt);
3728 vfprintf(fd_tr, fmt, ap);
3729 va_end(ap);
3730 fputc('\n', fd_tr);
3731 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003732}
3733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#endif
3735
3736/*
3737 * Return TRUE when saving and restoring the screen.
3738 */
3739 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003740swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 return (full_screen && *T_TI != NUL);
3743}
3744
Bram Moolenaarecadf432018-03-20 11:17:04 +01003745#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746/*
3747 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3748 */
3749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003750setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752# ifdef FEAT_MOUSE_TTY
3753 int checkfor;
3754# endif
3755
3756# ifdef FEAT_MOUSESHAPE
3757 update_mouseshape(-1);
3758# endif
3759
3760# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3761# ifdef FEAT_GUI
3762 /* In the GUI the mouse is always enabled. */
3763 if (gui.in_use)
3764 return;
3765# endif
3766 /* be quick when mouse is off */
3767 if (*p_mouse == NUL || has_mouse_termcode == 0)
3768 return;
3769
3770 /* don't switch mouse on when not in raw mode (Ex mode) */
3771 if (cur_tmode != TMODE_RAW)
3772 {
3773 mch_setmouse(FALSE);
3774 return;
3775 }
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (VIsual_active)
3778 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003779 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 checkfor = MOUSE_RETURN;
3781 else if (State & INSERT)
3782 checkfor = MOUSE_INSERT;
3783 else if (State & CMDLINE)
3784 checkfor = MOUSE_COMMAND;
3785 else if (State == CONFIRM || State == EXTERNCMD)
3786 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3787 else
3788 checkfor = MOUSE_NORMAL; /* assume normal mode */
3789
3790 if (mouse_has(checkfor))
3791 mch_setmouse(TRUE);
3792 else
3793 mch_setmouse(FALSE);
3794# endif
3795}
3796
3797/*
3798 * Return TRUE if
3799 * - "c" is in 'mouse', or
3800 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3801 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3802 * normal editing mode (not at hit-return message).
3803 */
3804 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003805mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 char_u *p;
3808
3809 for (p = p_mouse; *p; ++p)
3810 switch (*p)
3811 {
3812 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3813 return TRUE;
3814 break;
3815 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3816 return TRUE;
3817 break;
3818 default: if (c == *p) return TRUE; break;
3819 }
3820 return FALSE;
3821}
3822
3823/*
3824 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3825 */
3826 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003827mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 return (p_mousem[0] == 'p');
3830}
3831#endif
3832
3833/*
3834 * By outputting the 'cursor very visible' termcap code, for some windowed
3835 * terminals this makes the screen scrolled to the correct position.
3836 * Used when starting Vim or returning from a shell.
3837 */
3838 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003839scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003841 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003844 out_str(T_CVS);
3845 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847}
3848
3849static int cursor_is_off = FALSE;
3850
3851/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003852 * Enable the cursor without checking if it's already enabled.
3853 */
3854 void
3855cursor_on_force(void)
3856{
3857 out_str(T_VE);
3858 cursor_is_off = FALSE;
3859}
3860
3861/*
3862 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 */
3864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003865cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003868 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
3871/*
3872 * Disable the cursor.
3873 */
3874 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003875cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003877 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003879 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 cursor_is_off = TRUE;
3881 }
3882}
3883
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003884#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003886 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003887 */
3888 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003889term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003890{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003891 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003892 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003893
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003894 /* Only do something when redrawing the screen and we can restore the
3895 * mode. */
3896 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003897 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003898# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003899 if (forced && initial_cursor_shape > 0)
3900 /* Restore to initial values. */
3901 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003902# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003903 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003904 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003905
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003906 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003907 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003908 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003909 {
3910 if (*T_CSR != NUL)
3911 p = T_CSR; /* Replace mode cursor */
3912 else
3913 p = T_CSI; /* fall back to Insert mode cursor */
3914 if (*p != NUL)
3915 {
3916 out_str(p);
3917 showing_mode = REPLACE;
3918 }
3919 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003920 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003921 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003922 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003923 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003924 {
3925 out_str(T_CSI); /* Insert mode cursor */
3926 showing_mode = INSERT;
3927 }
3928 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003929 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003930 {
3931 out_str(T_CEI); /* non-Insert mode cursor */
3932 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003933 }
3934}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003935
3936# if defined(FEAT_TERMINAL) || defined(PROTO)
3937 void
3938term_cursor_color(char_u *color)
3939{
3940 if (*T_CSC != NUL)
3941 {
3942 out_str(T_CSC); /* set cursor color start */
3943 out_str_nf(color);
3944 out_str(T_CEC); /* set cursor color end */
3945 out_flush();
3946 }
3947}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003948# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003949
Bram Moolenaar4db25542017-08-28 22:43:05 +02003950 int
3951blink_state_is_inverted()
3952{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003953#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003954 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3955 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003956#else
3957 return FALSE;
3958#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003959}
3960
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003961/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003962 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003963 */
3964 void
3965term_cursor_shape(int shape, int blink)
3966{
3967 if (*T_CSH != NUL)
3968 {
3969 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3970 out_flush();
3971 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003972 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003973 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003974 int do_blink = blink;
3975
3976 /* t_SH is empty: try setting just the blink state.
3977 * The blink flags are XORed together, if the initial blinking from
3978 * style and shape differs, we need to invert the flag here. */
3979 if (blink_state_is_inverted())
3980 do_blink = !blink;
3981
3982 if (do_blink && *T_VS != NUL)
3983 {
3984 out_str(T_VS);
3985 out_flush();
3986 }
3987 else if (!do_blink && *T_CVS != NUL)
3988 {
3989 out_str(T_CVS);
3990 out_flush();
3991 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003992 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003993}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003994#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995
3996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * Set scrolling region for window 'wp'.
3998 * The region starts 'off' lines from the start of the window.
3999 * Also set the vertical scroll region for a vertically split window. Always
4000 * the full width of the window, excluding the vertical separator.
4001 */
4002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004003scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4006 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004008 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4009 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 screen_start(); /* don't know where cursor is now */
4011}
4012
4013/*
4014 * Reset scrolling region to the whole screen.
4015 */
4016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004017scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (*T_CSV != NUL)
4021 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 screen_start(); /* don't know where cursor is now */
4023}
4024
4025
4026/*
4027 * List of terminal codes that are currently recognized.
4028 */
4029
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004030static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 char_u name[2]; /* termcap name of entry */
4033 char_u *code; /* terminal code (in allocated memory) */
4034 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004035 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036} *termcodes = NULL;
4037
4038static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4039static int tc_len = 0; /* current number of entries in termcodes[] */
4040
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004041static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004044clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 while (tc_len > 0)
4047 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004048 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 tc_max_len = 0;
4050
4051#ifdef HAVE_TGETENT
4052 BC = (char *)empty_option;
4053 UP = (char *)empty_option;
4054 PC = NUL; /* set pad character to NUL */
4055 ospeed = 0;
4056#endif
4057
4058 need_gather = TRUE; /* need to fill termleader[] */
4059}
4060
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061#define ATC_FROM_TERM 55
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063/*
4064 * Add a new entry to the list of terminal codes.
4065 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004066 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4067 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
4069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004070add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 struct termcode *new_tc;
4073 int i, j;
4074 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004075 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 if (string == NULL || *string == NUL)
4078 {
4079 del_termcode(name);
4080 return;
4081 }
4082
Bram Moolenaar45500912014-07-09 20:51:07 +02004083#if defined(WIN3264) && !defined(FEAT_GUI)
4084 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 if (s == NULL)
4089 return;
4090
4091 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004094 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 s[0] = term_7to8bit(string);
4096 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004097
4098#if defined(WIN3264) && !defined(FEAT_GUI)
4099 if (s[0] == K_NUL)
4100 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004101 STRMOVE(s + 1, s);
4102 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004103 }
4104#endif
4105
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004106 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 need_gather = TRUE; /* need to fill termleader[] */
4109
4110 /*
4111 * need to make space for more entries
4112 */
4113 if (tc_len == tc_max_len)
4114 {
4115 tc_max_len += 20;
4116 new_tc = (struct termcode *)alloc(
4117 (unsigned)(tc_max_len * sizeof(struct termcode)));
4118 if (new_tc == NULL)
4119 {
4120 tc_max_len -= 20;
4121 return;
4122 }
4123 for (i = 0; i < tc_len; ++i)
4124 new_tc[i] = termcodes[i];
4125 vim_free(termcodes);
4126 termcodes = new_tc;
4127 }
4128
4129 /*
4130 * Look for existing entry with the same name, it is replaced.
4131 * Look for an existing entry that is alphabetical higher, the new entry
4132 * is inserted in front of it.
4133 */
4134 for (i = 0; i < tc_len; ++i)
4135 {
4136 if (termcodes[i].name[0] < name[0])
4137 continue;
4138 if (termcodes[i].name[0] == name[0])
4139 {
4140 if (termcodes[i].name[1] < name[1])
4141 continue;
4142 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 if (termcodes[i].name[1] == name[1])
4146 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 if (flags == ATC_FROM_TERM && (j = termcode_star(
4148 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004149 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 /* Don't replace ESC[123;*X or ESC O*X with another when
4151 * invoked from got_code_from_term(). */
4152 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004153 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 && s[len - 1]
4155 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004157 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004158 vim_free(s);
4159 return;
4160 }
4161 }
4162 else
4163 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004164 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165 vim_free(termcodes[i].code);
4166 --tc_len;
4167 break;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 }
4171 /*
4172 * Found alphabetical larger entry, move rest to insert new entry
4173 */
4174 for (j = tc_len; j > i; --j)
4175 termcodes[j] = termcodes[j - 1];
4176 break;
4177 }
4178
4179 termcodes[i].name[0] = name[0];
4180 termcodes[i].name[1] = name[1];
4181 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004183
4184 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4185 * accept modifiers. */
4186 termcodes[i].modlen = 0;
4187 j = termcode_star(s, len);
4188 if (j > 0)
4189 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 ++tc_len;
4191}
4192
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004194 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004196 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 */
4198 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004199termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004200{
4201 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4202 if (len >= 3 && code[len - 2] == '*')
4203 {
4204 if (len >= 5 && code[len - 3] == ';')
4205 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004206 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004207 return 1;
4208 }
4209 return 0;
4210}
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004213find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 int i;
4216
4217 for (i = 0; i < tc_len; ++i)
4218 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4219 return termcodes[i].code;
4220 return NULL;
4221}
4222
4223#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4224 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004225get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 if (i >= tc_len)
4228 return NULL;
4229 return &termcodes[i].name[0];
4230}
4231#endif
4232
4233 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004234del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int i;
4237
4238 if (termcodes == NULL) /* nothing there yet */
4239 return;
4240
4241 need_gather = TRUE; /* need to fill termleader[] */
4242
4243 for (i = 0; i < tc_len; ++i)
4244 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4245 {
4246 del_termcode_idx(i);
4247 return;
4248 }
4249 /* not found. Give error message? */
4250}
4251
4252 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004253del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 int i;
4256
4257 vim_free(termcodes[idx].code);
4258 --tc_len;
4259 for (i = idx; i < tc_len; ++i)
4260 termcodes[i] = termcodes[i + 1];
4261}
4262
4263#ifdef FEAT_TERMRESPONSE
4264/*
4265 * Called when detected that the terminal sends 8-bit codes.
4266 * Convert all 7-bit codes to their 8-bit equivalent.
4267 */
4268 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004269switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
4271 int i;
4272 int c;
4273
4274 /* Only need to do something when not already using 8-bit codes. */
4275 if (!term_is_8bit(T_NAME))
4276 {
4277 for (i = 0; i < tc_len; ++i)
4278 {
4279 c = term_7to8bit(termcodes[i].code);
4280 if (c != 0)
4281 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004282 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 termcodes[i].code[0] = c;
4284 }
4285 }
4286 need_gather = TRUE; /* need to fill termleader[] */
4287 }
4288 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004289 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291#endif
4292
4293#ifdef CHECK_DOUBLE_CLICK
4294static linenr_T orig_topline = 0;
4295# ifdef FEAT_DIFF
4296static int orig_topfill = 0;
4297# endif
4298#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004299#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300/*
4301 * Checking for double clicks ourselves.
4302 * "orig_topline" is used to avoid detecting a double-click when the window
4303 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4304 */
4305/*
4306 * Set orig_topline. Used when jumping to another window, so that a double
4307 * click still works.
4308 */
4309 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004310set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 orig_topline = wp->w_topline;
4313# ifdef FEAT_DIFF
4314 orig_topfill = wp->w_topfill;
4315# endif
4316}
4317#endif
4318
4319/*
4320 * Check if typebuf.tb_buf[] contains a terminal key code.
4321 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4322 * + max_offset].
4323 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004324 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 * With a match, the match is removed, the replacement code is inserted in
4326 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4327 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004328 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4329 * "buflen" is then the length of the string in buf[] and is updated for
4330 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 */
4332 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004333check_termcode(
4334 int max_offset,
4335 char_u *buf,
4336 int bufsize,
4337 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
4339 char_u *tp;
4340 char_u *p;
4341 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004344 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int offset;
4346 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004347 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004348 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int new_slen;
4351 int extra;
4352 char_u string[MAX_KEY_CODE_LEN + 1];
4353 int i, j;
4354 int idx = 0;
4355#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004356# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4357 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 char_u bytes[6];
4359 int num_bytes;
4360# endif
4361 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 int is_click, is_drag;
4363 int wheel_code = 0;
4364 int current_button;
4365 static int held_button = MOUSE_RELEASE;
4366 static int orig_num_clicks = 1;
4367 static int orig_mouse_code = 0x0;
4368# ifdef CHECK_DOUBLE_CLICK
4369 static int orig_mouse_col = 0;
4370 static int orig_mouse_row = 0;
4371 static struct timeval orig_mouse_time = {0, 0};
4372 /* time of previous mouse click */
4373 struct timeval mouse_time; /* time of current mouse click */
4374 long timediff; /* elapsed time in msec */
4375# endif
4376#endif
4377 int cpo_koffset;
4378#ifdef FEAT_MOUSE_GPM
4379 extern int gpm_flag; /* gpm library variable */
4380#endif
4381
4382 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4383
4384 /*
4385 * Speed up the checks for terminal codes by gathering all first bytes
4386 * used in termleader[]. Often this is just a single <Esc>.
4387 */
4388 if (need_gather)
4389 gather_termleader();
4390
4391 /*
4392 * Check at several positions in typebuf.tb_buf[], to catch something like
4393 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4394 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004395 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 * This is used often, KEEP IT FAST!
4397 */
4398 for (offset = 0; offset < max_offset; ++offset)
4399 {
4400 if (buf == NULL)
4401 {
4402 if (offset >= typebuf.tb_len)
4403 break;
4404 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4405 len = typebuf.tb_len - offset; /* length of the input */
4406 }
4407 else
4408 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004409 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 break;
4411 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004412 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414
4415 /*
4416 * Don't check characters after K_SPECIAL, those are already
4417 * translated terminal chars (avoid translating ~@^Hx).
4418 */
4419 if (*tp == K_SPECIAL)
4420 {
4421 offset += 2; /* there are always 2 extra characters */
4422 continue;
4423 }
4424
4425 /*
4426 * Skip this position if the character does not appear as the first
4427 * character in term_strings. This speeds up a lot, since most
4428 * termcodes start with the same character (ESC or CSI).
4429 */
4430 i = *tp;
4431 for (p = termleader; *p && *p != i; ++p)
4432 ;
4433 if (*p == NUL)
4434 continue;
4435
4436 /*
4437 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4438 * are in Insert mode.
4439 */
4440 if (*tp == ESC && !p_ek && (State & INSERT))
4441 continue;
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004444 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004445 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447#ifdef FEAT_GUI
4448 if (gui.in_use)
4449 {
4450 /*
4451 * GUI special key codes are all of the form [CSI xx].
4452 */
4453 if (*tp == CSI) /* Special key from GUI */
4454 {
4455 if (len < 3)
4456 return -1; /* Shouldn't happen */
4457 slen = 3;
4458 key_name[0] = tp[1];
4459 key_name[1] = tp[2];
4460 }
4461 }
4462 else
4463#endif /* FEAT_GUI */
4464 {
4465 for (idx = 0; idx < tc_len; ++idx)
4466 {
4467 /*
4468 * Ignore the entry if we are not at the start of
4469 * typebuf.tb_buf[]
4470 * and there are not enough characters to make a match.
4471 * But only when the 'K' flag is in 'cpoptions'.
4472 */
4473 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004474 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (cpo_koffset && offset && len < slen)
4476 continue;
4477 if (STRNCMP(termcodes[idx].code, tp,
4478 (size_t)(slen > len ? len : slen)) == 0)
4479 {
4480 if (len < slen) /* got a partial sequence */
4481 return -1; /* need to get more chars */
4482
4483 /*
4484 * When found a keypad key, check if there is another key
4485 * that matches and use that one. This makes <Home> to be
4486 * found instead of <kHome> when they produce the same
4487 * key code.
4488 */
4489 if (termcodes[idx].name[0] == 'K'
4490 && VIM_ISDIGIT(termcodes[idx].name[1]))
4491 {
4492 for (j = idx + 1; j < tc_len; ++j)
4493 if (termcodes[j].len == slen &&
4494 STRNCMP(termcodes[idx].code,
4495 termcodes[j].code, slen) == 0)
4496 {
4497 idx = j;
4498 break;
4499 }
4500 }
4501
4502 key_name[0] = termcodes[idx].name[0];
4503 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 break;
4505 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004506
4507 /*
4508 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004509 * <Esc>[123;*X (modslen == slen - 3)
4510 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4511 * When there is a modifier the * matches a number.
4512 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004513 */
4514 if (termcodes[idx].modlen > 0)
4515 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 modslen = termcodes[idx].modlen;
4517 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004518 continue;
4519 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004520 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004521 {
4522 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004523
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004524 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004525 return -1; /* need to get more chars */
4526
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004527 if (tp[modslen] == termcodes[idx].code[slen - 1])
4528 slen = modslen + 1; /* no modifiers */
4529 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004530 continue; /* no match */
4531 else
4532 {
4533 /* Skip over the digits, the final char must
4534 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004535 for (j = slen - 2; j < len && (isdigit(tp[j])
4536 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004537 ;
4538 ++j;
4539 if (len < j) /* got a partial sequence */
4540 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004541 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4542 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004543
Bram Moolenaara529ce02017-06-22 22:37:57 +02004544 modifiers_start = tp + slen - 2;
4545
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004546 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004547 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004548 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004549 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004550 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004552 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004553 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004554 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004555 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004556
4557 slen = j;
4558 }
4559 key_name[0] = termcodes[idx].name[0];
4560 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004561 break;
4562 }
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 }
4566
4567#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004568 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004569 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004570 * detecting the start of these mouse codes they might as well be
4571 * another key code or terminal response. */
4572# ifdef FEAT_MOUSE_DEC
4573 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004574# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004575# ifdef FEAT_MOUSE_PTERM
4576 || key_name[0] == KS_PTERM_MOUSE
4577# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004580 /* Check for some responses from the terminal starting with
4581 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004582 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004583 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004584 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004585 * Also eat other possible responses to t_RV, rxvt returns
4586 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4587 * mrxvt has been reported to have "+" in the version. Assume
4588 * the escape sequence ends with a letter or one of "{|}~".
4589 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004590 * - Cursor position report: <Esc>[{row};{col}R
4591 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004592 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004593 *
4594 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004595 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004596 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004597
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004598 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004599 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004600 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004601 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004603 int col = 0;
4604 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004605 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004608 for (i = 2 + (tp[0] != CSI); i < len
4609 && !(tp[i] >= '{' && tp[i] <= '~')
4610 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004611 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004612 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004613 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004614 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004617 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004618 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004619 return -1;
4620 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004621 if (extra > 0)
4622 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004623
4624 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4625 * u7_status is not "sent", it may be from a previous Vim that
4626 * just exited. But not for <S-F3>, it sends something
4627 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004628 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004629 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004630 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004631 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004632 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004633
Bram Moolenaarb255b902018-04-24 21:40:10 +02004634 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004635 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004636 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004637 if (col == 2)
4638 aw = "single";
4639 else if (col == 3)
4640 aw = "double";
4641 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4642 {
4643 /* Setting the option causes a screen redraw. Do
4644 * that right away if possible, keeping any
4645 * messages. */
4646 set_option_value((char_u *)"ambw", 0L,
4647 (char_u *)aw, 0);
4648# ifdef DEBUG_TERMRESPONSE
4649 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004650 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004651
Bram Moolenaarb255b902018-04-24 21:40:10 +02004652 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004653 }
4654# else
4655 redraw_asap(CLEAR);
4656# endif
4657 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004658 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004659 key_name[0] = (int)KS_EXTRA;
4660 key_name[1] = (int)KE_IGNORE;
4661 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004662# ifdef FEAT_EVAL
4663 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4664# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004667 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4668 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004670 int version = col;
4671
Bram Moolenaarb255b902018-04-24 21:40:10 +02004672 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004673 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004674 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /* If this code starts with CSI, you can bet that the
4677 * terminal uses 8-bit codes. */
4678 if (tp[0] == CSI)
4679 switch_to_8bit();
4680
4681 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004682 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 * Ignore it for when the user has set 'term' to xterm,
4684 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004685 if (version > 20000)
4686 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004688 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004690 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004691# ifdef FEAT_MOUSE_SGR
4692 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004693 int is_mintty = FALSE;
4694
4695 // mintty 2.9.5 sends 77;20905;0c.
4696 // (77 is ASCII 'M' for mintty.)
4697 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4698 is_mintty = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004699# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004702 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004704 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 check_for_codes = TRUE;
4706 need_gather = TRUE;
4707 req_codes_from_term();
4708 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004709
4710 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004711 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004712 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004713 {
4714 /* If run from Vim $COLORS is set to the number of
4715 * colors the terminal supports. Otherwise assume
4716 * 256, libvterm supports even more. */
4717 if (mch_getenv((char_u *)"COLORS") == NULL)
4718 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004719# ifdef FEAT_MOUSE_SGR
4720 /* Libvterm can handle SGR mouse reporting. */
4721 if (!option_was_set((char_u *)"ttym"))
4722 set_option_value((char_u *)"ttym", 0L,
4723 (char_u *)"sgr", 0);
4724# endif
4725 }
4726
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004727 if (version == 95)
4728 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004729 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004730 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4731 {
4732 is_not_xterm = TRUE;
4733 is_mac_terminal = TRUE;
4734 }
4735# ifdef FEAT_MOUSE_SGR
Bram Moolenaare330ef42018-07-06 23:11:40 +02004736 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004737 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4738 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004739 else
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004740# endif
Bram Moolenaare330ef42018-07-06 23:11:40 +02004741 // old iTerm2 sends 0;95;
4742 if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
4743 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004744 }
4745
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004746 /* Only set 'ttymouse' automatically if it was not set
4747 * by the user already. */
4748 if (!option_was_set((char_u *)"ttym"))
4749 {
4750# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004751 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004752 * Terminal.app, iTerm2 and mintty. */
4753 if (version >= 277 || is_iterm2 || is_mac_terminal
4754 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004755 set_option_value((char_u *)"ttym", 0L,
4756 (char_u *)"sgr", 0);
4757 else
4758# endif
4759 /* if xterm version >= 95 use mouse dragging */
4760 if (version >= 95)
4761 set_option_value((char_u *)"ttym", 0L,
4762 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004763 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004764
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004765 /* Detect terminals that set $TERM to something like
4766 * "xterm-256colors" but are not fully xterm
4767 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004768
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004769 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004770 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004771 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004772 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004773 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004774 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004775 is_not_xterm = TRUE;
4776
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004777 /* PuTTY sends 0;136;0
4778 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004779 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004780 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004781 is_not_xterm = TRUE;
4782
4783 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004784 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004785 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004786 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004787
Bram Moolenaar668324e2018-06-30 17:09:26 +02004788 // Xterm first responded to this request at patch level
4789 // 95, so assume anything below 95 is not xterm.
4790 if (version < 95)
4791 is_not_xterm = TRUE;
4792
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004793 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004794 * set. Only supported properly by xterm since version
4795 * 279 (otherwise it returns 0x18).
4796 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004797 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004798 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004799 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004800 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004801 && *T_CSH != NUL
4802 && *T_CRS != NUL)
4803 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004804 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004805 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004806 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004807 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004808 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004809
4810 /* Only request the cursor blink mode if t_RC set. Not
4811 * for Gnome terminal, it can't handle t_RC, it
4812 * echoes the characters to the screen. */
4813 if (rbm_status == STATUS_GET
4814 && !is_not_xterm
4815 && *T_CRC != NUL)
4816 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004817 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004818 out_str(T_CRC);
4819 rbm_status = STATUS_SENT;
4820 need_flush = TRUE;
4821 }
4822
4823 if (need_flush)
4824 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004826 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004828 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 apply_autocmds(EVENT_TERMRESPONSE,
4831 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 key_name[0] = (int)KS_EXTRA;
4833 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004835
Bram Moolenaar4db25542017-08-28 22:43:05 +02004836 /* Check blinking cursor from xterm:
4837 * {lead}?12;1$y set
4838 * {lead}?12;2$y not set
4839 *
4840 * {lead} can be <Esc>[ or CSI
4841 */
4842 else if (rbm_status == STATUS_SENT
4843 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4844 && i == j + 6
4845 && tp[j + 1] == '1'
4846 && tp[j + 2] == '2'
4847 && tp[j + 3] == ';'
4848 && tp[i - 1] == '$'
4849 && tp[i] == 'y')
4850 {
4851 initial_cursor_blink = (tp[j + 4] == '1');
4852 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004853 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004854 key_name[0] = (int)KS_EXTRA;
4855 key_name[1] = (int)KE_IGNORE;
4856 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004857# ifdef FEAT_EVAL
4858 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4859# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004860 }
4861
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004862 /*
4863 * Check for a window position response from the terminal:
4864 * {lead}3;{x}:{y}t
4865 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004866 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004867 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4868 || (len >= 3 && tp[0] == CSI))
4869 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4870 && tp[j + 1] == ';')
4871 {
4872 j += 2;
4873 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4874 ;
4875 if (i < len && tp[i] == ';')
4876 {
4877 winpos_x = atoi((char *)tp + j);
4878 j = i + 1;
4879 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4880 ;
4881 if (i < len && tp[i] == 't')
4882 {
4883 winpos_y = atoi((char *)tp + j);
4884 /* got finished code: consume it */
4885 key_name[0] = (int)KS_EXTRA;
4886 key_name[1] = (int)KE_IGNORE;
4887 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004888
4889 if (--did_request_winpos <= 0)
4890 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004891 }
4892 }
4893 if (i == len)
4894 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004895 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004896 return -1;
4897 }
4898 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004899 }
4900
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004901 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004902 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004903 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004904 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004905 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004906 * {lead} can be <Esc>] or OSC
4907 * {tail} can be '\007', <Esc>\ or STERM.
4908 *
4909 * Consume any code that starts with "{lead}11;", it's also
4910 * possible that "rgba" is following.
4911 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004912 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004913 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4914 || tp[0] == OSC))
4915 {
4916 j = 1 + (tp[0] == ESC);
4917 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004918 || (argp[1] != '1' && argp[1] != '0')
4919 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004920 i = 0; /* no match */
4921 else
4922 for (i = j; i < len; ++i)
4923 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4924 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004925 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004926 int is_bg = argp[1] == '1';
4927
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004928 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004929 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004930 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004931#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004932 int rval = hexhex2nr(tp + j + 7);
4933 int gval = hexhex2nr(tp + j + 12);
4934 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004935#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004936 if (is_bg)
4937 {
4938 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004939 + tp[j+17]) ? "light" : "dark";
4940
Bram Moolenaarb255b902018-04-24 21:40:10 +02004941 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004942 rbg_status = STATUS_GOT;
4943#ifdef FEAT_TERMINAL
4944 bg_r = rval;
4945 bg_g = gval;
4946 bg_b = bval;
4947#endif
4948 if (!option_was_set((char_u *)"bg")
4949 && STRCMP(p_bg, newval) != 0)
4950 {
4951 /* value differs, apply it */
4952 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004953 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004954 reset_option_was_set((char_u *)"bg");
4955 redraw_asap(CLEAR);
4956 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004957 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004958#ifdef FEAT_TERMINAL
4959 else
4960 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004961 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004962 rfg_status = STATUS_GOT;
4963 fg_r = rval;
4964 fg_g = gval;
4965 fg_b = bval;
4966 }
4967#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004968 }
4969
4970 /* got finished code: consume it */
4971 key_name[0] = (int)KS_EXTRA;
4972 key_name[1] = (int)KE_IGNORE;
4973 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004974# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004975 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4976 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004977# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004978 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004979 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004980 if (i == len)
4981 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004982 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004983 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004987 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004988 * {lead}{flag}+r<hex bytes><{tail}
4989 *
4990 * {lead} can be <Esc>P or DCS
4991 * {flag} can be '0' or '1'
4992 * {tail} can be Esc>\ or STERM
4993 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004994 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004995 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004996 *
4997 * {lead} can be <Esc>P or DCS
4998 * {tail} can be Esc>\ or STERM
4999 *
5000 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005001 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02005002 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005003 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 || tp[0] == DCS))
5005 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005006 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005007 if (len < j + 3)
5008 i = len; /* need more chars */
5009 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005010 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005011 else if (argp[1] == '+')
5012 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005013 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005014 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005015 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 || tp[i] == STERM)
5017 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005018 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 got_code_from_term(tp + j, i);
5020 key_name[0] = (int)KS_EXTRA;
5021 key_name[1] = (int)KE_IGNORE;
5022 slen = i + 1 + (tp[i] == ESC);
5023 break;
5024 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005025 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005026 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005027 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005028 /* Probably the cursor shape response. Make sure that "i"
5029 * is equal to "len" when there are not sufficient
5030 * characters. */
5031 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005032 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005033 if (i - j == 3 && !isdigit(tp[i]))
5034 break;
5035 if (i - j == 4 && tp[i] != ' ')
5036 break;
5037 if (i - j == 5 && tp[i] != 'q')
5038 break;
5039 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5040 break;
5041 if ((i - j == 6 && tp[i] == STERM)
5042 || (i - j == 7 && tp[i] == '\\'))
5043 {
5044 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005045
Bram Moolenaar590ec872018-03-02 20:58:42 +01005046 /* 0, 1 = block blink, 2 = block
5047 * 3 = underline blink, 4 = underline
5048 * 5 = vertical bar blink, 6 = vertical bar */
5049 number = number == 0 ? 1 : number;
5050 initial_cursor_shape = (number + 1) / 2;
5051 /* The blink flag is actually inverted, compared to
5052 * the value set with T_SH. */
5053 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005054 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01005055 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005056 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005057
Bram Moolenaar590ec872018-03-02 20:58:42 +01005058 key_name[0] = (int)KS_EXTRA;
5059 key_name[1] = (int)KE_IGNORE;
5060 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005061# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005062 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005063# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005064 break;
5065 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005066 }
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005070 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005071 /* These codes arrive many together, each code can be
5072 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005073 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005074 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 }
5078#endif
5079
5080 if (key_name[0] == NUL)
5081 continue; /* No match at this position, try next one */
5082
5083 /* We only get here when we have a complete termcode match */
5084
5085#ifdef FEAT_MOUSE
5086# ifdef FEAT_GUI
5087 /*
5088 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5089 * so that we know which window to scroll later.
5090 */
5091 if (gui.in_use
5092 && key_name[0] == (int)KS_EXTRA
5093 && (key_name[1] == (int)KE_X1MOUSE
5094 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005095 || key_name[1] == (int)KE_MOUSELEFT
5096 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 || key_name[1] == (int)KE_MOUSEDOWN
5098 || key_name[1] == (int)KE_MOUSEUP))
5099 {
5100 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5101 if (num_bytes == -1) /* not enough coordinates */
5102 return -1;
5103 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5104 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5105 slen += num_bytes;
5106 }
5107 else
5108# endif
5109 /*
5110 * If it is a mouse click, get the coordinates.
5111 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005112 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005114 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115# endif
5116# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005117 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118# endif
5119# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005120 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121# endif
5122# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005123 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005125# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005126 || key_name[0] == KS_URXVT_MOUSE
5127# endif
5128# ifdef FEAT_MOUSE_SGR
5129 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005130 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 )
5133 {
5134 is_click = is_drag = FALSE;
5135
Bram Moolenaar864207d2008-06-24 22:14:38 +00005136# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5137 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (key_name[0] == (int)KS_MOUSE)
5139 {
5140 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005141 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 * s == encoded button state:
5143 * 0x20 = left button down
5144 * 0x21 = middle button down
5145 * 0x22 = right button down
5146 * 0x23 = any button release
5147 * 0x60 = button 4 down (scroll wheel down)
5148 * 0x61 = button 5 down (scroll wheel up)
5149 * add 0x04 for SHIFT
5150 * add 0x08 for ALT
5151 * add 0x10 for CTRL
5152 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005153 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5154 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 * c == column + ' ' + 1 == column + 33
5156 * r == row + ' ' + 1 == row + 33
5157 *
5158 * The coordinates are passed on through global variables.
5159 * Ugly, but this avoids trouble with mouse clicks at an
5160 * unexpected moment and allows for mapping them.
5161 */
5162 for (;;)
5163 {
5164#ifdef FEAT_GUI
5165 if (gui.in_use)
5166 {
5167 /* GUI uses more bits for columns > 223 */
5168 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5169 if (num_bytes == -1) /* not enough coordinates */
5170 return -1;
5171 mouse_code = bytes[0];
5172 mouse_col = 128 * (bytes[1] - ' ' - 1)
5173 + bytes[2] - ' ' - 1;
5174 mouse_row = 128 * (bytes[3] - ' ' - 1)
5175 + bytes[4] - ' ' - 1;
5176 }
5177 else
5178#endif
5179 {
5180 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5181 if (num_bytes == -1) /* not enough coordinates */
5182 return -1;
5183 mouse_code = bytes[0];
5184 mouse_col = bytes[1] - ' ' - 1;
5185 mouse_row = bytes[2] - ' ' - 1;
5186 }
5187 slen += num_bytes;
5188
5189 /* If the following bytes is also a mouse code and it has
5190 * the same code, dump this one and get the next. This
5191 * makes dragging a whole lot faster. */
5192#ifdef FEAT_GUI
5193 if (gui.in_use)
5194 j = 3;
5195 else
5196#endif
5197 j = termcodes[idx].len;
5198 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5199 && tp[slen + j] == mouse_code
5200 && tp[slen + j + 1] != NUL
5201 && tp[slen + j + 2] != NUL
5202#ifdef FEAT_GUI
5203 && (!gui.in_use
5204 || (tp[slen + j + 3] != NUL
5205 && tp[slen + j + 4] != NUL))
5206#endif
5207 )
5208 slen += j;
5209 else
5210 break;
5211 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005214# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5215 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005216 || key_name[0] == KS_SGR_MOUSE
5217 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005218 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005219 /* URXVT 1015 mouse reporting mode:
5220 * Almost identical to xterm mouse mode, except the values
5221 * are decimal instead of bytes.
5222 *
5223 * \033[%d;%d;%dM
5224 * ^-- row
5225 * ^----- column
5226 * ^-------- code
5227 *
5228 * SGR 1006 mouse reporting mode:
5229 * Almost identical to xterm mouse mode, except the values
5230 * are decimal instead of bytes.
5231 *
5232 * \033[<%d;%d;%dM
5233 * ^-- row
5234 * ^----- column
5235 * ^-------- code
5236 *
5237 * \033[<%d;%d;%dm : mouse release event
5238 * ^-- row
5239 * ^----- column
5240 * ^-------- code
5241 */
5242 p = modifiers_start;
5243 if (p == NULL)
5244 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005245
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005246 mouse_code = getdigits(&p);
5247 if (*p++ != ';')
5248 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005249
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005250 /* when mouse reporting is SGR, add 32 to mouse code */
5251 if (key_name[0] == KS_SGR_MOUSE
5252 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5253 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005254
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005255 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5256 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005257
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005258 mouse_col = getdigits(&p) - 1;
5259 if (*p++ != ';')
5260 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005261
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005262 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005263
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005264 /* The modifiers were the mouse coordinates, not the
5265 * modifier keys (alt/shift/ctrl/meta) state. */
5266 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005267 }
5268# endif
5269
5270 if (key_name[0] == (int)KS_MOUSE
5271#ifdef FEAT_MOUSE_URXVT
5272 || key_name[0] == (int)KS_URXVT_MOUSE
5273#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005274#ifdef FEAT_MOUSE_SGR
5275 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005276 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005277#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005278 )
5279 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005280# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /*
5282 * Handle mouse events.
5283 * Recognize the xterm mouse wheel, but not in the GUI, the
5284 * Linux console with GPM and the MS-DOS or Win32 console
5285 * (multi-clicks use >= 0x60).
5286 */
5287 if (mouse_code >= MOUSEWHEEL_LOW
5288# ifdef FEAT_GUI
5289 && !gui.in_use
5290# endif
5291# ifdef FEAT_MOUSE_GPM
5292 && gpm_flag == 0
5293# endif
5294 )
5295 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005296# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5297 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5298 /* mouse-move event, using MOUSE_DRAG works */
5299 mouse_code = MOUSE_DRAG;
5300 else
5301# endif
5302 /* Keep the mouse_code before it's changed, so that we
5303 * remember that it was a mouse wheel click. */
5304 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306# ifdef FEAT_MOUSE_XTERM
5307 else if (held_button == MOUSE_RELEASE
5308# ifdef FEAT_GUI
5309 && !gui.in_use
5310# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005311 && (mouse_code == 0x23 || mouse_code == 0x24
5312 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005314 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5315 * And 0x40 and 0x41 are used by some xterm emulator. */
5316 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5317 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319# endif
5320
5321# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5322 else if (use_xterm_mouse() > 1)
5323 {
5324 if (mouse_code & MOUSE_DRAG_XTERM)
5325 mouse_code |= MOUSE_DRAG;
5326 }
5327# endif
5328# ifdef FEAT_XCLIPBOARD
5329 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5330 {
5331 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5332 stop_xterm_trace();
5333 else
5334 start_xterm_trace(mouse_code);
5335 }
5336# endif
5337# endif
5338 }
5339# endif /* !UNIX || FEAT_MOUSE_XTERM */
5340# ifdef FEAT_MOUSE_NET
5341 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5342 {
5343 int mc, mr;
5344
5345 /* expect a rather limited sequence like: balancing {
5346 * \033}6,45\r
5347 * '6' is the row, 45 is the column
5348 */
5349 p = tp + slen;
5350 mr = getdigits(&p);
5351 if (*p++ != ',')
5352 return -1;
5353 mc = getdigits(&p);
5354 if (*p++ != '\r')
5355 return -1;
5356
5357 mouse_col = mc - 1;
5358 mouse_row = mr - 1;
5359 mouse_code = MOUSE_LEFT;
5360 slen += (int)(p - (tp + slen));
5361 }
5362# endif /* FEAT_MOUSE_NET */
5363# ifdef FEAT_MOUSE_JSB
5364 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5365 {
5366 int mult, val, iter, button, status;
5367
5368 /* JSBTERM Input Model
5369 * \033[0~zw uniq escape sequence
5370 * (L-x) Left button pressed - not pressed x not reporting
5371 * (M-x) Middle button pressed - not pressed x not reporting
5372 * (R-x) Right button pressed - not pressed x not reporting
5373 * (SDmdu) Single , Double click, m mouse move d button down
5374 * u button up
5375 * ### X cursor position padded to 3 digits
5376 * ### Y cursor position padded to 3 digits
5377 * (s-x) SHIFT key pressed - not pressed x not reporting
5378 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005379 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 */
5381
5382 p = tp + slen;
5383 button = mouse_code = 0;
5384 switch (*p++)
5385 {
5386 case 'L': button = 1; break;
5387 case '-': break;
5388 case 'x': break; /* ignore sequence */
5389 default: return -1; /* Unknown Result */
5390 }
5391 switch (*p++)
5392 {
5393 case 'M': button |= 2; break;
5394 case '-': break;
5395 case 'x': break; /* ignore sequence */
5396 default: return -1; /* Unknown Result */
5397 }
5398 switch (*p++)
5399 {
5400 case 'R': button |= 4; break;
5401 case '-': break;
5402 case 'x': break; /* ignore sequence */
5403 default: return -1; /* Unknown Result */
5404 }
5405 status = *p++;
5406 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5407 mult /= 10, p++)
5408 if (*p >= '0' && *p <= '9')
5409 val += (*p - '0') * mult;
5410 else
5411 return -1;
5412 mouse_col = val;
5413 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5414 mult /= 10, p++)
5415 if (*p >= '0' && *p <= '9')
5416 val += (*p - '0') * mult;
5417 else
5418 return -1;
5419 mouse_row = val;
5420 switch (*p++)
5421 {
5422 case 's': button |= 8; break; /* SHIFT key Pressed */
5423 case '-': break; /* Not Pressed */
5424 case 'x': break; /* Not Reporting */
5425 default: return -1; /* Unknown Result */
5426 }
5427 switch (*p++)
5428 {
5429 case 'c': button |= 16; break; /* CTRL key Pressed */
5430 case '-': break; /* Not Pressed */
5431 case 'x': break; /* Not Reporting */
5432 default: return -1; /* Unknown Result */
5433 }
5434 if (*p++ != '\033')
5435 return -1;
5436 if (*p++ != '\\')
5437 return -1;
5438 switch (status)
5439 {
5440 case 'D': /* Double Click */
5441 case 'S': /* Single Click */
5442 if (button & 1) mouse_code |= MOUSE_LEFT;
5443 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5444 if (button & 4) mouse_code |= MOUSE_RIGHT;
5445 if (button & 8) mouse_code |= MOUSE_SHIFT;
5446 if (button & 16) mouse_code |= MOUSE_CTRL;
5447 break;
5448 case 'm': /* Mouse move */
5449 if (button & 1) mouse_code |= MOUSE_LEFT;
5450 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5451 if (button & 4) mouse_code |= MOUSE_RIGHT;
5452 if (button & 8) mouse_code |= MOUSE_SHIFT;
5453 if (button & 16) mouse_code |= MOUSE_CTRL;
5454 if ((button & 7) != 0)
5455 {
5456 held_button = mouse_code;
5457 mouse_code |= MOUSE_DRAG;
5458 }
5459 is_drag = TRUE;
5460 showmode();
5461 break;
5462 case 'd': /* Button Down */
5463 if (button & 1) mouse_code |= MOUSE_LEFT;
5464 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5465 if (button & 4) mouse_code |= MOUSE_RIGHT;
5466 if (button & 8) mouse_code |= MOUSE_SHIFT;
5467 if (button & 16) mouse_code |= MOUSE_CTRL;
5468 break;
5469 case 'u': /* Button Up */
5470 if (button & 1)
5471 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5472 if (button & 2)
5473 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5474 if (button & 4)
5475 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5476 if (button & 8)
5477 mouse_code |= MOUSE_SHIFT;
5478 if (button & 16)
5479 mouse_code |= MOUSE_CTRL;
5480 break;
5481 default: return -1; /* Unknown Result */
5482 }
5483
5484 slen += (p - (tp + slen));
5485 }
5486# endif /* FEAT_MOUSE_JSB */
5487# ifdef FEAT_MOUSE_DEC
5488 if (key_name[0] == (int)KS_DEC_MOUSE)
5489 {
5490 /* The DEC Locator Input Model
5491 * Netterm delivers the code sequence:
5492 * \033[2;4;24;80&w (left button down)
5493 * \033[3;0;24;80&w (left button up)
5494 * \033[6;1;24;80&w (right button down)
5495 * \033[7;0;24;80&w (right button up)
5496 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5497 * Pe is the event code
5498 * Pb is the button code
5499 * Pr is the row coordinate
5500 * Pc is the column coordinate
5501 * Pp is the third coordinate (page number)
5502 * Pe, the event code indicates what event caused this report
5503 * The following event codes are defined:
5504 * 0 - request, the terminal received an explicit request
5505 * for a locator report, but the locator is unavailable
5506 * 1 - request, the terminal received an explicit request
5507 * for a locator report
5508 * 2 - left button down
5509 * 3 - left button up
5510 * 4 - middle button down
5511 * 5 - middle button up
5512 * 6 - right button down
5513 * 7 - right button up
5514 * 8 - fourth button down
5515 * 9 - fourth button up
5516 * 10 - locator outside filter rectangle
5517 * Pb, the button code, ASCII decimal 0-15 indicating which
5518 * buttons are down if any. The state of the four buttons
5519 * on the locator correspond to the low four bits of the
5520 * decimal value,
5521 * "1" means button depressed
5522 * 0 - no buttons down,
5523 * 1 - right,
5524 * 2 - middle,
5525 * 4 - left,
5526 * 8 - fourth
5527 * Pr is the row coordinate of the locator position in the page,
5528 * encoded as an ASCII decimal value.
5529 * If Pr is omitted, the locator position is undefined
5530 * (outside the terminal window for example).
5531 * Pc is the column coordinate of the locator position in the
5532 * page, encoded as an ASCII decimal value.
5533 * If Pc is omitted, the locator position is undefined
5534 * (outside the terminal window for example).
5535 * Pp is the page coordinate of the locator position
5536 * encoded as an ASCII decimal value.
5537 * The page coordinate may be omitted if the locator is on
5538 * page one (the default). We ignore it anyway.
5539 */
5540 int Pe, Pb, Pr, Pc;
5541
5542 p = tp + slen;
5543
5544 /* get event status */
5545 Pe = getdigits(&p);
5546 if (*p++ != ';')
5547 return -1;
5548
5549 /* get button status */
5550 Pb = getdigits(&p);
5551 if (*p++ != ';')
5552 return -1;
5553
5554 /* get row status */
5555 Pr = getdigits(&p);
5556 if (*p++ != ';')
5557 return -1;
5558
5559 /* get column status */
5560 Pc = getdigits(&p);
5561
5562 /* the page parameter is optional */
5563 if (*p == ';')
5564 {
5565 p++;
5566 (void)getdigits(&p);
5567 }
5568 if (*p++ != '&')
5569 return -1;
5570 if (*p++ != 'w')
5571 return -1;
5572
5573 mouse_code = 0;
5574 switch (Pe)
5575 {
5576 case 0: return -1; /* position request while unavailable */
5577 case 1: /* a response to a locator position request includes
5578 the status of all buttons */
5579 Pb &= 7; /* mask off and ignore fourth button */
5580 if (Pb & 4)
5581 mouse_code = MOUSE_LEFT;
5582 if (Pb & 2)
5583 mouse_code = MOUSE_MIDDLE;
5584 if (Pb & 1)
5585 mouse_code = MOUSE_RIGHT;
5586 if (Pb)
5587 {
5588 held_button = mouse_code;
5589 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005590 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 is_drag = TRUE;
5593 showmode();
5594 break;
5595 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005596 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 break;
5598 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5599 break;
5600 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005601 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 break;
5603 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5604 break;
5605 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005606 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5609 break;
5610 case 8: return -1; /* fourth button down */
5611 case 9: return -1; /* fourth button up */
5612 case 10: return -1; /* mouse outside of filter rectangle */
5613 default: return -1; /* should never occur */
5614 }
5615
5616 mouse_col = Pc - 1;
5617 mouse_row = Pr - 1;
5618
5619 slen += (int)(p - (tp + slen));
5620 }
5621# endif /* FEAT_MOUSE_DEC */
5622# ifdef FEAT_MOUSE_PTERM
5623 if (key_name[0] == (int)KS_PTERM_MOUSE)
5624 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005625 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 p = tp + slen;
5628
5629 action = getdigits(&p);
5630 if (*p++ != ';')
5631 return -1;
5632
5633 mouse_row = getdigits(&p);
5634 if (*p++ != ';')
5635 return -1;
5636 mouse_col = getdigits(&p);
5637 if (*p++ != ';')
5638 return -1;
5639
5640 button = getdigits(&p);
5641 mouse_code = 0;
5642
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005643 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 case 4: mouse_code = MOUSE_LEFT; break;
5646 case 1: mouse_code = MOUSE_RIGHT; break;
5647 case 2: mouse_code = MOUSE_MIDDLE; break;
5648 default: return -1;
5649 }
5650
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005651 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 case 31: /* Initial press */
5654 if (*p++ != ';')
5655 return -1;
5656
5657 num_clicks = getdigits(&p); /* Not used */
5658 break;
5659
5660 case 32: /* Release */
5661 mouse_code |= MOUSE_RELEASE;
5662 break;
5663
5664 case 33: /* Drag */
5665 held_button = mouse_code;
5666 mouse_code |= MOUSE_DRAG;
5667 break;
5668
5669 default:
5670 return -1;
5671 }
5672
5673 if (*p++ != 't')
5674 return -1;
5675
5676 slen += (p - (tp + slen));
5677 }
5678# endif /* FEAT_MOUSE_PTERM */
5679
5680 /* Interpret the mouse code */
5681 current_button = (mouse_code & MOUSE_CLICK_MASK);
5682 if (current_button == MOUSE_RELEASE
5683# ifdef FEAT_MOUSE_XTERM
5684 && wheel_code == 0
5685# endif
5686 )
5687 {
5688 /*
5689 * If we get a mouse drag or release event when
5690 * there is no mouse button held down (held_button ==
5691 * MOUSE_RELEASE), produce a K_IGNORE below.
5692 * (can happen when you hold down two buttons
5693 * and then let them go, or click in the menu bar, but not
5694 * on a menu, and drag into the text).
5695 */
5696 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5697 is_drag = TRUE;
5698 current_button = held_button;
5699 }
5700 else if (wheel_code == 0)
5701 {
5702# ifdef CHECK_DOUBLE_CLICK
5703# ifdef FEAT_MOUSE_GPM
5704# ifdef FEAT_GUI
5705 /*
5706 * Only for Unix, when GUI or gpm is not active, we handle
5707 * multi-clicks here.
5708 */
5709 if (gpm_flag == 0 && !gui.in_use)
5710# else
5711 if (gpm_flag == 0)
5712# endif
5713# else
5714# ifdef FEAT_GUI
5715 if (!gui.in_use)
5716# endif
5717# endif
5718 {
5719 /*
5720 * Compute the time elapsed since the previous mouse click.
5721 */
5722 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005723 if (orig_mouse_time.tv_sec == 0)
5724 {
5725 /*
5726 * Avoid computing the difference between mouse_time
5727 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005728 * difference would be huge and would cause
5729 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005730 */
5731 timediff = p_mouset;
5732 }
5733 else
5734 {
5735 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005736 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005737 if (timediff < 0)
5738 --orig_mouse_time.tv_sec;
5739 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005740 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 orig_mouse_time = mouse_time;
5743 if (mouse_code == orig_mouse_code
5744 && timediff < p_mouset
5745 && orig_num_clicks != 4
5746 && orig_mouse_col == mouse_col
5747 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005748 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005750 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005752 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005753 /* Double click in tab pages line also works
5754 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005755 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005756 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ++orig_num_clicks;
5758 else
5759 orig_num_clicks = 1;
5760 orig_mouse_col = mouse_col;
5761 orig_mouse_row = mouse_row;
5762 orig_topline = curwin->w_topline;
5763#ifdef FEAT_DIFF
5764 orig_topfill = curwin->w_topfill;
5765#endif
5766 }
5767# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5768 else
5769 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5770# endif
5771# else
5772 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5773# endif
5774 is_click = TRUE;
5775 orig_mouse_code = mouse_code;
5776 }
5777 if (!is_drag)
5778 held_button = mouse_code & MOUSE_CLICK_MASK;
5779
5780 /*
5781 * Translate the actual mouse event into a pseudo mouse event.
5782 * First work out what modifiers are to be used.
5783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if (orig_mouse_code & MOUSE_SHIFT)
5785 modifiers |= MOD_MASK_SHIFT;
5786 if (orig_mouse_code & MOUSE_CTRL)
5787 modifiers |= MOD_MASK_CTRL;
5788 if (orig_mouse_code & MOUSE_ALT)
5789 modifiers |= MOD_MASK_ALT;
5790 if (orig_num_clicks == 2)
5791 modifiers |= MOD_MASK_2CLICK;
5792 else if (orig_num_clicks == 3)
5793 modifiers |= MOD_MASK_3CLICK;
5794 else if (orig_num_clicks == 4)
5795 modifiers |= MOD_MASK_4CLICK;
5796
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005797 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5798 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005800 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005801 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005802 {
5803 if (wheel_code & MOUSE_CTRL)
5804 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005805 if (wheel_code & MOUSE_ALT)
5806 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 key_name[1] = (wheel_code & 1)
5808 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005809 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 else
5812 key_name[1] = get_pseudo_mouse_code(current_button,
5813 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005814
5815 /* Make sure the mouse position is valid. Some terminals may
5816 * return weird values. */
5817 if (mouse_col >= Columns)
5818 mouse_col = Columns - 1;
5819 if (mouse_row >= Rows)
5820 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822#endif /* FEAT_MOUSE */
5823
5824#ifdef FEAT_GUI
5825 /*
5826 * If using the GUI, then we get menu and scrollbar events.
5827 *
5828 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5829 * four bytes which are to be taken as a pointer to the vimmenu_T
5830 * structure.
5831 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005832 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005833 * is one byte with the tab index.
5834 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5836 * by one byte representing the scrollbar number, and then four bytes
5837 * representing a long_u which is the new value of the scrollbar.
5838 *
5839 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5840 * KE_FILLER followed by four bytes representing a long_u which is the
5841 * new value of the scrollbar.
5842 */
5843# ifdef FEAT_MENU
5844 else if (key_name[0] == (int)KS_MENU)
5845 {
5846 long_u val;
5847
5848 num_bytes = get_long_from_buf(tp + slen, &val);
5849 if (num_bytes == -1)
5850 return -1;
5851 current_menu = (vimmenu_T *)val;
5852 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005853
5854 /* The menu may have been deleted right after it was used, check
5855 * for that. */
5856 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5857 {
5858 key_name[0] = KS_EXTRA;
5859 key_name[1] = (int)KE_IGNORE;
5860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005863# ifdef FEAT_GUI_TABLINE
5864 else if (key_name[0] == (int)KS_TABLINE)
5865 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005866 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005867 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5868 if (num_bytes == -1)
5869 return -1;
5870 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005871 if (current_tab == 255) /* -1 in a byte gives 255 */
5872 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005873 slen += num_bytes;
5874 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005875 else if (key_name[0] == (int)KS_TABMENU)
5876 {
5877 /* Selecting tabline tab or using its menu. */
5878 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5879 if (num_bytes == -1)
5880 return -1;
5881 current_tab = (int)bytes[0];
5882 current_tabmenu = (int)bytes[1];
5883 slen += num_bytes;
5884 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886# ifndef USE_ON_FLY_SCROLL
5887 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5888 {
5889 long_u val;
5890
5891 /* Get the last scrollbar event in the queue of the same type */
5892 j = 0;
5893 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5894 && tp[j + 2] != NUL; ++i)
5895 {
5896 j += 3;
5897 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5898 if (num_bytes == -1)
5899 break;
5900 if (i == 0)
5901 current_scrollbar = (int)bytes[0];
5902 else if (current_scrollbar != (int)bytes[0])
5903 break;
5904 j += num_bytes;
5905 num_bytes = get_long_from_buf(tp + j, &val);
5906 if (num_bytes == -1)
5907 break;
5908 scrollbar_value = val;
5909 j += num_bytes;
5910 slen = j;
5911 }
5912 if (i == 0) /* not enough characters to make one */
5913 return -1;
5914 }
5915 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5916 {
5917 long_u val;
5918
5919 /* Get the last horiz. scrollbar event in the queue */
5920 j = 0;
5921 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5922 && tp[j + 2] != NUL; ++i)
5923 {
5924 j += 3;
5925 num_bytes = get_long_from_buf(tp + j, &val);
5926 if (num_bytes == -1)
5927 break;
5928 scrollbar_value = val;
5929 j += num_bytes;
5930 slen = j;
5931 }
5932 if (i == 0) /* not enough characters to make one */
5933 return -1;
5934 }
5935# endif /* !USE_ON_FLY_SCROLL */
5936#endif /* FEAT_GUI */
5937
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005938 /*
5939 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5940 */
5941 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5942
5943 /*
5944 * Add any modifier codes to our string.
5945 */
5946 new_slen = 0; /* Length of what will replace the termcode */
5947 if (modifiers != 0)
5948 {
5949 /* Some keys have the modifier included. Need to handle that here
5950 * to make mappings work. */
5951 key = simplify_key(key, &modifiers);
5952 if (modifiers != 0)
5953 {
5954 string[new_slen++] = K_SPECIAL;
5955 string[new_slen++] = (int)KS_MODIFIER;
5956 string[new_slen++] = modifiers;
5957 }
5958 }
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005961 key_name[0] = KEY2TERMCAP0(key);
5962 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005964 {
5965 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005966 if (has_mbyte)
5967 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5968 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005969 string[new_slen++] = key_name[1];
5970 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005971 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5972 && key_name[1] == KE_IGNORE)
5973 {
5974 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5975 * to indicate what happened. */
5976 retval = KEYLEN_REMOVED;
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 else
5979 {
5980 string[new_slen++] = K_SPECIAL;
5981 string[new_slen++] = key_name[0];
5982 string[new_slen++] = key_name[1];
5983 }
5984 string[new_slen] = NUL;
5985 extra = new_slen - slen;
5986 if (buf == NULL)
5987 {
5988 if (extra < 0)
5989 /* remove matched chars, taking care of noremap */
5990 del_typebuf(-extra, offset);
5991 else if (extra > 0)
5992 /* insert the extra space we need */
5993 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5994
5995 /*
5996 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5997 * typebuf.tb_buf[]!
5998 */
5999 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
6000 (size_t)new_slen);
6001 }
6002 else
6003 {
6004 if (extra < 0)
6005 /* remove matched characters */
6006 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006007 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006009 {
6010 /* Insert the extra space we need. If there is insufficient
6011 * space return -1. */
6012 if (*buflen + extra + new_slen >= bufsize)
6013 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006015 (size_t)(*buflen - offset));
6016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006018 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006020 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022
Bram Moolenaar2951b772013-07-03 12:45:31 +02006023#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006024 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006025#endif
6026
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 return 0; /* no match found */
6028}
6029
Bram Moolenaar9377df32017-10-15 13:22:01 +02006030#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006031/*
6032 * Get the text foreground color, if known.
6033 */
6034 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006035term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006036{
6037 if (rfg_status == STATUS_GOT)
6038 {
6039 *r = fg_r;
6040 *g = fg_g;
6041 *b = fg_b;
6042 }
6043}
6044
6045/*
6046 * Get the text background color, if known.
6047 */
6048 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006049term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006050{
6051 if (rbg_status == STATUS_GOT)
6052 {
6053 *r = bg_r;
6054 *g = bg_g;
6055 *b = bg_b;
6056 }
6057}
6058#endif
6059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060/*
6061 * Replace any terminal code strings in from[] with the equivalent internal
6062 * vim representation. This is used for the "from" and "to" part of a
6063 * mapping, and the "to" part of a menu command.
6064 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6065 * '<'.
6066 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6067 *
6068 * The replacement is done in result[] and finally copied into allocated
6069 * memory. If this all works well *bufp is set to the allocated memory and a
6070 * pointer to it is returned. If something fails *bufp is set to NULL and from
6071 * is returned.
6072 *
6073 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6074 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6075 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6076 * instead of a CTRL-V.
6077 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006078 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006079replace_termcodes(
6080 char_u *from,
6081 char_u **bufp,
6082 int from_part,
6083 int do_lt, /* also translate <lt> */
6084 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 int i;
6087 int slen;
6088 int key;
6089 int dlen = 0;
6090 char_u *src;
6091 int do_backslash; /* backslash is a special character */
6092 int do_special; /* recognize <> key codes */
6093 int do_key_code; /* recognize raw key codes */
6094 char_u *result; /* buffer for resulting string */
6095
6096 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006097 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6099
6100 /*
6101 * Allocate space for the translation. Worst case a single character is
6102 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6103 */
6104 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6105 if (result == NULL) /* out of memory */
6106 {
6107 *bufp = NULL;
6108 return from;
6109 }
6110
6111 src = from;
6112
6113 /*
6114 * Check for #n at start only: function key n
6115 */
6116 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6117 {
6118 result[dlen++] = K_SPECIAL;
6119 result[dlen++] = 'k';
6120 if (src[1] == '0')
6121 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6122 else
6123 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6124 src += 2;
6125 }
6126
6127 /*
6128 * Copy each byte from *from to result[dlen]
6129 */
6130 while (*src != NUL)
6131 {
6132 /*
6133 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006134 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 */
6136 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6137 {
6138#ifdef FEAT_EVAL
6139 /*
6140 * Replace <SID> by K_SNR <script-nr> _.
6141 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6142 */
6143 if (STRNICMP(src, "<SID>", 5) == 0)
6144 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006145 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006146 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 else
6148 {
6149 src += 5;
6150 result[dlen++] = K_SPECIAL;
6151 result[dlen++] = (int)KS_EXTRA;
6152 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006153 sprintf((char *)result + dlen, "%ld",
6154 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 dlen += (int)STRLEN(result + dlen);
6156 result[dlen++] = '_';
6157 continue;
6158 }
6159 }
6160#endif
6161
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006162 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (slen)
6164 {
6165 dlen += slen;
6166 continue;
6167 }
6168 }
6169
6170 /*
6171 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6172 * Note that this is also checked after replacing the <> form.
6173 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6174 * it could be a character in the file.
6175 */
6176 if (do_key_code)
6177 {
6178 i = find_term_bykeys(src);
6179 if (i >= 0)
6180 {
6181 result[dlen++] = K_SPECIAL;
6182 result[dlen++] = termcodes[i].name[0];
6183 result[dlen++] = termcodes[i].name[1];
6184 src += termcodes[i].len;
6185 /* If terminal code matched, continue after it. */
6186 continue;
6187 }
6188 }
6189
6190#ifdef FEAT_EVAL
6191 if (do_special)
6192 {
6193 char_u *p, *s, len;
6194
6195 /*
6196 * Replace <Leader> by the value of "mapleader".
6197 * Replace <LocalLeader> by the value of "maplocalleader".
6198 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6199 */
6200 if (STRNICMP(src, "<Leader>", 8) == 0)
6201 {
6202 len = 8;
6203 p = get_var_value((char_u *)"g:mapleader");
6204 }
6205 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6206 {
6207 len = 13;
6208 p = get_var_value((char_u *)"g:maplocalleader");
6209 }
6210 else
6211 {
6212 len = 0;
6213 p = NULL;
6214 }
6215 if (len != 0)
6216 {
6217 /* Allow up to 8 * 6 characters for "mapleader". */
6218 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6219 s = (char_u *)"\\";
6220 else
6221 s = p;
6222 while (*s != NUL)
6223 result[dlen++] = *s++;
6224 src += len;
6225 continue;
6226 }
6227 }
6228#endif
6229
6230 /*
6231 * Remove CTRL-V and ignore the next character.
6232 * For "from" side the CTRL-V at the end is included, for the "to"
6233 * part it is removed.
6234 * If 'cpoptions' does not contain 'B', also accept a backslash.
6235 */
6236 key = *src;
6237 if (key == Ctrl_V || (do_backslash && key == '\\'))
6238 {
6239 ++src; /* skip CTRL-V or backslash */
6240 if (*src == NUL)
6241 {
6242 if (from_part)
6243 result[dlen++] = key;
6244 break;
6245 }
6246 }
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006249 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 /*
6252 * If the character is K_SPECIAL, replace it with K_SPECIAL
6253 * KS_SPECIAL KE_FILLER.
6254 * If compiled with the GUI replace CSI with K_CSI.
6255 */
6256 if (*src == K_SPECIAL)
6257 {
6258 result[dlen++] = K_SPECIAL;
6259 result[dlen++] = KS_SPECIAL;
6260 result[dlen++] = KE_FILLER;
6261 }
6262# ifdef FEAT_GUI
6263 else if (*src == CSI)
6264 {
6265 result[dlen++] = K_SPECIAL;
6266 result[dlen++] = KS_EXTRA;
6267 result[dlen++] = (int)KE_CSI;
6268 }
6269# endif
6270 else
6271 result[dlen++] = *src;
6272 ++src;
6273 }
6274 }
6275 result[dlen] = NUL;
6276
6277 /*
6278 * Copy the new string to allocated memory.
6279 * If this fails, just return from.
6280 */
6281 if ((*bufp = vim_strsave(result)) != NULL)
6282 from = *bufp;
6283 vim_free(result);
6284 return from;
6285}
6286
6287/*
6288 * Find a termcode with keys 'src' (must be NUL terminated).
6289 * Return the index in termcodes[], or -1 if not found.
6290 */
6291 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006292find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006295 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
6297 for (i = 0; i < tc_len; ++i)
6298 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006299 if (slen == termcodes[i].len
6300 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 return i;
6302 }
6303 return -1;
6304}
6305
6306/*
6307 * Gather the first characters in the terminal key codes into a string.
6308 * Used to speed up check_termcode().
6309 */
6310 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006311gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 int i;
6314 int len = 0;
6315
6316#ifdef FEAT_GUI
6317 if (gui.in_use)
6318 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6319#endif
6320#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006321 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 termleader[len++] = DCS; /* the termcode response starts with DCS
6323 in 8-bit mode */
6324#endif
6325 termleader[len] = NUL;
6326
6327 for (i = 0; i < tc_len; ++i)
6328 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6329 {
6330 termleader[len++] = termcodes[i].code[0];
6331 termleader[len] = NUL;
6332 }
6333
6334 need_gather = FALSE;
6335}
6336
6337/*
6338 * Show all termcodes (for ":set termcap")
6339 * This code looks a lot like showoptions(), but is different.
6340 */
6341 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006342show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int col;
6345 int *items;
6346 int item_count;
6347 int run;
6348 int row, rows;
6349 int cols;
6350 int i;
6351 int len;
6352
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006353#define INC3 27 /* try to make three columns */
6354#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355#define GAP 2 /* spaces between columns */
6356
6357 if (tc_len == 0) /* no terminal codes (must be GUI) */
6358 return;
6359 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6360 if (items == NULL)
6361 return;
6362
6363 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006364 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 /*
6367 * do the loop two times:
6368 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006369 * 2. display the medium items (medium length strings)
6370 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006372 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
6374 /*
6375 * collect the items in items[]
6376 */
6377 item_count = 0;
6378 for (i = 0; i < tc_len; i++)
6379 {
6380 len = show_one_termcode(termcodes[i].name,
6381 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006382 if (len <= INC3 - GAP ? run == 1
6383 : len <= INC2 - GAP ? run == 2
6384 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 items[item_count++] = i;
6386 }
6387
6388 /*
6389 * display the items
6390 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006391 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006393 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (cols == 0)
6395 cols = 1;
6396 rows = (item_count + cols - 1) / cols;
6397 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006398 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 rows = item_count;
6400 for (row = 0; row < rows && !got_int; ++row)
6401 {
6402 msg_putchar('\n'); /* go to next line */
6403 if (got_int) /* 'q' typed in more */
6404 break;
6405 col = 0;
6406 for (i = row; i < item_count; i += rows)
6407 {
6408 msg_col = col; /* make columns */
6409 show_one_termcode(termcodes[items[i]].name,
6410 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006411 if (run == 2)
6412 col += INC2;
6413 else
6414 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416 out_flush();
6417 ui_breakcheck();
6418 }
6419 }
6420 vim_free(items);
6421}
6422
6423/*
6424 * Show one termcode entry.
6425 * Output goes into IObuff[]
6426 */
6427 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006428show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 char_u *p;
6431 int len;
6432
6433 if (name[0] > '~')
6434 {
6435 IObuff[0] = ' ';
6436 IObuff[1] = ' ';
6437 IObuff[2] = ' ';
6438 IObuff[3] = ' ';
6439 }
6440 else
6441 {
6442 IObuff[0] = 't';
6443 IObuff[1] = '_';
6444 IObuff[2] = name[0];
6445 IObuff[3] = name[1];
6446 }
6447 IObuff[4] = ' ';
6448
6449 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6450 if (p[1] != 't')
6451 STRCPY(IObuff + 5, p);
6452 else
6453 IObuff[5] = NUL;
6454 len = (int)STRLEN(IObuff);
6455 do
6456 IObuff[len++] = ' ';
6457 while (len < 17);
6458 IObuff[len] = NUL;
6459 if (code == NULL)
6460 len += 4;
6461 else
6462 len += vim_strsize(code);
6463
6464 if (printit)
6465 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006466 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006468 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 else
6470 msg_outtrans(code);
6471 }
6472 return len;
6473}
6474
6475#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6476/*
6477 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6478 * termcap codes from the terminal itself.
6479 * We get them one by one to avoid a very long response string.
6480 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006481static int xt_index_in = 0;
6482static int xt_index_out = 0;
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006485req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 xt_index_out = 0;
6488 xt_index_in = 0;
6489 req_more_codes_from_term();
6490}
6491
6492 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006493req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 char buf[11];
6496 int old_idx = xt_index_out;
6497
6498 /* Don't do anything when going to exit. */
6499 if (exiting)
6500 return;
6501
6502 /* Send up to 10 more requests out than we received. Avoid sending too
6503 * many, there can be a buffer overflow somewhere. */
6504 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6505 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006506 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006507
Bram Moolenaarb255b902018-04-24 21:40:10 +02006508 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6509 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 out_str_nf((char_u *)buf);
6511 ++xt_index_out;
6512 }
6513
6514 /* Send the codes out right away. */
6515 if (xt_index_out != old_idx)
6516 out_flush();
6517}
6518
6519/*
6520 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6521 * A "0" instead of the "1" indicates a code that isn't supported.
6522 * Both <name> and <string> are encoded in hex.
6523 * "code" points to the "0" or "1".
6524 */
6525 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006526got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528#define XT_LEN 100
6529 char_u name[3];
6530 char_u str[XT_LEN];
6531 int i;
6532 int j = 0;
6533 int c;
6534
6535 /* A '1' means the code is supported, a '0' means it isn't.
6536 * When half the length is > XT_LEN we can't use it.
6537 * Our names are currently all 2 characters. */
6538 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6539 {
6540 /* Get the name from the response and find it in the table. */
6541 name[0] = hexhex2nr(code + 3);
6542 name[1] = hexhex2nr(code + 5);
6543 name[2] = NUL;
6544 for (i = 0; key_names[i] != NULL; ++i)
6545 {
6546 if (STRCMP(key_names[i], name) == 0)
6547 {
6548 xt_index_in = i;
6549 break;
6550 }
6551 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006552
Bram Moolenaarb255b902018-04-24 21:40:10 +02006553 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (key_names[i] != NULL)
6556 {
6557 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6558 str[j++] = c;
6559 str[j] = NUL;
6560 if (name[0] == 'C' && name[1] == 'o')
6561 {
6562 /* Color count is not a key code. */
6563 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006564 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566 else
6567 {
6568 /* First delete any existing entry with the same code. */
6569 i = find_term_bykeys(str);
6570 if (i >= 0)
6571 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006572 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574 }
6575 }
6576
6577 /* May request more codes now that we received one. */
6578 ++xt_index_in;
6579 req_more_codes_from_term();
6580}
6581
6582/*
6583 * Check if there are any unanswered requests and deal with them.
6584 * This is called before starting an external program or getting direct
6585 * keyboard input. We don't want responses to be send to that program or
6586 * handled as typed text.
6587 */
6588 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006589check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 int c;
6592
6593 /* If no codes requested or all are answered, no need to wait. */
6594 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6595 return;
6596
6597 /* Vgetc() will check for and handle any response.
6598 * Keep calling vpeekc() until we don't get any responses. */
6599 ++no_mapping;
6600 ++allow_keys;
6601 for (;;)
6602 {
6603 c = vpeekc();
6604 if (c == NUL) /* nothing available */
6605 break;
6606
6607 /* If a response is recognized it's replaced with K_IGNORE, must read
6608 * it from the input stream. If there is no K_IGNORE we can't do
6609 * anything, break here (there might be some responses further on, but
6610 * we don't want to throw away any typed chars). */
6611 if (c != K_SPECIAL && c != K_IGNORE)
6612 break;
6613 c = vgetc();
6614 if (c != K_IGNORE)
6615 {
6616 vungetc(c);
6617 break;
6618 }
6619 }
6620 --no_mapping;
6621 --allow_keys;
6622}
6623#endif
6624
6625#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6626/*
6627 * Translate an internal mapping/abbreviation representation into the
6628 * corresponding external one recognized by :map/:abbrev commands;
6629 * respects the current B/k/< settings of 'cpoption'.
6630 *
6631 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006632 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 *
6634 * It uses a growarray to build the translation string since the
6635 * latter can be wider than the original description. The caller has to
6636 * free the string afterwards.
6637 *
6638 * Returns NULL when there is a problem.
6639 */
6640 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006641translate_mapping(
6642 char_u *str,
6643 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 garray_T ga;
6646 int c;
6647 int modifiers;
6648 int cpo_bslash;
6649 int cpo_special;
6650 int cpo_keycode;
6651
6652 ga_init(&ga);
6653 ga.ga_itemsize = 1;
6654 ga.ga_growsize = 40;
6655
6656 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6657 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6658 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6659
6660 for (; *str; ++str)
6661 {
6662 c = *str;
6663 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6664 {
6665 modifiers = 0;
6666 if (str[1] == KS_MODIFIER)
6667 {
6668 str++;
6669 modifiers = *++str;
6670 c = *++str;
6671 }
6672 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6673 {
6674 int i;
6675
6676 /* try to find special key in termcodes */
6677 for (i = 0; i < tc_len; ++i)
6678 if (termcodes[i].name[0] == str[1]
6679 && termcodes[i].name[1] == str[2])
6680 break;
6681 if (i < tc_len)
6682 {
6683 ga_concat(&ga, termcodes[i].code);
6684 str += 2;
6685 continue; /* for (str) */
6686 }
6687 }
6688 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6689 {
6690 if (expmap && cpo_special)
6691 {
6692 ga_clear(&ga);
6693 return NULL;
6694 }
6695 c = TO_SPECIAL(str[1], str[2]);
6696 if (c == K_ZERO) /* display <Nul> as ^@ */
6697 c = NUL;
6698 str += 2;
6699 }
6700 if (IS_SPECIAL(c) || modifiers) /* special key */
6701 {
6702 if (expmap && cpo_special)
6703 {
6704 ga_clear(&ga);
6705 return NULL;
6706 }
6707 ga_concat(&ga, get_special_key_name(c, modifiers));
6708 continue; /* for (str) */
6709 }
6710 }
6711 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6712 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6713 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6714 if (c)
6715 ga_append(&ga, c);
6716 }
6717 ga_append(&ga, NUL);
6718 return (char_u *)(ga.ga_data);
6719}
6720#endif
6721
6722#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6723static char ksme_str[20];
6724static char ksmr_str[20];
6725static char ksmd_str[20];
6726
6727/*
6728 * For Win32 console: update termcap codes for existing console attributes.
6729 */
6730 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006731update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 struct builtin_term *p;
6734
6735 p = find_builtin_term(DEFAULT_TERM);
6736 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6737 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6738 attr | 0x08); /* FOREGROUND_INTENSITY */
6739 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6740 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6741
6742 while (p->bt_string != NULL)
6743 {
6744 if (p->bt_entry == (int)KS_ME)
6745 p->bt_string = &ksme_str[0];
6746 else if (p->bt_entry == (int)KS_MR)
6747 p->bt_string = &ksmr_str[0];
6748 else if (p->bt_entry == (int)KS_MD)
6749 p->bt_string = &ksmd_str[0];
6750 ++p;
6751 }
6752}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006753
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006754# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006755# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006756struct ks_tbl_s
6757{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006758 int code; // value of KS_
6759 char *vtp; // code in vtp mode
6760 char *vtp2; // code in vtp2 mode
6761 char buf[KSSIZE]; // save buffer in non-vtp mode
6762 char vbuf[KSSIZE]; // save buffer in vtp mode
6763 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6764 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006765};
6766
6767static struct ks_tbl_s ks_tbl[] =
6768{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006769 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6770 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6771 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6772 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6773 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6774 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6775 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6776 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6777 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006778# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006779 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6780 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006781# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006782 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6783 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006784# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006785 {(int)KS_CCO, "256", "256"}, // colors
6786 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006787};
6788
6789 static struct builtin_term *
6790find_first_tcap(
6791 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006792 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006793{
6794 struct builtin_term *p;
6795
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006796 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006797 if (p->bt_entry == code)
6798 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006799 return NULL;
6800}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006801# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006802
6803/*
6804 * For Win32 console: replace the sequence immediately after termguicolors.
6805 */
6806 void
6807swap_tcap(void)
6808{
6809# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006810 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006811 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006812 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006813 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006814 int mode;
6815 enum
6816 {
6817 CMODEINDEX,
6818 CMODE24,
6819 CMODE256
6820 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006821
6822 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006823 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006824 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006825 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006826 {
6827 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006828 if (bt != NULL)
6829 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006830 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6831 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6832 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6833
6834 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6835 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006836 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006837 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006838 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006839 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006840 }
6841
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006842 if (p_tgc)
6843 mode = CMODE24;
6844 else if (t_colors >= 256)
6845 mode = CMODE256;
6846 else
6847 mode = CMODEINDEX;
6848
6849 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006850 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006851 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6852 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006853 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006854 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006855 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006856 case CMODEINDEX:
6857 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6858 break;
6859 case CMODE24:
6860 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6861 break;
6862 default:
6863 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006864 }
6865 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006866 }
6867
6868 if (mode != curr_mode)
6869 {
6870 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006871 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006872 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6873 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006874 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006875 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006876 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006877 case CMODEINDEX:
6878 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6879 break;
6880 case CMODE24:
6881 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6882 break;
6883 default:
6884 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006885 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006886 }
6887 }
6888
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006889 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006890 }
6891# endif
6892}
6893
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006895
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006896#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006897 static int
6898hex_digit(int c)
6899{
6900 if (isdigit(c))
6901 return c - '0';
6902 c = TOLOWER_ASC(c);
6903 if (c >= 'a' && c <= 'f')
6904 return c - 'a' + 10;
6905 return 0x1ffffff;
6906}
6907
6908 guicolor_T
6909gui_get_color_cmn(char_u *name)
6910{
Bram Moolenaar28726652017-01-06 18:16:19 +01006911 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6912 * values as used by the MS-Windows GDI api. It should be used only for
6913 * MS-Windows GDI builds. */
6914# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6915# undef RGB
6916# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006917# ifndef RGB
6918# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6919# endif
6920# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006921 FILE *fd;
6922 char line[LINE_LEN];
6923 char_u *fname;
6924 int r, g, b, i;
6925 guicolor_T color;
6926
6927 struct rgbcolor_table_S {
6928 char_u *color_name;
6929 guicolor_T color;
6930 };
6931
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006932 /* Only non X11 colors (not present in rgb.txt) and colors in
6933 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006934 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006935 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6936 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6937 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6938 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6939 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6940 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6941 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6942 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6943 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6944 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6945 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6946 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6947 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006948 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6949 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006950 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006951 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006952 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006953 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6954 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6955 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6956 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6957 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6958 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6959 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6960 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6961 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006962 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006963 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006964 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6965 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006966 };
6967
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006968 static struct rgbcolor_table_S *colornames_table;
6969 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006970
6971 if (name[0] == '#' && STRLEN(name) == 7)
6972 {
6973 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006974 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006975 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6976 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6977 if (color > 0xffffff)
6978 return INVALCOLOR;
6979 return color;
6980 }
6981
6982 /* Check if the name is one of the colors we know */
6983 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6984 if (STRICMP(name, rgb_table[i].color_name) == 0)
6985 return rgb_table[i].color;
6986
6987 /*
6988 * Last attempt. Look in the file "$VIM/rgb.txt".
6989 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006990 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006991 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006992 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006993
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006994 /* colornames_table not yet initialized */
6995 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6996 if (fname == NULL)
6997 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006998
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006999 fd = fopen((char *)fname, "rt");
7000 vim_free(fname);
7001 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02007002 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007003 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007004 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007005 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02007006 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007007
7008 for (counting = 1; counting >= 0; --counting)
7009 {
7010 if (!counting)
7011 {
7012 colornames_table = (struct rgbcolor_table_S *)alloc(
7013 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
7014 if (colornames_table == NULL)
7015 {
7016 fclose(fd);
7017 return INVALCOLOR;
7018 }
7019 rewind(fd);
7020 }
7021 size = 0;
7022
7023 while (!feof(fd))
7024 {
7025 size_t len;
7026 int pos;
7027
Bram Moolenaar42335f52018-09-13 15:33:43 +02007028 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007029 len = strlen(line);
7030
7031 if (len <= 1 || line[len - 1] != '\n')
7032 continue;
7033
7034 line[len - 1] = '\0';
7035
7036 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7037 if (i != 3)
7038 continue;
7039
7040 if (!counting)
7041 {
7042 char_u *s = vim_strsave((char_u *)line + pos);
7043
7044 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007045 {
7046 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007047 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007048 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007049 colornames_table[size].color_name = s;
7050 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7051 }
7052 size++;
7053 }
7054 }
7055 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007056 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007057
7058 for (i = 0; i < size; i++)
7059 if (STRICMP(name, colornames_table[i].color_name) == 0)
7060 return colornames_table[i].color;
7061
Bram Moolenaarab302212016-04-26 20:59:29 +02007062 return INVALCOLOR;
7063}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007064
7065 guicolor_T
7066gui_get_rgb_color_cmn(int r, int g, int b)
7067{
7068 guicolor_T color = RGB(r, g, b);
7069
7070 if (color > 0xffffff)
7071 return INVALCOLOR;
7072 return color;
7073}
Bram Moolenaarab302212016-04-26 20:59:29 +02007074#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007075
7076#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
7077 || defined(PROTO)
7078static int cube_value[] = {
7079 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7080};
7081
7082static int grey_ramp[] = {
7083 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7084 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7085};
7086
7087# ifdef FEAT_TERMINAL
7088# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007089# else
7090# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007091# endif
7092
Bram Moolenaar9894e392018-05-05 14:29:06 +02007093static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007094// R G B idx
7095 { 0, 0, 0, 1}, // black
7096 {224, 0, 0, 2}, // dark red
7097 { 0, 224, 0, 3}, // dark green
7098 {224, 224, 0, 4}, // dark yellow / brown
7099 { 0, 0, 224, 5}, // dark blue
7100 {224, 0, 224, 6}, // dark magenta
7101 { 0, 224, 224, 7}, // dark cyan
7102 {224, 224, 224, 8}, // light grey
7103
7104 {128, 128, 128, 9}, // dark grey
7105 {255, 64, 64, 10}, // light red
7106 { 64, 255, 64, 11}, // light green
7107 {255, 255, 64, 12}, // yellow
7108 { 64, 64, 255, 13}, // light blue
7109 {255, 64, 255, 14}, // light magenta
7110 { 64, 255, 255, 15}, // light cyan
7111 {255, 255, 255, 16}, // white
7112};
7113
7114 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007115cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007116{
7117 int idx;
7118
7119 if (nr < 16)
7120 {
7121 *r = ansi_table[nr][0];
7122 *g = ansi_table[nr][1];
7123 *b = ansi_table[nr][2];
7124 *ansi_idx = ansi_table[nr][3];
7125 }
7126 else if (nr < 232)
7127 {
7128 /* 216 color cube */
7129 idx = nr - 16;
7130 *r = cube_value[idx / 36 % 6];
7131 *g = cube_value[idx / 6 % 6];
7132 *b = cube_value[idx % 6];
7133 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7134 }
7135 else if (nr < 256)
7136 {
7137 /* 24 grey scale ramp */
7138 idx = nr - 232;
7139 *r = grey_ramp[idx];
7140 *g = grey_ramp[idx];
7141 *b = grey_ramp[idx];
7142 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7143 }
7144 else
7145 {
7146 *r = 0;
7147 *g = 0;
7148 *b = 0;
7149 *ansi_idx = 0;
7150 }
7151}
7152#endif