blob: ffc988483c4e942bd08e9cd9b26a530d500c4eb2 [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 Moolenaar4f974752019-02-17 17:44:42 +0100536# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537/*
538 * These codes are valid for the Win32 Console . The entries that start with
539 * ESC | are translated into console calls in os_win32.c. The function keys
540 * are also translated in os_win32.c.
541 */
542 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100543 {(int)KS_CE, "\033|K"}, // clear to end of line
544 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100546 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100548 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100550 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100552 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
553 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100555 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
556 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100558 {(int)KS_CL, "\033|J"}, // clear screen
559 {(int)KS_CD, "\033|j"}, // clear to end of display
560 {(int)KS_VI, "\033|v"}, // cursor invisible
561 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar6982f422019-02-16 16:48:01 +0100563 {(int)KS_ME, "\033|0m"}, // normal
564 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
565 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_SO, "\033|31m"}, // standout: white on blue
568 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_SO, "\033|F"}, // standout: high intensity
571 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100573 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
574 {(int)KS_CZR, "\033|0m"}, // italic end
575 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
576 {(int)KS_UE, "\033|0m"}, // underscore end
577 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CAB, "\033|%p1%db"}, // set background color
580 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CAB, "\033|%db"}, // set background color
583 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584# endif
585
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100588 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {(int)KS_LE, "\b"},
590# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100593 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100595 {(int)KS_VB, "\033|B"}, // visual bell
596 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
597 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100599 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100601 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100603# ifdef FEAT_TERMGUICOLORS
604 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
605 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 {K_UP, "\316H"},
609 {K_DOWN, "\316P"},
610 {K_LEFT, "\316K"},
611 {K_RIGHT, "\316M"},
612 {K_S_UP, "\316\304"},
613 {K_S_DOWN, "\316\317"},
614 {K_S_LEFT, "\316\311"},
615 {K_C_LEFT, "\316s"},
616 {K_S_RIGHT, "\316\313"},
617 {K_C_RIGHT, "\316t"},
618 {K_S_TAB, "\316\017"},
619 {K_F1, "\316;"},
620 {K_F2, "\316<"},
621 {K_F3, "\316="},
622 {K_F4, "\316>"},
623 {K_F5, "\316?"},
624 {K_F6, "\316@"},
625 {K_F7, "\316A"},
626 {K_F8, "\316B"},
627 {K_F9, "\316C"},
628 {K_F10, "\316D"},
629 {K_F11, "\316\205"},
630 {K_F12, "\316\206"},
631 {K_S_F1, "\316T"},
632 {K_S_F2, "\316U"},
633 {K_S_F3, "\316V"},
634 {K_S_F4, "\316W"},
635 {K_S_F5, "\316X"},
636 {K_S_F6, "\316Y"},
637 {K_S_F7, "\316Z"},
638 {K_S_F8, "\316["},
639 {K_S_F9, "\316\\"},
640 {K_S_F10, "\316]"},
641 {K_S_F11, "\316\207"},
642 {K_S_F12, "\316\210"},
643 {K_INS, "\316R"},
644 {K_DEL, "\316S"},
645 {K_HOME, "\316G"},
646 {K_S_HOME, "\316\302"},
647 {K_C_HOME, "\316w"},
648 {K_END, "\316O"},
649 {K_S_END, "\316\315"},
650 {K_C_END, "\316u"},
651 {K_PAGEDOWN, "\316Q"},
652 {K_PAGEUP, "\316I"},
653 {K_KPLUS, "\316N"},
654 {K_KMINUS, "\316J"},
655 {K_KMULTIPLY, "\316\067"},
656 {K_K0, "\316\332"},
657 {K_K1, "\316\336"},
658 {K_K2, "\316\342"},
659 {K_K3, "\316\346"},
660 {K_K4, "\316\352"},
661 {K_K5, "\316\356"},
662 {K_K6, "\316\362"},
663 {K_K7, "\316\366"},
664 {K_K8, "\316\372"},
665 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100666 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# endif
668
669# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
670/*
671 * VT320 is working as an ANSI terminal compatible DEC terminal.
672 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 * TODO:- rewrite ESC[ codes to CSI
674 * - keyboard languages (CSI ? 26 n)
675 */
676 {(int)KS_NAME, "vt320"},
677 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
678 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
679# ifdef TERMINFO
680 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
681# else
682 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
683# endif
684 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
685# ifdef TERMINFO
686 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
687# else
688 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
689# endif
690 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000691 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
692 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
694 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000695 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
696 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
697 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
698 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
699 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
700 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
701 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
702 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
703 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
704 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {(int)KS_MS, "y"},
706 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100707 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {(int)KS_LE, "\b"},
709# ifdef TERMINFO
710 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
711 ESC_STR "[%i%p1%d;%p2%dH")},
712# else
713 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
714# endif
715# ifdef TERMINFO
716 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
717# else
718 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
719# endif
720 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
721 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
722 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
723 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200724 // Note: cursor key sequences for application cursor mode are omitted,
725 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
727 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
728 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
729 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
730 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
732 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
733 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
734 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
735 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
738 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
739 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
740 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
741 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
742 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
743 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
744 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
745 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
746 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
747 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
748 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
749 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
750 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
751 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200752 // These sequences starting with <Esc> O may interfere with what the user
753 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
755 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
756 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
757 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
758 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200759 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
760 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
761 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
762 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
763 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
764 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
765 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
766 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
767 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
768 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
770# endif
771
772# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
773/*
774 * Ordinary vt52
775 */
776 {(int)KS_NAME, "vt52"},
777 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
778 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100779# ifdef TERMINFO
780 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
781 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
782# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100786 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
788 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {K_UP, IF_EB("\033A", ESC_STR "A")},
790 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
791 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
792 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100793 {K_F1, IF_EB("\033P", ESC_STR "P")},
794 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
795 {K_F3, IF_EB("\033R", ESC_STR "R")},
796# ifdef __MINT__
797 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
798 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
799 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
800 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
801 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
803 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
804 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
805 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {K_F4, IF_EB("\033S", ESC_STR "S")},
807 {K_F5, IF_EB("\033T", ESC_STR "T")},
808 {K_F6, IF_EB("\033U", ESC_STR "U")},
809 {K_F7, IF_EB("\033V", ESC_STR "V")},
810 {K_F8, IF_EB("\033W", ESC_STR "W")},
811 {K_F9, IF_EB("\033X", ESC_STR "X")},
812 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
813 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
814 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
815 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
816 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
817 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
818 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
819 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
820 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
821 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
822 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
823 {K_INS, IF_EB("\033I", ESC_STR "I")},
824 {K_HOME, IF_EB("\033E", ESC_STR "E")},
825 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
826 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
827# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {(int)KS_MS, "y"},
830# endif
831# endif
832
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200833# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200834 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
836 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
837# ifdef TERMINFO
838 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
839# else
840 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
841# endif
842 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
843# ifdef TERMINFO
844 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
845# else
846 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
847# endif
848# ifdef TERMINFO
849 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
850 ESC_STR "[%i%p1%d;%p2%dr")},
851# else
852 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
853# endif
854 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
855 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
856 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
857 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
858 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
859 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
860 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200861 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
862 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {(int)KS_MS, "y"},
864 {(int)KS_UT, "y"},
865 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200866 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
867 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200868 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200869 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200870# ifdef TERMINFO
871 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
872# else
873 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
874# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200875 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200876 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877# ifdef TERMINFO
878 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
879 ESC_STR "[%i%p1%d;%p2%dH")},
880# else
881 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
882# endif
883 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
884# ifdef TERMINFO
885 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
886# else
887 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
888# endif
889 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
890 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
891# ifdef FEAT_XTERM_SAVE
892 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
893 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
894 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
895# endif
896 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
897 {(int)KS_CIE, "\007"},
898 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
899 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200900 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
901 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902# ifdef TERMINFO
903 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
904 ESC_STR "[8;%p1%d;%p2%dt")},
905 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
906 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200907 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# else
909 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
910 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200911 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# endif
913 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200914 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200915 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100916 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200917# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200918 /* These are printf strings, not terminal codes. */
919 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
920 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
921# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100922 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
923 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200924 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
925 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
926 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
927 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000928
929 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
930 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
931 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
932 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
933 /* An extra set of cursor keys for vt100 mode */
934 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
935 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
936 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
937 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000939 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
940 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
941 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
942 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000943 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
944 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
945 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
946 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
947 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
948 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
949 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
950 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
951 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
952 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
953 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
954 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000956 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
957 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
958 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
959 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000960 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
961 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000962 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000963 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000964 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000965 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000966 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
967 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000969 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000970 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000971 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
972 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100973 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
974 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
975 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
976 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
977 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
978 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200979 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
980 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
981 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
982 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
983 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
984 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
985 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
986 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
987 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
988 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +0100989 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
990 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
991 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000994 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
995 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
996 /* F14 and F15 are missing, because they send the same codes as the undo
997 * and help key, although they don't work on all keyboards. */
998 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
999 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1000 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1001 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1002 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1003
1004 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1005 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1006 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1007 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1008 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1009 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1010 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1011 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1012 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1013 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1014
1015 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1016 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1017 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1018 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1019 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1020 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1021 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022# endif
1023
1024# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1025/*
1026 * iris-ansi for Silicon Graphics machines.
1027 */
1028 {(int)KS_NAME, "iris-ansi"},
1029 {(int)KS_CE, "\033[K"},
1030 {(int)KS_CD, "\033[J"},
1031 {(int)KS_AL, "\033[L"},
1032# ifdef TERMINFO
1033 {(int)KS_CAL, "\033[%p1%dL"},
1034# else
1035 {(int)KS_CAL, "\033[%dL"},
1036# endif
1037 {(int)KS_DL, "\033[M"},
1038# ifdef TERMINFO
1039 {(int)KS_CDL, "\033[%p1%dM"},
1040# else
1041 {(int)KS_CDL, "\033[%dM"},
1042# endif
1043#if 0 /* The scroll region is not working as Vim expects. */
1044# ifdef TERMINFO
1045 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1046# else
1047 {(int)KS_CS, "\033[%i%d;%dr"},
1048# endif
1049#endif
1050 {(int)KS_CL, "\033[H\033[2J"},
1051 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1052 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1053 {(int)KS_TI, "\033[=6h"},
1054 {(int)KS_TE, "\033[=6l"},
1055 {(int)KS_SE, "\033[21;27m"},
1056 {(int)KS_SO, "\033[1;7m"},
1057 {(int)KS_ME, "\033[m"},
1058 {(int)KS_MR, "\033[7m"},
1059 {(int)KS_MD, "\033[1m"},
1060 {(int)KS_CCO, "8"}, /* allow 8 colors */
1061 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1062 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1063 {(int)KS_US, "\033[4m"}, /* underline on */
1064 {(int)KS_UE, "\033[24m"}, /* underline off */
1065# ifdef TERMINFO
1066 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1067 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1068 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1069 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1070# else
1071 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1072 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1073 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1074 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1075# endif
1076 {(int)KS_MS, "y"}, /* guessed */
1077 {(int)KS_UT, "y"}, /* guessed */
1078 {(int)KS_LE, "\b"},
1079# ifdef TERMINFO
1080 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1081# else
1082 {(int)KS_CM, "\033[%i%d;%dH"},
1083# endif
1084 {(int)KS_SR, "\033M"},
1085# ifdef TERMINFO
1086 {(int)KS_CRI, "\033[%p1%dC"},
1087# else
1088 {(int)KS_CRI, "\033[%dC"},
1089# endif
1090 {(int)KS_CIS, "\033P3.y"},
1091 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1092 {(int)KS_TS, "\033P1.y"},
1093 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1094# ifdef TERMINFO
1095 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1096 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1097# else
1098 {(int)KS_CWS, "\033[203;%d;%d/y"},
1099 {(int)KS_CWP, "\033[205;%d;%d/y"},
1100# endif
1101 {K_UP, "\033[A"},
1102 {K_DOWN, "\033[B"},
1103 {K_LEFT, "\033[D"},
1104 {K_RIGHT, "\033[C"},
1105 {K_S_UP, "\033[161q"},
1106 {K_S_DOWN, "\033[164q"},
1107 {K_S_LEFT, "\033[158q"},
1108 {K_S_RIGHT, "\033[167q"},
1109 {K_F1, "\033[001q"},
1110 {K_F2, "\033[002q"},
1111 {K_F3, "\033[003q"},
1112 {K_F4, "\033[004q"},
1113 {K_F5, "\033[005q"},
1114 {K_F6, "\033[006q"},
1115 {K_F7, "\033[007q"},
1116 {K_F8, "\033[008q"},
1117 {K_F9, "\033[009q"},
1118 {K_F10, "\033[010q"},
1119 {K_F11, "\033[011q"},
1120 {K_F12, "\033[012q"},
1121 {K_S_F1, "\033[013q"},
1122 {K_S_F2, "\033[014q"},
1123 {K_S_F3, "\033[015q"},
1124 {K_S_F4, "\033[016q"},
1125 {K_S_F5, "\033[017q"},
1126 {K_S_F6, "\033[018q"},
1127 {K_S_F7, "\033[019q"},
1128 {K_S_F8, "\033[020q"},
1129 {K_S_F9, "\033[021q"},
1130 {K_S_F10, "\033[022q"},
1131 {K_S_F11, "\033[023q"},
1132 {K_S_F12, "\033[024q"},
1133 {K_INS, "\033[139q"},
1134 {K_HOME, "\033[H"},
1135 {K_END, "\033[146q"},
1136 {K_PAGEUP, "\033[150q"},
1137 {K_PAGEDOWN, "\033[154q"},
1138# endif
1139
1140# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1141/*
1142 * for debugging
1143 */
1144 {(int)KS_NAME, "debug"},
1145 {(int)KS_CE, "[CE]"},
1146 {(int)KS_CD, "[CD]"},
1147 {(int)KS_AL, "[AL]"},
1148# ifdef TERMINFO
1149 {(int)KS_CAL, "[CAL%p1%d]"},
1150# else
1151 {(int)KS_CAL, "[CAL%d]"},
1152# endif
1153 {(int)KS_DL, "[DL]"},
1154# ifdef TERMINFO
1155 {(int)KS_CDL, "[CDL%p1%d]"},
1156# else
1157 {(int)KS_CDL, "[CDL%d]"},
1158# endif
1159# ifdef TERMINFO
1160 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1161# else
1162 {(int)KS_CS, "[%dCS%d]"},
1163# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001164# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001166# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# endif
1169# ifdef TERMINFO
1170 {(int)KS_CAB, "[CAB%p1%d]"},
1171 {(int)KS_CAF, "[CAF%p1%d]"},
1172 {(int)KS_CSB, "[CSB%p1%d]"},
1173 {(int)KS_CSF, "[CSF%p1%d]"},
1174# else
1175 {(int)KS_CAB, "[CAB%d]"},
1176 {(int)KS_CAF, "[CAF%d]"},
1177 {(int)KS_CSB, "[CSB%d]"},
1178 {(int)KS_CSF, "[CSF%d]"},
1179# endif
1180 {(int)KS_OP, "[OP]"},
1181 {(int)KS_LE, "[LE]"},
1182 {(int)KS_CL, "[CL]"},
1183 {(int)KS_VI, "[VI]"},
1184 {(int)KS_VE, "[VE]"},
1185 {(int)KS_VS, "[VS]"},
1186 {(int)KS_ME, "[ME]"},
1187 {(int)KS_MR, "[MR]"},
1188 {(int)KS_MB, "[MB]"},
1189 {(int)KS_MD, "[MD]"},
1190 {(int)KS_SE, "[SE]"},
1191 {(int)KS_SO, "[SO]"},
1192 {(int)KS_UE, "[UE]"},
1193 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001194 {(int)KS_UCE, "[UCE]"},
1195 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001196 {(int)KS_STE, "[STE]"},
1197 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {(int)KS_MS, "[MS]"},
1199 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001200 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201# ifdef TERMINFO
1202 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1203# else
1204 {(int)KS_CM, "[%dCM%d]"},
1205# endif
1206 {(int)KS_SR, "[SR]"},
1207# ifdef TERMINFO
1208 {(int)KS_CRI, "[CRI%p1%d]"},
1209# else
1210 {(int)KS_CRI, "[CRI%d]"},
1211# endif
1212 {(int)KS_VB, "[VB]"},
1213 {(int)KS_KS, "[KS]"},
1214 {(int)KS_KE, "[KE]"},
1215 {(int)KS_TI, "[TI]"},
1216 {(int)KS_TE, "[TE]"},
1217 {(int)KS_CIS, "[CIS]"},
1218 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001219 {(int)KS_CSC, "[CSC]"},
1220 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {(int)KS_TS, "[TS]"},
1222 {(int)KS_FS, "[FS]"},
1223# ifdef TERMINFO
1224 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1225 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1226# else
1227 {(int)KS_CWS, "[%dCWS%d]"},
1228 {(int)KS_CWP, "[%dCWP%d]"},
1229# endif
1230 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001231 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001232 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001233 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {K_UP, "[KU]"},
1235 {K_DOWN, "[KD]"},
1236 {K_LEFT, "[KL]"},
1237 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001238 {K_XUP, "[xKU]"},
1239 {K_XDOWN, "[xKD]"},
1240 {K_XLEFT, "[xKL]"},
1241 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {K_S_UP, "[S-KU]"},
1243 {K_S_DOWN, "[S-KD]"},
1244 {K_S_LEFT, "[S-KL]"},
1245 {K_C_LEFT, "[C-KL]"},
1246 {K_S_RIGHT, "[S-KR]"},
1247 {K_C_RIGHT, "[C-KR]"},
1248 {K_F1, "[F1]"},
1249 {K_XF1, "[xF1]"},
1250 {K_F2, "[F2]"},
1251 {K_XF2, "[xF2]"},
1252 {K_F3, "[F3]"},
1253 {K_XF3, "[xF3]"},
1254 {K_F4, "[F4]"},
1255 {K_XF4, "[xF4]"},
1256 {K_F5, "[F5]"},
1257 {K_F6, "[F6]"},
1258 {K_F7, "[F7]"},
1259 {K_F8, "[F8]"},
1260 {K_F9, "[F9]"},
1261 {K_F10, "[F10]"},
1262 {K_F11, "[F11]"},
1263 {K_F12, "[F12]"},
1264 {K_S_F1, "[S-F1]"},
1265 {K_S_XF1, "[S-xF1]"},
1266 {K_S_F2, "[S-F2]"},
1267 {K_S_XF2, "[S-xF2]"},
1268 {K_S_F3, "[S-F3]"},
1269 {K_S_XF3, "[S-xF3]"},
1270 {K_S_F4, "[S-F4]"},
1271 {K_S_XF4, "[S-xF4]"},
1272 {K_S_F5, "[S-F5]"},
1273 {K_S_F6, "[S-F6]"},
1274 {K_S_F7, "[S-F7]"},
1275 {K_S_F8, "[S-F8]"},
1276 {K_S_F9, "[S-F9]"},
1277 {K_S_F10, "[S-F10]"},
1278 {K_S_F11, "[S-F11]"},
1279 {K_S_F12, "[S-F12]"},
1280 {K_HELP, "[HELP]"},
1281 {K_UNDO, "[UNDO]"},
1282 {K_BS, "[BS]"},
1283 {K_INS, "[INS]"},
1284 {K_KINS, "[KINS]"},
1285 {K_DEL, "[DEL]"},
1286 {K_KDEL, "[KDEL]"},
1287 {K_HOME, "[HOME]"},
1288 {K_S_HOME, "[C-HOME]"},
1289 {K_C_HOME, "[C-HOME]"},
1290 {K_KHOME, "[KHOME]"},
1291 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001292 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {K_END, "[END]"},
1294 {K_S_END, "[C-END]"},
1295 {K_C_END, "[C-END]"},
1296 {K_KEND, "[KEND]"},
1297 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001298 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {K_PAGEUP, "[PAGEUP]"},
1300 {K_PAGEDOWN, "[PAGEDOWN]"},
1301 {K_KPAGEUP, "[KPAGEUP]"},
1302 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1303 {K_MOUSE, "[MOUSE]"},
1304 {K_KPLUS, "[KPLUS]"},
1305 {K_KMINUS, "[KMINUS]"},
1306 {K_KDIVIDE, "[KDIVIDE]"},
1307 {K_KMULTIPLY, "[KMULTIPLY]"},
1308 {K_KENTER, "[KENTER]"},
1309 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001310 {K_PS, "[PASTE-START]"},
1311 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_K0, "[K0]"},
1313 {K_K1, "[K1]"},
1314 {K_K2, "[K2]"},
1315 {K_K3, "[K3]"},
1316 {K_K4, "[K4]"},
1317 {K_K5, "[K5]"},
1318 {K_K6, "[K6]"},
1319 {K_K7, "[K7]"},
1320 {K_K8, "[K8]"},
1321 {K_K9, "[K9]"},
1322# endif
1323
1324#endif /* NO_BUILTIN_TCAPS */
1325
1326/*
1327 * The most minimal terminal: only clear screen and cursor positioning
1328 * Always included.
1329 */
1330 {(int)KS_NAME, "dumb"},
1331 {(int)KS_CL, "\014"},
1332#ifdef TERMINFO
1333 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1334 ESC_STR "[%i%p1%d;%p2%dH")},
1335#else
1336 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1337#endif
1338
1339/*
1340 * end marker
1341 */
1342 {(int)KS_NAME, NULL}
1343
1344}; /* end of builtin_termcaps */
1345
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001346#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001347 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001348termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001349{
Bram Moolenaarab302212016-04-26 20:59:29 +02001350 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001351}
1352
1353 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001354termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001355{
1356 guicolor_T t;
1357
1358 if (*name == NUL)
1359 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001360 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361
1362 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001363 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364 return t;
1365}
1366
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001367 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001368termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001369{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001370 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001371}
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374/*
1375 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#ifdef AMIGA
1378# define DEFAULT_TERM (char_u *)"amiga"
1379#endif
1380
1381#ifdef MSWIN
1382# define DEFAULT_TERM (char_u *)"win32"
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#if defined(UNIX) && !defined(__MINT__)
1386# define DEFAULT_TERM (char_u *)"ansi"
1387#endif
1388
1389#ifdef __MINT__
1390# define DEFAULT_TERM (char_u *)"vt52"
1391#endif
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393#ifdef VMS
1394# define DEFAULT_TERM (char_u *)"vt320"
1395#endif
1396
1397#ifdef __BEOS__
1398# undef DEFAULT_TERM
1399# define DEFAULT_TERM (char_u *)"beos-ansi"
1400#endif
1401
1402#ifndef DEFAULT_TERM
1403# define DEFAULT_TERM (char_u *)"dumb"
1404#endif
1405
1406/*
1407 * Term_strings contains currently used terminal output strings.
1408 * It is initialized with the default values by parse_builtin_tcap().
1409 * The values can be changed by setting the option with the same name.
1410 */
1411char_u *(term_strings[(int)KS_LAST + 1]);
1412
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001413static int need_gather = FALSE; /* need to fill termleader[] */
1414static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001416static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001417static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#endif
1419
1420 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001421find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 struct builtin_term *p;
1424
1425 p = builtin_termcaps;
1426 while (p->bt_string != NULL)
1427 {
1428 if (p->bt_entry == (int)KS_NAME)
1429 {
1430#ifdef UNIX
1431 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1432 return p;
1433 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1434 return p;
1435 else
1436#endif
1437#ifdef VMS
1438 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1439 return p;
1440 else
1441#endif
1442 if (STRCMP(term, p->bt_string) == 0)
1443 return p;
1444 }
1445 ++p;
1446 }
1447 return p;
1448}
1449
1450/*
1451 * Parsing of the builtin termcap entries.
1452 * Caller should check if 'name' is a valid builtin term.
1453 * The terminal's name is not set, as this is already done in termcapinit().
1454 */
1455 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001456parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 struct builtin_term *p;
1459 char_u name[2];
1460 int term_8bit;
1461
1462 p = find_builtin_term(term);
1463 term_8bit = term_is_8bit(term);
1464
1465 /* Do not parse if builtin term not found */
1466 if (p->bt_string == NULL)
1467 return;
1468
1469 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1470 {
1471 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1472 {
1473 /* Only set the value if it wasn't set yet. */
1474 if (term_strings[p->bt_entry] == NULL
1475 || term_strings[p->bt_entry] == empty_option)
1476 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001477#ifdef FEAT_EVAL
1478 int opt_idx = -1;
1479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /* 8bit terminal: use CSI instead of <Esc>[ */
1481 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1482 {
1483 char_u *s, *t;
1484
1485 s = vim_strsave((char_u *)p->bt_string);
1486 if (s != NULL)
1487 {
1488 for (t = s; *t; ++t)
1489 if (term_7to8bit(t))
1490 {
1491 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001492 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001495#ifdef FEAT_EVAL
1496 opt_idx =
1497#endif
1498 set_term_option_alloced(
1499 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 }
1502 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001505#ifdef FEAT_EVAL
1506 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1507#endif
1508 }
1509#ifdef FEAT_EVAL
1510 set_term_option_sctx_idx(NULL, opt_idx);
1511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 }
1514 else
1515 {
1516 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1517 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1518 if (find_termcode(name) == NULL)
1519 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1520 }
1521 }
1522}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524/*
1525 * Set number of colors.
1526 * Store it as a number in t_colors.
1527 * Store it as a string in T_CCO (using nr_colors[]).
1528 */
1529 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001530set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 char_u nr_colors[20]; /* string for number of colors */
1533
1534 t_colors = nr;
1535 if (t_colors > 1)
1536 sprintf((char *)nr_colors, "%d", t_colors);
1537 else
1538 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001541
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001542#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001543/*
1544 * Set the color count to "val" and redraw if it changed.
1545 */
1546 static void
1547may_adjust_color_count(int val)
1548{
1549 if (val != t_colors)
1550 {
1551 /* Nr of colors changed, initialize highlighting and
1552 * redraw everything. This causes a redraw, which usually
1553 * clears the message. Try keeping the message if it
1554 * might work. */
1555 set_keep_msg_from_hist();
1556 set_color_count(val);
1557 init_highlight(TRUE, FALSE);
1558# ifdef DEBUG_TERMRESPONSE
1559 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001560 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001561
Bram Moolenaarb255b902018-04-24 21:40:10 +02001562 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001563 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001564#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001565 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001566#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001567 }
1568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
1570
1571#ifdef HAVE_TGETENT
1572static char *(key_names[]) =
1573{
1574#ifdef FEAT_TERMRESPONSE
1575 /* Do this one first, it may cause a screen redraw. */
1576 "Co",
1577#endif
1578 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 "#2", "#4", "%i", "*7",
1580 "k1", "k2", "k3", "k4", "k5", "k6",
1581 "k7", "k8", "k9", "k;", "F1", "F2",
1582 "%1", "&8", "kb", "kI", "kD", "kh",
1583 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1584 NULL
1585};
1586#endif
1587
Bram Moolenaar9289df52018-05-10 14:40:57 +02001588#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001589 static void
1590get_term_entries(int *height, int *width)
1591{
1592 static struct {
1593 enum SpecialKey dest; /* index in term_strings[] */
1594 char *name; /* termcap name for string */
1595 } string_names[] =
1596 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1597 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1598 {KS_CL, "cl"}, {KS_CD, "cd"},
1599 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1600 {KS_ME, "me"}, {KS_MR, "mr"},
1601 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1602 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1603 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1604 {KS_STE,"Te"}, {KS_STS,"Ts"},
1605 {KS_CM, "cm"}, {KS_SR, "sr"},
1606 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1607 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1608 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1609 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1610 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1611 {KS_VS, "vs"}, {KS_CVS, "VS"},
1612 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1613 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1614 {KS_TS, "ts"}, {KS_FS, "fs"},
1615 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1616 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1617 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1618 {KS_8F, "8f"}, {KS_8B, "8b"},
1619 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1620 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001621 {KS_CST, "ST"}, {KS_CRT, "RT"},
1622 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001623 {(enum SpecialKey)0, NULL}
1624 };
1625 int i;
1626 char_u *p;
1627 static char_u tstrbuf[TBUFSZ];
1628 char_u *tp = tstrbuf;
1629
1630 /*
1631 * get output strings
1632 */
1633 for (i = 0; string_names[i].name != NULL; ++i)
1634 {
1635 if (TERM_STR(string_names[i].dest) == NULL
1636 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001637 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001638 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001639#ifdef FEAT_EVAL
1640 set_term_option_sctx_idx(string_names[i].name, -1);
1641#endif
1642 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001643 }
1644
1645 /* tgetflag() returns 1 if the flag is present, 0 if not and
1646 * possibly -1 if the flag doesn't exist. */
1647 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1648 T_MS = (char_u *)"y";
1649 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1650 T_XS = (char_u *)"y";
1651 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1652 T_XN = (char_u *)"y";
1653 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1654 T_DB = (char_u *)"y";
1655 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1656 T_DA = (char_u *)"y";
1657 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1658 T_UT = (char_u *)"y";
1659
1660 /*
1661 * get key codes
1662 */
1663 for (i = 0; key_names[i] != NULL; ++i)
1664 if (find_termcode((char_u *)key_names[i]) == NULL)
1665 {
1666 p = TGETSTR(key_names[i], &tp);
1667 /* if cursor-left == backspace, ignore it (televideo 925) */
1668 if (p != NULL
1669 && (*p != Ctrl_H
1670 || key_names[i][0] != 'k'
1671 || key_names[i][1] != 'l'))
1672 add_termcode((char_u *)key_names[i], p, FALSE);
1673 }
1674
1675 if (*height == 0)
1676 *height = tgetnum("li");
1677 if (*width == 0)
1678 *width = tgetnum("co");
1679
1680 /*
1681 * Get number of colors (if not done already).
1682 */
1683 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001684 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001685 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001686#ifdef FEAT_EVAL
1687 set_term_option_sctx_idx("Co", -1);
1688#endif
1689 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001690
1691# ifndef hpux
1692 BC = (char *)TGETSTR("bc", &tp);
1693 UP = (char *)TGETSTR("up", &tp);
1694 p = TGETSTR("pc", &tp);
1695 if (p)
1696 PC = *p;
1697# endif
1698}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001699#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700
1701 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001702report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001703{
1704 struct builtin_term *termp;
1705
1706 mch_errmsg("\r\n");
1707 if (error_msg != NULL)
1708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001709 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001710 mch_errmsg("\r\n");
1711 }
1712 mch_errmsg("'");
1713 mch_errmsg((char *)term);
1714 mch_errmsg(_("' not known. Available builtin terminals are:"));
1715 mch_errmsg("\r\n");
1716 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1717 {
1718 if (termp->bt_entry == (int)KS_NAME)
1719 {
1720#ifdef HAVE_TGETENT
1721 mch_errmsg(" builtin_");
1722#else
1723 mch_errmsg(" ");
1724#endif
1725 mch_errmsg(termp->bt_string);
1726 mch_errmsg("\r\n");
1727 }
1728 }
1729}
1730
1731 static void
1732report_default_term(char_u *term)
1733{
1734 mch_errmsg(_("defaulting to '"));
1735 mch_errmsg((char *)term);
1736 mch_errmsg("'\r\n");
1737 if (emsg_silent == 0)
1738 {
1739 screen_start(); /* don't know where cursor is now */
1740 out_flush();
1741 if (!is_not_a_term())
1742 ui_delay(2000L, TRUE);
1743 }
1744}
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
1747 * Set terminal options for terminal "term".
1748 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1749 *
1750 * While doing this, until ttest(), some options may be NULL, be careful.
1751 */
1752 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001753set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 struct builtin_term *termp;
1756#ifdef HAVE_TGETENT
1757 int builtin_first = p_tbi;
1758 int try;
1759 int termcap_cleared = FALSE;
1760#endif
1761 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 char_u *bs_p, *del_p;
1764
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001765 /* In silect mode (ex -s) we don't use the 'term' option. */
1766 if (silent_mode)
1767 return OK;
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 detected_8bit = FALSE; /* reset 8-bit detection */
1770
1771 if (term_is_builtin(term))
1772 {
1773 term += 8;
1774#ifdef HAVE_TGETENT
1775 builtin_first = 1;
1776#endif
1777 }
1778
1779/*
1780 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1781 * If builtin_first is TRUE:
1782 * 0. try builtin termcap
1783 * 1. try external termcap
1784 * 2. if both fail default to a builtin terminal
1785 * If builtin_first is FALSE:
1786 * 1. try external termcap
1787 * 2. try builtin termcap, if both fail default to a builtin terminal
1788 */
1789#ifdef HAVE_TGETENT
1790 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1791 {
1792 /*
1793 * Use external termcap
1794 */
1795 if (try == 1)
1796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 /*
1800 * If the external termcap does not have a matching entry, try the
1801 * builtin ones.
1802 */
1803 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (!termcap_cleared)
1806 {
1807 clear_termoptions(); /* clear old options */
1808 termcap_cleared = TRUE;
1809 }
1810
Bram Moolenaar69e05692018-05-10 14:11:52 +02001811 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 }
1814 else /* try == 0 || try == 2 */
1815#endif /* HAVE_TGETENT */
1816 /*
1817 * Use builtin termcap
1818 */
1819 {
1820#ifdef HAVE_TGETENT
1821 /*
1822 * If builtin termcap was already used, there is no need to search
1823 * for the builtin termcap again, quit now.
1824 */
1825 if (try == 2 && builtin_first && termcap_cleared)
1826 break;
1827#endif
1828 /*
1829 * search for 'term' in builtin_termcaps[]
1830 */
1831 termp = find_builtin_term(term);
1832 if (termp->bt_string == NULL) /* did not find it */
1833 {
1834#ifdef HAVE_TGETENT
1835 /*
1836 * If try == 0, first try the external termcap. If that is not
1837 * found we'll get back here with try == 2.
1838 * If termcap_cleared is set we used the external termcap,
1839 * don't complain about not finding the term in the builtin
1840 * termcap.
1841 */
1842 if (try == 0) /* try external one */
1843 continue;
1844 if (termcap_cleared) /* found in external termcap */
1845 break;
1846#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001847 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 /* when user typed :set term=xxx, quit here */
1850 if (starting != NO_SCREEN)
1851 {
1852 screen_start(); /* don't know where cursor is now */
1853 wait_return(TRUE);
1854 return FAIL;
1855 }
1856 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001857 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001858 set_string_option_direct((char_u *)"term", -1, term,
1859 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 display_errors();
1861 }
1862 out_flush();
1863#ifdef HAVE_TGETENT
1864 if (!termcap_cleared)
1865 {
1866#endif
1867 clear_termoptions(); /* clear old options */
1868#ifdef HAVE_TGETENT
1869 termcap_cleared = TRUE;
1870 }
1871#endif
1872 parse_builtin_tcap(term);
1873#ifdef FEAT_GUI
1874 if (term_is_gui(term))
1875 {
1876 out_flush();
1877 gui_init();
1878 /* If starting the GUI failed, don't do any of the other
1879 * things for this terminal */
1880 if (!gui.in_use)
1881 return FAIL;
1882#ifdef HAVE_TGETENT
1883 break; /* don't try using external termcap */
1884#endif
1885 }
1886#endif /* FEAT_GUI */
1887 }
1888#ifdef HAVE_TGETENT
1889 }
1890#endif
1891
1892/*
1893 * special: There is no info in the termcap about whether the cursor
1894 * positioning is relative to the start of the screen or to the start of the
1895 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1896 * relative.
1897 */
1898 if (STRCMP(term, "pcterm") == 0)
1899 T_CCS = (char_u *)"yes";
1900 else
1901 T_CCS = empty_option;
1902
1903#ifdef UNIX
1904/*
1905 * Any "stty" settings override the default for t_kb from the termcap.
1906 * This is in os_unix.c, because it depends a lot on the version of unix that
1907 * is being used.
1908 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1909 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001910# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 get_stty();
1914#endif
1915
1916/*
1917 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1918 * didn't work, use the default CTRL-H
1919 * The default for t_kD is DEL, unless t_kb is DEL.
1920 * The vim_strsave'd strings are probably lost forever, well it's only two
1921 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1922 * directly.
1923 */
1924#ifdef FEAT_GUI
1925 if (!gui.in_use)
1926#endif
1927 {
1928 bs_p = find_termcode((char_u *)"kb");
1929 del_p = find_termcode((char_u *)"kD");
1930 if (bs_p == NULL || *bs_p == NUL)
1931 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1932 if ((del_p == NULL || *del_p == NUL) &&
1933 (bs_p == NULL || *bs_p != DEL))
1934 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1935 }
1936
1937#if defined(UNIX) || defined(VMS)
1938 term_is_xterm = vim_is_xterm(term);
1939#endif
1940
1941#ifdef FEAT_MOUSE
1942# if defined(UNIX) || defined(VMS)
1943# ifdef FEAT_MOUSE_TTY
1944 /*
1945 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1946 * The termcode for the mouse is added as a side effect in option.c.
1947 */
1948 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001949 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001952 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 if (use_xterm_mouse())
1955 p = NULL; /* keep existing value, might be "xterm2" */
1956 else
1957 p = (char_u *)"xterm";
1958 }
1959# endif
1960 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001963 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1964 * "xterm2" in check_termcode(). */
1965 reset_option_was_set((char_u *)"ttym");
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (p == NULL
1968# ifdef FEAT_GUI
1969 || gui.in_use
1970# endif
1971 )
1972 check_mouse_termcode(); /* set mouse termcode anyway */
1973 }
1974# endif
1975# else
1976 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1977# endif
1978#endif /* FEAT_MOUSE */
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980#ifdef USE_TERM_CONSOLE
1981 /* DEFAULT_TERM indicates that it is the machine console. */
1982 if (STRCMP(term, DEFAULT_TERM) != 0)
1983 term_console = FALSE;
1984 else
1985 {
1986 term_console = TRUE;
1987# ifdef AMIGA
1988 win_resize_on(); /* enable window resizing reports */
1989# endif
1990 }
1991#endif
1992
1993#if defined(UNIX) || defined(VMS)
1994 /*
1995 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1996 */
1997 if (vim_is_fastterm(term))
1998 p_tf = TRUE;
1999#endif
2000#ifdef USE_TERM_CONSOLE
2001 /*
2002 * 'ttyfast' is default on consoles
2003 */
2004 if (term_console)
2005 p_tf = TRUE;
2006#endif
2007
2008 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2009
2010 full_screen = TRUE; /* we can use termcap codes from now on */
2011 set_term_defaults(); /* use current values as defaults */
2012#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002013 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002014 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#endif
2016
2017 /*
2018 * Initialize the terminal with the appropriate termcap codes.
2019 * Set the mouse and window title if possible.
2020 * Don't do this when starting, need to parse the .vimrc first, because it
2021 * may redefine t_TI etc.
2022 */
2023 if (starting != NO_SCREEN)
2024 {
2025 starttermcap(); /* may change terminal mode */
2026#ifdef FEAT_MOUSE
2027 setmouse(); /* may start using the mouse */
2028#endif
2029#ifdef FEAT_TITLE
2030 maketitle(); /* may display window title */
2031#endif
2032 }
2033
2034 /* display initial screen after ttest() checking. jw. */
2035 if (width <= 0 || height <= 0)
2036 {
2037 /* termcap failed to report size */
2038 /* set defaults, in case ui_get_shellsize() also fails */
2039 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002040#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 height = 25; /* console is often 25 lines */
2042#else
2043 height = 24; /* most terminals are 24 lines */
2044#endif
2045 }
2046 set_shellsize(width, height, FALSE); /* may change Rows */
2047 if (starting != NO_SCREEN)
2048 {
2049 if (scroll_region)
2050 scroll_region_reset(); /* In case Rows changed */
2051 check_map_keycodes(); /* check mappings for terminal codes used */
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002054 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 /*
2057 * Execute the TermChanged autocommands for each buffer that is
2058 * loaded.
2059 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002060 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002061 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
2063 if (curbuf->b_ml.ml_mfp != NULL)
2064 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2065 curbuf);
2066 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002067 if (bufref_valid(&old_curbuf))
2068 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
2072#ifdef FEAT_TERMRESPONSE
2073 may_req_termresponse();
2074#endif
2075
2076 return OK;
2077}
2078
2079#if defined(FEAT_MOUSE) || defined(PROTO)
2080
2081# ifdef FEAT_MOUSE_TTY
2082# define HMT_NORMAL 1
2083# define HMT_NETTERM 2
2084# define HMT_DEC 4
2085# define HMT_JSBTERM 8
2086# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002087# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002088# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002089# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090static int has_mouse_termcode = 0;
2091# endif
2092
Bram Moolenaar864207d2008-06-24 22:14:38 +00002093# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002095set_mouse_termcode(
2096 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2097 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 char_u name[2];
2100
2101 name[0] = n;
2102 name[1] = KE_FILLER;
2103 add_termcode(name, s, FALSE);
2104# ifdef FEAT_MOUSE_TTY
2105# ifdef FEAT_MOUSE_JSB
2106 if (n == KS_JSBTERM_MOUSE)
2107 has_mouse_termcode |= HMT_JSBTERM;
2108 else
2109# endif
2110# ifdef FEAT_MOUSE_NET
2111 if (n == KS_NETTERM_MOUSE)
2112 has_mouse_termcode |= HMT_NETTERM;
2113 else
2114# endif
2115# ifdef FEAT_MOUSE_DEC
2116 if (n == KS_DEC_MOUSE)
2117 has_mouse_termcode |= HMT_DEC;
2118 else
2119# endif
2120# ifdef FEAT_MOUSE_PTERM
2121 if (n == KS_PTERM_MOUSE)
2122 has_mouse_termcode |= HMT_PTERM;
2123 else
2124# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002125# ifdef FEAT_MOUSE_URXVT
2126 if (n == KS_URXVT_MOUSE)
2127 has_mouse_termcode |= HMT_URXVT;
2128 else
2129# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002130 if (n == KS_SGR_MOUSE)
2131 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002132 else if (n == KS_SGR_MOUSE_RELEASE)
2133 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 has_mouse_termcode |= HMT_NORMAL;
2136# endif
2137}
2138# endif
2139
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002140# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002141 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002143del_mouse_termcode(
2144 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 char_u name[2];
2147
2148 name[0] = n;
2149 name[1] = KE_FILLER;
2150 del_termcode(name);
2151# ifdef FEAT_MOUSE_TTY
2152# ifdef FEAT_MOUSE_JSB
2153 if (n == KS_JSBTERM_MOUSE)
2154 has_mouse_termcode &= ~HMT_JSBTERM;
2155 else
2156# endif
2157# ifdef FEAT_MOUSE_NET
2158 if (n == KS_NETTERM_MOUSE)
2159 has_mouse_termcode &= ~HMT_NETTERM;
2160 else
2161# endif
2162# ifdef FEAT_MOUSE_DEC
2163 if (n == KS_DEC_MOUSE)
2164 has_mouse_termcode &= ~HMT_DEC;
2165 else
2166# endif
2167# ifdef FEAT_MOUSE_PTERM
2168 if (n == KS_PTERM_MOUSE)
2169 has_mouse_termcode &= ~HMT_PTERM;
2170 else
2171# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002172# ifdef FEAT_MOUSE_URXVT
2173 if (n == KS_URXVT_MOUSE)
2174 has_mouse_termcode &= ~HMT_URXVT;
2175 else
2176# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002177 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
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 has_mouse_termcode &= ~HMT_NORMAL;
2183# endif
2184}
2185# endif
2186#endif
2187
2188#ifdef HAVE_TGETENT
2189/*
2190 * Call tgetent()
2191 * Return error message if it fails, NULL if it's OK.
2192 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002193 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002194tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195{
2196 int i;
2197
2198 i = TGETENT(tbuf, term);
2199 if (i < 0 /* -1 is always an error */
2200# ifdef TGETENT_ZERO_ERR
2201 || i == 0 /* sometimes zero is also an error */
2202# endif
2203 )
2204 {
2205 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2206 * tgetent() with the always existing "dumb" entry to avoid a crash or
2207 * hang. */
2208 (void)TGETENT(tbuf, "dumb");
2209
2210 if (i < 0)
2211# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (i == 0)
2214# endif
2215#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220 }
2221 return NULL;
2222}
2223
2224/*
2225 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2226 * Fix that here.
2227 */
2228 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002229vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 char *p;
2232
2233 p = tgetstr(s, (char **)pp);
2234 if (p == (char *)-1)
2235 p = NULL;
2236 return (char_u *)p;
2237}
2238#endif /* HAVE_TGETENT */
2239
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002240#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241/*
2242 * Get Columns and Rows from the termcap. Used after a window signal if the
2243 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2244 * and "li" entries never change. But on some systems this works.
2245 * Errors while getting the entries are ignored.
2246 */
2247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002248getlinecol(
2249 long *cp, /* pointer to columns */
2250 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 char_u tbuf[TBUFSZ];
2253
2254 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2255 {
2256 if (*cp == 0)
2257 *cp = tgetnum("co");
2258 if (*rp == 0)
2259 *rp = tgetnum("li");
2260 }
2261}
2262#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2263
2264/*
2265 * Get a string entry from the termcap and add it to the list of termcodes.
2266 * Used for <t_xx> special keys.
2267 * Give an error message for failure when not sourcing.
2268 * If force given, replace an existing entry.
2269 * Return FAIL if the entry was not found, OK if the entry was added.
2270 */
2271 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002272add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 char_u *term;
2275 int key;
2276 struct builtin_term *termp;
2277#ifdef HAVE_TGETENT
2278 char_u *string;
2279 int i;
2280 int builtin_first;
2281 char_u tbuf[TBUFSZ];
2282 char_u tstrbuf[TBUFSZ];
2283 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#endif
2286
2287/*
2288 * If the GUI is running or will start in a moment, we only support the keys
2289 * that the GUI can produce.
2290 */
2291#ifdef FEAT_GUI
2292 if (gui.in_use || gui.starting)
2293 return gui_mch_haskey(name);
2294#endif
2295
2296 if (!force && find_termcode(name) != NULL) /* it's already there */
2297 return OK;
2298
2299 term = T_NAME;
2300 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2301 return FAIL;
2302
2303 if (term_is_builtin(term)) /* name starts with "builtin_" */
2304 {
2305 term += 8;
2306#ifdef HAVE_TGETENT
2307 builtin_first = TRUE;
2308#endif
2309 }
2310#ifdef HAVE_TGETENT
2311 else
2312 builtin_first = p_tbi;
2313#endif
2314
2315#ifdef HAVE_TGETENT
2316/*
2317 * We can get the entry from the builtin termcap and from the external one.
2318 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2319 * builtin termcap first.
2320 * If 'ttybuiltin' is off, try external termcap first.
2321 */
2322 for (i = 0; i < 2; ++i)
2323 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002324 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#endif
2326 /*
2327 * Search in builtin termcap
2328 */
2329 {
2330 termp = find_builtin_term(term);
2331 if (termp->bt_string != NULL) /* found it */
2332 {
2333 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002334 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 while (termp->bt_entry != (int)KS_NAME)
2336 {
2337 if ((int)termp->bt_entry == key)
2338 {
2339 add_termcode(name, (char_u *)termp->bt_string,
2340 term_is_8bit(term));
2341 return OK;
2342 }
2343 ++termp;
2344 }
2345 }
2346 }
2347#ifdef HAVE_TGETENT
2348 else
2349 /*
2350 * Search in external termcap
2351 */
2352 {
2353 error_msg = tgetent_error(tbuf, term);
2354 if (error_msg == NULL)
2355 {
2356 string = TGETSTR((char *)name, &tp);
2357 if (string != NULL && *string != NUL)
2358 {
2359 add_termcode(name, string, FALSE);
2360 return OK;
2361 }
2362 }
2363 }
2364 }
2365#endif
2366
2367 if (sourcing_name == NULL)
2368 {
2369#ifdef HAVE_TGETENT
2370 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 else
2373#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002374 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 return FAIL;
2377}
2378
2379 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002380term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2383}
2384
2385/*
2386 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2387 * Assume that the terminal is using 8-bit controls when the name contains
2388 * "8bit", like in "xterm-8bit".
2389 */
2390 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002391term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2394}
2395
2396/*
2397 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002398 * <Esc>[ -> CSI <M_C_[>
2399 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * <Esc>O -> <M-C-O>
2401 */
2402 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002403term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
2405 if (*p == ESC)
2406 {
2407 if (p[1] == '[')
2408 return CSI;
2409 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002410 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (p[1] == 'O')
2412 return 0x8f;
2413 }
2414 return 0;
2415}
2416
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002417#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002419term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2422}
2423#endif
2424
2425#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2426
2427 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002428tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 static char_u buf[16];
2431 char_u *p;
2432
2433 p = buf + 15;
2434 *p = '\0';
2435 do
2436 {
2437 --p;
2438 *p = (char_u) (i % 10 + '0');
2439 i /= 10;
2440 }
2441 while (i > 0 && p > buf);
2442 return p;
2443}
2444#endif
2445
2446#ifndef HAVE_TGETENT
2447
2448/*
2449 * minimal tgoto() implementation.
2450 * no padding and we only parse for %i %d and %+char
2451 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002452 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002453tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 static char buf[30];
2456 char *p, *s, *e;
2457
2458 if (!cm)
2459 return "OOPS";
2460 e = buf + 29;
2461 for (s = buf; s < e && *cm; cm++)
2462 {
2463 if (*cm != '%')
2464 {
2465 *s++ = *cm;
2466 continue;
2467 }
2468 switch (*++cm)
2469 {
2470 case 'd':
2471 p = (char *)tltoa((unsigned long)y);
2472 y = x;
2473 while (*p)
2474 *s++ = *p++;
2475 break;
2476 case 'i':
2477 x++;
2478 y++;
2479 break;
2480 case '+':
2481 *s++ = (char)(*++cm + y);
2482 y = x;
2483 break;
2484 case '%':
2485 *s++ = *cm;
2486 break;
2487 default:
2488 return "OOPS";
2489 }
2490 }
2491 *s = '\0';
2492 return buf;
2493}
2494
2495#endif /* HAVE_TGETENT */
2496
2497/*
2498 * Set the terminal name and initialize the terminal options.
2499 * If "name" is NULL or empty, get the terminal name from the environment.
2500 * If that fails, use the default terminal name.
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 char_u *term;
2506
2507 if (name != NULL && *name == NUL)
2508 name = NULL; /* empty name is equal to no name */
2509 term = name;
2510
2511#ifdef __BEOS__
2512 /*
2513 * TERM environment variable is normally set to 'ansi' on the Bebox;
2514 * Since the BeBox doesn't quite support full ANSI yet, we use our
2515 * own custom 'ansi-beos' termcap instead, unless the -T option has
2516 * been given on the command line.
2517 */
2518 if (term == NULL
2519 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2520 term = DEFAULT_TERM;
2521#endif
2522#ifndef MSWIN
2523 if (term == NULL)
2524 term = mch_getenv((char_u *)"TERM");
2525#endif
2526 if (term == NULL || *term == NUL)
2527 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002528 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 /* Set the default terminal name. */
2531 set_string_default("term", term);
2532 set_string_default("ttytype", term);
2533
2534 /*
2535 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2536 */
2537 set_termname(T_NAME != NULL ? T_NAME : term);
2538}
2539
2540/*
2541 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2542 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002543#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2545static char_u out_buf[OUT_SIZE + 1];
2546static int out_pos = 0; /* number of chars in out_buf */
2547
2548/*
2549 * out_flush(): flush the output buffer
2550 */
2551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002552out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 int len;
2555
2556 if (out_pos != 0)
2557 {
2558 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2559 len = out_pos;
2560 out_pos = 0;
2561 ui_write(out_buf, len);
2562 }
2563}
2564
Bram Moolenaara338adc2018-01-31 20:51:47 +01002565/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002566 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2567 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002568 */
2569 void
2570out_flush_cursor(
2571 int force UNUSED, /* when TRUE, update cursor even when not moved */
2572 int clear_selection UNUSED) /* clear selection under cursor */
2573{
2574 mch_disable_flush();
2575 out_flush();
2576 mch_enable_flush();
2577#ifdef FEAT_GUI
2578 if (gui.in_use)
2579 {
2580 gui_update_cursor(force, clear_selection);
2581 gui_may_flush();
2582 }
2583#endif
2584}
2585
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587/*
2588 * Sometimes a byte out of a multi-byte character is written with out_char().
2589 * To avoid flushing half of the character, call this function first.
2590 */
2591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2595 out_flush();
2596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598#ifdef FEAT_GUI
2599/*
2600 * out_trash(): Throw away the contents of the output buffer
2601 */
2602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002603out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 out_pos = 0;
2606}
2607#endif
2608
2609/*
2610 * out_char(c): put a byte into the output buffer.
2611 * Flush it if it becomes full.
2612 * This should not be used for outputting text on the screen (use functions
2613 * like msg_puts() and screen_putchar() for that).
2614 */
2615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002616out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaard0573012017-10-28 21:11:06 +02002618#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2620 out_char('\r');
2621#endif
2622
2623 out_buf[out_pos++] = c;
2624
2625 /* For testing we flush each time. */
2626 if (out_pos >= OUT_SIZE || p_wd)
2627 out_flush();
2628}
2629
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002630static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632/*
2633 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2634 */
2635 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002636out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
Bram Moolenaard0573012017-10-28 21:11:06 +02002638#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2640 out_char_nf('\r');
2641#endif
2642
2643 out_buf[out_pos++] = c;
2644
2645 if (out_pos >= OUT_SIZE)
2646 out_flush();
2647}
2648
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002649#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2650 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651/*
2652 * A never-padding out_str.
2653 * use this whenever you don't want to run the string through tputs.
2654 * tputs above is harmless, but tputs from the termcap library
2655 * is likely to strip off leading digits, that it mistakes for padding
2656 * information, and "%i", "%d", etc.
2657 * This should only be used for writing terminal codes, not for outputting
2658 * normal text (use functions like msg_puts() and screen_putchar() for that).
2659 */
2660 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002661out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2664 out_flush();
2665 while (*s)
2666 out_char_nf(*s++);
2667
2668 /* For testing we write one string at a time. */
2669 if (p_wd)
2670 out_flush();
2671}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002675 * A conditional-flushing out_str, mainly for visualbell.
2676 * Handles a delay internally, because termlib may not respect the delay or do
2677 * it at the wrong time.
2678 * Note: Only for terminal strings.
2679 */
2680 void
2681out_str_cf(char_u *s)
2682{
2683 if (s != NULL && *s)
2684 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002685#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002686 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002687#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002688
2689#ifdef FEAT_GUI
2690 /* Don't use tputs() when GUI is used, ncurses crashes. */
2691 if (gui.in_use)
2692 {
2693 out_str_nf(s);
2694 return;
2695 }
2696#endif
2697 if (out_pos > OUT_SIZE - 20)
2698 out_flush();
2699#ifdef HAVE_TGETENT
2700 for (p = s; *s; ++s)
2701 {
2702 /* flush just before delay command */
2703 if (*s == '$' && *(s + 1) == '<')
2704 {
2705 char_u save_c = *s;
2706 int duration = atoi((char *)s + 2);
2707
2708 *s = NUL;
2709 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2710 *s = save_c;
2711 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002712# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002713 /* Only sleep here if we can limit this happening in
2714 * vim_beep(). */
2715 p = vim_strchr(s, '>');
2716 if (p == NULL || duration <= 0)
2717 {
2718 /* can't parse the time, don't sleep here */
2719 p = s;
2720 }
2721 else
2722 {
2723 ++p;
2724 do_sleep(duration);
2725 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002726# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002727 /* Rely on the terminal library to sleep. */
2728 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002730 break;
2731 }
2732 }
2733 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2734#else
2735 while (*s)
2736 out_char_nf(*s++);
2737#endif
2738
2739 /* For testing we write one string at a time. */
2740 if (p_wd)
2741 out_flush();
2742 }
2743}
2744
2745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 * out_str(s): Put a character string a byte at a time into the output buffer.
2747 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2748 * This should only be used for writing terminal codes, not for outputting
2749 * normal text (use functions like msg_puts() and screen_putchar() for that).
2750 */
2751 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002752out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753{
2754 if (s != NULL && *s)
2755 {
2756#ifdef FEAT_GUI
2757 /* Don't use tputs() when GUI is used, ncurses crashes. */
2758 if (gui.in_use)
2759 {
2760 out_str_nf(s);
2761 return;
2762 }
2763#endif
2764 /* avoid terminal strings being split up */
2765 if (out_pos > OUT_SIZE - 20)
2766 out_flush();
2767#ifdef HAVE_TGETENT
2768 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2769#else
2770 while (*s)
2771 out_char_nf(*s++);
2772#endif
2773
2774 /* For testing we write one string at a time. */
2775 if (p_wd)
2776 out_flush();
2777 }
2778}
2779
2780/*
2781 * cursor positioning using termcap parser. (jw)
2782 */
2783 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002784term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 OUT_STR(tgoto((char *)T_CM, col, row));
2787}
2788
2789 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002790term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 OUT_STR(tgoto((char *)T_CRI, 0, i));
2793}
2794
2795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2799}
2800
2801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002802term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2805}
2806
2807#if defined(HAVE_TGETENT) || defined(PROTO)
2808 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002809term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 /* Can't handle a negative value here */
2812 if (x < 0)
2813 x = 0;
2814 if (y < 0)
2815 y = 0;
2816 OUT_STR(tgoto((char *)T_CWP, y, x));
2817}
2818
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002819# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2820/*
2821 * Return TRUE if we can request the terminal for a response.
2822 */
2823 static int
2824can_get_termresponse()
2825{
2826 return cur_tmode == TMODE_RAW
2827 && termcap_active
2828# ifdef UNIX
2829 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2830# endif
2831 && p_ek;
2832}
2833
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002834static int winpos_x = -1;
2835static int winpos_y = -1;
2836static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002837
Bram Moolenaar113e1072019-01-20 15:30:40 +01002838# if (defined(FEAT_EVAL) && defined(HAVE_TGETENT)) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002839/*
2840 * Try getting the Vim window position from the terminal.
2841 * Returns OK or FAIL.
2842 */
2843 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002844term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845{
2846 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002847 int prev_winpos_x = winpos_x;
2848 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002849
2850 if (*T_CGP == NUL || !can_get_termresponse())
2851 return FAIL;
2852 winpos_x = -1;
2853 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002854 ++did_request_winpos;
2855 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002856 OUT_STR(T_CGP);
2857 out_flush();
2858
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002859 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002860 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002861 {
2862 (void)vpeekc_nomap();
2863 if (winpos_x >= 0 && winpos_y >= 0)
2864 {
2865 *x = winpos_x;
2866 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002867 return OK;
2868 }
2869 ui_delay(10, FALSE);
2870 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002871 /* Do not reset "did_request_winpos", if we timed out the response might
2872 * still come later and we must consume it. */
2873
2874 winpos_x = prev_winpos_x;
2875 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002876 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002877 {
2878 /* Polling: return previous values if we have them. */
2879 *x = winpos_x;
2880 *y = winpos_y;
2881 return OK;
2882 }
2883
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002884 return FALSE;
2885}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002886# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002887# endif
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002890term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002892 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893}
2894#endif
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002897term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002900 int i = *s == CSI ? 1 : 2;
2901 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 /* Special handling of 16 colors, because termcap can't handle it */
2904 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2905 /* Also accept CSI instead of <Esc>[ */
2906 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002907 && ((s[0] == ESC && s[1] == '[')
2908#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2909 || (s[0] == ESC && s[1] == '|')
2910#endif
2911 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 && s[i] != NUL
2913 && (STRCMP(s + i + 1, "%p1%dm") == 0
2914 || STRCMP(s + i + 1, "%dm") == 0)
2915 && (s[i] == '3' || s[i] == '4'))
2916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002918 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002920 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002922 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002923#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002924 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002925#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002926 IF_EB("\033[", ESC_STR "[")) : "\233";
2927 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2928 : (n >= 16 ? "48;5;" : "10");
2929
2930 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2932 }
2933 else
2934 OUT_STR(tgoto((char *)s, 0, n));
2935}
2936
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002937 void
2938term_fg_color(int n)
2939{
2940 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2941 if (*T_CAF)
2942 term_color(T_CAF, n);
2943 else if (*T_CSF)
2944 term_color(T_CSF, n);
2945}
2946
2947 void
2948term_bg_color(int n)
2949{
2950 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2951 if (*T_CAB)
2952 term_color(T_CAB, n);
2953 else if (*T_CSB)
2954 term_color(T_CSB, n);
2955}
2956
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002957#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002958
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002959#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2960#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2961#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002962
2963 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002964term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002965{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002966#define MAX_COLOR_STR_LEN 100
2967 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002968
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002969 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002970 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002971 OUT_STR(buf);
2972}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002973
2974 void
2975term_fg_rgb_color(guicolor_T rgb)
2976{
2977 term_rgb_color(T_8F, rgb);
2978}
2979
2980 void
2981term_bg_rgb_color(guicolor_T rgb)
2982{
2983 term_rgb_color(T_8B, rgb);
2984}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002985#endif
2986
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002987#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2988 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989/*
2990 * Generic function to set window title, using t_ts and t_fs.
2991 */
2992 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002993term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 /* t_ts takes one argument: column in status line */
2996 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2997 out_str_nf(title);
2998 out_str(T_FS); /* set title end */
2999 out_flush();
3000}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003001
3002/*
3003 * Tell the terminal to push (save) the title and/or icon, so that it can be
3004 * popped (restored) later.
3005 */
3006 void
3007term_push_title(int which)
3008{
3009 if ((which & SAVE_RESTORE_TITLE) && *T_CST != NUL)
3010 {
3011 OUT_STR(T_CST);
3012 out_flush();
3013 }
3014
3015 if ((which & SAVE_RESTORE_ICON) && *T_SSI != NUL)
3016 {
3017 OUT_STR(T_SSI);
3018 out_flush();
3019 }
3020}
3021
3022/*
3023 * Tell the terminal to pop the title and/or icon.
3024 */
3025 void
3026term_pop_title(int which)
3027{
3028 if ((which & SAVE_RESTORE_TITLE) && *T_CRT != NUL)
3029 {
3030 OUT_STR(T_CRT);
3031 out_flush();
3032 }
3033
3034 if ((which & SAVE_RESTORE_ICON) && *T_SRI != NUL)
3035 {
3036 OUT_STR(T_SRI);
3037 out_flush();
3038 }
3039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#endif
3041
3042/*
3043 * Make sure we have a valid set or terminal options.
3044 * Replace all entries that are NULL by empty_option
3045 */
3046 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003047ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003049 char_u *env_colors;
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 check_options(); /* make sure no options are NULL */
3052
3053 /*
3054 * MUST have "cm": cursor motion.
3055 */
3056 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003057 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 /*
3060 * if "cs" defined, use a scroll region, it's faster.
3061 */
3062 if (*T_CS != NUL)
3063 scroll_region = TRUE;
3064 else
3065 scroll_region = FALSE;
3066
3067 if (pairs)
3068 {
3069 /*
3070 * optional pairs
3071 */
3072 /* TP goes to normal mode for TI (invert) and TB (bold) */
3073 if (*T_ME == NUL)
3074 T_ME = T_MR = T_MD = T_MB = empty_option;
3075 if (*T_SO == NUL || *T_SE == NUL)
3076 T_SO = T_SE = empty_option;
3077 if (*T_US == NUL || *T_UE == NUL)
3078 T_US = T_UE = empty_option;
3079 if (*T_CZH == NUL || *T_CZR == NUL)
3080 T_CZH = T_CZR = empty_option;
3081
3082 /* T_VE is needed even though T_VI is not defined */
3083 if (*T_VE == NUL)
3084 T_VI = empty_option;
3085
3086 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3087 if (*T_ME == NUL)
3088 {
3089 T_ME = T_SE;
3090 T_MR = T_SO;
3091 T_MD = T_SO;
3092 }
3093
3094 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3095 if (*T_SO == NUL)
3096 {
3097 T_SE = T_ME;
3098 if (*T_MR == NUL)
3099 T_SO = T_MD;
3100 else
3101 T_SO = T_MR;
3102 }
3103
3104 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3105 if (*T_CZH == NUL)
3106 {
3107 T_CZR = T_ME;
3108 if (*T_MR == NUL)
3109 T_CZH = T_MD;
3110 else
3111 T_CZH = T_MR;
3112 }
3113
3114 /* "Sb" and "Sf" come in pairs */
3115 if (*T_CSB == NUL || *T_CSF == NUL)
3116 {
3117 T_CSB = empty_option;
3118 T_CSF = empty_option;
3119 }
3120
3121 /* "AB" and "AF" come in pairs */
3122 if (*T_CAB == NUL || *T_CAF == NUL)
3123 {
3124 T_CAB = empty_option;
3125 T_CAF = empty_option;
3126 }
3127
3128 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3129 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003130 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 /* Set 'weirdinvert' according to value of 't_xs' */
3133 p_wiv = (*T_XS != NUL);
3134 }
3135 need_gather = TRUE;
3136
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003137 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003139 env_colors = mch_getenv((char_u *)"COLORS");
3140 if (env_colors != NULL && isdigit(*env_colors))
3141 {
3142 int colors = atoi((char *)env_colors);
3143
3144 if (colors != t_colors)
3145 set_color_count(colors);
3146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148
3149#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3150 || defined(PROTO)
3151/*
3152 * Represent the given long_u as individual bytes, with the most significant
3153 * byte first, and store them in dst.
3154 */
3155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003156add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 int i;
3159 int shift;
3160
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003161 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 shift = 8 * (sizeof(long_u) - i);
3164 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3165 }
3166}
3167
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168/*
3169 * Interpret the next string of bytes in buf as a long integer, with the most
3170 * significant byte first. Note that it is assumed that buf has been through
3171 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3172 * Puts result in val, and returns the number of bytes read from buf
3173 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3174 * were present.
3175 */
3176 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003177get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179 int len;
3180 char_u bytes[sizeof(long_u)];
3181 int i;
3182 int shift;
3183
3184 *val = 0;
3185 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3186 if (len != -1)
3187 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003188 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 shift = 8 * (sizeof(long_u) - 1 - i);
3191 *val += (long_u)bytes[i] << shift;
3192 }
3193 }
3194 return len;
3195}
3196#endif
3197
3198#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003199 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3200 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201/*
3202 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3203 * that buf has been through inchar(). Returns the actual number of bytes used
3204 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3205 * available.
3206 */
3207 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003208get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int len = 0;
3211 int i;
3212 char_u c;
3213
3214 for (i = 0; i < num_bytes; i++)
3215 {
3216 if ((c = buf[len++]) == NUL)
3217 return -1;
3218 if (c == K_SPECIAL)
3219 {
3220 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3221 return -1;
3222 if (buf[len++] == (int)KS_ZERO)
3223 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003224 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3225 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3226 if (buf[len++] == (int)KE_CSI)
3227 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003229 else if (c == CSI && buf[len] == KS_EXTRA
3230 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003231 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3232 * the start of a special key, see add_to_input_buf_csi(). */
3233 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 bytes[i] = c;
3235 }
3236 return len;
3237}
3238#endif
3239
3240/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003241 * Check if the new shell size is valid, correct it if it's too small or way
3242 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
3244 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003245check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (Rows < min_rows()) /* need room for one window and command line */
3248 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003249 limit_screen_size();
3250}
3251
3252/*
3253 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3254 */
3255 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003256limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003257{
3258 if (Columns < MIN_COLUMNS)
3259 Columns = MIN_COLUMNS;
3260 else if (Columns > 10000)
3261 Columns = 10000;
3262 if (Rows > 1000)
3263 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
Bram Moolenaar86b68352004-12-27 21:59:20 +00003266/*
3267 * Invoked just before the screen structures are going to be (re)allocated.
3268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003270win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 static int old_Rows = 0;
3273 static int old_Columns = 0;
3274
3275 if (old_Rows != Rows || old_Columns != Columns)
3276 ui_new_shellsize();
3277 if (old_Rows != Rows)
3278 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003279 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003280 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003281 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 old_Rows = Rows;
3283 shell_new_rows(); /* update window sizes */
3284 }
3285 if (old_Columns != Columns)
3286 {
3287 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290}
3291
3292/*
3293 * Call this function when the Vim shell has been resized in any way.
3294 * Will obtain the current size and redraw (also when size didn't change).
3295 */
3296 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003297shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 set_shellsize(0, 0, FALSE);
3300}
3301
3302/*
3303 * Check if the shell size changed. Handle a resize.
3304 * When the size didn't change, nothing happens.
3305 */
3306 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003307shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 int old_Rows = Rows;
3310 int old_Columns = Columns;
3311
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003312 if (!exiting
3313#ifdef FEAT_GUI
3314 /* Do not get the size when executing a shell command during
3315 * startup. */
3316 && !gui.starting
3317#endif
3318 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003319 {
3320 (void)ui_get_shellsize();
3321 check_shellsize();
3322 if (old_Rows != Rows || old_Columns != Columns)
3323 shell_resized();
3324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327/*
3328 * Set size of the Vim shell.
3329 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3330 * window size (this is used for the :win command).
3331 * If 'mustset' is FALSE, we may try to get the real window size and if
3332 * it fails use 'width' and 'height'.
3333 */
3334 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003335set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 static int busy = FALSE;
3338
3339 /*
3340 * Avoid recursiveness, can happen when setting the window size causes
3341 * another window-changed signal.
3342 */
3343 if (busy)
3344 return;
3345
3346 if (width < 0 || height < 0) /* just checking... */
3347 return;
3348
Bram Moolenaara971b822011-09-14 14:43:25 +02003349 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003351 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 State = SETWSIZE;
3353 return;
3354 }
3355
Bram Moolenaara971b822011-09-14 14:43:25 +02003356 /* curwin->w_buffer can be NULL when we are closing a window and the
3357 * buffer has already been closed and removing a scrollbar causes a resize
3358 * event. Don't resize then, it will happen after entering another buffer.
3359 */
3360 if (curwin->w_buffer == NULL)
3361 return;
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 ++busy;
3364
3365#ifdef AMIGA
3366 out_flush(); /* must do this before mch_get_shellsize() for
3367 some obscure reason */
3368#endif
3369
3370 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3371 {
3372 Rows = height;
3373 Columns = width;
3374 check_shellsize();
3375 ui_set_shellsize(mustset);
3376 }
3377 else
3378 check_shellsize();
3379
3380 /* The window layout used to be adjusted here, but it now happens in
3381 * screenalloc() (also invoked from screenclear()). That is because the
3382 * "busy" check above may skip this, but not screenalloc(). */
3383
3384 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3385 screenclear();
3386 else
3387 screen_start(); /* don't know where cursor is now */
3388
3389 if (starting != NO_SCREEN)
3390 {
3391#ifdef FEAT_TITLE
3392 maketitle();
3393#endif
3394 changed_line_abv_curs();
3395 invalidate_botline();
3396
3397 /*
3398 * We only redraw when it's needed:
3399 * - While at the more prompt or executing an external command, don't
3400 * redraw, but position the cursor.
3401 * - While editing the command line, only redraw that.
3402 * - in Ex mode, don't redraw anything.
3403 * - Otherwise, redraw right now, and position the cursor.
3404 * Always need to call update_screen() or screenalloc(), to make
3405 * sure Rows/Columns and the size of ScreenLines[] is correct!
3406 */
3407 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3408 || exmode_active)
3409 {
3410 screenalloc(FALSE);
3411 repeat_message();
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
3414 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003415 if (curwin->w_p_scb)
3416 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003417 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003418 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003419 update_screen(NOT_VALID);
3420 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003421 }
3422 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003423 {
3424 update_topline();
3425#if defined(FEAT_INS_EXPAND)
3426 if (pum_visible())
3427 {
3428 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003429 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003430 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003431#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003432 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003433 if (redrawing())
3434 setcursor();
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 cursor_on(); /* redrawing may have switched it off */
3438 }
3439 out_flush();
3440 --busy;
3441}
3442
3443/*
3444 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3445 * commands and Ex mode).
3446 */
3447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003448settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450#ifdef FEAT_GUI
3451 /* don't set the term where gvim was started to any mode */
3452 if (gui.in_use)
3453 return;
3454#endif
3455
3456 if (full_screen)
3457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 /*
3459 * When returning after calling a shell we want to really set the
3460 * terminal to raw mode, even though we think it already is, because
3461 * the shell program may have reset the terminal mode.
3462 * When we think the terminal is normal, don't try to set it to
3463 * normal again, because that causes problems (logout!) on some
3464 * machines.
3465 */
3466 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3467 {
3468#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003469# ifdef FEAT_GUI
3470 if (!gui.in_use && !gui.starting)
3471# endif
3472 {
3473 /* May need to check for T_CRV response and termcodes, it
3474 * doesn't work in Cooked mode, an external program may get
3475 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003476 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3477 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003478#ifdef FEAT_TERMINAL
3479 || rfg_status == STATUS_SENT
3480#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003481 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003482 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003483 || rcs_status == STATUS_SENT
3484 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003485 (void)vpeekc_nomap();
3486 check_for_codes_from_term();
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
3489#ifdef FEAT_MOUSE_TTY
3490 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003491 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003493 if (tmode != TMODE_RAW)
3494 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003496 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 cur_tmode = tmode;
3498#ifdef FEAT_MOUSE
3499 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003500 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003502 if (tmode == TMODE_RAW)
3503 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 out_flush();
3505 }
3506#ifdef FEAT_TERMRESPONSE
3507 may_req_termresponse();
3508#endif
3509 }
3510}
3511
3512 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003513starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 if (full_screen && !termcap_active)
3516 {
3517 out_str(T_TI); /* start termcap mode */
3518 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003519 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 out_flush();
3521 termcap_active = TRUE;
3522 screen_start(); /* don't know where cursor is now */
3523#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003524# ifdef FEAT_GUI
3525 if (!gui.in_use && !gui.starting)
3526# endif
3527 {
3528 may_req_termresponse();
3529 /* Immediately check for a response. If t_Co changes, we don't
3530 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003531 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003532 check_for_codes_from_term();
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#endif
3535 }
3536}
3537
3538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003539stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
3541 screen_stop_highlight();
3542 reset_cterm_colors();
3543 if (termcap_active)
3544 {
3545#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003546# ifdef FEAT_GUI
3547 if (!gui.in_use && !gui.starting)
3548# endif
3549 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003550 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003551 if (crv_status == STATUS_SENT
3552 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003553# ifdef FEAT_TERMINAL
3554 || rfg_status == STATUS_SENT
3555# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003556 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003557 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003558 || rcs_status == STATUS_SENT
3559 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003560 {
3561# ifdef UNIX
3562 /* Give the terminal a chance to respond. */
3563 mch_delay(100L, FALSE);
3564# endif
3565# ifdef TCIFLUSH
3566 /* Discard data received but not read. */
3567 if (exiting)
3568 tcflush(fileno(stdin), TCIFLUSH);
3569# endif
3570 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003571 /* Check for termcodes first, otherwise an external program may
3572 * get them. */
3573 check_for_codes_from_term();
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003576 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 out_str(T_KE); /* stop "keypad transmit" mode */
3578 out_flush();
3579 termcap_active = FALSE;
3580 cursor_on(); /* just in case it is still off */
3581 out_str(T_TE); /* stop termcap mode */
3582 screen_start(); /* don't know where cursor is now */
3583 out_flush();
3584 }
3585}
3586
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003587#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588/*
3589 * Request version string (for xterm) when needed.
3590 * Only do this after switching to raw mode, otherwise the result will be
3591 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003592 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003593 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * Only do this after termcap mode has been started, otherwise the codes for
3595 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003596 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3597 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003598 * On Unix only do it when both output and input are a tty (avoid writing
3599 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 * The result is caught in check_termcode().
3601 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003603may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003605 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003606 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003607 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 && *T_CRV != NUL)
3609 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003610 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003612 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /* check for the characters now, otherwise they might be eaten by
3614 * get_keystroke() */
3615 out_flush();
3616 (void)vpeekc_nomap();
3617 }
3618}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003619
Bram Moolenaar9584b312013-03-13 19:29:28 +01003620/*
3621 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003622 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003623 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003624 * If the terminal treats \u25bd as single width, the position is (1, 1),
3625 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003626 * This function has the side effect that changes cursor position, so
3627 * it must be called immediately after entering termcap mode.
3628 */
3629 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003630may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003631{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003632 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003633 && can_get_termresponse()
3634 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003635 && *T_U7 != NUL
3636 && !option_was_set((char_u *)"ambiwidth"))
3637 {
3638 char_u buf[16];
3639
Bram Moolenaarb255b902018-04-24 21:40:10 +02003640 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003641 /* Do this in the second row. In the first row the returned sequence
3642 * may be CSI 1;2R, which is the same as <S-F3>. */
3643 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003644 buf[mb_char2bytes(0x25bd, buf)] = 0;
3645 out_str(buf);
3646 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003647 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003648 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003649
3650 /* This overwrites a few characters on the screen, a redraw is needed
3651 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003652 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003653 out_str((char_u *)" ");
3654 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003655
Bram Moolenaar05684312017-12-09 15:11:24 +01003656 /* Need to reset the known cursor position. */
3657 screen_start();
3658
Bram Moolenaar9584b312013-03-13 19:29:28 +01003659 /* check for the characters now, otherwise they might be eaten by
3660 * get_keystroke() */
3661 out_flush();
3662 (void)vpeekc_nomap();
3663 }
3664}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003665
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003666/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003667 * Similar to requesting the version string: Request the terminal background
3668 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003669 */
3670 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003671may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003672{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003673 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003674 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003675 int didit = FALSE;
3676
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003677# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003678 /* Only request foreground if t_RF is set. */
3679 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3680 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003681 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003682 out_str(T_RFG);
3683 rfg_status = STATUS_SENT;
3684 didit = TRUE;
3685 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003686# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003687
3688 /* Only request background if t_RB is set. */
3689 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003690 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003691 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003692 out_str(T_RBG);
3693 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003694 didit = TRUE;
3695 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003696
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003697 if (didit)
3698 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003699 /* check for the characters now, otherwise they might be eaten by
3700 * get_keystroke() */
3701 out_flush();
3702 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003703 }
3704 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003705}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003706
Bram Moolenaar2951b772013-07-03 12:45:31 +02003707# ifdef DEBUG_TERMRESPONSE
3708 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003709log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003710{
3711 static FILE *fd_tr = NULL;
3712 static proftime_T start;
3713 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003714 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003715
3716 if (fd_tr == NULL)
3717 {
3718 fd_tr = fopen("termresponse.log", "w");
3719 profile_start(&start);
3720 }
3721 now = start;
3722 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003723 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3724 must_redraw == NOT_VALID ? "NV"
3725 : must_redraw == CLEAR ? "CL" : " ");
3726 va_start(ap, fmt);
3727 vfprintf(fd_tr, fmt, ap);
3728 va_end(ap);
3729 fputc('\n', fd_tr);
3730 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003731}
3732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#endif
3734
3735/*
3736 * Return TRUE when saving and restoring the screen.
3737 */
3738 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003739swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 return (full_screen && *T_TI != NUL);
3742}
3743
Bram Moolenaarecadf432018-03-20 11:17:04 +01003744#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745/*
3746 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3747 */
3748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003749setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751# ifdef FEAT_MOUSE_TTY
3752 int checkfor;
3753# endif
3754
3755# ifdef FEAT_MOUSESHAPE
3756 update_mouseshape(-1);
3757# endif
3758
3759# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3760# ifdef FEAT_GUI
3761 /* In the GUI the mouse is always enabled. */
3762 if (gui.in_use)
3763 return;
3764# endif
3765 /* be quick when mouse is off */
3766 if (*p_mouse == NUL || has_mouse_termcode == 0)
3767 return;
3768
3769 /* don't switch mouse on when not in raw mode (Ex mode) */
3770 if (cur_tmode != TMODE_RAW)
3771 {
3772 mch_setmouse(FALSE);
3773 return;
3774 }
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (VIsual_active)
3777 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003778 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 checkfor = MOUSE_RETURN;
3780 else if (State & INSERT)
3781 checkfor = MOUSE_INSERT;
3782 else if (State & CMDLINE)
3783 checkfor = MOUSE_COMMAND;
3784 else if (State == CONFIRM || State == EXTERNCMD)
3785 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3786 else
3787 checkfor = MOUSE_NORMAL; /* assume normal mode */
3788
3789 if (mouse_has(checkfor))
3790 mch_setmouse(TRUE);
3791 else
3792 mch_setmouse(FALSE);
3793# endif
3794}
3795
3796/*
3797 * Return TRUE if
3798 * - "c" is in 'mouse', or
3799 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3800 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3801 * normal editing mode (not at hit-return message).
3802 */
3803 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003804mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 char_u *p;
3807
3808 for (p = p_mouse; *p; ++p)
3809 switch (*p)
3810 {
3811 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3812 return TRUE;
3813 break;
3814 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3815 return TRUE;
3816 break;
3817 default: if (c == *p) return TRUE; break;
3818 }
3819 return FALSE;
3820}
3821
3822/*
3823 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3824 */
3825 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003826mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
3828 return (p_mousem[0] == 'p');
3829}
3830#endif
3831
3832/*
3833 * By outputting the 'cursor very visible' termcap code, for some windowed
3834 * terminals this makes the screen scrolled to the correct position.
3835 * Used when starting Vim or returning from a shell.
3836 */
3837 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003838scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003840 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003843 out_str(T_CVS);
3844 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846}
3847
3848static int cursor_is_off = FALSE;
3849
3850/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003851 * Enable the cursor without checking if it's already enabled.
3852 */
3853 void
3854cursor_on_force(void)
3855{
3856 out_str(T_VE);
3857 cursor_is_off = FALSE;
3858}
3859
3860/*
3861 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
3863 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003864cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003867 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868}
3869
3870/*
3871 * Disable the cursor.
3872 */
3873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003876 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003878 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 cursor_is_off = TRUE;
3880 }
3881}
3882
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003883#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003885 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003886 */
3887 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003888term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003889{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003890 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003891 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003892
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003893 /* Only do something when redrawing the screen and we can restore the
3894 * mode. */
3895 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003896 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003897# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003898 if (forced && initial_cursor_shape > 0)
3899 /* Restore to initial values. */
3900 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003901# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003902 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003903 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003904
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003905 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003906 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003907 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003908 {
3909 if (*T_CSR != NUL)
3910 p = T_CSR; /* Replace mode cursor */
3911 else
3912 p = T_CSI; /* fall back to Insert mode cursor */
3913 if (*p != NUL)
3914 {
3915 out_str(p);
3916 showing_mode = REPLACE;
3917 }
3918 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003919 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003920 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003921 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003922 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003923 {
3924 out_str(T_CSI); /* Insert mode cursor */
3925 showing_mode = INSERT;
3926 }
3927 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003928 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003929 {
3930 out_str(T_CEI); /* non-Insert mode cursor */
3931 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 }
3933}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003934
3935# if defined(FEAT_TERMINAL) || defined(PROTO)
3936 void
3937term_cursor_color(char_u *color)
3938{
3939 if (*T_CSC != NUL)
3940 {
3941 out_str(T_CSC); /* set cursor color start */
3942 out_str_nf(color);
3943 out_str(T_CEC); /* set cursor color end */
3944 out_flush();
3945 }
3946}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003947# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003948
Bram Moolenaar4db25542017-08-28 22:43:05 +02003949 int
3950blink_state_is_inverted()
3951{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003952#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003953 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3954 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003955#else
3956 return FALSE;
3957#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003958}
3959
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003960/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003961 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003962 */
3963 void
3964term_cursor_shape(int shape, int blink)
3965{
3966 if (*T_CSH != NUL)
3967 {
3968 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3969 out_flush();
3970 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003971 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003972 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003973 int do_blink = blink;
3974
3975 /* t_SH is empty: try setting just the blink state.
3976 * The blink flags are XORed together, if the initial blinking from
3977 * style and shape differs, we need to invert the flag here. */
3978 if (blink_state_is_inverted())
3979 do_blink = !blink;
3980
3981 if (do_blink && *T_VS != NUL)
3982 {
3983 out_str(T_VS);
3984 out_flush();
3985 }
3986 else if (!do_blink && *T_CVS != NUL)
3987 {
3988 out_str(T_CVS);
3989 out_flush();
3990 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003991 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003992}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003993#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003994
3995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * Set scrolling region for window 'wp'.
3997 * The region starts 'off' lines from the start of the window.
3998 * Also set the vertical scroll region for a vertically split window. Always
3999 * the full width of the window, excluding the vertical separator.
4000 */
4001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004002scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
4004 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4005 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004007 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4008 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 screen_start(); /* don't know where cursor is now */
4010}
4011
4012/*
4013 * Reset scrolling region to the whole screen.
4014 */
4015 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004016scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
4018 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 if (*T_CSV != NUL)
4020 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 screen_start(); /* don't know where cursor is now */
4022}
4023
4024
4025/*
4026 * List of terminal codes that are currently recognized.
4027 */
4028
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004029static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 char_u name[2]; /* termcap name of entry */
4032 char_u *code; /* terminal code (in allocated memory) */
4033 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004034 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035} *termcodes = NULL;
4036
4037static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4038static int tc_len = 0; /* current number of entries in termcodes[] */
4039
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004040static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004043clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 while (tc_len > 0)
4046 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004047 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 tc_max_len = 0;
4049
4050#ifdef HAVE_TGETENT
4051 BC = (char *)empty_option;
4052 UP = (char *)empty_option;
4053 PC = NUL; /* set pad character to NUL */
4054 ospeed = 0;
4055#endif
4056
4057 need_gather = TRUE; /* need to fill termleader[] */
4058}
4059
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004060#define ATC_FROM_TERM 55
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062/*
4063 * Add a new entry to the list of terminal codes.
4064 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004065 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4066 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 */
4068 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004069add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 struct termcode *new_tc;
4072 int i, j;
4073 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004074 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 if (string == NULL || *string == NUL)
4077 {
4078 del_termcode(name);
4079 return;
4080 }
4081
Bram Moolenaar4f974752019-02-17 17:44:42 +01004082#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02004083 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4084#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (s == NULL)
4088 return;
4089
4090 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004091 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004093 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 s[0] = term_7to8bit(string);
4095 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004096
Bram Moolenaar4f974752019-02-17 17:44:42 +01004097#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02004098 if (s[0] == K_NUL)
4099 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004100 STRMOVE(s + 1, s);
4101 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004102 }
4103#endif
4104
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004105 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 need_gather = TRUE; /* need to fill termleader[] */
4108
4109 /*
4110 * need to make space for more entries
4111 */
4112 if (tc_len == tc_max_len)
4113 {
4114 tc_max_len += 20;
4115 new_tc = (struct termcode *)alloc(
4116 (unsigned)(tc_max_len * sizeof(struct termcode)));
4117 if (new_tc == NULL)
4118 {
4119 tc_max_len -= 20;
4120 return;
4121 }
4122 for (i = 0; i < tc_len; ++i)
4123 new_tc[i] = termcodes[i];
4124 vim_free(termcodes);
4125 termcodes = new_tc;
4126 }
4127
4128 /*
4129 * Look for existing entry with the same name, it is replaced.
4130 * Look for an existing entry that is alphabetical higher, the new entry
4131 * is inserted in front of it.
4132 */
4133 for (i = 0; i < tc_len; ++i)
4134 {
4135 if (termcodes[i].name[0] < name[0])
4136 continue;
4137 if (termcodes[i].name[0] == name[0])
4138 {
4139 if (termcodes[i].name[1] < name[1])
4140 continue;
4141 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 */
4144 if (termcodes[i].name[1] == name[1])
4145 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 if (flags == ATC_FROM_TERM && (j = termcode_star(
4147 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004148 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 /* Don't replace ESC[123;*X or ESC O*X with another when
4150 * invoked from got_code_from_term(). */
4151 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004152 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 && s[len - 1]
4154 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004155 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004156 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004157 vim_free(s);
4158 return;
4159 }
4160 }
4161 else
4162 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004164 vim_free(termcodes[i].code);
4165 --tc_len;
4166 break;
4167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169 }
4170 /*
4171 * Found alphabetical larger entry, move rest to insert new entry
4172 */
4173 for (j = tc_len; j > i; --j)
4174 termcodes[j] = termcodes[j - 1];
4175 break;
4176 }
4177
4178 termcodes[i].name[0] = name[0];
4179 termcodes[i].name[1] = name[1];
4180 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004181 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182
4183 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4184 * accept modifiers. */
4185 termcodes[i].modlen = 0;
4186 j = termcode_star(s, len);
4187 if (j > 0)
4188 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 ++tc_len;
4190}
4191
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004193 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004194 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004195 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004196 */
4197 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004198termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004199{
4200 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4201 if (len >= 3 && code[len - 2] == '*')
4202 {
4203 if (len >= 5 && code[len - 3] == ';')
4204 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004205 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004206 return 1;
4207 }
4208 return 0;
4209}
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004212find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int i;
4215
4216 for (i = 0; i < tc_len; ++i)
4217 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4218 return termcodes[i].code;
4219 return NULL;
4220}
4221
4222#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4223 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004224get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 if (i >= tc_len)
4227 return NULL;
4228 return &termcodes[i].name[0];
4229}
4230#endif
4231
4232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004233del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235 int i;
4236
4237 if (termcodes == NULL) /* nothing there yet */
4238 return;
4239
4240 need_gather = TRUE; /* need to fill termleader[] */
4241
4242 for (i = 0; i < tc_len; ++i)
4243 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4244 {
4245 del_termcode_idx(i);
4246 return;
4247 }
4248 /* not found. Give error message? */
4249}
4250
4251 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004252del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int i;
4255
4256 vim_free(termcodes[idx].code);
4257 --tc_len;
4258 for (i = idx; i < tc_len; ++i)
4259 termcodes[i] = termcodes[i + 1];
4260}
4261
4262#ifdef FEAT_TERMRESPONSE
4263/*
4264 * Called when detected that the terminal sends 8-bit codes.
4265 * Convert all 7-bit codes to their 8-bit equivalent.
4266 */
4267 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004268switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269{
4270 int i;
4271 int c;
4272
4273 /* Only need to do something when not already using 8-bit codes. */
4274 if (!term_is_8bit(T_NAME))
4275 {
4276 for (i = 0; i < tc_len; ++i)
4277 {
4278 c = term_7to8bit(termcodes[i].code);
4279 if (c != 0)
4280 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004281 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 termcodes[i].code[0] = c;
4283 }
4284 }
4285 need_gather = TRUE; /* need to fill termleader[] */
4286 }
4287 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004288 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289}
4290#endif
4291
4292#ifdef CHECK_DOUBLE_CLICK
4293static linenr_T orig_topline = 0;
4294# ifdef FEAT_DIFF
4295static int orig_topfill = 0;
4296# endif
4297#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004298#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299/*
4300 * Checking for double clicks ourselves.
4301 * "orig_topline" is used to avoid detecting a double-click when the window
4302 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4303 */
4304/*
4305 * Set orig_topline. Used when jumping to another window, so that a double
4306 * click still works.
4307 */
4308 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004309set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 orig_topline = wp->w_topline;
4312# ifdef FEAT_DIFF
4313 orig_topfill = wp->w_topfill;
4314# endif
4315}
4316#endif
4317
4318/*
4319 * Check if typebuf.tb_buf[] contains a terminal key code.
4320 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4321 * + max_offset].
4322 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004323 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * With a match, the match is removed, the replacement code is inserted in
4325 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4326 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004327 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4328 * "buflen" is then the length of the string in buf[] and is updated for
4329 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 */
4331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004332check_termcode(
4333 int max_offset,
4334 char_u *buf,
4335 int bufsize,
4336 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 char_u *tp;
4339 char_u *p;
4340 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004343 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 int offset;
4345 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004347 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int new_slen;
4350 int extra;
4351 char_u string[MAX_KEY_CODE_LEN + 1];
4352 int i, j;
4353 int idx = 0;
4354#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004355# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4356 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 char_u bytes[6];
4358 int num_bytes;
4359# endif
4360 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 int is_click, is_drag;
4362 int wheel_code = 0;
4363 int current_button;
4364 static int held_button = MOUSE_RELEASE;
4365 static int orig_num_clicks = 1;
4366 static int orig_mouse_code = 0x0;
4367# ifdef CHECK_DOUBLE_CLICK
4368 static int orig_mouse_col = 0;
4369 static int orig_mouse_row = 0;
4370 static struct timeval orig_mouse_time = {0, 0};
4371 /* time of previous mouse click */
4372 struct timeval mouse_time; /* time of current mouse click */
4373 long timediff; /* elapsed time in msec */
4374# endif
4375#endif
4376 int cpo_koffset;
4377#ifdef FEAT_MOUSE_GPM
4378 extern int gpm_flag; /* gpm library variable */
4379#endif
4380
4381 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4382
4383 /*
4384 * Speed up the checks for terminal codes by gathering all first bytes
4385 * used in termleader[]. Often this is just a single <Esc>.
4386 */
4387 if (need_gather)
4388 gather_termleader();
4389
4390 /*
4391 * Check at several positions in typebuf.tb_buf[], to catch something like
4392 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4393 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004394 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * This is used often, KEEP IT FAST!
4396 */
4397 for (offset = 0; offset < max_offset; ++offset)
4398 {
4399 if (buf == NULL)
4400 {
4401 if (offset >= typebuf.tb_len)
4402 break;
4403 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4404 len = typebuf.tb_len - offset; /* length of the input */
4405 }
4406 else
4407 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004408 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 break;
4410 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004411 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413
4414 /*
4415 * Don't check characters after K_SPECIAL, those are already
4416 * translated terminal chars (avoid translating ~@^Hx).
4417 */
4418 if (*tp == K_SPECIAL)
4419 {
4420 offset += 2; /* there are always 2 extra characters */
4421 continue;
4422 }
4423
4424 /*
4425 * Skip this position if the character does not appear as the first
4426 * character in term_strings. This speeds up a lot, since most
4427 * termcodes start with the same character (ESC or CSI).
4428 */
4429 i = *tp;
4430 for (p = termleader; *p && *p != i; ++p)
4431 ;
4432 if (*p == NUL)
4433 continue;
4434
4435 /*
4436 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4437 * are in Insert mode.
4438 */
4439 if (*tp == ESC && !p_ek && (State & INSERT))
4440 continue;
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004443 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446#ifdef FEAT_GUI
4447 if (gui.in_use)
4448 {
4449 /*
4450 * GUI special key codes are all of the form [CSI xx].
4451 */
4452 if (*tp == CSI) /* Special key from GUI */
4453 {
4454 if (len < 3)
4455 return -1; /* Shouldn't happen */
4456 slen = 3;
4457 key_name[0] = tp[1];
4458 key_name[1] = tp[2];
4459 }
4460 }
4461 else
4462#endif /* FEAT_GUI */
4463 {
4464 for (idx = 0; idx < tc_len; ++idx)
4465 {
4466 /*
4467 * Ignore the entry if we are not at the start of
4468 * typebuf.tb_buf[]
4469 * and there are not enough characters to make a match.
4470 * But only when the 'K' flag is in 'cpoptions'.
4471 */
4472 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004473 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (cpo_koffset && offset && len < slen)
4475 continue;
4476 if (STRNCMP(termcodes[idx].code, tp,
4477 (size_t)(slen > len ? len : slen)) == 0)
4478 {
4479 if (len < slen) /* got a partial sequence */
4480 return -1; /* need to get more chars */
4481
4482 /*
4483 * When found a keypad key, check if there is another key
4484 * that matches and use that one. This makes <Home> to be
4485 * found instead of <kHome> when they produce the same
4486 * key code.
4487 */
4488 if (termcodes[idx].name[0] == 'K'
4489 && VIM_ISDIGIT(termcodes[idx].name[1]))
4490 {
4491 for (j = idx + 1; j < tc_len; ++j)
4492 if (termcodes[j].len == slen &&
4493 STRNCMP(termcodes[idx].code,
4494 termcodes[j].code, slen) == 0)
4495 {
4496 idx = j;
4497 break;
4498 }
4499 }
4500
4501 key_name[0] = termcodes[idx].name[0];
4502 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 break;
4504 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004505
4506 /*
4507 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004508 * <Esc>[123;*X (modslen == slen - 3)
4509 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4510 * When there is a modifier the * matches a number.
4511 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004512 */
4513 if (termcodes[idx].modlen > 0)
4514 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515 modslen = termcodes[idx].modlen;
4516 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004517 continue;
4518 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004520 {
4521 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004522
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004524 return -1; /* need to get more chars */
4525
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 if (tp[modslen] == termcodes[idx].code[slen - 1])
4527 slen = modslen + 1; /* no modifiers */
4528 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004529 continue; /* no match */
4530 else
4531 {
4532 /* Skip over the digits, the final char must
4533 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004534 for (j = slen - 2; j < len && (isdigit(tp[j])
4535 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004536 ;
4537 ++j;
4538 if (len < j) /* got a partial sequence */
4539 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4541 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004542
Bram Moolenaara529ce02017-06-22 22:37:57 +02004543 modifiers_start = tp + slen - 2;
4544
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004545 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004546 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004547 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004548 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004549 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004550 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004551 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004552 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004553 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004554 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004555
4556 slen = j;
4557 }
4558 key_name[0] = termcodes[idx].name[0];
4559 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004560 break;
4561 }
4562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 }
4565
4566#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004567 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004568 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004569 * detecting the start of these mouse codes they might as well be
4570 * another key code or terminal response. */
4571# ifdef FEAT_MOUSE_DEC
4572 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004573# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004574# ifdef FEAT_MOUSE_PTERM
4575 || key_name[0] == KS_PTERM_MOUSE
4576# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004577 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004579 /* Check for some responses from the terminal starting with
4580 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004581 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004582 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004583 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004584 * Also eat other possible responses to t_RV, rxvt returns
4585 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4586 * mrxvt has been reported to have "+" in the version. Assume
4587 * the escape sequence ends with a letter or one of "{|}~".
4588 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004589 * - Cursor position report: <Esc>[{row};{col}R
4590 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004591 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004592 *
4593 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004594 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004595 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004596
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004597 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004598 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004599 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004600 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004602 int col = 0;
4603 int semicols = 0;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004604 int row_char = NUL;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004607 for (i = 2 + (tp[0] != CSI); i < len
4608 && !(tp[i] >= '{' && tp[i] <= '~')
4609 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004610 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004611 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004612 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004613 row_char = tp[i - 1];
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004616 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004617 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004618 return -1;
4619 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004620 if (extra > 0)
4621 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004622
4623 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4624 * u7_status is not "sent", it may be from a previous Vim that
4625 * just exited. But not for <S-F3>, it sends something
4626 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004627 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004628 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004629 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004630 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004631 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004632
Bram Moolenaarb255b902018-04-24 21:40:10 +02004633 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004634 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004635 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004636 if (col == 2)
4637 aw = "single";
4638 else if (col == 3)
4639 aw = "double";
4640 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4641 {
4642 /* Setting the option causes a screen redraw. Do
4643 * that right away if possible, keeping any
4644 * messages. */
4645 set_option_value((char_u *)"ambw", 0L,
4646 (char_u *)aw, 0);
4647# ifdef DEBUG_TERMRESPONSE
4648 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004649 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004650
Bram Moolenaarb255b902018-04-24 21:40:10 +02004651 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004652 }
4653# else
4654 redraw_asap(CLEAR);
4655# endif
4656 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004657 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004658 key_name[0] = (int)KS_EXTRA;
4659 key_name[1] = (int)KE_IGNORE;
4660 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004661# ifdef FEAT_EVAL
4662 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4663# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004666 else if (*T_CRV != NUL && i > 2 + (tp[0] != CSI)
4667 && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004669 int version = col;
4670
Bram Moolenaarb255b902018-04-24 21:40:10 +02004671 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004672 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004673 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 /* If this code starts with CSI, you can bet that the
4676 * terminal uses 8-bit codes. */
4677 if (tp[0] == CSI)
4678 switch_to_8bit();
4679
4680 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004681 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 * Ignore it for when the user has set 'term' to xterm,
4683 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004684 if (version > 20000)
4685 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004687 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004689 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004690 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004691 int is_mintty = FALSE;
4692
4693 // mintty 2.9.5 sends 77;20905;0c.
4694 // (77 is ASCII 'M' for mintty.)
4695 if (STRNCMP(tp + extra - 3, "77;", 3) == 0)
4696 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004699 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004701 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 check_for_codes = TRUE;
4703 need_gather = TRUE;
4704 req_codes_from_term();
4705 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004706
4707 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004708 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004709 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004710 {
4711 /* If run from Vim $COLORS is set to the number of
4712 * colors the terminal supports. Otherwise assume
4713 * 256, libvterm supports even more. */
4714 if (mch_getenv((char_u *)"COLORS") == NULL)
4715 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004716 /* Libvterm can handle SGR mouse reporting. */
4717 if (!option_was_set((char_u *)"ttym"))
4718 set_option_value((char_u *)"ttym", 0L,
4719 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004720 }
4721
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004722 if (version == 95)
4723 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004724 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004725 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4726 {
4727 is_not_xterm = TRUE;
4728 is_mac_terminal = TRUE;
4729 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004730 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004731 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4732 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004733 // old iTerm2 sends 0;95;
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004734 else if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004735 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004736 }
4737
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004738 /* Only set 'ttymouse' automatically if it was not set
4739 * by the user already. */
4740 if (!option_was_set((char_u *)"ttym"))
4741 {
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004742 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar20091c12018-12-07 13:18:19 +01004743 * Terminal.app, iTerm2 and mintty. */
4744 if (version >= 277 || is_iterm2 || is_mac_terminal
4745 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004746 set_option_value((char_u *)"ttym", 0L,
4747 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004748 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004749 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004750 set_option_value((char_u *)"ttym", 0L,
4751 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004752 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004753
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004754 /* Detect terminals that set $TERM to something like
4755 * "xterm-256colors" but are not fully xterm
4756 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004757
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004758 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004759 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004760 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004761 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004762 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004763 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004764 is_not_xterm = TRUE;
4765
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004766 /* PuTTY sends 0;136;0
4767 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004768 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004769 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004770 is_not_xterm = TRUE;
4771
4772 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004773 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004774 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004775 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004776
Bram Moolenaar668324e2018-06-30 17:09:26 +02004777 // Xterm first responded to this request at patch level
4778 // 95, so assume anything below 95 is not xterm.
4779 if (version < 95)
4780 is_not_xterm = TRUE;
4781
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004782 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004783 * set. Only supported properly by xterm since version
4784 * 279 (otherwise it returns 0x18).
4785 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004786 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004787 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004788 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004789 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004790 && *T_CSH != NUL
4791 && *T_CRS != NUL)
4792 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004793 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004794 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004795 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004796 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004797 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004798
4799 /* Only request the cursor blink mode if t_RC set. Not
4800 * for Gnome terminal, it can't handle t_RC, it
4801 * echoes the characters to the screen. */
4802 if (rbm_status == STATUS_GET
4803 && !is_not_xterm
4804 && *T_CRC != NUL)
4805 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004806 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004807 out_str(T_CRC);
4808 rbm_status = STATUS_SENT;
4809 need_flush = TRUE;
4810 }
4811
4812 if (need_flush)
4813 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004815 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004817 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 apply_autocmds(EVENT_TERMRESPONSE,
4820 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 key_name[0] = (int)KS_EXTRA;
4822 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004824
Bram Moolenaar4db25542017-08-28 22:43:05 +02004825 /* Check blinking cursor from xterm:
4826 * {lead}?12;1$y set
4827 * {lead}?12;2$y not set
4828 *
4829 * {lead} can be <Esc>[ or CSI
4830 */
4831 else if (rbm_status == STATUS_SENT
4832 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4833 && i == j + 6
4834 && tp[j + 1] == '1'
4835 && tp[j + 2] == '2'
4836 && tp[j + 3] == ';'
4837 && tp[i - 1] == '$'
4838 && tp[i] == 'y')
4839 {
4840 initial_cursor_blink = (tp[j + 4] == '1');
4841 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004842 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004843 key_name[0] = (int)KS_EXTRA;
4844 key_name[1] = (int)KE_IGNORE;
4845 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004846# ifdef FEAT_EVAL
4847 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4848# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004849 }
4850
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004851 /*
4852 * Check for a window position response from the terminal:
4853 * {lead}3;{x}:{y}t
4854 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004855 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004856 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4857 || (len >= 3 && tp[0] == CSI))
4858 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4859 && tp[j + 1] == ';')
4860 {
4861 j += 2;
4862 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4863 ;
4864 if (i < len && tp[i] == ';')
4865 {
4866 winpos_x = atoi((char *)tp + j);
4867 j = i + 1;
4868 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4869 ;
4870 if (i < len && tp[i] == 't')
4871 {
4872 winpos_y = atoi((char *)tp + j);
4873 /* got finished code: consume it */
4874 key_name[0] = (int)KS_EXTRA;
4875 key_name[1] = (int)KE_IGNORE;
4876 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004877
4878 if (--did_request_winpos <= 0)
4879 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004880 }
4881 }
4882 if (i == len)
4883 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004884 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004885 return -1;
4886 }
4887 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004888 }
4889
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004890 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004891 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004892 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004893 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004894 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004895 * {lead} can be <Esc>] or OSC
4896 * {tail} can be '\007', <Esc>\ or STERM.
4897 *
4898 * Consume any code that starts with "{lead}11;", it's also
4899 * possible that "rgba" is following.
4900 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004901 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004902 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4903 || tp[0] == OSC))
4904 {
4905 j = 1 + (tp[0] == ESC);
4906 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004907 || (argp[1] != '1' && argp[1] != '0')
4908 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004909 i = 0; /* no match */
4910 else
4911 for (i = j; i < len; ++i)
4912 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4913 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004914 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004915 int is_bg = argp[1] == '1';
4916
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004917 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004918 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004919 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004920#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004921 int rval = hexhex2nr(tp + j + 7);
4922 int gval = hexhex2nr(tp + j + 12);
4923 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004924#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004925 if (is_bg)
4926 {
4927 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004928 + tp[j+17]) ? "light" : "dark";
4929
Bram Moolenaarb255b902018-04-24 21:40:10 +02004930 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004931 rbg_status = STATUS_GOT;
4932#ifdef FEAT_TERMINAL
4933 bg_r = rval;
4934 bg_g = gval;
4935 bg_b = bval;
4936#endif
4937 if (!option_was_set((char_u *)"bg")
4938 && STRCMP(p_bg, newval) != 0)
4939 {
4940 /* value differs, apply it */
4941 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004942 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004943 reset_option_was_set((char_u *)"bg");
4944 redraw_asap(CLEAR);
4945 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004946 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004947#ifdef FEAT_TERMINAL
4948 else
4949 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004950 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004951 rfg_status = STATUS_GOT;
4952 fg_r = rval;
4953 fg_g = gval;
4954 fg_b = bval;
4955 }
4956#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004957 }
4958
4959 /* got finished code: consume it */
4960 key_name[0] = (int)KS_EXTRA;
4961 key_name[1] = (int)KE_IGNORE;
4962 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004963# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004964 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4965 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004966# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004967 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004968 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004969 if (i == len)
4970 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004971 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004972 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004976 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004977 * {lead}{flag}+r<hex bytes><{tail}
4978 *
4979 * {lead} can be <Esc>P or DCS
4980 * {flag} can be '0' or '1'
4981 * {tail} can be Esc>\ or STERM
4982 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004983 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004984 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004985 *
4986 * {lead} can be <Esc>P or DCS
4987 * {tail} can be Esc>\ or STERM
4988 *
4989 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004990 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004991 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004992 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 || tp[0] == DCS))
4994 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004995 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004996 if (len < j + 3)
4997 i = len; /* need more chars */
4998 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004999 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005000 else if (argp[1] == '+')
5001 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005002 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005003 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005004 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 || tp[i] == STERM)
5006 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005007 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 got_code_from_term(tp + j, i);
5009 key_name[0] = (int)KS_EXTRA;
5010 key_name[1] = (int)KE_IGNORE;
5011 slen = i + 1 + (tp[i] == ESC);
5012 break;
5013 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005014 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005015 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005016 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005017 /* Probably the cursor shape response. Make sure that "i"
5018 * is equal to "len" when there are not sufficient
5019 * characters. */
5020 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005021 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005022 if (i - j == 3 && !isdigit(tp[i]))
5023 break;
5024 if (i - j == 4 && tp[i] != ' ')
5025 break;
5026 if (i - j == 5 && tp[i] != 'q')
5027 break;
5028 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5029 break;
5030 if ((i - j == 6 && tp[i] == STERM)
5031 || (i - j == 7 && tp[i] == '\\'))
5032 {
5033 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005034
Bram Moolenaar590ec872018-03-02 20:58:42 +01005035 /* 0, 1 = block blink, 2 = block
5036 * 3 = underline blink, 4 = underline
5037 * 5 = vertical bar blink, 6 = vertical bar */
5038 number = number == 0 ? 1 : number;
5039 initial_cursor_shape = (number + 1) / 2;
5040 /* The blink flag is actually inverted, compared to
5041 * the value set with T_SH. */
5042 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005043 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01005044 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005045 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005046
Bram Moolenaar590ec872018-03-02 20:58:42 +01005047 key_name[0] = (int)KS_EXTRA;
5048 key_name[1] = (int)KE_IGNORE;
5049 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005050# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005051 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005052# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005053 break;
5054 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005055 }
5056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005059 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005060 /* These codes arrive many together, each code can be
5061 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005062 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005063 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066 }
5067#endif
5068
5069 if (key_name[0] == NUL)
5070 continue; /* No match at this position, try next one */
5071
5072 /* We only get here when we have a complete termcode match */
5073
5074#ifdef FEAT_MOUSE
5075# ifdef FEAT_GUI
5076 /*
5077 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5078 * so that we know which window to scroll later.
5079 */
5080 if (gui.in_use
5081 && key_name[0] == (int)KS_EXTRA
5082 && (key_name[1] == (int)KE_X1MOUSE
5083 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005084 || key_name[1] == (int)KE_MOUSELEFT
5085 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 || key_name[1] == (int)KE_MOUSEDOWN
5087 || key_name[1] == (int)KE_MOUSEUP))
5088 {
5089 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5090 if (num_bytes == -1) /* not enough coordinates */
5091 return -1;
5092 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5093 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5094 slen += num_bytes;
5095 }
5096 else
5097# endif
5098 /*
5099 * If it is a mouse click, get the coordinates.
5100 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005101 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005103 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104# endif
5105# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005106 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107# endif
5108# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005109 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110# endif
5111# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005112 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005114# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005115 || key_name[0] == KS_URXVT_MOUSE
5116# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005117 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005118 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 is_click = is_drag = FALSE;
5121
Bram Moolenaar864207d2008-06-24 22:14:38 +00005122# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5123 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (key_name[0] == (int)KS_MOUSE)
5125 {
5126 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005127 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 * s == encoded button state:
5129 * 0x20 = left button down
5130 * 0x21 = middle button down
5131 * 0x22 = right button down
5132 * 0x23 = any button release
5133 * 0x60 = button 4 down (scroll wheel down)
5134 * 0x61 = button 5 down (scroll wheel up)
5135 * add 0x04 for SHIFT
5136 * add 0x08 for ALT
5137 * add 0x10 for CTRL
5138 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005139 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5140 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 * c == column + ' ' + 1 == column + 33
5142 * r == row + ' ' + 1 == row + 33
5143 *
5144 * The coordinates are passed on through global variables.
5145 * Ugly, but this avoids trouble with mouse clicks at an
5146 * unexpected moment and allows for mapping them.
5147 */
5148 for (;;)
5149 {
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005150# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (gui.in_use)
5152 {
5153 /* GUI uses more bits for columns > 223 */
5154 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5155 if (num_bytes == -1) /* not enough coordinates */
5156 return -1;
5157 mouse_code = bytes[0];
5158 mouse_col = 128 * (bytes[1] - ' ' - 1)
5159 + bytes[2] - ' ' - 1;
5160 mouse_row = 128 * (bytes[3] - ' ' - 1)
5161 + bytes[4] - ' ' - 1;
5162 }
5163 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5167 if (num_bytes == -1) /* not enough coordinates */
5168 return -1;
5169 mouse_code = bytes[0];
5170 mouse_col = bytes[1] - ' ' - 1;
5171 mouse_row = bytes[2] - ' ' - 1;
5172 }
5173 slen += num_bytes;
5174
5175 /* If the following bytes is also a mouse code and it has
5176 * the same code, dump this one and get the next. This
5177 * makes dragging a whole lot faster. */
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005178# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (gui.in_use)
5180 j = 3;
5181 else
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 j = termcodes[idx].len;
5184 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5185 && tp[slen + j] == mouse_code
5186 && tp[slen + j + 1] != NUL
5187 && tp[slen + j + 2] != NUL
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005188# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 && (!gui.in_use
5190 || (tp[slen + j + 3] != NUL
5191 && tp[slen + j + 4] != NUL))
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 )
5194 slen += j;
5195 else
5196 break;
5197 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005200 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005201 || key_name[0] == KS_SGR_MOUSE
5202 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005203 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005204 /* URXVT 1015 mouse reporting mode:
5205 * Almost identical to xterm mouse mode, except the values
5206 * are decimal instead of bytes.
5207 *
5208 * \033[%d;%d;%dM
5209 * ^-- row
5210 * ^----- column
5211 * ^-------- code
5212 *
5213 * SGR 1006 mouse reporting mode:
5214 * Almost identical to xterm mouse mode, except the values
5215 * are decimal instead of bytes.
5216 *
5217 * \033[<%d;%d;%dM
5218 * ^-- row
5219 * ^----- column
5220 * ^-------- code
5221 *
5222 * \033[<%d;%d;%dm : mouse release event
5223 * ^-- row
5224 * ^----- column
5225 * ^-------- code
5226 */
5227 p = modifiers_start;
5228 if (p == NULL)
5229 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005230
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005231 mouse_code = getdigits(&p);
5232 if (*p++ != ';')
5233 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005234
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005235 /* when mouse reporting is SGR, add 32 to mouse code */
5236 if (key_name[0] == KS_SGR_MOUSE
5237 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5238 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005239
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005240 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5241 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005242
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005243 mouse_col = getdigits(&p) - 1;
5244 if (*p++ != ';')
5245 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005246
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005247 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005248
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005249 /* The modifiers were the mouse coordinates, not the
5250 * modifier keys (alt/shift/ctrl/meta) state. */
5251 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005252 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005253
5254 if (key_name[0] == (int)KS_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005255# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005256 || key_name[0] == (int)KS_URXVT_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005257# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005258 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005259 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005260 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005261# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 /*
5263 * Handle mouse events.
5264 * Recognize the xterm mouse wheel, but not in the GUI, the
5265 * Linux console with GPM and the MS-DOS or Win32 console
5266 * (multi-clicks use >= 0x60).
5267 */
5268 if (mouse_code >= MOUSEWHEEL_LOW
5269# ifdef FEAT_GUI
5270 && !gui.in_use
5271# endif
5272# ifdef FEAT_MOUSE_GPM
5273 && gpm_flag == 0
5274# endif
5275 )
5276 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005277# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5278 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5279 /* mouse-move event, using MOUSE_DRAG works */
5280 mouse_code = MOUSE_DRAG;
5281 else
5282# endif
5283 /* Keep the mouse_code before it's changed, so that we
5284 * remember that it was a mouse wheel click. */
5285 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287# ifdef FEAT_MOUSE_XTERM
5288 else if (held_button == MOUSE_RELEASE
5289# ifdef FEAT_GUI
5290 && !gui.in_use
5291# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005292 && (mouse_code == 0x23 || mouse_code == 0x24
5293 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005295 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5296 * And 0x40 and 0x41 are used by some xterm emulator. */
5297 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5298 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300# endif
5301
5302# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5303 else if (use_xterm_mouse() > 1)
5304 {
5305 if (mouse_code & MOUSE_DRAG_XTERM)
5306 mouse_code |= MOUSE_DRAG;
5307 }
5308# endif
5309# ifdef FEAT_XCLIPBOARD
5310 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5311 {
5312 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5313 stop_xterm_trace();
5314 else
5315 start_xterm_trace(mouse_code);
5316 }
5317# endif
5318# endif
5319 }
5320# endif /* !UNIX || FEAT_MOUSE_XTERM */
5321# ifdef FEAT_MOUSE_NET
5322 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5323 {
5324 int mc, mr;
5325
5326 /* expect a rather limited sequence like: balancing {
5327 * \033}6,45\r
5328 * '6' is the row, 45 is the column
5329 */
5330 p = tp + slen;
5331 mr = getdigits(&p);
5332 if (*p++ != ',')
5333 return -1;
5334 mc = getdigits(&p);
5335 if (*p++ != '\r')
5336 return -1;
5337
5338 mouse_col = mc - 1;
5339 mouse_row = mr - 1;
5340 mouse_code = MOUSE_LEFT;
5341 slen += (int)(p - (tp + slen));
5342 }
5343# endif /* FEAT_MOUSE_NET */
5344# ifdef FEAT_MOUSE_JSB
5345 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5346 {
5347 int mult, val, iter, button, status;
5348
5349 /* JSBTERM Input Model
5350 * \033[0~zw uniq escape sequence
5351 * (L-x) Left button pressed - not pressed x not reporting
5352 * (M-x) Middle button pressed - not pressed x not reporting
5353 * (R-x) Right button pressed - not pressed x not reporting
5354 * (SDmdu) Single , Double click, m mouse move d button down
5355 * u button up
5356 * ### X cursor position padded to 3 digits
5357 * ### Y cursor position padded to 3 digits
5358 * (s-x) SHIFT key pressed - not pressed x not reporting
5359 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005360 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
5362
5363 p = tp + slen;
5364 button = mouse_code = 0;
5365 switch (*p++)
5366 {
5367 case 'L': button = 1; break;
5368 case '-': break;
5369 case 'x': break; /* ignore sequence */
5370 default: return -1; /* Unknown Result */
5371 }
5372 switch (*p++)
5373 {
5374 case 'M': button |= 2; break;
5375 case '-': break;
5376 case 'x': break; /* ignore sequence */
5377 default: return -1; /* Unknown Result */
5378 }
5379 switch (*p++)
5380 {
5381 case 'R': button |= 4; break;
5382 case '-': break;
5383 case 'x': break; /* ignore sequence */
5384 default: return -1; /* Unknown Result */
5385 }
5386 status = *p++;
5387 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5388 mult /= 10, p++)
5389 if (*p >= '0' && *p <= '9')
5390 val += (*p - '0') * mult;
5391 else
5392 return -1;
5393 mouse_col = val;
5394 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5395 mult /= 10, p++)
5396 if (*p >= '0' && *p <= '9')
5397 val += (*p - '0') * mult;
5398 else
5399 return -1;
5400 mouse_row = val;
5401 switch (*p++)
5402 {
5403 case 's': button |= 8; break; /* SHIFT key Pressed */
5404 case '-': break; /* Not Pressed */
5405 case 'x': break; /* Not Reporting */
5406 default: return -1; /* Unknown Result */
5407 }
5408 switch (*p++)
5409 {
5410 case 'c': button |= 16; break; /* CTRL key Pressed */
5411 case '-': break; /* Not Pressed */
5412 case 'x': break; /* Not Reporting */
5413 default: return -1; /* Unknown Result */
5414 }
5415 if (*p++ != '\033')
5416 return -1;
5417 if (*p++ != '\\')
5418 return -1;
5419 switch (status)
5420 {
5421 case 'D': /* Double Click */
5422 case 'S': /* Single Click */
5423 if (button & 1) mouse_code |= MOUSE_LEFT;
5424 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5425 if (button & 4) mouse_code |= MOUSE_RIGHT;
5426 if (button & 8) mouse_code |= MOUSE_SHIFT;
5427 if (button & 16) mouse_code |= MOUSE_CTRL;
5428 break;
5429 case 'm': /* Mouse move */
5430 if (button & 1) mouse_code |= MOUSE_LEFT;
5431 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5432 if (button & 4) mouse_code |= MOUSE_RIGHT;
5433 if (button & 8) mouse_code |= MOUSE_SHIFT;
5434 if (button & 16) mouse_code |= MOUSE_CTRL;
5435 if ((button & 7) != 0)
5436 {
5437 held_button = mouse_code;
5438 mouse_code |= MOUSE_DRAG;
5439 }
5440 is_drag = TRUE;
5441 showmode();
5442 break;
5443 case 'd': /* Button Down */
5444 if (button & 1) mouse_code |= MOUSE_LEFT;
5445 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5446 if (button & 4) mouse_code |= MOUSE_RIGHT;
5447 if (button & 8) mouse_code |= MOUSE_SHIFT;
5448 if (button & 16) mouse_code |= MOUSE_CTRL;
5449 break;
5450 case 'u': /* Button Up */
5451 if (button & 1)
5452 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5453 if (button & 2)
5454 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5455 if (button & 4)
5456 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5457 if (button & 8)
5458 mouse_code |= MOUSE_SHIFT;
5459 if (button & 16)
5460 mouse_code |= MOUSE_CTRL;
5461 break;
5462 default: return -1; /* Unknown Result */
5463 }
5464
5465 slen += (p - (tp + slen));
5466 }
5467# endif /* FEAT_MOUSE_JSB */
5468# ifdef FEAT_MOUSE_DEC
5469 if (key_name[0] == (int)KS_DEC_MOUSE)
5470 {
5471 /* The DEC Locator Input Model
5472 * Netterm delivers the code sequence:
5473 * \033[2;4;24;80&w (left button down)
5474 * \033[3;0;24;80&w (left button up)
5475 * \033[6;1;24;80&w (right button down)
5476 * \033[7;0;24;80&w (right button up)
5477 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5478 * Pe is the event code
5479 * Pb is the button code
5480 * Pr is the row coordinate
5481 * Pc is the column coordinate
5482 * Pp is the third coordinate (page number)
5483 * Pe, the event code indicates what event caused this report
5484 * The following event codes are defined:
5485 * 0 - request, the terminal received an explicit request
5486 * for a locator report, but the locator is unavailable
5487 * 1 - request, the terminal received an explicit request
5488 * for a locator report
5489 * 2 - left button down
5490 * 3 - left button up
5491 * 4 - middle button down
5492 * 5 - middle button up
5493 * 6 - right button down
5494 * 7 - right button up
5495 * 8 - fourth button down
5496 * 9 - fourth button up
5497 * 10 - locator outside filter rectangle
5498 * Pb, the button code, ASCII decimal 0-15 indicating which
5499 * buttons are down if any. The state of the four buttons
5500 * on the locator correspond to the low four bits of the
5501 * decimal value,
5502 * "1" means button depressed
5503 * 0 - no buttons down,
5504 * 1 - right,
5505 * 2 - middle,
5506 * 4 - left,
5507 * 8 - fourth
5508 * Pr is the row coordinate of the locator position in the page,
5509 * encoded as an ASCII decimal value.
5510 * If Pr is omitted, the locator position is undefined
5511 * (outside the terminal window for example).
5512 * Pc is the column coordinate of the locator position in the
5513 * page, encoded as an ASCII decimal value.
5514 * If Pc is omitted, the locator position is undefined
5515 * (outside the terminal window for example).
5516 * Pp is the page coordinate of the locator position
5517 * encoded as an ASCII decimal value.
5518 * The page coordinate may be omitted if the locator is on
5519 * page one (the default). We ignore it anyway.
5520 */
5521 int Pe, Pb, Pr, Pc;
5522
5523 p = tp + slen;
5524
5525 /* get event status */
5526 Pe = getdigits(&p);
5527 if (*p++ != ';')
5528 return -1;
5529
5530 /* get button status */
5531 Pb = getdigits(&p);
5532 if (*p++ != ';')
5533 return -1;
5534
5535 /* get row status */
5536 Pr = getdigits(&p);
5537 if (*p++ != ';')
5538 return -1;
5539
5540 /* get column status */
5541 Pc = getdigits(&p);
5542
5543 /* the page parameter is optional */
5544 if (*p == ';')
5545 {
5546 p++;
5547 (void)getdigits(&p);
5548 }
5549 if (*p++ != '&')
5550 return -1;
5551 if (*p++ != 'w')
5552 return -1;
5553
5554 mouse_code = 0;
5555 switch (Pe)
5556 {
5557 case 0: return -1; /* position request while unavailable */
5558 case 1: /* a response to a locator position request includes
5559 the status of all buttons */
5560 Pb &= 7; /* mask off and ignore fourth button */
5561 if (Pb & 4)
5562 mouse_code = MOUSE_LEFT;
5563 if (Pb & 2)
5564 mouse_code = MOUSE_MIDDLE;
5565 if (Pb & 1)
5566 mouse_code = MOUSE_RIGHT;
5567 if (Pb)
5568 {
5569 held_button = mouse_code;
5570 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005571 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 is_drag = TRUE;
5574 showmode();
5575 break;
5576 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005577 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 break;
5579 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5580 break;
5581 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005582 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 break;
5584 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5585 break;
5586 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005587 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 break;
5589 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5590 break;
5591 case 8: return -1; /* fourth button down */
5592 case 9: return -1; /* fourth button up */
5593 case 10: return -1; /* mouse outside of filter rectangle */
5594 default: return -1; /* should never occur */
5595 }
5596
5597 mouse_col = Pc - 1;
5598 mouse_row = Pr - 1;
5599
5600 slen += (int)(p - (tp + slen));
5601 }
5602# endif /* FEAT_MOUSE_DEC */
5603# ifdef FEAT_MOUSE_PTERM
5604 if (key_name[0] == (int)KS_PTERM_MOUSE)
5605 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005606 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608 p = tp + slen;
5609
5610 action = getdigits(&p);
5611 if (*p++ != ';')
5612 return -1;
5613
5614 mouse_row = getdigits(&p);
5615 if (*p++ != ';')
5616 return -1;
5617 mouse_col = getdigits(&p);
5618 if (*p++ != ';')
5619 return -1;
5620
5621 button = getdigits(&p);
5622 mouse_code = 0;
5623
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005624 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 case 4: mouse_code = MOUSE_LEFT; break;
5627 case 1: mouse_code = MOUSE_RIGHT; break;
5628 case 2: mouse_code = MOUSE_MIDDLE; break;
5629 default: return -1;
5630 }
5631
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005632 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 case 31: /* Initial press */
5635 if (*p++ != ';')
5636 return -1;
5637
5638 num_clicks = getdigits(&p); /* Not used */
5639 break;
5640
5641 case 32: /* Release */
5642 mouse_code |= MOUSE_RELEASE;
5643 break;
5644
5645 case 33: /* Drag */
5646 held_button = mouse_code;
5647 mouse_code |= MOUSE_DRAG;
5648 break;
5649
5650 default:
5651 return -1;
5652 }
5653
5654 if (*p++ != 't')
5655 return -1;
5656
5657 slen += (p - (tp + slen));
5658 }
5659# endif /* FEAT_MOUSE_PTERM */
5660
5661 /* Interpret the mouse code */
5662 current_button = (mouse_code & MOUSE_CLICK_MASK);
5663 if (current_button == MOUSE_RELEASE
5664# ifdef FEAT_MOUSE_XTERM
5665 && wheel_code == 0
5666# endif
5667 )
5668 {
5669 /*
5670 * If we get a mouse drag or release event when
5671 * there is no mouse button held down (held_button ==
5672 * MOUSE_RELEASE), produce a K_IGNORE below.
5673 * (can happen when you hold down two buttons
5674 * and then let them go, or click in the menu bar, but not
5675 * on a menu, and drag into the text).
5676 */
5677 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5678 is_drag = TRUE;
5679 current_button = held_button;
5680 }
5681 else if (wheel_code == 0)
5682 {
5683# ifdef CHECK_DOUBLE_CLICK
5684# ifdef FEAT_MOUSE_GPM
5685# ifdef FEAT_GUI
5686 /*
5687 * Only for Unix, when GUI or gpm is not active, we handle
5688 * multi-clicks here.
5689 */
5690 if (gpm_flag == 0 && !gui.in_use)
5691# else
5692 if (gpm_flag == 0)
5693# endif
5694# else
5695# ifdef FEAT_GUI
5696 if (!gui.in_use)
5697# endif
5698# endif
5699 {
5700 /*
5701 * Compute the time elapsed since the previous mouse click.
5702 */
5703 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005704 if (orig_mouse_time.tv_sec == 0)
5705 {
5706 /*
5707 * Avoid computing the difference between mouse_time
5708 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005709 * difference would be huge and would cause
5710 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005711 */
5712 timediff = p_mouset;
5713 }
5714 else
5715 {
5716 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005717 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005718 if (timediff < 0)
5719 --orig_mouse_time.tv_sec;
5720 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005721 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 orig_mouse_time = mouse_time;
5724 if (mouse_code == orig_mouse_code
5725 && timediff < p_mouset
5726 && orig_num_clicks != 4
5727 && orig_mouse_col == mouse_col
5728 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005729 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005731 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005733 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005734 /* Double click in tab pages line also works
5735 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005736 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005737 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 ++orig_num_clicks;
5739 else
5740 orig_num_clicks = 1;
5741 orig_mouse_col = mouse_col;
5742 orig_mouse_row = mouse_row;
5743 orig_topline = curwin->w_topline;
5744#ifdef FEAT_DIFF
5745 orig_topfill = curwin->w_topfill;
5746#endif
5747 }
5748# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5749 else
5750 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5751# endif
5752# else
5753 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5754# endif
5755 is_click = TRUE;
5756 orig_mouse_code = mouse_code;
5757 }
5758 if (!is_drag)
5759 held_button = mouse_code & MOUSE_CLICK_MASK;
5760
5761 /*
5762 * Translate the actual mouse event into a pseudo mouse event.
5763 * First work out what modifiers are to be used.
5764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (orig_mouse_code & MOUSE_SHIFT)
5766 modifiers |= MOD_MASK_SHIFT;
5767 if (orig_mouse_code & MOUSE_CTRL)
5768 modifiers |= MOD_MASK_CTRL;
5769 if (orig_mouse_code & MOUSE_ALT)
5770 modifiers |= MOD_MASK_ALT;
5771 if (orig_num_clicks == 2)
5772 modifiers |= MOD_MASK_2CLICK;
5773 else if (orig_num_clicks == 3)
5774 modifiers |= MOD_MASK_3CLICK;
5775 else if (orig_num_clicks == 4)
5776 modifiers |= MOD_MASK_4CLICK;
5777
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005778 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5779 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005781 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005782 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005783 {
5784 if (wheel_code & MOUSE_CTRL)
5785 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005786 if (wheel_code & MOUSE_ALT)
5787 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 key_name[1] = (wheel_code & 1)
5789 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005790 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793 key_name[1] = get_pseudo_mouse_code(current_button,
5794 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005795
5796 /* Make sure the mouse position is valid. Some terminals may
5797 * return weird values. */
5798 if (mouse_col >= Columns)
5799 mouse_col = Columns - 1;
5800 if (mouse_row >= Rows)
5801 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803#endif /* FEAT_MOUSE */
5804
5805#ifdef FEAT_GUI
5806 /*
5807 * If using the GUI, then we get menu and scrollbar events.
5808 *
5809 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5810 * four bytes which are to be taken as a pointer to the vimmenu_T
5811 * structure.
5812 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005813 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005814 * is one byte with the tab index.
5815 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5817 * by one byte representing the scrollbar number, and then four bytes
5818 * representing a long_u which is the new value of the scrollbar.
5819 *
5820 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5821 * KE_FILLER followed by four bytes representing a long_u which is the
5822 * new value of the scrollbar.
5823 */
5824# ifdef FEAT_MENU
5825 else if (key_name[0] == (int)KS_MENU)
5826 {
5827 long_u val;
5828
5829 num_bytes = get_long_from_buf(tp + slen, &val);
5830 if (num_bytes == -1)
5831 return -1;
5832 current_menu = (vimmenu_T *)val;
5833 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005834
5835 /* The menu may have been deleted right after it was used, check
5836 * for that. */
5837 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5838 {
5839 key_name[0] = KS_EXTRA;
5840 key_name[1] = (int)KE_IGNORE;
5841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005844# ifdef FEAT_GUI_TABLINE
5845 else if (key_name[0] == (int)KS_TABLINE)
5846 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005847 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005848 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5849 if (num_bytes == -1)
5850 return -1;
5851 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005852 if (current_tab == 255) /* -1 in a byte gives 255 */
5853 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005854 slen += num_bytes;
5855 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005856 else if (key_name[0] == (int)KS_TABMENU)
5857 {
5858 /* Selecting tabline tab or using its menu. */
5859 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5860 if (num_bytes == -1)
5861 return -1;
5862 current_tab = (int)bytes[0];
5863 current_tabmenu = (int)bytes[1];
5864 slen += num_bytes;
5865 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867# ifndef USE_ON_FLY_SCROLL
5868 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5869 {
5870 long_u val;
5871
5872 /* Get the last scrollbar event in the queue of the same type */
5873 j = 0;
5874 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5875 && tp[j + 2] != NUL; ++i)
5876 {
5877 j += 3;
5878 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5879 if (num_bytes == -1)
5880 break;
5881 if (i == 0)
5882 current_scrollbar = (int)bytes[0];
5883 else if (current_scrollbar != (int)bytes[0])
5884 break;
5885 j += num_bytes;
5886 num_bytes = get_long_from_buf(tp + j, &val);
5887 if (num_bytes == -1)
5888 break;
5889 scrollbar_value = val;
5890 j += num_bytes;
5891 slen = j;
5892 }
5893 if (i == 0) /* not enough characters to make one */
5894 return -1;
5895 }
5896 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5897 {
5898 long_u val;
5899
5900 /* Get the last horiz. scrollbar event in the queue */
5901 j = 0;
5902 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5903 && tp[j + 2] != NUL; ++i)
5904 {
5905 j += 3;
5906 num_bytes = get_long_from_buf(tp + j, &val);
5907 if (num_bytes == -1)
5908 break;
5909 scrollbar_value = val;
5910 j += num_bytes;
5911 slen = j;
5912 }
5913 if (i == 0) /* not enough characters to make one */
5914 return -1;
5915 }
5916# endif /* !USE_ON_FLY_SCROLL */
5917#endif /* FEAT_GUI */
5918
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005919 /*
5920 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5921 */
5922 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5923
5924 /*
5925 * Add any modifier codes to our string.
5926 */
5927 new_slen = 0; /* Length of what will replace the termcode */
5928 if (modifiers != 0)
5929 {
5930 /* Some keys have the modifier included. Need to handle that here
5931 * to make mappings work. */
5932 key = simplify_key(key, &modifiers);
5933 if (modifiers != 0)
5934 {
5935 string[new_slen++] = K_SPECIAL;
5936 string[new_slen++] = (int)KS_MODIFIER;
5937 string[new_slen++] = modifiers;
5938 }
5939 }
5940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005942 key_name[0] = KEY2TERMCAP0(key);
5943 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005945 {
5946 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005947 if (has_mbyte)
5948 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5949 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005950 string[new_slen++] = key_name[1];
5951 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005952 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5953 && key_name[1] == KE_IGNORE)
5954 {
5955 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5956 * to indicate what happened. */
5957 retval = KEYLEN_REMOVED;
5958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 else
5960 {
5961 string[new_slen++] = K_SPECIAL;
5962 string[new_slen++] = key_name[0];
5963 string[new_slen++] = key_name[1];
5964 }
5965 string[new_slen] = NUL;
5966 extra = new_slen - slen;
5967 if (buf == NULL)
5968 {
5969 if (extra < 0)
5970 /* remove matched chars, taking care of noremap */
5971 del_typebuf(-extra, offset);
5972 else if (extra > 0)
5973 /* insert the extra space we need */
5974 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5975
5976 /*
5977 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5978 * typebuf.tb_buf[]!
5979 */
5980 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5981 (size_t)new_slen);
5982 }
5983 else
5984 {
5985 if (extra < 0)
5986 /* remove matched characters */
5987 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005988 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005990 {
5991 /* Insert the extra space we need. If there is insufficient
5992 * space return -1. */
5993 if (*buflen + extra + new_slen >= bufsize)
5994 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005996 (size_t)(*buflen - offset));
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005999 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006001 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003
Bram Moolenaar2951b772013-07-03 12:45:31 +02006004#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006005 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006006#endif
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 return 0; /* no match found */
6009}
6010
Bram Moolenaar9377df32017-10-15 13:22:01 +02006011#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006012/*
6013 * Get the text foreground color, if known.
6014 */
6015 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006016term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006017{
6018 if (rfg_status == STATUS_GOT)
6019 {
6020 *r = fg_r;
6021 *g = fg_g;
6022 *b = fg_b;
6023 }
6024}
6025
6026/*
6027 * Get the text background color, if known.
6028 */
6029 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006030term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006031{
6032 if (rbg_status == STATUS_GOT)
6033 {
6034 *r = bg_r;
6035 *g = bg_g;
6036 *b = bg_b;
6037 }
6038}
6039#endif
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041/*
6042 * Replace any terminal code strings in from[] with the equivalent internal
6043 * vim representation. This is used for the "from" and "to" part of a
6044 * mapping, and the "to" part of a menu command.
6045 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6046 * '<'.
6047 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6048 *
6049 * The replacement is done in result[] and finally copied into allocated
6050 * memory. If this all works well *bufp is set to the allocated memory and a
6051 * pointer to it is returned. If something fails *bufp is set to NULL and from
6052 * is returned.
6053 *
6054 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6055 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6056 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6057 * instead of a CTRL-V.
6058 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006059 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006060replace_termcodes(
6061 char_u *from,
6062 char_u **bufp,
6063 int from_part,
6064 int do_lt, /* also translate <lt> */
6065 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 int i;
6068 int slen;
6069 int key;
6070 int dlen = 0;
6071 char_u *src;
6072 int do_backslash; /* backslash is a special character */
6073 int do_special; /* recognize <> key codes */
6074 int do_key_code; /* recognize raw key codes */
6075 char_u *result; /* buffer for resulting string */
6076
6077 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006078 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6080
6081 /*
6082 * Allocate space for the translation. Worst case a single character is
6083 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6084 */
6085 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6086 if (result == NULL) /* out of memory */
6087 {
6088 *bufp = NULL;
6089 return from;
6090 }
6091
6092 src = from;
6093
6094 /*
6095 * Check for #n at start only: function key n
6096 */
6097 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6098 {
6099 result[dlen++] = K_SPECIAL;
6100 result[dlen++] = 'k';
6101 if (src[1] == '0')
6102 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6103 else
6104 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6105 src += 2;
6106 }
6107
6108 /*
6109 * Copy each byte from *from to result[dlen]
6110 */
6111 while (*src != NUL)
6112 {
6113 /*
6114 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006115 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 */
6117 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6118 {
6119#ifdef FEAT_EVAL
6120 /*
6121 * Replace <SID> by K_SNR <script-nr> _.
6122 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6123 */
6124 if (STRNICMP(src, "<SID>", 5) == 0)
6125 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006126 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006127 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 else
6129 {
6130 src += 5;
6131 result[dlen++] = K_SPECIAL;
6132 result[dlen++] = (int)KS_EXTRA;
6133 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006134 sprintf((char *)result + dlen, "%ld",
6135 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 dlen += (int)STRLEN(result + dlen);
6137 result[dlen++] = '_';
6138 continue;
6139 }
6140 }
6141#endif
6142
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006143 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (slen)
6145 {
6146 dlen += slen;
6147 continue;
6148 }
6149 }
6150
6151 /*
6152 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6153 * Note that this is also checked after replacing the <> form.
6154 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6155 * it could be a character in the file.
6156 */
6157 if (do_key_code)
6158 {
6159 i = find_term_bykeys(src);
6160 if (i >= 0)
6161 {
6162 result[dlen++] = K_SPECIAL;
6163 result[dlen++] = termcodes[i].name[0];
6164 result[dlen++] = termcodes[i].name[1];
6165 src += termcodes[i].len;
6166 /* If terminal code matched, continue after it. */
6167 continue;
6168 }
6169 }
6170
6171#ifdef FEAT_EVAL
6172 if (do_special)
6173 {
6174 char_u *p, *s, len;
6175
6176 /*
6177 * Replace <Leader> by the value of "mapleader".
6178 * Replace <LocalLeader> by the value of "maplocalleader".
6179 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6180 */
6181 if (STRNICMP(src, "<Leader>", 8) == 0)
6182 {
6183 len = 8;
6184 p = get_var_value((char_u *)"g:mapleader");
6185 }
6186 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6187 {
6188 len = 13;
6189 p = get_var_value((char_u *)"g:maplocalleader");
6190 }
6191 else
6192 {
6193 len = 0;
6194 p = NULL;
6195 }
6196 if (len != 0)
6197 {
6198 /* Allow up to 8 * 6 characters for "mapleader". */
6199 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6200 s = (char_u *)"\\";
6201 else
6202 s = p;
6203 while (*s != NUL)
6204 result[dlen++] = *s++;
6205 src += len;
6206 continue;
6207 }
6208 }
6209#endif
6210
6211 /*
6212 * Remove CTRL-V and ignore the next character.
6213 * For "from" side the CTRL-V at the end is included, for the "to"
6214 * part it is removed.
6215 * If 'cpoptions' does not contain 'B', also accept a backslash.
6216 */
6217 key = *src;
6218 if (key == Ctrl_V || (do_backslash && key == '\\'))
6219 {
6220 ++src; /* skip CTRL-V or backslash */
6221 if (*src == NUL)
6222 {
6223 if (from_part)
6224 result[dlen++] = key;
6225 break;
6226 }
6227 }
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006230 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 /*
6233 * If the character is K_SPECIAL, replace it with K_SPECIAL
6234 * KS_SPECIAL KE_FILLER.
6235 * If compiled with the GUI replace CSI with K_CSI.
6236 */
6237 if (*src == K_SPECIAL)
6238 {
6239 result[dlen++] = K_SPECIAL;
6240 result[dlen++] = KS_SPECIAL;
6241 result[dlen++] = KE_FILLER;
6242 }
6243# ifdef FEAT_GUI
6244 else if (*src == CSI)
6245 {
6246 result[dlen++] = K_SPECIAL;
6247 result[dlen++] = KS_EXTRA;
6248 result[dlen++] = (int)KE_CSI;
6249 }
6250# endif
6251 else
6252 result[dlen++] = *src;
6253 ++src;
6254 }
6255 }
6256 result[dlen] = NUL;
6257
6258 /*
6259 * Copy the new string to allocated memory.
6260 * If this fails, just return from.
6261 */
6262 if ((*bufp = vim_strsave(result)) != NULL)
6263 from = *bufp;
6264 vim_free(result);
6265 return from;
6266}
6267
6268/*
6269 * Find a termcode with keys 'src' (must be NUL terminated).
6270 * Return the index in termcodes[], or -1 if not found.
6271 */
6272 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006273find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006276 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
6278 for (i = 0; i < tc_len; ++i)
6279 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006280 if (slen == termcodes[i].len
6281 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return i;
6283 }
6284 return -1;
6285}
6286
6287/*
6288 * Gather the first characters in the terminal key codes into a string.
6289 * Used to speed up check_termcode().
6290 */
6291 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006292gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 int i;
6295 int len = 0;
6296
6297#ifdef FEAT_GUI
6298 if (gui.in_use)
6299 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6300#endif
6301#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006302 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 termleader[len++] = DCS; /* the termcode response starts with DCS
6304 in 8-bit mode */
6305#endif
6306 termleader[len] = NUL;
6307
6308 for (i = 0; i < tc_len; ++i)
6309 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6310 {
6311 termleader[len++] = termcodes[i].code[0];
6312 termleader[len] = NUL;
6313 }
6314
6315 need_gather = FALSE;
6316}
6317
6318/*
6319 * Show all termcodes (for ":set termcap")
6320 * This code looks a lot like showoptions(), but is different.
6321 */
6322 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006323show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 int col;
6326 int *items;
6327 int item_count;
6328 int run;
6329 int row, rows;
6330 int cols;
6331 int i;
6332 int len;
6333
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006334#define INC3 27 /* try to make three columns */
6335#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#define GAP 2 /* spaces between columns */
6337
6338 if (tc_len == 0) /* no terminal codes (must be GUI) */
6339 return;
6340 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6341 if (items == NULL)
6342 return;
6343
6344 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006345 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 /*
6348 * do the loop two times:
6349 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006350 * 2. display the medium items (medium length strings)
6351 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006353 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 /*
6356 * collect the items in items[]
6357 */
6358 item_count = 0;
6359 for (i = 0; i < tc_len; i++)
6360 {
6361 len = show_one_termcode(termcodes[i].name,
6362 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006363 if (len <= INC3 - GAP ? run == 1
6364 : len <= INC2 - GAP ? run == 2
6365 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 items[item_count++] = i;
6367 }
6368
6369 /*
6370 * display the items
6371 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006372 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006374 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (cols == 0)
6376 cols = 1;
6377 rows = (item_count + cols - 1) / cols;
6378 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006379 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 rows = item_count;
6381 for (row = 0; row < rows && !got_int; ++row)
6382 {
6383 msg_putchar('\n'); /* go to next line */
6384 if (got_int) /* 'q' typed in more */
6385 break;
6386 col = 0;
6387 for (i = row; i < item_count; i += rows)
6388 {
6389 msg_col = col; /* make columns */
6390 show_one_termcode(termcodes[items[i]].name,
6391 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006392 if (run == 2)
6393 col += INC2;
6394 else
6395 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
6397 out_flush();
6398 ui_breakcheck();
6399 }
6400 }
6401 vim_free(items);
6402}
6403
6404/*
6405 * Show one termcode entry.
6406 * Output goes into IObuff[]
6407 */
6408 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006409show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 char_u *p;
6412 int len;
6413
6414 if (name[0] > '~')
6415 {
6416 IObuff[0] = ' ';
6417 IObuff[1] = ' ';
6418 IObuff[2] = ' ';
6419 IObuff[3] = ' ';
6420 }
6421 else
6422 {
6423 IObuff[0] = 't';
6424 IObuff[1] = '_';
6425 IObuff[2] = name[0];
6426 IObuff[3] = name[1];
6427 }
6428 IObuff[4] = ' ';
6429
6430 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6431 if (p[1] != 't')
6432 STRCPY(IObuff + 5, p);
6433 else
6434 IObuff[5] = NUL;
6435 len = (int)STRLEN(IObuff);
6436 do
6437 IObuff[len++] = ' ';
6438 while (len < 17);
6439 IObuff[len] = NUL;
6440 if (code == NULL)
6441 len += 4;
6442 else
6443 len += vim_strsize(code);
6444
6445 if (printit)
6446 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006447 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006449 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 else
6451 msg_outtrans(code);
6452 }
6453 return len;
6454}
6455
6456#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6457/*
6458 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6459 * termcap codes from the terminal itself.
6460 * We get them one by one to avoid a very long response string.
6461 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006462static int xt_index_in = 0;
6463static int xt_index_out = 0;
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006466req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 xt_index_out = 0;
6469 xt_index_in = 0;
6470 req_more_codes_from_term();
6471}
6472
6473 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006474req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 char buf[11];
6477 int old_idx = xt_index_out;
6478
6479 /* Don't do anything when going to exit. */
6480 if (exiting)
6481 return;
6482
6483 /* Send up to 10 more requests out than we received. Avoid sending too
6484 * many, there can be a buffer overflow somewhere. */
6485 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6486 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006487 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006488
Bram Moolenaarb255b902018-04-24 21:40:10 +02006489 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6490 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 out_str_nf((char_u *)buf);
6492 ++xt_index_out;
6493 }
6494
6495 /* Send the codes out right away. */
6496 if (xt_index_out != old_idx)
6497 out_flush();
6498}
6499
6500/*
6501 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6502 * A "0" instead of the "1" indicates a code that isn't supported.
6503 * Both <name> and <string> are encoded in hex.
6504 * "code" points to the "0" or "1".
6505 */
6506 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006507got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509#define XT_LEN 100
6510 char_u name[3];
6511 char_u str[XT_LEN];
6512 int i;
6513 int j = 0;
6514 int c;
6515
6516 /* A '1' means the code is supported, a '0' means it isn't.
6517 * When half the length is > XT_LEN we can't use it.
6518 * Our names are currently all 2 characters. */
6519 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6520 {
6521 /* Get the name from the response and find it in the table. */
6522 name[0] = hexhex2nr(code + 3);
6523 name[1] = hexhex2nr(code + 5);
6524 name[2] = NUL;
6525 for (i = 0; key_names[i] != NULL; ++i)
6526 {
6527 if (STRCMP(key_names[i], name) == 0)
6528 {
6529 xt_index_in = i;
6530 break;
6531 }
6532 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006533
Bram Moolenaarb255b902018-04-24 21:40:10 +02006534 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6535
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (key_names[i] != NULL)
6537 {
6538 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6539 str[j++] = c;
6540 str[j] = NUL;
6541 if (name[0] == 'C' && name[1] == 'o')
6542 {
6543 /* Color count is not a key code. */
6544 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006545 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547 else
6548 {
6549 /* First delete any existing entry with the same code. */
6550 i = find_term_bykeys(str);
6551 if (i >= 0)
6552 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006553 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
6555 }
6556 }
6557
6558 /* May request more codes now that we received one. */
6559 ++xt_index_in;
6560 req_more_codes_from_term();
6561}
6562
6563/*
6564 * Check if there are any unanswered requests and deal with them.
6565 * This is called before starting an external program or getting direct
6566 * keyboard input. We don't want responses to be send to that program or
6567 * handled as typed text.
6568 */
6569 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006570check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 int c;
6573
6574 /* If no codes requested or all are answered, no need to wait. */
6575 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6576 return;
6577
6578 /* Vgetc() will check for and handle any response.
6579 * Keep calling vpeekc() until we don't get any responses. */
6580 ++no_mapping;
6581 ++allow_keys;
6582 for (;;)
6583 {
6584 c = vpeekc();
6585 if (c == NUL) /* nothing available */
6586 break;
6587
6588 /* If a response is recognized it's replaced with K_IGNORE, must read
6589 * it from the input stream. If there is no K_IGNORE we can't do
6590 * anything, break here (there might be some responses further on, but
6591 * we don't want to throw away any typed chars). */
6592 if (c != K_SPECIAL && c != K_IGNORE)
6593 break;
6594 c = vgetc();
6595 if (c != K_IGNORE)
6596 {
6597 vungetc(c);
6598 break;
6599 }
6600 }
6601 --no_mapping;
6602 --allow_keys;
6603}
6604#endif
6605
6606#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6607/*
6608 * Translate an internal mapping/abbreviation representation into the
6609 * corresponding external one recognized by :map/:abbrev commands;
6610 * respects the current B/k/< settings of 'cpoption'.
6611 *
6612 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006613 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 *
6615 * It uses a growarray to build the translation string since the
6616 * latter can be wider than the original description. The caller has to
6617 * free the string afterwards.
6618 *
6619 * Returns NULL when there is a problem.
6620 */
6621 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006622translate_mapping(
6623 char_u *str,
6624 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 garray_T ga;
6627 int c;
6628 int modifiers;
6629 int cpo_bslash;
6630 int cpo_special;
6631 int cpo_keycode;
6632
6633 ga_init(&ga);
6634 ga.ga_itemsize = 1;
6635 ga.ga_growsize = 40;
6636
6637 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6638 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6639 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6640
6641 for (; *str; ++str)
6642 {
6643 c = *str;
6644 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6645 {
6646 modifiers = 0;
6647 if (str[1] == KS_MODIFIER)
6648 {
6649 str++;
6650 modifiers = *++str;
6651 c = *++str;
6652 }
6653 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6654 {
6655 int i;
6656
6657 /* try to find special key in termcodes */
6658 for (i = 0; i < tc_len; ++i)
6659 if (termcodes[i].name[0] == str[1]
6660 && termcodes[i].name[1] == str[2])
6661 break;
6662 if (i < tc_len)
6663 {
6664 ga_concat(&ga, termcodes[i].code);
6665 str += 2;
6666 continue; /* for (str) */
6667 }
6668 }
6669 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6670 {
6671 if (expmap && cpo_special)
6672 {
6673 ga_clear(&ga);
6674 return NULL;
6675 }
6676 c = TO_SPECIAL(str[1], str[2]);
6677 if (c == K_ZERO) /* display <Nul> as ^@ */
6678 c = NUL;
6679 str += 2;
6680 }
6681 if (IS_SPECIAL(c) || modifiers) /* special key */
6682 {
6683 if (expmap && cpo_special)
6684 {
6685 ga_clear(&ga);
6686 return NULL;
6687 }
6688 ga_concat(&ga, get_special_key_name(c, modifiers));
6689 continue; /* for (str) */
6690 }
6691 }
6692 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6693 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6694 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6695 if (c)
6696 ga_append(&ga, c);
6697 }
6698 ga_append(&ga, NUL);
6699 return (char_u *)(ga.ga_data);
6700}
6701#endif
6702
Bram Moolenaar4f974752019-02-17 17:44:42 +01006703#if (defined(MSWIN) && !defined(FEAT_GUI)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704static char ksme_str[20];
6705static char ksmr_str[20];
6706static char ksmd_str[20];
6707
6708/*
6709 * For Win32 console: update termcap codes for existing console attributes.
6710 */
6711 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006712update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 struct builtin_term *p;
6715
6716 p = find_builtin_term(DEFAULT_TERM);
6717 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6718 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6719 attr | 0x08); /* FOREGROUND_INTENSITY */
6720 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6721 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6722
6723 while (p->bt_string != NULL)
6724 {
6725 if (p->bt_entry == (int)KS_ME)
6726 p->bt_string = &ksme_str[0];
6727 else if (p->bt_entry == (int)KS_MR)
6728 p->bt_string = &ksmr_str[0];
6729 else if (p->bt_entry == (int)KS_MD)
6730 p->bt_string = &ksmd_str[0];
6731 ++p;
6732 }
6733}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006734
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006735# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006736# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006737struct ks_tbl_s
6738{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006739 int code; // value of KS_
6740 char *vtp; // code in vtp mode
6741 char *vtp2; // code in vtp2 mode
6742 char buf[KSSIZE]; // save buffer in non-vtp mode
6743 char vbuf[KSSIZE]; // save buffer in vtp mode
6744 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6745 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006746};
6747
6748static struct ks_tbl_s ks_tbl[] =
6749{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006750 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6751 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6752 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6753 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6754 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6755 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6756 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6757 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6758 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006759# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006760 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6761 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006762 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6763 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006764# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006765 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6766 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006767 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6768 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006769# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006770 {(int)KS_CCO, "256", "256"}, // colors
6771 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006772};
6773
6774 static struct builtin_term *
6775find_first_tcap(
6776 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006777 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006778{
6779 struct builtin_term *p;
6780
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006781 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006782 if (p->bt_entry == code)
6783 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006784 return NULL;
6785}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006786# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006787
6788/*
6789 * For Win32 console: replace the sequence immediately after termguicolors.
6790 */
6791 void
6792swap_tcap(void)
6793{
6794# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006795 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006796 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006797 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006798 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006799 int mode;
6800 enum
6801 {
6802 CMODEINDEX,
6803 CMODE24,
6804 CMODE256
6805 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006806
6807 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006808 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006809 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006810 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006811 {
6812 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006813 if (bt != NULL)
6814 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006815 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6816 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6817 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6818
6819 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6820 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006821 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006822 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006823 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006824 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006825 }
6826
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006827 if (p_tgc)
6828 mode = CMODE24;
6829 else if (t_colors >= 256)
6830 mode = CMODE256;
6831 else
6832 mode = CMODEINDEX;
6833
6834 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006835 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006836 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6837 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006838 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006839 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006840 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006841 case CMODEINDEX:
6842 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6843 break;
6844 case CMODE24:
6845 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6846 break;
6847 default:
6848 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006849 }
6850 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006851 }
6852
6853 if (mode != curr_mode)
6854 {
6855 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006856 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006857 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6858 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006859 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006860 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006861 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006862 case CMODEINDEX:
6863 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6864 break;
6865 case CMODE24:
6866 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6867 break;
6868 default:
6869 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006870 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006871 }
6872 }
6873
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006874 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006875 }
6876# endif
6877}
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006880
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006881#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006882 static int
6883hex_digit(int c)
6884{
6885 if (isdigit(c))
6886 return c - '0';
6887 c = TOLOWER_ASC(c);
6888 if (c >= 'a' && c <= 'f')
6889 return c - 'a' + 10;
6890 return 0x1ffffff;
6891}
6892
6893 guicolor_T
6894gui_get_color_cmn(char_u *name)
6895{
Bram Moolenaar28726652017-01-06 18:16:19 +01006896 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6897 * values as used by the MS-Windows GDI api. It should be used only for
6898 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006899# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006900# undef RGB
6901# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006902# ifndef RGB
6903# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6904# endif
6905# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006906 FILE *fd;
6907 char line[LINE_LEN];
6908 char_u *fname;
6909 int r, g, b, i;
6910 guicolor_T color;
6911
6912 struct rgbcolor_table_S {
6913 char_u *color_name;
6914 guicolor_T color;
6915 };
6916
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006917 /* Only non X11 colors (not present in rgb.txt) and colors in
6918 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006919 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006920 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6921 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6922 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6923 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6924 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6925 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6926 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6927 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6928 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6929 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6930 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6931 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6932 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006933 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6934 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006935 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006936 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006937 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006938 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6939 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6940 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6941 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6942 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6943 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6944 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6945 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6946 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006947 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006948 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006949 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6950 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006951 };
6952
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006953 static struct rgbcolor_table_S *colornames_table;
6954 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006955
6956 if (name[0] == '#' && STRLEN(name) == 7)
6957 {
6958 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006959 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006960 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6961 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6962 if (color > 0xffffff)
6963 return INVALCOLOR;
6964 return color;
6965 }
6966
6967 /* Check if the name is one of the colors we know */
6968 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6969 if (STRICMP(name, rgb_table[i].color_name) == 0)
6970 return rgb_table[i].color;
6971
6972 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006973 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006974 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006975 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006976 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006977 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006978
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006979 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006980 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6981 if (fname == NULL)
6982 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006983
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006984 fd = fopen((char *)fname, "rt");
6985 vim_free(fname);
6986 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006987 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006988 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006989 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006990 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006991 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006992 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006993
6994 for (counting = 1; counting >= 0; --counting)
6995 {
6996 if (!counting)
6997 {
6998 colornames_table = (struct rgbcolor_table_S *)alloc(
6999 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
7000 if (colornames_table == NULL)
7001 {
7002 fclose(fd);
7003 return INVALCOLOR;
7004 }
7005 rewind(fd);
7006 }
7007 size = 0;
7008
7009 while (!feof(fd))
7010 {
7011 size_t len;
7012 int pos;
7013
Bram Moolenaar42335f52018-09-13 15:33:43 +02007014 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007015 len = strlen(line);
7016
7017 if (len <= 1 || line[len - 1] != '\n')
7018 continue;
7019
7020 line[len - 1] = '\0';
7021
7022 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7023 if (i != 3)
7024 continue;
7025
7026 if (!counting)
7027 {
7028 char_u *s = vim_strsave((char_u *)line + pos);
7029
7030 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007031 {
7032 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007033 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007034 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007035 colornames_table[size].color_name = s;
7036 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7037 }
7038 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01007039
7040 // The distributed rgb.txt has less than 1000 entries. Limit to
7041 // 10000, just in case the file was messed up.
7042 if (size == 10000)
7043 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007044 }
7045 }
7046 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007047 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007048
7049 for (i = 0; i < size; i++)
7050 if (STRICMP(name, colornames_table[i].color_name) == 0)
7051 return colornames_table[i].color;
7052
Bram Moolenaarab302212016-04-26 20:59:29 +02007053 return INVALCOLOR;
7054}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007055
7056 guicolor_T
7057gui_get_rgb_color_cmn(int r, int g, int b)
7058{
7059 guicolor_T color = RGB(r, g, b);
7060
7061 if (color > 0xffffff)
7062 return INVALCOLOR;
7063 return color;
7064}
Bram Moolenaarab302212016-04-26 20:59:29 +02007065#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007066
Bram Moolenaar4f974752019-02-17 17:44:42 +01007067#if (defined(MSWIN) && !defined(FEAT_GUI_MSWIN)) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007068 || defined(PROTO)
7069static int cube_value[] = {
7070 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7071};
7072
7073static int grey_ramp[] = {
7074 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7075 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7076};
7077
7078# ifdef FEAT_TERMINAL
7079# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007080# else
7081# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007082# endif
7083
Bram Moolenaar9894e392018-05-05 14:29:06 +02007084static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007085// R G B idx
7086 { 0, 0, 0, 1}, // black
7087 {224, 0, 0, 2}, // dark red
7088 { 0, 224, 0, 3}, // dark green
7089 {224, 224, 0, 4}, // dark yellow / brown
7090 { 0, 0, 224, 5}, // dark blue
7091 {224, 0, 224, 6}, // dark magenta
7092 { 0, 224, 224, 7}, // dark cyan
7093 {224, 224, 224, 8}, // light grey
7094
7095 {128, 128, 128, 9}, // dark grey
7096 {255, 64, 64, 10}, // light red
7097 { 64, 255, 64, 11}, // light green
7098 {255, 255, 64, 12}, // yellow
7099 { 64, 64, 255, 13}, // light blue
7100 {255, 64, 255, 14}, // light magenta
7101 { 64, 255, 255, 15}, // light cyan
7102 {255, 255, 255, 16}, // white
7103};
7104
7105 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007106cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007107{
7108 int idx;
7109
7110 if (nr < 16)
7111 {
7112 *r = ansi_table[nr][0];
7113 *g = ansi_table[nr][1];
7114 *b = ansi_table[nr][2];
7115 *ansi_idx = ansi_table[nr][3];
7116 }
7117 else if (nr < 232)
7118 {
7119 /* 216 color cube */
7120 idx = nr - 16;
7121 *r = cube_value[idx / 36 % 6];
7122 *g = cube_value[idx / 6 % 6];
7123 *b = cube_value[idx % 6];
7124 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7125 }
7126 else if (nr < 256)
7127 {
7128 /* 24 grey scale ramp */
7129 idx = nr - 232;
7130 *r = grey_ramp[idx];
7131 *g = grey_ramp[idx];
7132 *b = grey_ramp[idx];
7133 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7134 }
7135 else
7136 {
7137 *r = 0;
7138 *g = 0;
7139 *b = 0;
7140 *ansi_idx = 0;
7141 }
7142}
7143#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007144