blob: 97ddf8bc4a1295cf875411b7c3ad30e77c8cb10c [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 struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010079static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static void req_codes_from_term(void);
82static void req_more_codes_from_term(void);
83static void got_code_from_term(char_u *code, int len);
84static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
86#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000087 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
88 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010089static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void del_termcode_idx(int idx);
92static int term_is_builtin(char_u *name);
93static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#endif
97
98#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010099static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
101/*
102 * Here is our own prototype for tgetstr(), any prototypes from the include
103 * files have been disabled by the define at the start of this file.
104 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100105char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200108 /* Change this to "if 1" to debug what happens with termresponse. */
109# if 0
110# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200111static void log_tr(const char *fmt, ...);
112# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200113# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200114# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200115# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200116
117# define STATUS_GET 1 /* send request when switching to RAW mode */
118# define STATUS_SENT 2 /* did send request, waiting for response */
119# define STATUS_GOT 3 /* received response */
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200122static int crv_status = STATUS_GET;
123
Bram Moolenaar9584b312013-03-13 19:29:28 +0100124/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200125static int u7_status = STATUS_GET;
126
Bram Moolenaar9377df32017-10-15 13:22:01 +0200127# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200128/* Request foreground color report: */
129static int rfg_status = STATUS_GET;
130static int fg_r = 0;
131static int fg_g = 0;
132static int fg_b = 0;
133static int bg_r = 255;
134static int bg_g = 255;
135static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200136# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200137
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200138/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200139static int rbg_status = STATUS_GET;
140
Bram Moolenaar4db25542017-08-28 22:43:05 +0200141/* Request cursor blinking mode report: */
142static int rbm_status = STATUS_GET;
143
144/* Request cursor style report: */
145static int rcs_status = STATUS_GET;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100146
147/* Request windos position report: */
148static int winpos_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# endif
150
151/*
152 * Don't declare these variables if termcap.h contains them.
153 * Autoconf checks if these variables should be declared extern (not all
154 * systems have them).
155 * Some versions define ospeed to be speed_t, but that is incompatible with
156 * BSD, where ospeed is short and speed_t is long.
157 */
158# ifndef HAVE_OSPEED
159# ifdef OSPEED_EXTERN
160extern short ospeed;
161# else
162short ospeed;
163# endif
164# endif
165# ifndef HAVE_UP_BC_PC
166# ifdef UP_BC_PC_EXTERN
167extern char *UP, *BC, PC;
168# else
169char *UP, *BC, PC;
170# endif
171# endif
172
173# define TGETSTR(s, p) vim_tgetstr((s), (p))
174# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif /* HAVE_TGETENT */
177
178static int detected_8bit = FALSE; /* detected 8-bit terminal */
179
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200180#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200181/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200182 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200183static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200184
185/* The blink flag from the style response may be inverted from the actual
186 * blinking state, xterm XORs the flags. */
187static int initial_cursor_shape_blink = FALSE;
188
189/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200190static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200191#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200192
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000193static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194{
195
196#if defined(FEAT_GUI)
197/*
198 * GUI pseudo term-cap.
199 */
200 {(int)KS_NAME, "gui"},
201 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
202 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
203# ifdef TERMINFO
204 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
205# else
206 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
207# endif
208 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
209# ifdef TERMINFO
210 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
211 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# else
214 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
215 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# endif
218 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
219 /* attributes switched on with 'h', off with * 'H' */
220 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
221 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
222 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
223 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
224 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
225 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
226 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000227 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
228 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200229 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
230 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
232 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
233 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
234 {(int)KS_MS, "y"},
235 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100236 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {(int)KS_LE, "\b"}, /* cursor-left = BS */
238 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
239# ifdef TERMINFO
240 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
241# else
242 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
243# endif
244 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000245 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#endif
247
248#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
251/*
252 * Amiga console window, default for Amiga
253 */
254 {(int)KS_NAME, "amiga"},
255 {(int)KS_CE, "\033[K"},
256 {(int)KS_CD, "\033[J"},
257 {(int)KS_AL, "\033[L"},
258# ifdef TERMINFO
259 {(int)KS_CAL, "\033[%p1%dL"},
260# else
261 {(int)KS_CAL, "\033[%dL"},
262# endif
263 {(int)KS_DL, "\033[M"},
264# ifdef TERMINFO
265 {(int)KS_CDL, "\033[%p1%dM"},
266# else
267 {(int)KS_CDL, "\033[%dM"},
268# endif
269 {(int)KS_CL, "\014"},
270 {(int)KS_VI, "\033[0 p"},
271 {(int)KS_VE, "\033[1 p"},
272 {(int)KS_ME, "\033[0m"},
273 {(int)KS_MR, "\033[7m"},
274 {(int)KS_MD, "\033[1m"},
275 {(int)KS_SE, "\033[0m"},
276 {(int)KS_SO, "\033[33m"},
277 {(int)KS_US, "\033[4m"},
278 {(int)KS_UE, "\033[0m"},
279 {(int)KS_CZH, "\033[3m"},
280 {(int)KS_CZR, "\033[0m"},
281#if defined(__MORPHOS__) || defined(__AROS__)
282 {(int)KS_CCO, "8"}, /* allow 8 colors */
283# ifdef TERMINFO
284 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
285 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
286# else
287 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
288 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
289# endif
290 {(int)KS_OP, "\033[m"}, /* reset colors */
291#endif
292 {(int)KS_MS, "y"},
293 {(int)KS_UT, "y"}, /* guessed */
294 {(int)KS_LE, "\b"},
295# ifdef TERMINFO
296 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
297# else
298 {(int)KS_CM, "\033[%i%d;%dH"},
299# endif
300#if defined(__MORPHOS__)
301 {(int)KS_SR, "\033M"},
302#endif
303# ifdef TERMINFO
304 {(int)KS_CRI, "\033[%p1%dC"},
305# else
306 {(int)KS_CRI, "\033[%dC"},
307# endif
308 {K_UP, "\233A"},
309 {K_DOWN, "\233B"},
310 {K_LEFT, "\233D"},
311 {K_RIGHT, "\233C"},
312 {K_S_UP, "\233T"},
313 {K_S_DOWN, "\233S"},
314 {K_S_LEFT, "\233 A"},
315 {K_S_RIGHT, "\233 @"},
316 {K_S_TAB, "\233Z"},
317 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
318 {K_F2, "\233\061~"},
319 {K_F3, "\233\062~"},
320 {K_F4, "\233\063~"},
321 {K_F5, "\233\064~"},
322 {K_F6, "\233\065~"},
323 {K_F7, "\233\066~"},
324 {K_F8, "\233\067~"},
325 {K_F9, "\233\070~"},
326 {K_F10, "\233\071~"},
327 {K_S_F1, "\233\061\060~"},
328 {K_S_F2, "\233\061\061~"},
329 {K_S_F3, "\233\061\062~"},
330 {K_S_F4, "\233\061\063~"},
331 {K_S_F5, "\233\061\064~"},
332 {K_S_F6, "\233\061\065~"},
333 {K_S_F7, "\233\061\066~"},
334 {K_S_F8, "\233\061\067~"},
335 {K_S_F9, "\233\061\070~"},
336 {K_S_F10, "\233\061\071~"},
337 {K_HELP, "\233?~"},
338 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
339 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
340 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
341 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
342 {K_END, "\233\064\065~"}, /* 101 key keyboard */
343
344 {BT_EXTRA_KEYS, ""},
345 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
346 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
347 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
348# endif
349
350# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
351/*
352 * almost standard ANSI terminal, default for bebox
353 */
354 {(int)KS_NAME, "beos-ansi"},
355 {(int)KS_CE, "\033[K"},
356 {(int)KS_CD, "\033[J"},
357 {(int)KS_AL, "\033[L"},
358# ifdef TERMINFO
359 {(int)KS_CAL, "\033[%p1%dL"},
360# else
361 {(int)KS_CAL, "\033[%dL"},
362# endif
363 {(int)KS_DL, "\033[M"},
364# ifdef TERMINFO
365 {(int)KS_CDL, "\033[%p1%dM"},
366# else
367 {(int)KS_CDL, "\033[%dM"},
368# endif
369#ifdef BEOS_PR_OR_BETTER
370# ifdef TERMINFO
371 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
372# else
373 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
374# endif
375#endif
376 {(int)KS_CL, "\033[H\033[2J"},
377#ifdef notyet
378 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
379 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
380#endif
381 {(int)KS_ME, "\033[m"}, /* normal mode */
382 {(int)KS_MR, "\033[7m"}, /* reverse */
383 {(int)KS_MD, "\033[1m"}, /* bold */
384 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
385 {(int)KS_SE, "\033[m"}, /* standout end */
386 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
387 {(int)KS_CZR, "\033[m"}, /* italic end */
388 {(int)KS_US, "\033[4m"}, /* underscore mode */
389 {(int)KS_UE, "\033[m"}, /* underscore end */
390 {(int)KS_CCO, "8"}, /* allow 8 colors */
391# ifdef TERMINFO
392 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
393 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
394# else
395 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
396 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
397# endif
398 {(int)KS_OP, "\033[m"}, /* reset colors */
399 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
400 {(int)KS_UT, "y"}, /* guessed */
401 {(int)KS_LE, "\b"},
402# ifdef TERMINFO
403 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
404# else
405 {(int)KS_CM, "\033[%i%d;%dH"},
406# endif
407 {(int)KS_SR, "\033M"},
408# ifdef TERMINFO
409 {(int)KS_CRI, "\033[%p1%dC"},
410# else
411 {(int)KS_CRI, "\033[%dC"},
412# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200413# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
417 {K_UP, "\033[A"},
418 {K_DOWN, "\033[B"},
419 {K_LEFT, "\033[D"},
420 {K_RIGHT, "\033[C"},
421# endif
422
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200423# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424/*
425 * standard ANSI terminal, default for unix
426 */
427 {(int)KS_NAME, "ansi"},
428 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
429 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
430# ifdef TERMINFO
431 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
432# else
433 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
434# endif
435 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
436# ifdef TERMINFO
437 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
438# else
439 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
440# endif
441 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
442 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
443 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
444 {(int)KS_MS, "y"},
445 {(int)KS_UT, "y"}, /* guessed */
446 {(int)KS_LE, "\b"},
447# ifdef TERMINFO
448 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
449# else
450 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
451# endif
452# ifdef TERMINFO
453 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
454# else
455 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
456# endif
457# endif
458
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200459# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
461 * These codes are valid when nansi.sys or equivalent has been installed.
462 * Function keys on a PC are preceded with a NUL. These are converted into
463 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
464 * CTRL-arrow is used instead of SHIFT-arrow.
465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_NAME, "pcansi"},
467 {(int)KS_DL, "\033[M"},
468 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {(int)KS_CE, "\033[K"},
470 {(int)KS_CL, "\033[2J"},
471 {(int)KS_ME, "\033[0m"},
472 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
473 {(int)KS_MD, "\033[1m"}, /* bold: white text */
474 {(int)KS_SE, "\033[0m"}, /* standout end */
475 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
476 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
477 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
478 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
479 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
480 {(int)KS_CCO, "8"}, /* allow 8 colors */
481# ifdef TERMINFO
482 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
483 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
484# else
485 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
486 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
487# endif
488 {(int)KS_OP, "\033[0m"}, /* reset colors */
489 {(int)KS_MS, "y"},
490 {(int)KS_UT, "y"}, /* guessed */
491 {(int)KS_LE, "\b"},
492# ifdef TERMINFO
493 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
494# else
495 {(int)KS_CM, "\033[%i%d;%dH"},
496# endif
497# ifdef TERMINFO
498 {(int)KS_CRI, "\033[%p1%dC"},
499# else
500 {(int)KS_CRI, "\033[%dC"},
501# endif
502 {K_UP, "\316H"},
503 {K_DOWN, "\316P"},
504 {K_LEFT, "\316K"},
505 {K_RIGHT, "\316M"},
506 {K_S_LEFT, "\316s"},
507 {K_S_RIGHT, "\316t"},
508 {K_F1, "\316;"},
509 {K_F2, "\316<"},
510 {K_F3, "\316="},
511 {K_F4, "\316>"},
512 {K_F5, "\316?"},
513 {K_F6, "\316@"},
514 {K_F7, "\316A"},
515 {K_F8, "\316B"},
516 {K_F9, "\316C"},
517 {K_F10, "\316D"},
518 {K_F11, "\316\205"}, /* guessed */
519 {K_F12, "\316\206"}, /* guessed */
520 {K_S_F1, "\316T"},
521 {K_S_F2, "\316U"},
522 {K_S_F3, "\316V"},
523 {K_S_F4, "\316W"},
524 {K_S_F5, "\316X"},
525 {K_S_F6, "\316Y"},
526 {K_S_F7, "\316Z"},
527 {K_S_F8, "\316["},
528 {K_S_F9, "\316\\"},
529 {K_S_F10, "\316]"},
530 {K_S_F11, "\316\207"}, /* guessed */
531 {K_S_F12, "\316\210"}, /* guessed */
532 {K_INS, "\316R"},
533 {K_DEL, "\316S"},
534 {K_HOME, "\316G"},
535 {K_END, "\316O"},
536 {K_PAGEDOWN, "\316Q"},
537 {K_PAGEUP, "\316I"},
538# endif
539
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200540# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/*
542 * These codes are valid for the Win32 Console . The entries that start with
543 * ESC | are translated into console calls in os_win32.c. The function keys
544 * are also translated in os_win32.c.
545 */
546 {(int)KS_NAME, "win32"},
547 {(int)KS_CE, "\033|K"}, /* clear to end of line */
548 {(int)KS_AL, "\033|L"}, /* add new blank line */
549# ifdef TERMINFO
550 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
551# else
552 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
553# endif
554 {(int)KS_DL, "\033|M"}, /* delete line */
555# ifdef TERMINFO
556 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
557# else
558 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
559# endif
560 {(int)KS_CL, "\033|J"}, /* clear screen */
561 {(int)KS_CD, "\033|j"}, /* clear to end of display */
562 {(int)KS_VI, "\033|v"}, /* cursor invisible */
563 {(int)KS_VE, "\033|V"}, /* cursor visible */
564
565 {(int)KS_ME, "\033|0m"}, /* normal */
566 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
567 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
568#if 1
569 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
570 {(int)KS_SE, "\033|0m"}, /* standout end */
571#else
572 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
573 {(int)KS_SE, "\033|f"}, /* standout end */
574#endif
575 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
576 {(int)KS_CZR, "\033|0m"}, /* italic end */
577 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
578 {(int)KS_UE, "\033|0m"}, /* underscore end */
579 {(int)KS_CCO, "16"}, /* allow 16 colors */
580# ifdef TERMINFO
581 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
582 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
583# else
584 {(int)KS_CAB, "\033|%db"}, /* set background color */
585 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
586# endif
587
588 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
589 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100590 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {(int)KS_LE, "\b"},
592# ifdef TERMINFO
593 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
594# else
595 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
596# endif
597 {(int)KS_VB, "\033|B"}, /* visual bell */
598 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
599 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
600# ifdef TERMINFO
601 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
602# else
603 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
604# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100605# ifdef FEAT_TERMGUICOLORS
606 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
607 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 {K_UP, "\316H"},
611 {K_DOWN, "\316P"},
612 {K_LEFT, "\316K"},
613 {K_RIGHT, "\316M"},
614 {K_S_UP, "\316\304"},
615 {K_S_DOWN, "\316\317"},
616 {K_S_LEFT, "\316\311"},
617 {K_C_LEFT, "\316s"},
618 {K_S_RIGHT, "\316\313"},
619 {K_C_RIGHT, "\316t"},
620 {K_S_TAB, "\316\017"},
621 {K_F1, "\316;"},
622 {K_F2, "\316<"},
623 {K_F3, "\316="},
624 {K_F4, "\316>"},
625 {K_F5, "\316?"},
626 {K_F6, "\316@"},
627 {K_F7, "\316A"},
628 {K_F8, "\316B"},
629 {K_F9, "\316C"},
630 {K_F10, "\316D"},
631 {K_F11, "\316\205"},
632 {K_F12, "\316\206"},
633 {K_S_F1, "\316T"},
634 {K_S_F2, "\316U"},
635 {K_S_F3, "\316V"},
636 {K_S_F4, "\316W"},
637 {K_S_F5, "\316X"},
638 {K_S_F6, "\316Y"},
639 {K_S_F7, "\316Z"},
640 {K_S_F8, "\316["},
641 {K_S_F9, "\316\\"},
642 {K_S_F10, "\316]"},
643 {K_S_F11, "\316\207"},
644 {K_S_F12, "\316\210"},
645 {K_INS, "\316R"},
646 {K_DEL, "\316S"},
647 {K_HOME, "\316G"},
648 {K_S_HOME, "\316\302"},
649 {K_C_HOME, "\316w"},
650 {K_END, "\316O"},
651 {K_S_END, "\316\315"},
652 {K_C_END, "\316u"},
653 {K_PAGEDOWN, "\316Q"},
654 {K_PAGEUP, "\316I"},
655 {K_KPLUS, "\316N"},
656 {K_KMINUS, "\316J"},
657 {K_KMULTIPLY, "\316\067"},
658 {K_K0, "\316\332"},
659 {K_K1, "\316\336"},
660 {K_K2, "\316\342"},
661 {K_K3, "\316\346"},
662 {K_K4, "\316\352"},
663 {K_K5, "\316\356"},
664 {K_K6, "\316\362"},
665 {K_K7, "\316\366"},
666 {K_K8, "\316\372"},
667 {K_K9, "\316\376"},
668# endif
669
670# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
671/*
672 * VT320 is working as an ANSI terminal compatible DEC terminal.
673 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 * TODO:- rewrite ESC[ codes to CSI
675 * - keyboard languages (CSI ? 26 n)
676 */
677 {(int)KS_NAME, "vt320"},
678 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
679 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
680# ifdef TERMINFO
681 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
682# else
683 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
684# endif
685 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
686# ifdef TERMINFO
687 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
688# else
689 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
690# endif
691 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000692 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
693 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
695 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000696 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
697 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
698 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
699 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
700 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
701 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
702 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
703 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
704 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
705 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {(int)KS_MS, "y"},
707 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100708 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {(int)KS_LE, "\b"},
710# ifdef TERMINFO
711 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
712 ESC_STR "[%i%p1%d;%p2%dH")},
713# else
714 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
715# endif
716# ifdef TERMINFO
717 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
718# else
719 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
720# endif
721 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
722 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
723 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
724 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200725 // Note: cursor key sequences for application cursor mode are omitted,
726 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
728 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
729 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
730 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
731 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
733 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
734 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
735 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
736 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000737 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
739 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
740 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
741 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
742 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
743 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
744 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
745 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
746 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
747 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
748 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
749 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
750 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
751 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
752 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200753 // These sequences starting with <Esc> O may interfere with what the user
754 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
756 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
757 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
758 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
759 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200760 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
761 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
762 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
763 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
764 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
765 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
766 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
767 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
768 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
769 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
771# endif
772
773# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
774/*
775 * Ordinary vt52
776 */
777 {(int)KS_NAME, "vt52"},
778 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
779 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100780# ifdef TERMINFO
781 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
782 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
783# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100787 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
789 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {K_UP, IF_EB("\033A", ESC_STR "A")},
791 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
792 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
793 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100794 {K_F1, IF_EB("\033P", ESC_STR "P")},
795 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
796 {K_F3, IF_EB("\033R", ESC_STR "R")},
797# ifdef __MINT__
798 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
799 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
800 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
801 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
802 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
804 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
805 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
806 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {K_F4, IF_EB("\033S", ESC_STR "S")},
808 {K_F5, IF_EB("\033T", ESC_STR "T")},
809 {K_F6, IF_EB("\033U", ESC_STR "U")},
810 {K_F7, IF_EB("\033V", ESC_STR "V")},
811 {K_F8, IF_EB("\033W", ESC_STR "W")},
812 {K_F9, IF_EB("\033X", ESC_STR "X")},
813 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
814 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
815 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
816 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
817 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
818 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
819 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
820 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
821 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
822 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
823 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
824 {K_INS, IF_EB("\033I", ESC_STR "I")},
825 {K_HOME, IF_EB("\033E", ESC_STR "E")},
826 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
827 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
828# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {(int)KS_MS, "y"},
831# endif
832# endif
833
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200834# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200835 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
837 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
838# ifdef TERMINFO
839 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
840# else
841 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
842# endif
843 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
844# ifdef TERMINFO
845 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
846# else
847 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
848# endif
849# ifdef TERMINFO
850 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
851 ESC_STR "[%i%p1%d;%p2%dr")},
852# else
853 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
854# endif
855 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
856 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
857 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
858 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
859 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
860 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
861 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200862 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
863 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {(int)KS_MS, "y"},
865 {(int)KS_UT, "y"},
866 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200867 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
868 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200869 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200870 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200871# ifdef TERMINFO
872 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
873# else
874 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
875# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200876 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200877 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878# ifdef TERMINFO
879 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
880 ESC_STR "[%i%p1%d;%p2%dH")},
881# else
882 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
883# endif
884 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
885# ifdef TERMINFO
886 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
887# else
888 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
889# endif
890 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
891 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
892# ifdef FEAT_XTERM_SAVE
893 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
894 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
895 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
896# endif
897 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
898 {(int)KS_CIE, "\007"},
899 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
900 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200901 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
902 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903# ifdef TERMINFO
904 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
905 ESC_STR "[8;%p1%d;%p2%dt")},
906 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
907 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200908 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909# else
910 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
911 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200912 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913# endif
914 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200915 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200916 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100917 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200918# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200919 /* These are printf strings, not terminal codes. */
920 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
921 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
922# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100923 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
924 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200925 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
926 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
927 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
928 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000929
930 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
931 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
932 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
933 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
934 /* An extra set of cursor keys for vt100 mode */
935 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
936 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
937 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
938 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000940 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
941 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
942 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
943 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000944 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
945 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
946 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
947 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
948 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
949 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
950 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
951 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
952 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
953 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
954 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
955 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000957 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
958 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
959 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
960 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000961 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
962 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000963 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000964 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000965 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000966 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000967 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
968 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000969 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000970 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000971 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000972 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
973 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100974 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
975 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
976 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
977 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
978 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
979 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200980 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
981 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
982 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
983 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
984 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
985 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
986 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
987 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
988 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
989 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +0100990 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
991 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
992 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000995 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
996 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
997 /* F14 and F15 are missing, because they send the same codes as the undo
998 * and help key, although they don't work on all keyboards. */
999 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1000 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1001 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1002 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1003 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1004
1005 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1006 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1007 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1008 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1009 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1010 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1011 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1012 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1013 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1014 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1015
1016 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1017 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1018 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1019 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1020 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1021 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1022 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023# endif
1024
1025# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1026/*
1027 * iris-ansi for Silicon Graphics machines.
1028 */
1029 {(int)KS_NAME, "iris-ansi"},
1030 {(int)KS_CE, "\033[K"},
1031 {(int)KS_CD, "\033[J"},
1032 {(int)KS_AL, "\033[L"},
1033# ifdef TERMINFO
1034 {(int)KS_CAL, "\033[%p1%dL"},
1035# else
1036 {(int)KS_CAL, "\033[%dL"},
1037# endif
1038 {(int)KS_DL, "\033[M"},
1039# ifdef TERMINFO
1040 {(int)KS_CDL, "\033[%p1%dM"},
1041# else
1042 {(int)KS_CDL, "\033[%dM"},
1043# endif
1044#if 0 /* The scroll region is not working as Vim expects. */
1045# ifdef TERMINFO
1046 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1047# else
1048 {(int)KS_CS, "\033[%i%d;%dr"},
1049# endif
1050#endif
1051 {(int)KS_CL, "\033[H\033[2J"},
1052 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1053 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1054 {(int)KS_TI, "\033[=6h"},
1055 {(int)KS_TE, "\033[=6l"},
1056 {(int)KS_SE, "\033[21;27m"},
1057 {(int)KS_SO, "\033[1;7m"},
1058 {(int)KS_ME, "\033[m"},
1059 {(int)KS_MR, "\033[7m"},
1060 {(int)KS_MD, "\033[1m"},
1061 {(int)KS_CCO, "8"}, /* allow 8 colors */
1062 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1063 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1064 {(int)KS_US, "\033[4m"}, /* underline on */
1065 {(int)KS_UE, "\033[24m"}, /* underline off */
1066# ifdef TERMINFO
1067 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1068 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1069 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1070 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1071# else
1072 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1073 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1074 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1075 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1076# endif
1077 {(int)KS_MS, "y"}, /* guessed */
1078 {(int)KS_UT, "y"}, /* guessed */
1079 {(int)KS_LE, "\b"},
1080# ifdef TERMINFO
1081 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1082# else
1083 {(int)KS_CM, "\033[%i%d;%dH"},
1084# endif
1085 {(int)KS_SR, "\033M"},
1086# ifdef TERMINFO
1087 {(int)KS_CRI, "\033[%p1%dC"},
1088# else
1089 {(int)KS_CRI, "\033[%dC"},
1090# endif
1091 {(int)KS_CIS, "\033P3.y"},
1092 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1093 {(int)KS_TS, "\033P1.y"},
1094 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1095# ifdef TERMINFO
1096 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1097 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1098# else
1099 {(int)KS_CWS, "\033[203;%d;%d/y"},
1100 {(int)KS_CWP, "\033[205;%d;%d/y"},
1101# endif
1102 {K_UP, "\033[A"},
1103 {K_DOWN, "\033[B"},
1104 {K_LEFT, "\033[D"},
1105 {K_RIGHT, "\033[C"},
1106 {K_S_UP, "\033[161q"},
1107 {K_S_DOWN, "\033[164q"},
1108 {K_S_LEFT, "\033[158q"},
1109 {K_S_RIGHT, "\033[167q"},
1110 {K_F1, "\033[001q"},
1111 {K_F2, "\033[002q"},
1112 {K_F3, "\033[003q"},
1113 {K_F4, "\033[004q"},
1114 {K_F5, "\033[005q"},
1115 {K_F6, "\033[006q"},
1116 {K_F7, "\033[007q"},
1117 {K_F8, "\033[008q"},
1118 {K_F9, "\033[009q"},
1119 {K_F10, "\033[010q"},
1120 {K_F11, "\033[011q"},
1121 {K_F12, "\033[012q"},
1122 {K_S_F1, "\033[013q"},
1123 {K_S_F2, "\033[014q"},
1124 {K_S_F3, "\033[015q"},
1125 {K_S_F4, "\033[016q"},
1126 {K_S_F5, "\033[017q"},
1127 {K_S_F6, "\033[018q"},
1128 {K_S_F7, "\033[019q"},
1129 {K_S_F8, "\033[020q"},
1130 {K_S_F9, "\033[021q"},
1131 {K_S_F10, "\033[022q"},
1132 {K_S_F11, "\033[023q"},
1133 {K_S_F12, "\033[024q"},
1134 {K_INS, "\033[139q"},
1135 {K_HOME, "\033[H"},
1136 {K_END, "\033[146q"},
1137 {K_PAGEUP, "\033[150q"},
1138 {K_PAGEDOWN, "\033[154q"},
1139# endif
1140
1141# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1142/*
1143 * for debugging
1144 */
1145 {(int)KS_NAME, "debug"},
1146 {(int)KS_CE, "[CE]"},
1147 {(int)KS_CD, "[CD]"},
1148 {(int)KS_AL, "[AL]"},
1149# ifdef TERMINFO
1150 {(int)KS_CAL, "[CAL%p1%d]"},
1151# else
1152 {(int)KS_CAL, "[CAL%d]"},
1153# endif
1154 {(int)KS_DL, "[DL]"},
1155# ifdef TERMINFO
1156 {(int)KS_CDL, "[CDL%p1%d]"},
1157# else
1158 {(int)KS_CDL, "[CDL%d]"},
1159# endif
1160# ifdef TERMINFO
1161 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1162# else
1163 {(int)KS_CS, "[%dCS%d]"},
1164# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001165# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001167# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# endif
1170# ifdef TERMINFO
1171 {(int)KS_CAB, "[CAB%p1%d]"},
1172 {(int)KS_CAF, "[CAF%p1%d]"},
1173 {(int)KS_CSB, "[CSB%p1%d]"},
1174 {(int)KS_CSF, "[CSF%p1%d]"},
1175# else
1176 {(int)KS_CAB, "[CAB%d]"},
1177 {(int)KS_CAF, "[CAF%d]"},
1178 {(int)KS_CSB, "[CSB%d]"},
1179 {(int)KS_CSF, "[CSF%d]"},
1180# endif
1181 {(int)KS_OP, "[OP]"},
1182 {(int)KS_LE, "[LE]"},
1183 {(int)KS_CL, "[CL]"},
1184 {(int)KS_VI, "[VI]"},
1185 {(int)KS_VE, "[VE]"},
1186 {(int)KS_VS, "[VS]"},
1187 {(int)KS_ME, "[ME]"},
1188 {(int)KS_MR, "[MR]"},
1189 {(int)KS_MB, "[MB]"},
1190 {(int)KS_MD, "[MD]"},
1191 {(int)KS_SE, "[SE]"},
1192 {(int)KS_SO, "[SO]"},
1193 {(int)KS_UE, "[UE]"},
1194 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001195 {(int)KS_UCE, "[UCE]"},
1196 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001197 {(int)KS_STE, "[STE]"},
1198 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {(int)KS_MS, "[MS]"},
1200 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001201 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202# ifdef TERMINFO
1203 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1204# else
1205 {(int)KS_CM, "[%dCM%d]"},
1206# endif
1207 {(int)KS_SR, "[SR]"},
1208# ifdef TERMINFO
1209 {(int)KS_CRI, "[CRI%p1%d]"},
1210# else
1211 {(int)KS_CRI, "[CRI%d]"},
1212# endif
1213 {(int)KS_VB, "[VB]"},
1214 {(int)KS_KS, "[KS]"},
1215 {(int)KS_KE, "[KE]"},
1216 {(int)KS_TI, "[TI]"},
1217 {(int)KS_TE, "[TE]"},
1218 {(int)KS_CIS, "[CIS]"},
1219 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001220 {(int)KS_CSC, "[CSC]"},
1221 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {(int)KS_TS, "[TS]"},
1223 {(int)KS_FS, "[FS]"},
1224# ifdef TERMINFO
1225 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1226 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1227# else
1228 {(int)KS_CWS, "[%dCWS%d]"},
1229 {(int)KS_CWP, "[%dCWP%d]"},
1230# endif
1231 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001232 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001233 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001234 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_UP, "[KU]"},
1236 {K_DOWN, "[KD]"},
1237 {K_LEFT, "[KL]"},
1238 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001239 {K_XUP, "[xKU]"},
1240 {K_XDOWN, "[xKD]"},
1241 {K_XLEFT, "[xKL]"},
1242 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {K_S_UP, "[S-KU]"},
1244 {K_S_DOWN, "[S-KD]"},
1245 {K_S_LEFT, "[S-KL]"},
1246 {K_C_LEFT, "[C-KL]"},
1247 {K_S_RIGHT, "[S-KR]"},
1248 {K_C_RIGHT, "[C-KR]"},
1249 {K_F1, "[F1]"},
1250 {K_XF1, "[xF1]"},
1251 {K_F2, "[F2]"},
1252 {K_XF2, "[xF2]"},
1253 {K_F3, "[F3]"},
1254 {K_XF3, "[xF3]"},
1255 {K_F4, "[F4]"},
1256 {K_XF4, "[xF4]"},
1257 {K_F5, "[F5]"},
1258 {K_F6, "[F6]"},
1259 {K_F7, "[F7]"},
1260 {K_F8, "[F8]"},
1261 {K_F9, "[F9]"},
1262 {K_F10, "[F10]"},
1263 {K_F11, "[F11]"},
1264 {K_F12, "[F12]"},
1265 {K_S_F1, "[S-F1]"},
1266 {K_S_XF1, "[S-xF1]"},
1267 {K_S_F2, "[S-F2]"},
1268 {K_S_XF2, "[S-xF2]"},
1269 {K_S_F3, "[S-F3]"},
1270 {K_S_XF3, "[S-xF3]"},
1271 {K_S_F4, "[S-F4]"},
1272 {K_S_XF4, "[S-xF4]"},
1273 {K_S_F5, "[S-F5]"},
1274 {K_S_F6, "[S-F6]"},
1275 {K_S_F7, "[S-F7]"},
1276 {K_S_F8, "[S-F8]"},
1277 {K_S_F9, "[S-F9]"},
1278 {K_S_F10, "[S-F10]"},
1279 {K_S_F11, "[S-F11]"},
1280 {K_S_F12, "[S-F12]"},
1281 {K_HELP, "[HELP]"},
1282 {K_UNDO, "[UNDO]"},
1283 {K_BS, "[BS]"},
1284 {K_INS, "[INS]"},
1285 {K_KINS, "[KINS]"},
1286 {K_DEL, "[DEL]"},
1287 {K_KDEL, "[KDEL]"},
1288 {K_HOME, "[HOME]"},
1289 {K_S_HOME, "[C-HOME]"},
1290 {K_C_HOME, "[C-HOME]"},
1291 {K_KHOME, "[KHOME]"},
1292 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001293 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {K_END, "[END]"},
1295 {K_S_END, "[C-END]"},
1296 {K_C_END, "[C-END]"},
1297 {K_KEND, "[KEND]"},
1298 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001299 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {K_PAGEUP, "[PAGEUP]"},
1301 {K_PAGEDOWN, "[PAGEDOWN]"},
1302 {K_KPAGEUP, "[KPAGEUP]"},
1303 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1304 {K_MOUSE, "[MOUSE]"},
1305 {K_KPLUS, "[KPLUS]"},
1306 {K_KMINUS, "[KMINUS]"},
1307 {K_KDIVIDE, "[KDIVIDE]"},
1308 {K_KMULTIPLY, "[KMULTIPLY]"},
1309 {K_KENTER, "[KENTER]"},
1310 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001311 {K_PS, "[PASTE-START]"},
1312 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {K_K0, "[K0]"},
1314 {K_K1, "[K1]"},
1315 {K_K2, "[K2]"},
1316 {K_K3, "[K3]"},
1317 {K_K4, "[K4]"},
1318 {K_K5, "[K5]"},
1319 {K_K6, "[K6]"},
1320 {K_K7, "[K7]"},
1321 {K_K8, "[K8]"},
1322 {K_K9, "[K9]"},
1323# endif
1324
1325#endif /* NO_BUILTIN_TCAPS */
1326
1327/*
1328 * The most minimal terminal: only clear screen and cursor positioning
1329 * Always included.
1330 */
1331 {(int)KS_NAME, "dumb"},
1332 {(int)KS_CL, "\014"},
1333#ifdef TERMINFO
1334 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1335 ESC_STR "[%i%p1%d;%p2%dH")},
1336#else
1337 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1338#endif
1339
1340/*
1341 * end marker
1342 */
1343 {(int)KS_NAME, NULL}
1344
1345}; /* end of builtin_termcaps */
1346
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001349termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001350{
Bram Moolenaarab302212016-04-26 20:59:29 +02001351 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001352}
1353
1354 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001355termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001356{
1357 guicolor_T t;
1358
1359 if (*name == NUL)
1360 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362
1363 if (t == INVALCOLOR)
1364 EMSG2(_("E254: Cannot allocate color %s"), name);
1365 return t;
1366}
1367
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001368 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001369termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001371 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001372}
1373#endif
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375/*
1376 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#ifdef AMIGA
1379# define DEFAULT_TERM (char_u *)"amiga"
1380#endif
1381
1382#ifdef MSWIN
1383# define DEFAULT_TERM (char_u *)"win32"
1384#endif
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#if defined(UNIX) && !defined(__MINT__)
1387# define DEFAULT_TERM (char_u *)"ansi"
1388#endif
1389
1390#ifdef __MINT__
1391# define DEFAULT_TERM (char_u *)"vt52"
1392#endif
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifdef VMS
1395# define DEFAULT_TERM (char_u *)"vt320"
1396#endif
1397
1398#ifdef __BEOS__
1399# undef DEFAULT_TERM
1400# define DEFAULT_TERM (char_u *)"beos-ansi"
1401#endif
1402
1403#ifndef DEFAULT_TERM
1404# define DEFAULT_TERM (char_u *)"dumb"
1405#endif
1406
1407/*
1408 * Term_strings contains currently used terminal output strings.
1409 * It is initialized with the default values by parse_builtin_tcap().
1410 * The values can be changed by setting the option with the same name.
1411 */
1412char_u *(term_strings[(int)KS_LAST + 1]);
1413
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001414static int need_gather = FALSE; /* need to fill termleader[] */
1415static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001417static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001418static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#endif
1420
1421 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001422find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 struct builtin_term *p;
1425
1426 p = builtin_termcaps;
1427 while (p->bt_string != NULL)
1428 {
1429 if (p->bt_entry == (int)KS_NAME)
1430 {
1431#ifdef UNIX
1432 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1433 return p;
1434 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1435 return p;
1436 else
1437#endif
1438#ifdef VMS
1439 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1440 return p;
1441 else
1442#endif
1443 if (STRCMP(term, p->bt_string) == 0)
1444 return p;
1445 }
1446 ++p;
1447 }
1448 return p;
1449}
1450
1451/*
1452 * Parsing of the builtin termcap entries.
1453 * Caller should check if 'name' is a valid builtin term.
1454 * The terminal's name is not set, as this is already done in termcapinit().
1455 */
1456 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001457parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 struct builtin_term *p;
1460 char_u name[2];
1461 int term_8bit;
1462
1463 p = find_builtin_term(term);
1464 term_8bit = term_is_8bit(term);
1465
1466 /* Do not parse if builtin term not found */
1467 if (p->bt_string == NULL)
1468 return;
1469
1470 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1471 {
1472 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1473 {
1474 /* Only set the value if it wasn't set yet. */
1475 if (term_strings[p->bt_entry] == NULL
1476 || term_strings[p->bt_entry] == empty_option)
1477 {
1478 /* 8bit terminal: use CSI instead of <Esc>[ */
1479 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1480 {
1481 char_u *s, *t;
1482
1483 s = vim_strsave((char_u *)p->bt_string);
1484 if (s != NULL)
1485 {
1486 for (t = s; *t; ++t)
1487 if (term_7to8bit(t))
1488 {
1489 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001490 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 term_strings[p->bt_entry] = s;
1493 set_term_option_alloced(&term_strings[p->bt_entry]);
1494 }
1495 }
1496 else
1497 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1498 }
1499 }
1500 else
1501 {
1502 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1503 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1504 if (find_termcode(name) == NULL)
1505 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1506 }
1507 }
1508}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
1511 * Set number of colors.
1512 * Store it as a number in t_colors.
1513 * Store it as a string in T_CCO (using nr_colors[]).
1514 */
1515 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001516set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 char_u nr_colors[20]; /* string for number of colors */
1519
1520 t_colors = nr;
1521 if (t_colors > 1)
1522 sprintf((char *)nr_colors, "%d", t_colors);
1523 else
1524 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001525 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001527
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001528#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001529/*
1530 * Set the color count to "val" and redraw if it changed.
1531 */
1532 static void
1533may_adjust_color_count(int val)
1534{
1535 if (val != t_colors)
1536 {
1537 /* Nr of colors changed, initialize highlighting and
1538 * redraw everything. This causes a redraw, which usually
1539 * clears the message. Try keeping the message if it
1540 * might work. */
1541 set_keep_msg_from_hist();
1542 set_color_count(val);
1543 init_highlight(TRUE, FALSE);
1544# ifdef DEBUG_TERMRESPONSE
1545 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001546 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001547
Bram Moolenaarb255b902018-04-24 21:40:10 +02001548 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001549 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001550#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001551 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001552#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001553 }
1554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#endif
1556
1557#ifdef HAVE_TGETENT
1558static char *(key_names[]) =
1559{
1560#ifdef FEAT_TERMRESPONSE
1561 /* Do this one first, it may cause a screen redraw. */
1562 "Co",
1563#endif
1564 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 "#2", "#4", "%i", "*7",
1566 "k1", "k2", "k3", "k4", "k5", "k6",
1567 "k7", "k8", "k9", "k;", "F1", "F2",
1568 "%1", "&8", "kb", "kI", "kD", "kh",
1569 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1570 NULL
1571};
1572#endif
1573
Bram Moolenaar9289df52018-05-10 14:40:57 +02001574#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001575 static void
1576get_term_entries(int *height, int *width)
1577{
1578 static struct {
1579 enum SpecialKey dest; /* index in term_strings[] */
1580 char *name; /* termcap name for string */
1581 } string_names[] =
1582 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1583 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1584 {KS_CL, "cl"}, {KS_CD, "cd"},
1585 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1586 {KS_ME, "me"}, {KS_MR, "mr"},
1587 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1588 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1589 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1590 {KS_STE,"Te"}, {KS_STS,"Ts"},
1591 {KS_CM, "cm"}, {KS_SR, "sr"},
1592 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1593 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1594 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1595 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1596 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1597 {KS_VS, "vs"}, {KS_CVS, "VS"},
1598 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1599 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1600 {KS_TS, "ts"}, {KS_FS, "fs"},
1601 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1602 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1603 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1604 {KS_8F, "8f"}, {KS_8B, "8b"},
1605 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1606 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001607 {KS_CST, "ST"}, {KS_CRT, "RT"},
1608 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001609 {(enum SpecialKey)0, NULL}
1610 };
1611 int i;
1612 char_u *p;
1613 static char_u tstrbuf[TBUFSZ];
1614 char_u *tp = tstrbuf;
1615
1616 /*
1617 * get output strings
1618 */
1619 for (i = 0; string_names[i].name != NULL; ++i)
1620 {
1621 if (TERM_STR(string_names[i].dest) == NULL
1622 || TERM_STR(string_names[i].dest) == empty_option)
1623 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
1624 }
1625
1626 /* tgetflag() returns 1 if the flag is present, 0 if not and
1627 * possibly -1 if the flag doesn't exist. */
1628 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1629 T_MS = (char_u *)"y";
1630 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1631 T_XS = (char_u *)"y";
1632 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1633 T_XN = (char_u *)"y";
1634 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1635 T_DB = (char_u *)"y";
1636 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1637 T_DA = (char_u *)"y";
1638 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1639 T_UT = (char_u *)"y";
1640
1641 /*
1642 * get key codes
1643 */
1644 for (i = 0; key_names[i] != NULL; ++i)
1645 if (find_termcode((char_u *)key_names[i]) == NULL)
1646 {
1647 p = TGETSTR(key_names[i], &tp);
1648 /* if cursor-left == backspace, ignore it (televideo 925) */
1649 if (p != NULL
1650 && (*p != Ctrl_H
1651 || key_names[i][0] != 'k'
1652 || key_names[i][1] != 'l'))
1653 add_termcode((char_u *)key_names[i], p, FALSE);
1654 }
1655
1656 if (*height == 0)
1657 *height = tgetnum("li");
1658 if (*width == 0)
1659 *width = tgetnum("co");
1660
1661 /*
1662 * Get number of colors (if not done already).
1663 */
1664 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
1665 set_color_count(tgetnum("Co"));
1666
1667# ifndef hpux
1668 BC = (char *)TGETSTR("bc", &tp);
1669 UP = (char *)TGETSTR("up", &tp);
1670 p = TGETSTR("pc", &tp);
1671 if (p)
1672 PC = *p;
1673# endif
1674}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001675#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001676
1677 static void
1678report_term_error(char_u *error_msg, char_u *term)
1679{
1680 struct builtin_term *termp;
1681
1682 mch_errmsg("\r\n");
1683 if (error_msg != NULL)
1684 {
1685 mch_errmsg((char *)error_msg);
1686 mch_errmsg("\r\n");
1687 }
1688 mch_errmsg("'");
1689 mch_errmsg((char *)term);
1690 mch_errmsg(_("' not known. Available builtin terminals are:"));
1691 mch_errmsg("\r\n");
1692 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1693 {
1694 if (termp->bt_entry == (int)KS_NAME)
1695 {
1696#ifdef HAVE_TGETENT
1697 mch_errmsg(" builtin_");
1698#else
1699 mch_errmsg(" ");
1700#endif
1701 mch_errmsg(termp->bt_string);
1702 mch_errmsg("\r\n");
1703 }
1704 }
1705}
1706
1707 static void
1708report_default_term(char_u *term)
1709{
1710 mch_errmsg(_("defaulting to '"));
1711 mch_errmsg((char *)term);
1712 mch_errmsg("'\r\n");
1713 if (emsg_silent == 0)
1714 {
1715 screen_start(); /* don't know where cursor is now */
1716 out_flush();
1717 if (!is_not_a_term())
1718 ui_delay(2000L, TRUE);
1719 }
1720}
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722/*
1723 * Set terminal options for terminal "term".
1724 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1725 *
1726 * While doing this, until ttest(), some options may be NULL, be careful.
1727 */
1728 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001729set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
1731 struct builtin_term *termp;
1732#ifdef HAVE_TGETENT
1733 int builtin_first = p_tbi;
1734 int try;
1735 int termcap_cleared = FALSE;
1736#endif
1737 int width = 0, height = 0;
1738 char_u *error_msg = NULL;
1739 char_u *bs_p, *del_p;
1740
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001741 /* In silect mode (ex -s) we don't use the 'term' option. */
1742 if (silent_mode)
1743 return OK;
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 detected_8bit = FALSE; /* reset 8-bit detection */
1746
1747 if (term_is_builtin(term))
1748 {
1749 term += 8;
1750#ifdef HAVE_TGETENT
1751 builtin_first = 1;
1752#endif
1753 }
1754
1755/*
1756 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1757 * If builtin_first is TRUE:
1758 * 0. try builtin termcap
1759 * 1. try external termcap
1760 * 2. if both fail default to a builtin terminal
1761 * If builtin_first is FALSE:
1762 * 1. try external termcap
1763 * 2. try builtin termcap, if both fail default to a builtin terminal
1764 */
1765#ifdef HAVE_TGETENT
1766 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1767 {
1768 /*
1769 * Use external termcap
1770 */
1771 if (try == 1)
1772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 /*
1776 * If the external termcap does not have a matching entry, try the
1777 * builtin ones.
1778 */
1779 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (!termcap_cleared)
1782 {
1783 clear_termoptions(); /* clear old options */
1784 termcap_cleared = TRUE;
1785 }
1786
Bram Moolenaar69e05692018-05-10 14:11:52 +02001787 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
1789 }
1790 else /* try == 0 || try == 2 */
1791#endif /* HAVE_TGETENT */
1792 /*
1793 * Use builtin termcap
1794 */
1795 {
1796#ifdef HAVE_TGETENT
1797 /*
1798 * If builtin termcap was already used, there is no need to search
1799 * for the builtin termcap again, quit now.
1800 */
1801 if (try == 2 && builtin_first && termcap_cleared)
1802 break;
1803#endif
1804 /*
1805 * search for 'term' in builtin_termcaps[]
1806 */
1807 termp = find_builtin_term(term);
1808 if (termp->bt_string == NULL) /* did not find it */
1809 {
1810#ifdef HAVE_TGETENT
1811 /*
1812 * If try == 0, first try the external termcap. If that is not
1813 * found we'll get back here with try == 2.
1814 * If termcap_cleared is set we used the external termcap,
1815 * don't complain about not finding the term in the builtin
1816 * termcap.
1817 */
1818 if (try == 0) /* try external one */
1819 continue;
1820 if (termcap_cleared) /* found in external termcap */
1821 break;
1822#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 /* when user typed :set term=xxx, quit here */
1826 if (starting != NO_SCREEN)
1827 {
1828 screen_start(); /* don't know where cursor is now */
1829 wait_return(TRUE);
1830 return FAIL;
1831 }
1832 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001833 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001834 set_string_option_direct((char_u *)"term", -1, term,
1835 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 display_errors();
1837 }
1838 out_flush();
1839#ifdef HAVE_TGETENT
1840 if (!termcap_cleared)
1841 {
1842#endif
1843 clear_termoptions(); /* clear old options */
1844#ifdef HAVE_TGETENT
1845 termcap_cleared = TRUE;
1846 }
1847#endif
1848 parse_builtin_tcap(term);
1849#ifdef FEAT_GUI
1850 if (term_is_gui(term))
1851 {
1852 out_flush();
1853 gui_init();
1854 /* If starting the GUI failed, don't do any of the other
1855 * things for this terminal */
1856 if (!gui.in_use)
1857 return FAIL;
1858#ifdef HAVE_TGETENT
1859 break; /* don't try using external termcap */
1860#endif
1861 }
1862#endif /* FEAT_GUI */
1863 }
1864#ifdef HAVE_TGETENT
1865 }
1866#endif
1867
1868/*
1869 * special: There is no info in the termcap about whether the cursor
1870 * positioning is relative to the start of the screen or to the start of the
1871 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1872 * relative.
1873 */
1874 if (STRCMP(term, "pcterm") == 0)
1875 T_CCS = (char_u *)"yes";
1876 else
1877 T_CCS = empty_option;
1878
1879#ifdef UNIX
1880/*
1881 * Any "stty" settings override the default for t_kb from the termcap.
1882 * This is in os_unix.c, because it depends a lot on the version of unix that
1883 * is being used.
1884 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1885 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001886# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 get_stty();
1890#endif
1891
1892/*
1893 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1894 * didn't work, use the default CTRL-H
1895 * The default for t_kD is DEL, unless t_kb is DEL.
1896 * The vim_strsave'd strings are probably lost forever, well it's only two
1897 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1898 * directly.
1899 */
1900#ifdef FEAT_GUI
1901 if (!gui.in_use)
1902#endif
1903 {
1904 bs_p = find_termcode((char_u *)"kb");
1905 del_p = find_termcode((char_u *)"kD");
1906 if (bs_p == NULL || *bs_p == NUL)
1907 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1908 if ((del_p == NULL || *del_p == NUL) &&
1909 (bs_p == NULL || *bs_p != DEL))
1910 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1911 }
1912
1913#if defined(UNIX) || defined(VMS)
1914 term_is_xterm = vim_is_xterm(term);
1915#endif
1916
1917#ifdef FEAT_MOUSE
1918# if defined(UNIX) || defined(VMS)
1919# ifdef FEAT_MOUSE_TTY
1920 /*
1921 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1922 * The termcode for the mouse is added as a side effect in option.c.
1923 */
1924 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001925 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001928 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 if (use_xterm_mouse())
1931 p = NULL; /* keep existing value, might be "xterm2" */
1932 else
1933 p = (char_u *)"xterm";
1934 }
1935# endif
1936 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001939 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1940 * "xterm2" in check_termcode(). */
1941 reset_option_was_set((char_u *)"ttym");
1942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (p == NULL
1944# ifdef FEAT_GUI
1945 || gui.in_use
1946# endif
1947 )
1948 check_mouse_termcode(); /* set mouse termcode anyway */
1949 }
1950# endif
1951# else
1952 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1953# endif
1954#endif /* FEAT_MOUSE */
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#ifdef USE_TERM_CONSOLE
1957 /* DEFAULT_TERM indicates that it is the machine console. */
1958 if (STRCMP(term, DEFAULT_TERM) != 0)
1959 term_console = FALSE;
1960 else
1961 {
1962 term_console = TRUE;
1963# ifdef AMIGA
1964 win_resize_on(); /* enable window resizing reports */
1965# endif
1966 }
1967#endif
1968
1969#if defined(UNIX) || defined(VMS)
1970 /*
1971 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1972 */
1973 if (vim_is_fastterm(term))
1974 p_tf = TRUE;
1975#endif
1976#ifdef USE_TERM_CONSOLE
1977 /*
1978 * 'ttyfast' is default on consoles
1979 */
1980 if (term_console)
1981 p_tf = TRUE;
1982#endif
1983
1984 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1985
1986 full_screen = TRUE; /* we can use termcap codes from now on */
1987 set_term_defaults(); /* use current values as defaults */
1988#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02001989 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001990 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#endif
1992
1993 /*
1994 * Initialize the terminal with the appropriate termcap codes.
1995 * Set the mouse and window title if possible.
1996 * Don't do this when starting, need to parse the .vimrc first, because it
1997 * may redefine t_TI etc.
1998 */
1999 if (starting != NO_SCREEN)
2000 {
2001 starttermcap(); /* may change terminal mode */
2002#ifdef FEAT_MOUSE
2003 setmouse(); /* may start using the mouse */
2004#endif
2005#ifdef FEAT_TITLE
2006 maketitle(); /* may display window title */
2007#endif
2008 }
2009
2010 /* display initial screen after ttest() checking. jw. */
2011 if (width <= 0 || height <= 0)
2012 {
2013 /* termcap failed to report size */
2014 /* set defaults, in case ui_get_shellsize() also fails */
2015 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002016#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 height = 25; /* console is often 25 lines */
2018#else
2019 height = 24; /* most terminals are 24 lines */
2020#endif
2021 }
2022 set_shellsize(width, height, FALSE); /* may change Rows */
2023 if (starting != NO_SCREEN)
2024 {
2025 if (scroll_region)
2026 scroll_region_reset(); /* In case Rows changed */
2027 check_map_keycodes(); /* check mappings for terminal codes used */
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002030 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 /*
2033 * Execute the TermChanged autocommands for each buffer that is
2034 * loaded.
2035 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002036 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002037 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 if (curbuf->b_ml.ml_mfp != NULL)
2040 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2041 curbuf);
2042 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002043 if (bufref_valid(&old_curbuf))
2044 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047
2048#ifdef FEAT_TERMRESPONSE
2049 may_req_termresponse();
2050#endif
2051
2052 return OK;
2053}
2054
2055#if defined(FEAT_MOUSE) || defined(PROTO)
2056
2057# ifdef FEAT_MOUSE_TTY
2058# define HMT_NORMAL 1
2059# define HMT_NETTERM 2
2060# define HMT_DEC 4
2061# define HMT_JSBTERM 8
2062# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002063# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002064# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002065# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066static int has_mouse_termcode = 0;
2067# endif
2068
Bram Moolenaar864207d2008-06-24 22:14:38 +00002069# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002071set_mouse_termcode(
2072 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2073 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 char_u name[2];
2076
2077 name[0] = n;
2078 name[1] = KE_FILLER;
2079 add_termcode(name, s, FALSE);
2080# ifdef FEAT_MOUSE_TTY
2081# ifdef FEAT_MOUSE_JSB
2082 if (n == KS_JSBTERM_MOUSE)
2083 has_mouse_termcode |= HMT_JSBTERM;
2084 else
2085# endif
2086# ifdef FEAT_MOUSE_NET
2087 if (n == KS_NETTERM_MOUSE)
2088 has_mouse_termcode |= HMT_NETTERM;
2089 else
2090# endif
2091# ifdef FEAT_MOUSE_DEC
2092 if (n == KS_DEC_MOUSE)
2093 has_mouse_termcode |= HMT_DEC;
2094 else
2095# endif
2096# ifdef FEAT_MOUSE_PTERM
2097 if (n == KS_PTERM_MOUSE)
2098 has_mouse_termcode |= HMT_PTERM;
2099 else
2100# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002101# ifdef FEAT_MOUSE_URXVT
2102 if (n == KS_URXVT_MOUSE)
2103 has_mouse_termcode |= HMT_URXVT;
2104 else
2105# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002106# ifdef FEAT_MOUSE_SGR
2107 if (n == KS_SGR_MOUSE)
2108 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002109 else if (n == KS_SGR_MOUSE_RELEASE)
2110 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002111 else
2112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 has_mouse_termcode |= HMT_NORMAL;
2114# endif
2115}
2116# endif
2117
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002118# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002119 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002121del_mouse_termcode(
2122 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 char_u name[2];
2125
2126 name[0] = n;
2127 name[1] = KE_FILLER;
2128 del_termcode(name);
2129# ifdef FEAT_MOUSE_TTY
2130# ifdef FEAT_MOUSE_JSB
2131 if (n == KS_JSBTERM_MOUSE)
2132 has_mouse_termcode &= ~HMT_JSBTERM;
2133 else
2134# endif
2135# ifdef FEAT_MOUSE_NET
2136 if (n == KS_NETTERM_MOUSE)
2137 has_mouse_termcode &= ~HMT_NETTERM;
2138 else
2139# endif
2140# ifdef FEAT_MOUSE_DEC
2141 if (n == KS_DEC_MOUSE)
2142 has_mouse_termcode &= ~HMT_DEC;
2143 else
2144# endif
2145# ifdef FEAT_MOUSE_PTERM
2146 if (n == KS_PTERM_MOUSE)
2147 has_mouse_termcode &= ~HMT_PTERM;
2148 else
2149# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002150# ifdef FEAT_MOUSE_URXVT
2151 if (n == KS_URXVT_MOUSE)
2152 has_mouse_termcode &= ~HMT_URXVT;
2153 else
2154# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002155# ifdef FEAT_MOUSE_SGR
2156 if (n == KS_SGR_MOUSE)
2157 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002158 else if (n == KS_SGR_MOUSE_RELEASE)
2159 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002160 else
2161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 has_mouse_termcode &= ~HMT_NORMAL;
2163# endif
2164}
2165# endif
2166#endif
2167
2168#ifdef HAVE_TGETENT
2169/*
2170 * Call tgetent()
2171 * Return error message if it fails, NULL if it's OK.
2172 */
2173 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002174tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 int i;
2177
2178 i = TGETENT(tbuf, term);
2179 if (i < 0 /* -1 is always an error */
2180# ifdef TGETENT_ZERO_ERR
2181 || i == 0 /* sometimes zero is also an error */
2182# endif
2183 )
2184 {
2185 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2186 * tgetent() with the always existing "dumb" entry to avoid a crash or
2187 * hang. */
2188 (void)TGETENT(tbuf, "dumb");
2189
2190 if (i < 0)
2191# ifdef TGETENT_ZERO_ERR
2192 return (char_u *)_("E557: Cannot open termcap file");
2193 if (i == 0)
2194# endif
2195#ifdef TERMINFO
2196 return (char_u *)_("E558: Terminal entry not found in terminfo");
2197#else
2198 return (char_u *)_("E559: Terminal entry not found in termcap");
2199#endif
2200 }
2201 return NULL;
2202}
2203
2204/*
2205 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2206 * Fix that here.
2207 */
2208 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002209vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 char *p;
2212
2213 p = tgetstr(s, (char **)pp);
2214 if (p == (char *)-1)
2215 p = NULL;
2216 return (char_u *)p;
2217}
2218#endif /* HAVE_TGETENT */
2219
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002220#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221/*
2222 * Get Columns and Rows from the termcap. Used after a window signal if the
2223 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2224 * and "li" entries never change. But on some systems this works.
2225 * Errors while getting the entries are ignored.
2226 */
2227 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002228getlinecol(
2229 long *cp, /* pointer to columns */
2230 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 char_u tbuf[TBUFSZ];
2233
2234 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2235 {
2236 if (*cp == 0)
2237 *cp = tgetnum("co");
2238 if (*rp == 0)
2239 *rp = tgetnum("li");
2240 }
2241}
2242#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2243
2244/*
2245 * Get a string entry from the termcap and add it to the list of termcodes.
2246 * Used for <t_xx> special keys.
2247 * Give an error message for failure when not sourcing.
2248 * If force given, replace an existing entry.
2249 * Return FAIL if the entry was not found, OK if the entry was added.
2250 */
2251 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002252add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254 char_u *term;
2255 int key;
2256 struct builtin_term *termp;
2257#ifdef HAVE_TGETENT
2258 char_u *string;
2259 int i;
2260 int builtin_first;
2261 char_u tbuf[TBUFSZ];
2262 char_u tstrbuf[TBUFSZ];
2263 char_u *tp = tstrbuf;
2264 char_u *error_msg = NULL;
2265#endif
2266
2267/*
2268 * If the GUI is running or will start in a moment, we only support the keys
2269 * that the GUI can produce.
2270 */
2271#ifdef FEAT_GUI
2272 if (gui.in_use || gui.starting)
2273 return gui_mch_haskey(name);
2274#endif
2275
2276 if (!force && find_termcode(name) != NULL) /* it's already there */
2277 return OK;
2278
2279 term = T_NAME;
2280 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2281 return FAIL;
2282
2283 if (term_is_builtin(term)) /* name starts with "builtin_" */
2284 {
2285 term += 8;
2286#ifdef HAVE_TGETENT
2287 builtin_first = TRUE;
2288#endif
2289 }
2290#ifdef HAVE_TGETENT
2291 else
2292 builtin_first = p_tbi;
2293#endif
2294
2295#ifdef HAVE_TGETENT
2296/*
2297 * We can get the entry from the builtin termcap and from the external one.
2298 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2299 * builtin termcap first.
2300 * If 'ttybuiltin' is off, try external termcap first.
2301 */
2302 for (i = 0; i < 2; ++i)
2303 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002304 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
2306 /*
2307 * Search in builtin termcap
2308 */
2309 {
2310 termp = find_builtin_term(term);
2311 if (termp->bt_string != NULL) /* found it */
2312 {
2313 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002314 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 while (termp->bt_entry != (int)KS_NAME)
2316 {
2317 if ((int)termp->bt_entry == key)
2318 {
2319 add_termcode(name, (char_u *)termp->bt_string,
2320 term_is_8bit(term));
2321 return OK;
2322 }
2323 ++termp;
2324 }
2325 }
2326 }
2327#ifdef HAVE_TGETENT
2328 else
2329 /*
2330 * Search in external termcap
2331 */
2332 {
2333 error_msg = tgetent_error(tbuf, term);
2334 if (error_msg == NULL)
2335 {
2336 string = TGETSTR((char *)name, &tp);
2337 if (string != NULL && *string != NUL)
2338 {
2339 add_termcode(name, string, FALSE);
2340 return OK;
2341 }
2342 }
2343 }
2344 }
2345#endif
2346
2347 if (sourcing_name == NULL)
2348 {
2349#ifdef HAVE_TGETENT
2350 if (error_msg != NULL)
2351 EMSG(error_msg);
2352 else
2353#endif
2354 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2355 }
2356 return FAIL;
2357}
2358
2359 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002360term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2363}
2364
2365/*
2366 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2367 * Assume that the terminal is using 8-bit controls when the name contains
2368 * "8bit", like in "xterm-8bit".
2369 */
2370 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002371term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2374}
2375
2376/*
2377 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002378 * <Esc>[ -> CSI <M_C_[>
2379 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 * <Esc>O -> <M-C-O>
2381 */
2382 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002383term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 if (*p == ESC)
2386 {
2387 if (p[1] == '[')
2388 return CSI;
2389 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002390 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (p[1] == 'O')
2392 return 0x8f;
2393 }
2394 return 0;
2395}
2396
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002397#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002399term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2402}
2403#endif
2404
2405#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2406
2407 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002408tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 static char_u buf[16];
2411 char_u *p;
2412
2413 p = buf + 15;
2414 *p = '\0';
2415 do
2416 {
2417 --p;
2418 *p = (char_u) (i % 10 + '0');
2419 i /= 10;
2420 }
2421 while (i > 0 && p > buf);
2422 return p;
2423}
2424#endif
2425
2426#ifndef HAVE_TGETENT
2427
2428/*
2429 * minimal tgoto() implementation.
2430 * no padding and we only parse for %i %d and %+char
2431 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002432static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002434 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002435tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 static char buf[30];
2438 char *p, *s, *e;
2439
2440 if (!cm)
2441 return "OOPS";
2442 e = buf + 29;
2443 for (s = buf; s < e && *cm; cm++)
2444 {
2445 if (*cm != '%')
2446 {
2447 *s++ = *cm;
2448 continue;
2449 }
2450 switch (*++cm)
2451 {
2452 case 'd':
2453 p = (char *)tltoa((unsigned long)y);
2454 y = x;
2455 while (*p)
2456 *s++ = *p++;
2457 break;
2458 case 'i':
2459 x++;
2460 y++;
2461 break;
2462 case '+':
2463 *s++ = (char)(*++cm + y);
2464 y = x;
2465 break;
2466 case '%':
2467 *s++ = *cm;
2468 break;
2469 default:
2470 return "OOPS";
2471 }
2472 }
2473 *s = '\0';
2474 return buf;
2475}
2476
2477#endif /* HAVE_TGETENT */
2478
2479/*
2480 * Set the terminal name and initialize the terminal options.
2481 * If "name" is NULL or empty, get the terminal name from the environment.
2482 * If that fails, use the default terminal name.
2483 */
2484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 char_u *term;
2488
2489 if (name != NULL && *name == NUL)
2490 name = NULL; /* empty name is equal to no name */
2491 term = name;
2492
2493#ifdef __BEOS__
2494 /*
2495 * TERM environment variable is normally set to 'ansi' on the Bebox;
2496 * Since the BeBox doesn't quite support full ANSI yet, we use our
2497 * own custom 'ansi-beos' termcap instead, unless the -T option has
2498 * been given on the command line.
2499 */
2500 if (term == NULL
2501 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2502 term = DEFAULT_TERM;
2503#endif
2504#ifndef MSWIN
2505 if (term == NULL)
2506 term = mch_getenv((char_u *)"TERM");
2507#endif
2508 if (term == NULL || *term == NUL)
2509 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002510 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /* Set the default terminal name. */
2513 set_string_default("term", term);
2514 set_string_default("ttytype", term);
2515
2516 /*
2517 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2518 */
2519 set_termname(T_NAME != NULL ? T_NAME : term);
2520}
2521
2522/*
2523 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2524 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002525#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2527static char_u out_buf[OUT_SIZE + 1];
2528static int out_pos = 0; /* number of chars in out_buf */
2529
2530/*
2531 * out_flush(): flush the output buffer
2532 */
2533 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002534out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 int len;
2537
2538 if (out_pos != 0)
2539 {
2540 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2541 len = out_pos;
2542 out_pos = 0;
2543 ui_write(out_buf, len);
2544 }
2545}
2546
Bram Moolenaara338adc2018-01-31 20:51:47 +01002547/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002548 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2549 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002550 */
2551 void
2552out_flush_cursor(
2553 int force UNUSED, /* when TRUE, update cursor even when not moved */
2554 int clear_selection UNUSED) /* clear selection under cursor */
2555{
2556 mch_disable_flush();
2557 out_flush();
2558 mch_enable_flush();
2559#ifdef FEAT_GUI
2560 if (gui.in_use)
2561 {
2562 gui_update_cursor(force, clear_selection);
2563 gui_may_flush();
2564 }
2565#endif
2566}
2567
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#if defined(FEAT_MBYTE) || defined(PROTO)
2570/*
2571 * Sometimes a byte out of a multi-byte character is written with out_char().
2572 * To avoid flushing half of the character, call this function first.
2573 */
2574 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002575out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
2577 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2578 out_flush();
2579}
2580#endif
2581
2582#ifdef FEAT_GUI
2583/*
2584 * out_trash(): Throw away the contents of the output buffer
2585 */
2586 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002587out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 out_pos = 0;
2590}
2591#endif
2592
2593/*
2594 * out_char(c): put a byte into the output buffer.
2595 * Flush it if it becomes full.
2596 * This should not be used for outputting text on the screen (use functions
2597 * like msg_puts() and screen_putchar() for that).
2598 */
2599 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002600out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
Bram Moolenaard0573012017-10-28 21:11:06 +02002602#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2604 out_char('\r');
2605#endif
2606
2607 out_buf[out_pos++] = c;
2608
2609 /* For testing we flush each time. */
2610 if (out_pos >= OUT_SIZE || p_wd)
2611 out_flush();
2612}
2613
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002614static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616/*
2617 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2618 */
2619 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002620out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
Bram Moolenaard0573012017-10-28 21:11:06 +02002622#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2624 out_char_nf('\r');
2625#endif
2626
2627 out_buf[out_pos++] = c;
2628
2629 if (out_pos >= OUT_SIZE)
2630 out_flush();
2631}
2632
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002633#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2634 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635/*
2636 * A never-padding out_str.
2637 * use this whenever you don't want to run the string through tputs.
2638 * tputs above is harmless, but tputs from the termcap library
2639 * is likely to strip off leading digits, that it mistakes for padding
2640 * information, and "%i", "%d", etc.
2641 * This should only be used for writing terminal codes, not for outputting
2642 * normal text (use functions like msg_puts() and screen_putchar() for that).
2643 */
2644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002645out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2648 out_flush();
2649 while (*s)
2650 out_char_nf(*s++);
2651
2652 /* For testing we write one string at a time. */
2653 if (p_wd)
2654 out_flush();
2655}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002659 * A conditional-flushing out_str, mainly for visualbell.
2660 * Handles a delay internally, because termlib may not respect the delay or do
2661 * it at the wrong time.
2662 * Note: Only for terminal strings.
2663 */
2664 void
2665out_str_cf(char_u *s)
2666{
2667 if (s != NULL && *s)
2668 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002669#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002670 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002671#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002672
2673#ifdef FEAT_GUI
2674 /* Don't use tputs() when GUI is used, ncurses crashes. */
2675 if (gui.in_use)
2676 {
2677 out_str_nf(s);
2678 return;
2679 }
2680#endif
2681 if (out_pos > OUT_SIZE - 20)
2682 out_flush();
2683#ifdef HAVE_TGETENT
2684 for (p = s; *s; ++s)
2685 {
2686 /* flush just before delay command */
2687 if (*s == '$' && *(s + 1) == '<')
2688 {
2689 char_u save_c = *s;
2690 int duration = atoi((char *)s + 2);
2691
2692 *s = NUL;
2693 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2694 *s = save_c;
2695 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002696# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002697 /* Only sleep here if we can limit this happening in
2698 * vim_beep(). */
2699 p = vim_strchr(s, '>');
2700 if (p == NULL || duration <= 0)
2701 {
2702 /* can't parse the time, don't sleep here */
2703 p = s;
2704 }
2705 else
2706 {
2707 ++p;
2708 do_sleep(duration);
2709 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002710# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002711 /* Rely on the terminal library to sleep. */
2712 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002713# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002714 break;
2715 }
2716 }
2717 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2718#else
2719 while (*s)
2720 out_char_nf(*s++);
2721#endif
2722
2723 /* For testing we write one string at a time. */
2724 if (p_wd)
2725 out_flush();
2726 }
2727}
2728
2729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 * out_str(s): Put a character string a byte at a time into the output buffer.
2731 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2732 * This should only be used for writing terminal codes, not for outputting
2733 * normal text (use functions like msg_puts() and screen_putchar() for that).
2734 */
2735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 if (s != NULL && *s)
2739 {
2740#ifdef FEAT_GUI
2741 /* Don't use tputs() when GUI is used, ncurses crashes. */
2742 if (gui.in_use)
2743 {
2744 out_str_nf(s);
2745 return;
2746 }
2747#endif
2748 /* avoid terminal strings being split up */
2749 if (out_pos > OUT_SIZE - 20)
2750 out_flush();
2751#ifdef HAVE_TGETENT
2752 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2753#else
2754 while (*s)
2755 out_char_nf(*s++);
2756#endif
2757
2758 /* For testing we write one string at a time. */
2759 if (p_wd)
2760 out_flush();
2761 }
2762}
2763
2764/*
2765 * cursor positioning using termcap parser. (jw)
2766 */
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 OUT_STR(tgoto((char *)T_CM, col, row));
2771}
2772
2773 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002774term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 OUT_STR(tgoto((char *)T_CRI, 0, i));
2777}
2778
2779 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002780term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2783}
2784
2785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002786term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2789}
2790
2791#if defined(HAVE_TGETENT) || defined(PROTO)
2792 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002793term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 /* Can't handle a negative value here */
2796 if (x < 0)
2797 x = 0;
2798 if (y < 0)
2799 y = 0;
2800 OUT_STR(tgoto((char *)T_CWP, y, x));
2801}
2802
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002803# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2804/*
2805 * Return TRUE if we can request the terminal for a response.
2806 */
2807 static int
2808can_get_termresponse()
2809{
2810 return cur_tmode == TMODE_RAW
2811 && termcap_active
2812# ifdef UNIX
2813 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2814# endif
2815 && p_ek;
2816}
2817
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002818static int winpos_x = -1;
2819static int winpos_y = -1;
2820static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002821
2822/*
2823 * Try getting the Vim window position from the terminal.
2824 * Returns OK or FAIL.
2825 */
2826 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002827term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002828{
2829 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002830 int prev_winpos_x = winpos_x;
2831 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002832
2833 if (*T_CGP == NUL || !can_get_termresponse())
2834 return FAIL;
2835 winpos_x = -1;
2836 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002837 ++did_request_winpos;
2838 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002839 OUT_STR(T_CGP);
2840 out_flush();
2841
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002842 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002843 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002844 {
2845 (void)vpeekc_nomap();
2846 if (winpos_x >= 0 && winpos_y >= 0)
2847 {
2848 *x = winpos_x;
2849 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002850 return OK;
2851 }
2852 ui_delay(10, FALSE);
2853 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002854 /* Do not reset "did_request_winpos", if we timed out the response might
2855 * still come later and we must consume it. */
2856
2857 winpos_x = prev_winpos_x;
2858 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002859 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002860 {
2861 /* Polling: return previous values if we have them. */
2862 *x = winpos_x;
2863 *y = winpos_y;
2864 return OK;
2865 }
2866
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002867 return FALSE;
2868}
2869# endif
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002872term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002874 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875}
2876#endif
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002879term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002882 int i = *s == CSI ? 1 : 2;
2883 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 /* Special handling of 16 colors, because termcap can't handle it */
2886 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2887 /* Also accept CSI instead of <Esc>[ */
2888 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002889 && ((s[0] == ESC && s[1] == '[')
2890#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2891 || (s[0] == ESC && s[1] == '|')
2892#endif
2893 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 && s[i] != NUL
2895 && (STRCMP(s + i + 1, "%p1%dm") == 0
2896 || STRCMP(s + i + 1, "%dm") == 0)
2897 && (s[i] == '3' || s[i] == '4'))
2898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002900 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002902 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002904 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002905#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002906 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002907#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002908 IF_EB("\033[", ESC_STR "[")) : "\233";
2909 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2910 : (n >= 16 ? "48;5;" : "10");
2911
2912 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2914 }
2915 else
2916 OUT_STR(tgoto((char *)s, 0, n));
2917}
2918
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002919 void
2920term_fg_color(int n)
2921{
2922 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2923 if (*T_CAF)
2924 term_color(T_CAF, n);
2925 else if (*T_CSF)
2926 term_color(T_CSF, n);
2927}
2928
2929 void
2930term_bg_color(int n)
2931{
2932 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2933 if (*T_CAB)
2934 term_color(T_CAB, n);
2935 else if (*T_CSB)
2936 term_color(T_CSB, n);
2937}
2938
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002939#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002940
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002941#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2942#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2943#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002944
2945 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002946term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002947{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002948#define MAX_COLOR_STR_LEN 100
2949 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002950
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002951 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002952 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002953 OUT_STR(buf);
2954}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002955
2956 void
2957term_fg_rgb_color(guicolor_T rgb)
2958{
2959 term_rgb_color(T_8F, rgb);
2960}
2961
2962 void
2963term_bg_rgb_color(guicolor_T rgb)
2964{
2965 term_rgb_color(T_8B, rgb);
2966}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002967#endif
2968
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002969#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2970 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971/*
2972 * Generic function to set window title, using t_ts and t_fs.
2973 */
2974 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002975term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 /* t_ts takes one argument: column in status line */
2978 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2979 out_str_nf(title);
2980 out_str(T_FS); /* set title end */
2981 out_flush();
2982}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002983
2984/*
2985 * Tell the terminal to push (save) the title and/or icon, so that it can be
2986 * popped (restored) later.
2987 */
2988 void
2989term_push_title(int which)
2990{
2991 if ((which & SAVE_RESTORE_TITLE) && *T_CST != NUL)
2992 {
2993 OUT_STR(T_CST);
2994 out_flush();
2995 }
2996
2997 if ((which & SAVE_RESTORE_ICON) && *T_SSI != NUL)
2998 {
2999 OUT_STR(T_SSI);
3000 out_flush();
3001 }
3002}
3003
3004/*
3005 * Tell the terminal to pop the title and/or icon.
3006 */
3007 void
3008term_pop_title(int which)
3009{
3010 if ((which & SAVE_RESTORE_TITLE) && *T_CRT != NUL)
3011 {
3012 OUT_STR(T_CRT);
3013 out_flush();
3014 }
3015
3016 if ((which & SAVE_RESTORE_ICON) && *T_SRI != NUL)
3017 {
3018 OUT_STR(T_SRI);
3019 out_flush();
3020 }
3021}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#endif
3023
3024/*
3025 * Make sure we have a valid set or terminal options.
3026 * Replace all entries that are NULL by empty_option
3027 */
3028 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003029ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003031 char_u *env_colors;
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 check_options(); /* make sure no options are NULL */
3034
3035 /*
3036 * MUST have "cm": cursor motion.
3037 */
3038 if (*T_CM == NUL)
3039 EMSG(_("E437: terminal capability \"cm\" required"));
3040
3041 /*
3042 * if "cs" defined, use a scroll region, it's faster.
3043 */
3044 if (*T_CS != NUL)
3045 scroll_region = TRUE;
3046 else
3047 scroll_region = FALSE;
3048
3049 if (pairs)
3050 {
3051 /*
3052 * optional pairs
3053 */
3054 /* TP goes to normal mode for TI (invert) and TB (bold) */
3055 if (*T_ME == NUL)
3056 T_ME = T_MR = T_MD = T_MB = empty_option;
3057 if (*T_SO == NUL || *T_SE == NUL)
3058 T_SO = T_SE = empty_option;
3059 if (*T_US == NUL || *T_UE == NUL)
3060 T_US = T_UE = empty_option;
3061 if (*T_CZH == NUL || *T_CZR == NUL)
3062 T_CZH = T_CZR = empty_option;
3063
3064 /* T_VE is needed even though T_VI is not defined */
3065 if (*T_VE == NUL)
3066 T_VI = empty_option;
3067
3068 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3069 if (*T_ME == NUL)
3070 {
3071 T_ME = T_SE;
3072 T_MR = T_SO;
3073 T_MD = T_SO;
3074 }
3075
3076 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3077 if (*T_SO == NUL)
3078 {
3079 T_SE = T_ME;
3080 if (*T_MR == NUL)
3081 T_SO = T_MD;
3082 else
3083 T_SO = T_MR;
3084 }
3085
3086 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3087 if (*T_CZH == NUL)
3088 {
3089 T_CZR = T_ME;
3090 if (*T_MR == NUL)
3091 T_CZH = T_MD;
3092 else
3093 T_CZH = T_MR;
3094 }
3095
3096 /* "Sb" and "Sf" come in pairs */
3097 if (*T_CSB == NUL || *T_CSF == NUL)
3098 {
3099 T_CSB = empty_option;
3100 T_CSF = empty_option;
3101 }
3102
3103 /* "AB" and "AF" come in pairs */
3104 if (*T_CAB == NUL || *T_CAF == NUL)
3105 {
3106 T_CAB = empty_option;
3107 T_CAF = empty_option;
3108 }
3109
3110 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3111 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003112 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /* Set 'weirdinvert' according to value of 't_xs' */
3115 p_wiv = (*T_XS != NUL);
3116 }
3117 need_gather = TRUE;
3118
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003119 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003121 env_colors = mch_getenv((char_u *)"COLORS");
3122 if (env_colors != NULL && isdigit(*env_colors))
3123 {
3124 int colors = atoi((char *)env_colors);
3125
3126 if (colors != t_colors)
3127 set_color_count(colors);
3128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3132 || defined(PROTO)
3133/*
3134 * Represent the given long_u as individual bytes, with the most significant
3135 * byte first, and store them in dst.
3136 */
3137 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003138add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 int i;
3141 int shift;
3142
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003143 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 shift = 8 * (sizeof(long_u) - i);
3146 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3147 }
3148}
3149
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003150static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152/*
3153 * Interpret the next string of bytes in buf as a long integer, with the most
3154 * significant byte first. Note that it is assumed that buf has been through
3155 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3156 * Puts result in val, and returns the number of bytes read from buf
3157 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3158 * were present.
3159 */
3160 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003161get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
3163 int len;
3164 char_u bytes[sizeof(long_u)];
3165 int i;
3166 int shift;
3167
3168 *val = 0;
3169 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3170 if (len != -1)
3171 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003172 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 shift = 8 * (sizeof(long_u) - 1 - i);
3175 *val += (long_u)bytes[i] << shift;
3176 }
3177 }
3178 return len;
3179}
3180#endif
3181
3182#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003183 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3184 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185/*
3186 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3187 * that buf has been through inchar(). Returns the actual number of bytes used
3188 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3189 * available.
3190 */
3191 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003192get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 int len = 0;
3195 int i;
3196 char_u c;
3197
3198 for (i = 0; i < num_bytes; i++)
3199 {
3200 if ((c = buf[len++]) == NUL)
3201 return -1;
3202 if (c == K_SPECIAL)
3203 {
3204 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3205 return -1;
3206 if (buf[len++] == (int)KS_ZERO)
3207 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003208 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3209 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3210 if (buf[len++] == (int)KE_CSI)
3211 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003213 else if (c == CSI && buf[len] == KS_EXTRA
3214 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003215 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3216 * the start of a special key, see add_to_input_buf_csi(). */
3217 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 bytes[i] = c;
3219 }
3220 return len;
3221}
3222#endif
3223
3224/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003225 * Check if the new shell size is valid, correct it if it's too small or way
3226 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 */
3228 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003229check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (Rows < min_rows()) /* need room for one window and command line */
3232 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003233 limit_screen_size();
3234}
3235
3236/*
3237 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3238 */
3239 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003240limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003241{
3242 if (Columns < MIN_COLUMNS)
3243 Columns = MIN_COLUMNS;
3244 else if (Columns > 10000)
3245 Columns = 10000;
3246 if (Rows > 1000)
3247 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248}
3249
Bram Moolenaar86b68352004-12-27 21:59:20 +00003250/*
3251 * Invoked just before the screen structures are going to be (re)allocated.
3252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 static int old_Rows = 0;
3257 static int old_Columns = 0;
3258
3259 if (old_Rows != Rows || old_Columns != Columns)
3260 ui_new_shellsize();
3261 if (old_Rows != Rows)
3262 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003263 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003264 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003265 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 old_Rows = Rows;
3267 shell_new_rows(); /* update window sizes */
3268 }
3269 if (old_Columns != Columns)
3270 {
3271 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274}
3275
3276/*
3277 * Call this function when the Vim shell has been resized in any way.
3278 * Will obtain the current size and redraw (also when size didn't change).
3279 */
3280 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003281shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 set_shellsize(0, 0, FALSE);
3284}
3285
3286/*
3287 * Check if the shell size changed. Handle a resize.
3288 * When the size didn't change, nothing happens.
3289 */
3290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 int old_Rows = Rows;
3294 int old_Columns = Columns;
3295
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003296 if (!exiting
3297#ifdef FEAT_GUI
3298 /* Do not get the size when executing a shell command during
3299 * startup. */
3300 && !gui.starting
3301#endif
3302 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003303 {
3304 (void)ui_get_shellsize();
3305 check_shellsize();
3306 if (old_Rows != Rows || old_Columns != Columns)
3307 shell_resized();
3308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
3312 * Set size of the Vim shell.
3313 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3314 * window size (this is used for the :win command).
3315 * If 'mustset' is FALSE, we may try to get the real window size and if
3316 * it fails use 'width' and 'height'.
3317 */
3318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 static int busy = FALSE;
3322
3323 /*
3324 * Avoid recursiveness, can happen when setting the window size causes
3325 * another window-changed signal.
3326 */
3327 if (busy)
3328 return;
3329
3330 if (width < 0 || height < 0) /* just checking... */
3331 return;
3332
Bram Moolenaara971b822011-09-14 14:43:25 +02003333 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003335 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 State = SETWSIZE;
3337 return;
3338 }
3339
Bram Moolenaara971b822011-09-14 14:43:25 +02003340 /* curwin->w_buffer can be NULL when we are closing a window and the
3341 * buffer has already been closed and removing a scrollbar causes a resize
3342 * event. Don't resize then, it will happen after entering another buffer.
3343 */
3344 if (curwin->w_buffer == NULL)
3345 return;
3346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 ++busy;
3348
3349#ifdef AMIGA
3350 out_flush(); /* must do this before mch_get_shellsize() for
3351 some obscure reason */
3352#endif
3353
3354 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3355 {
3356 Rows = height;
3357 Columns = width;
3358 check_shellsize();
3359 ui_set_shellsize(mustset);
3360 }
3361 else
3362 check_shellsize();
3363
3364 /* The window layout used to be adjusted here, but it now happens in
3365 * screenalloc() (also invoked from screenclear()). That is because the
3366 * "busy" check above may skip this, but not screenalloc(). */
3367
3368 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3369 screenclear();
3370 else
3371 screen_start(); /* don't know where cursor is now */
3372
3373 if (starting != NO_SCREEN)
3374 {
3375#ifdef FEAT_TITLE
3376 maketitle();
3377#endif
3378 changed_line_abv_curs();
3379 invalidate_botline();
3380
3381 /*
3382 * We only redraw when it's needed:
3383 * - While at the more prompt or executing an external command, don't
3384 * redraw, but position the cursor.
3385 * - While editing the command line, only redraw that.
3386 * - in Ex mode, don't redraw anything.
3387 * - Otherwise, redraw right now, and position the cursor.
3388 * Always need to call update_screen() or screenalloc(), to make
3389 * sure Rows/Columns and the size of ScreenLines[] is correct!
3390 */
3391 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3392 || exmode_active)
3393 {
3394 screenalloc(FALSE);
3395 repeat_message();
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else
3398 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003399 if (curwin->w_p_scb)
3400 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003401 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003402 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003403 update_screen(NOT_VALID);
3404 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003405 }
3406 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003407 {
3408 update_topline();
3409#if defined(FEAT_INS_EXPAND)
3410 if (pum_visible())
3411 {
3412 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003413 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003414 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003415#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003416 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003417 if (redrawing())
3418 setcursor();
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421 cursor_on(); /* redrawing may have switched it off */
3422 }
3423 out_flush();
3424 --busy;
3425}
3426
3427/*
3428 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3429 * commands and Ex mode).
3430 */
3431 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003432settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434#ifdef FEAT_GUI
3435 /* don't set the term where gvim was started to any mode */
3436 if (gui.in_use)
3437 return;
3438#endif
3439
3440 if (full_screen)
3441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /*
3443 * When returning after calling a shell we want to really set the
3444 * terminal to raw mode, even though we think it already is, because
3445 * the shell program may have reset the terminal mode.
3446 * When we think the terminal is normal, don't try to set it to
3447 * normal again, because that causes problems (logout!) on some
3448 * machines.
3449 */
3450 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3451 {
3452#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003453# ifdef FEAT_GUI
3454 if (!gui.in_use && !gui.starting)
3455# endif
3456 {
3457 /* May need to check for T_CRV response and termcodes, it
3458 * doesn't work in Cooked mode, an external program may get
3459 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003460 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3461 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003462#ifdef FEAT_TERMINAL
3463 || rfg_status == STATUS_SENT
3464#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003465 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003466 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003467 || rcs_status == STATUS_SENT
3468 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003469 (void)vpeekc_nomap();
3470 check_for_codes_from_term();
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#endif
3473#ifdef FEAT_MOUSE_TTY
3474 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003475 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003477 if (tmode != TMODE_RAW)
3478 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003480 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 cur_tmode = tmode;
3482#ifdef FEAT_MOUSE
3483 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003484 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003486 if (tmode == TMODE_RAW)
3487 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 out_flush();
3489 }
3490#ifdef FEAT_TERMRESPONSE
3491 may_req_termresponse();
3492#endif
3493 }
3494}
3495
3496 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003497starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 if (full_screen && !termcap_active)
3500 {
3501 out_str(T_TI); /* start termcap mode */
3502 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003503 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 out_flush();
3505 termcap_active = TRUE;
3506 screen_start(); /* don't know where cursor is now */
3507#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003508# ifdef FEAT_GUI
3509 if (!gui.in_use && !gui.starting)
3510# endif
3511 {
3512 may_req_termresponse();
3513 /* Immediately check for a response. If t_Co changes, we don't
3514 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003515 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003516 check_for_codes_from_term();
3517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#endif
3519 }
3520}
3521
3522 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003523stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 screen_stop_highlight();
3526 reset_cterm_colors();
3527 if (termcap_active)
3528 {
3529#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003530# ifdef FEAT_GUI
3531 if (!gui.in_use && !gui.starting)
3532# endif
3533 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003534 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003535 if (crv_status == STATUS_SENT
3536 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003537# ifdef FEAT_TERMINAL
3538 || rfg_status == STATUS_SENT
3539# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003540 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003541 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003542 || rcs_status == STATUS_SENT
3543 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003544 {
3545# ifdef UNIX
3546 /* Give the terminal a chance to respond. */
3547 mch_delay(100L, FALSE);
3548# endif
3549# ifdef TCIFLUSH
3550 /* Discard data received but not read. */
3551 if (exiting)
3552 tcflush(fileno(stdin), TCIFLUSH);
3553# endif
3554 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003555 /* Check for termcodes first, otherwise an external program may
3556 * get them. */
3557 check_for_codes_from_term();
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003560 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 out_str(T_KE); /* stop "keypad transmit" mode */
3562 out_flush();
3563 termcap_active = FALSE;
3564 cursor_on(); /* just in case it is still off */
3565 out_str(T_TE); /* stop termcap mode */
3566 screen_start(); /* don't know where cursor is now */
3567 out_flush();
3568 }
3569}
3570
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003571#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572/*
3573 * Request version string (for xterm) when needed.
3574 * Only do this after switching to raw mode, otherwise the result will be
3575 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003576 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003577 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 * Only do this after termcap mode has been started, otherwise the codes for
3579 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003580 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3581 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003582 * On Unix only do it when both output and input are a tty (avoid writing
3583 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * The result is caught in check_termcode().
3585 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003586 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003587may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003589 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003590 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003591 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 && *T_CRV != NUL)
3593 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003594 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003596 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 /* check for the characters now, otherwise they might be eaten by
3598 * get_keystroke() */
3599 out_flush();
3600 (void)vpeekc_nomap();
3601 }
3602}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003603
3604# if defined(FEAT_MBYTE) || defined(PROTO)
3605/*
3606 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003607 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003608 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003609 * If the terminal treats \u25bd as single width, the position is (1, 1),
3610 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003611 * This function has the side effect that changes cursor position, so
3612 * it must be called immediately after entering termcap mode.
3613 */
3614 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003615may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003616{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003617 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003618 && can_get_termresponse()
3619 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003620 && *T_U7 != NUL
3621 && !option_was_set((char_u *)"ambiwidth"))
3622 {
3623 char_u buf[16];
3624
Bram Moolenaarb255b902018-04-24 21:40:10 +02003625 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003626 /* Do this in the second row. In the first row the returned sequence
3627 * may be CSI 1;2R, which is the same as <S-F3>. */
3628 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003629 buf[mb_char2bytes(0x25bd, buf)] = 0;
3630 out_str(buf);
3631 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003632 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003633 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003634
3635 /* This overwrites a few characters on the screen, a redraw is needed
3636 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003637 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003638 out_str((char_u *)" ");
3639 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003640
Bram Moolenaar05684312017-12-09 15:11:24 +01003641 /* Need to reset the known cursor position. */
3642 screen_start();
3643
Bram Moolenaar9584b312013-03-13 19:29:28 +01003644 /* check for the characters now, otherwise they might be eaten by
3645 * get_keystroke() */
3646 out_flush();
3647 (void)vpeekc_nomap();
3648 }
3649}
3650# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003651
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003652/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003653 * Similar to requesting the version string: Request the terminal background
3654 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003655 */
3656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003657may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003658{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003659 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003660 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003661 int didit = FALSE;
3662
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003663# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003664 /* Only request foreground if t_RF is set. */
3665 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3666 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003667 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003668 out_str(T_RFG);
3669 rfg_status = STATUS_SENT;
3670 didit = TRUE;
3671 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003672# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003673
3674 /* Only request background if t_RB is set. */
3675 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003676 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003677 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003678 out_str(T_RBG);
3679 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003680 didit = TRUE;
3681 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003682
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003683 if (didit)
3684 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003685 /* check for the characters now, otherwise they might be eaten by
3686 * get_keystroke() */
3687 out_flush();
3688 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003689 }
3690 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003691}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003692
Bram Moolenaar2951b772013-07-03 12:45:31 +02003693# ifdef DEBUG_TERMRESPONSE
3694 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003695log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003696{
3697 static FILE *fd_tr = NULL;
3698 static proftime_T start;
3699 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003700 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003701
3702 if (fd_tr == NULL)
3703 {
3704 fd_tr = fopen("termresponse.log", "w");
3705 profile_start(&start);
3706 }
3707 now = start;
3708 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003709 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3710 must_redraw == NOT_VALID ? "NV"
3711 : must_redraw == CLEAR ? "CL" : " ");
3712 va_start(ap, fmt);
3713 vfprintf(fd_tr, fmt, ap);
3714 va_end(ap);
3715 fputc('\n', fd_tr);
3716 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003717}
3718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#endif
3720
3721/*
3722 * Return TRUE when saving and restoring the screen.
3723 */
3724 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003725swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 return (full_screen && *T_TI != NUL);
3728}
3729
Bram Moolenaarecadf432018-03-20 11:17:04 +01003730#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3733 */
3734 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003735setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737# ifdef FEAT_MOUSE_TTY
3738 int checkfor;
3739# endif
3740
3741# ifdef FEAT_MOUSESHAPE
3742 update_mouseshape(-1);
3743# endif
3744
3745# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3746# ifdef FEAT_GUI
3747 /* In the GUI the mouse is always enabled. */
3748 if (gui.in_use)
3749 return;
3750# endif
3751 /* be quick when mouse is off */
3752 if (*p_mouse == NUL || has_mouse_termcode == 0)
3753 return;
3754
3755 /* don't switch mouse on when not in raw mode (Ex mode) */
3756 if (cur_tmode != TMODE_RAW)
3757 {
3758 mch_setmouse(FALSE);
3759 return;
3760 }
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (VIsual_active)
3763 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003764 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 checkfor = MOUSE_RETURN;
3766 else if (State & INSERT)
3767 checkfor = MOUSE_INSERT;
3768 else if (State & CMDLINE)
3769 checkfor = MOUSE_COMMAND;
3770 else if (State == CONFIRM || State == EXTERNCMD)
3771 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3772 else
3773 checkfor = MOUSE_NORMAL; /* assume normal mode */
3774
3775 if (mouse_has(checkfor))
3776 mch_setmouse(TRUE);
3777 else
3778 mch_setmouse(FALSE);
3779# endif
3780}
3781
3782/*
3783 * Return TRUE if
3784 * - "c" is in 'mouse', or
3785 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3786 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3787 * normal editing mode (not at hit-return message).
3788 */
3789 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003790mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 char_u *p;
3793
3794 for (p = p_mouse; *p; ++p)
3795 switch (*p)
3796 {
3797 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3798 return TRUE;
3799 break;
3800 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3801 return TRUE;
3802 break;
3803 default: if (c == *p) return TRUE; break;
3804 }
3805 return FALSE;
3806}
3807
3808/*
3809 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3810 */
3811 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003812mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
3814 return (p_mousem[0] == 'p');
3815}
3816#endif
3817
3818/*
3819 * By outputting the 'cursor very visible' termcap code, for some windowed
3820 * terminals this makes the screen scrolled to the correct position.
3821 * Used when starting Vim or returning from a shell.
3822 */
3823 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003824scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003826 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003829 out_str(T_CVS);
3830 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832}
3833
3834static int cursor_is_off = FALSE;
3835
3836/*
3837 * Enable the cursor.
3838 */
3839 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003840cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
3842 if (cursor_is_off)
Bram Moolenaarf1883472018-08-20 21:58:57 +02003843 {
3844 out_str(T_VE);
3845 cursor_is_off = FALSE;
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847}
3848
3849/*
3850 * Disable the cursor.
3851 */
3852 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003853cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003855 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003857 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 cursor_is_off = TRUE;
3859 }
3860}
3861
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003862#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003864 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003865 */
3866 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003867term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003868{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003869 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003870 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003871
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003872 /* Only do something when redrawing the screen and we can restore the
3873 * mode. */
3874 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003875 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003876# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003877 if (forced && initial_cursor_shape > 0)
3878 /* Restore to initial values. */
3879 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003880# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003881 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003882 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003883
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003884 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003885 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003886 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003887 {
3888 if (*T_CSR != NUL)
3889 p = T_CSR; /* Replace mode cursor */
3890 else
3891 p = T_CSI; /* fall back to Insert mode cursor */
3892 if (*p != NUL)
3893 {
3894 out_str(p);
3895 showing_mode = REPLACE;
3896 }
3897 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003898 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003899 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003900 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003901 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003902 {
3903 out_str(T_CSI); /* Insert mode cursor */
3904 showing_mode = INSERT;
3905 }
3906 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003907 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003908 {
3909 out_str(T_CEI); /* non-Insert mode cursor */
3910 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003911 }
3912}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003913
3914# if defined(FEAT_TERMINAL) || defined(PROTO)
3915 void
3916term_cursor_color(char_u *color)
3917{
3918 if (*T_CSC != NUL)
3919 {
3920 out_str(T_CSC); /* set cursor color start */
3921 out_str_nf(color);
3922 out_str(T_CEC); /* set cursor color end */
3923 out_flush();
3924 }
3925}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003926# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003927
Bram Moolenaar4db25542017-08-28 22:43:05 +02003928 int
3929blink_state_is_inverted()
3930{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003931#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003932 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3933 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003934#else
3935 return FALSE;
3936#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003937}
3938
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003939/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003940 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003941 */
3942 void
3943term_cursor_shape(int shape, int blink)
3944{
3945 if (*T_CSH != NUL)
3946 {
3947 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3948 out_flush();
3949 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003950 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003951 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003952 int do_blink = blink;
3953
3954 /* t_SH is empty: try setting just the blink state.
3955 * The blink flags are XORed together, if the initial blinking from
3956 * style and shape differs, we need to invert the flag here. */
3957 if (blink_state_is_inverted())
3958 do_blink = !blink;
3959
3960 if (do_blink && *T_VS != NUL)
3961 {
3962 out_str(T_VS);
3963 out_flush();
3964 }
3965 else if (!do_blink && *T_CVS != NUL)
3966 {
3967 out_str(T_CVS);
3968 out_flush();
3969 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003970 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003971}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003972#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003973
3974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * Set scrolling region for window 'wp'.
3976 * The region starts 'off' lines from the start of the window.
3977 * Also set the vertical scroll region for a vertically split window. Always
3978 * the full width of the window, excluding the vertical separator.
3979 */
3980 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003981scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3984 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003986 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3987 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 screen_start(); /* don't know where cursor is now */
3989}
3990
3991/*
3992 * Reset scrolling region to the whole screen.
3993 */
3994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003995scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (*T_CSV != NUL)
3999 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 screen_start(); /* don't know where cursor is now */
4001}
4002
4003
4004/*
4005 * List of terminal codes that are currently recognized.
4006 */
4007
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004008static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 char_u name[2]; /* termcap name of entry */
4011 char_u *code; /* terminal code (in allocated memory) */
4012 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004013 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014} *termcodes = NULL;
4015
4016static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
4017static int tc_len = 0; /* current number of entries in termcodes[] */
4018
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004019static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004022clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
4024 while (tc_len > 0)
4025 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004026 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 tc_max_len = 0;
4028
4029#ifdef HAVE_TGETENT
4030 BC = (char *)empty_option;
4031 UP = (char *)empty_option;
4032 PC = NUL; /* set pad character to NUL */
4033 ospeed = 0;
4034#endif
4035
4036 need_gather = TRUE; /* need to fill termleader[] */
4037}
4038
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004039#define ATC_FROM_TERM 55
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041/*
4042 * Add a new entry to the list of terminal codes.
4043 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004044 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4045 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 */
4047 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004048add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 struct termcode *new_tc;
4051 int i, j;
4052 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004053 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 if (string == NULL || *string == NUL)
4056 {
4057 del_termcode(name);
4058 return;
4059 }
4060
Bram Moolenaar45500912014-07-09 20:51:07 +02004061#if defined(WIN3264) && !defined(FEAT_GUI)
4062 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4063#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (s == NULL)
4067 return;
4068
4069 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004072 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 s[0] = term_7to8bit(string);
4074 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004075
4076#if defined(WIN3264) && !defined(FEAT_GUI)
4077 if (s[0] == K_NUL)
4078 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004079 STRMOVE(s + 1, s);
4080 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004081 }
4082#endif
4083
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004084 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 need_gather = TRUE; /* need to fill termleader[] */
4087
4088 /*
4089 * need to make space for more entries
4090 */
4091 if (tc_len == tc_max_len)
4092 {
4093 tc_max_len += 20;
4094 new_tc = (struct termcode *)alloc(
4095 (unsigned)(tc_max_len * sizeof(struct termcode)));
4096 if (new_tc == NULL)
4097 {
4098 tc_max_len -= 20;
4099 return;
4100 }
4101 for (i = 0; i < tc_len; ++i)
4102 new_tc[i] = termcodes[i];
4103 vim_free(termcodes);
4104 termcodes = new_tc;
4105 }
4106
4107 /*
4108 * Look for existing entry with the same name, it is replaced.
4109 * Look for an existing entry that is alphabetical higher, the new entry
4110 * is inserted in front of it.
4111 */
4112 for (i = 0; i < tc_len; ++i)
4113 {
4114 if (termcodes[i].name[0] < name[0])
4115 continue;
4116 if (termcodes[i].name[0] == name[0])
4117 {
4118 if (termcodes[i].name[1] < name[1])
4119 continue;
4120 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004121 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 */
4123 if (termcodes[i].name[1] == name[1])
4124 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004125 if (flags == ATC_FROM_TERM && (j = termcode_star(
4126 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004128 /* Don't replace ESC[123;*X or ESC O*X with another when
4129 * invoked from got_code_from_term(). */
4130 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004131 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004132 && s[len - 1]
4133 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004134 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004135 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004136 vim_free(s);
4137 return;
4138 }
4139 }
4140 else
4141 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004143 vim_free(termcodes[i].code);
4144 --tc_len;
4145 break;
4146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 }
4149 /*
4150 * Found alphabetical larger entry, move rest to insert new entry
4151 */
4152 for (j = tc_len; j > i; --j)
4153 termcodes[j] = termcodes[j - 1];
4154 break;
4155 }
4156
4157 termcodes[i].name[0] = name[0];
4158 termcodes[i].name[1] = name[1];
4159 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004160 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004161
4162 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4163 * accept modifiers. */
4164 termcodes[i].modlen = 0;
4165 j = termcode_star(s, len);
4166 if (j > 0)
4167 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 ++tc_len;
4169}
4170
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004171/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004172 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004174 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004175 */
4176 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004177termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004178{
4179 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4180 if (len >= 3 && code[len - 2] == '*')
4181 {
4182 if (len >= 5 && code[len - 3] == ';')
4183 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004184 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004185 return 1;
4186 }
4187 return 0;
4188}
4189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004191find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 int i;
4194
4195 for (i = 0; i < tc_len; ++i)
4196 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4197 return termcodes[i].code;
4198 return NULL;
4199}
4200
4201#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4202 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004203get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 if (i >= tc_len)
4206 return NULL;
4207 return &termcodes[i].name[0];
4208}
4209#endif
4210
4211 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004212del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int i;
4215
4216 if (termcodes == NULL) /* nothing there yet */
4217 return;
4218
4219 need_gather = TRUE; /* need to fill termleader[] */
4220
4221 for (i = 0; i < tc_len; ++i)
4222 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4223 {
4224 del_termcode_idx(i);
4225 return;
4226 }
4227 /* not found. Give error message? */
4228}
4229
4230 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004231del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
4233 int i;
4234
4235 vim_free(termcodes[idx].code);
4236 --tc_len;
4237 for (i = idx; i < tc_len; ++i)
4238 termcodes[i] = termcodes[i + 1];
4239}
4240
4241#ifdef FEAT_TERMRESPONSE
4242/*
4243 * Called when detected that the terminal sends 8-bit codes.
4244 * Convert all 7-bit codes to their 8-bit equivalent.
4245 */
4246 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004247switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
4249 int i;
4250 int c;
4251
4252 /* Only need to do something when not already using 8-bit codes. */
4253 if (!term_is_8bit(T_NAME))
4254 {
4255 for (i = 0; i < tc_len; ++i)
4256 {
4257 c = term_7to8bit(termcodes[i].code);
4258 if (c != 0)
4259 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004260 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 termcodes[i].code[0] = c;
4262 }
4263 }
4264 need_gather = TRUE; /* need to fill termleader[] */
4265 }
4266 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004267 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268}
4269#endif
4270
4271#ifdef CHECK_DOUBLE_CLICK
4272static linenr_T orig_topline = 0;
4273# ifdef FEAT_DIFF
4274static int orig_topfill = 0;
4275# endif
4276#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004277#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278/*
4279 * Checking for double clicks ourselves.
4280 * "orig_topline" is used to avoid detecting a double-click when the window
4281 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4282 */
4283/*
4284 * Set orig_topline. Used when jumping to another window, so that a double
4285 * click still works.
4286 */
4287 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004288set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
4290 orig_topline = wp->w_topline;
4291# ifdef FEAT_DIFF
4292 orig_topfill = wp->w_topfill;
4293# endif
4294}
4295#endif
4296
4297/*
4298 * Check if typebuf.tb_buf[] contains a terminal key code.
4299 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4300 * + max_offset].
4301 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004302 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 * With a match, the match is removed, the replacement code is inserted in
4304 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4305 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004306 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4307 * "buflen" is then the length of the string in buf[] and is updated for
4308 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 */
4310 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004311check_termcode(
4312 int max_offset,
4313 char_u *buf,
4314 int bufsize,
4315 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 char_u *tp;
4318 char_u *p;
4319 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004322 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int offset;
4324 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004326 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004327 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int new_slen;
4329 int extra;
4330 char_u string[MAX_KEY_CODE_LEN + 1];
4331 int i, j;
4332 int idx = 0;
4333#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004334# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4335 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 char_u bytes[6];
4337 int num_bytes;
4338# endif
4339 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 int is_click, is_drag;
4341 int wheel_code = 0;
4342 int current_button;
4343 static int held_button = MOUSE_RELEASE;
4344 static int orig_num_clicks = 1;
4345 static int orig_mouse_code = 0x0;
4346# ifdef CHECK_DOUBLE_CLICK
4347 static int orig_mouse_col = 0;
4348 static int orig_mouse_row = 0;
4349 static struct timeval orig_mouse_time = {0, 0};
4350 /* time of previous mouse click */
4351 struct timeval mouse_time; /* time of current mouse click */
4352 long timediff; /* elapsed time in msec */
4353# endif
4354#endif
4355 int cpo_koffset;
4356#ifdef FEAT_MOUSE_GPM
4357 extern int gpm_flag; /* gpm library variable */
4358#endif
4359
4360 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4361
4362 /*
4363 * Speed up the checks for terminal codes by gathering all first bytes
4364 * used in termleader[]. Often this is just a single <Esc>.
4365 */
4366 if (need_gather)
4367 gather_termleader();
4368
4369 /*
4370 * Check at several positions in typebuf.tb_buf[], to catch something like
4371 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4372 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004373 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 * This is used often, KEEP IT FAST!
4375 */
4376 for (offset = 0; offset < max_offset; ++offset)
4377 {
4378 if (buf == NULL)
4379 {
4380 if (offset >= typebuf.tb_len)
4381 break;
4382 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4383 len = typebuf.tb_len - offset; /* length of the input */
4384 }
4385 else
4386 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004387 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 break;
4389 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004390 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392
4393 /*
4394 * Don't check characters after K_SPECIAL, those are already
4395 * translated terminal chars (avoid translating ~@^Hx).
4396 */
4397 if (*tp == K_SPECIAL)
4398 {
4399 offset += 2; /* there are always 2 extra characters */
4400 continue;
4401 }
4402
4403 /*
4404 * Skip this position if the character does not appear as the first
4405 * character in term_strings. This speeds up a lot, since most
4406 * termcodes start with the same character (ESC or CSI).
4407 */
4408 i = *tp;
4409 for (p = termleader; *p && *p != i; ++p)
4410 ;
4411 if (*p == NUL)
4412 continue;
4413
4414 /*
4415 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4416 * are in Insert mode.
4417 */
4418 if (*tp == ESC && !p_ek && (State & INSERT))
4419 continue;
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004422 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425#ifdef FEAT_GUI
4426 if (gui.in_use)
4427 {
4428 /*
4429 * GUI special key codes are all of the form [CSI xx].
4430 */
4431 if (*tp == CSI) /* Special key from GUI */
4432 {
4433 if (len < 3)
4434 return -1; /* Shouldn't happen */
4435 slen = 3;
4436 key_name[0] = tp[1];
4437 key_name[1] = tp[2];
4438 }
4439 }
4440 else
4441#endif /* FEAT_GUI */
4442 {
4443 for (idx = 0; idx < tc_len; ++idx)
4444 {
4445 /*
4446 * Ignore the entry if we are not at the start of
4447 * typebuf.tb_buf[]
4448 * and there are not enough characters to make a match.
4449 * But only when the 'K' flag is in 'cpoptions'.
4450 */
4451 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004452 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (cpo_koffset && offset && len < slen)
4454 continue;
4455 if (STRNCMP(termcodes[idx].code, tp,
4456 (size_t)(slen > len ? len : slen)) == 0)
4457 {
4458 if (len < slen) /* got a partial sequence */
4459 return -1; /* need to get more chars */
4460
4461 /*
4462 * When found a keypad key, check if there is another key
4463 * that matches and use that one. This makes <Home> to be
4464 * found instead of <kHome> when they produce the same
4465 * key code.
4466 */
4467 if (termcodes[idx].name[0] == 'K'
4468 && VIM_ISDIGIT(termcodes[idx].name[1]))
4469 {
4470 for (j = idx + 1; j < tc_len; ++j)
4471 if (termcodes[j].len == slen &&
4472 STRNCMP(termcodes[idx].code,
4473 termcodes[j].code, slen) == 0)
4474 {
4475 idx = j;
4476 break;
4477 }
4478 }
4479
4480 key_name[0] = termcodes[idx].name[0];
4481 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004484
4485 /*
4486 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487 * <Esc>[123;*X (modslen == slen - 3)
4488 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4489 * When there is a modifier the * matches a number.
4490 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004491 */
4492 if (termcodes[idx].modlen > 0)
4493 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004494 modslen = termcodes[idx].modlen;
4495 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004496 continue;
4497 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004499 {
4500 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004501
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004502 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004503 return -1; /* need to get more chars */
4504
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004505 if (tp[modslen] == termcodes[idx].code[slen - 1])
4506 slen = modslen + 1; /* no modifiers */
4507 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004508 continue; /* no match */
4509 else
4510 {
4511 /* Skip over the digits, the final char must
4512 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004513 for (j = slen - 2; j < len && (isdigit(tp[j])
4514 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004515 ;
4516 ++j;
4517 if (len < j) /* got a partial sequence */
4518 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4520 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004521
Bram Moolenaara529ce02017-06-22 22:37:57 +02004522 modifiers_start = tp + slen - 2;
4523
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004524 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004525 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004526 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004527 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004528 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004529 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004530 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004532 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004534
4535 slen = j;
4536 }
4537 key_name[0] = termcodes[idx].name[0];
4538 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004539 break;
4540 }
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 }
4544
4545#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004546 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004547 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004548 * detecting the start of these mouse codes they might as well be
4549 * another key code or terminal response. */
4550# ifdef FEAT_MOUSE_DEC
4551 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004552# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004553# ifdef FEAT_MOUSE_PTERM
4554 || key_name[0] == KS_PTERM_MOUSE
4555# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004556 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004558 /* Check for some responses from the terminal starting with
4559 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004560 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004561 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004562 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004563 * Also eat other possible responses to t_RV, rxvt returns
4564 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4565 * mrxvt has been reported to have "+" in the version. Assume
4566 * the escape sequence ends with a letter or one of "{|}~".
4567 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004568 * - Cursor position report: <Esc>[{row};{col}R
4569 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004570 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004571 *
4572 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004573 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004574 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004575
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004576 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004577 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004578 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004579 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004581 int col = 0;
4582 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004583#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004584 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004585#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004588 for (i = 2 + (tp[0] != CSI); i < len
4589 && !(tp[i] >= '{' && tp[i] <= '~')
4590 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004591 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004592 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004593 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004594#ifdef FEAT_MBYTE
4595 row_char = tp[i - 1];
4596#endif
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004599 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004600 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004601 return -1;
4602 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004603 if (extra > 0)
4604 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004605
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004606#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004607 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4608 * u7_status is not "sent", it may be from a previous Vim that
4609 * just exited. But not for <S-F3>, it sends something
4610 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004611 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004612 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004613 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004614 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004615 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004616
Bram Moolenaarb255b902018-04-24 21:40:10 +02004617 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004618 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004619 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004620 if (col == 2)
4621 aw = "single";
4622 else if (col == 3)
4623 aw = "double";
4624 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4625 {
4626 /* Setting the option causes a screen redraw. Do
4627 * that right away if possible, keeping any
4628 * messages. */
4629 set_option_value((char_u *)"ambw", 0L,
4630 (char_u *)aw, 0);
4631# ifdef DEBUG_TERMRESPONSE
4632 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004633 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004634
Bram Moolenaarb255b902018-04-24 21:40:10 +02004635 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004636 }
4637# else
4638 redraw_asap(CLEAR);
4639# endif
4640 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004641 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004642 key_name[0] = (int)KS_EXTRA;
4643 key_name[1] = (int)KE_IGNORE;
4644 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004645# ifdef FEAT_EVAL
4646 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4647# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004648 }
4649 else
4650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004652 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004654 int version = col;
4655
Bram Moolenaarb255b902018-04-24 21:40:10 +02004656 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004657 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004658 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660 /* If this code starts with CSI, you can bet that the
4661 * terminal uses 8-bit codes. */
4662 if (tp[0] == CSI)
4663 switch_to_8bit();
4664
4665 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004666 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 * Ignore it for when the user has set 'term' to xterm,
4668 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004669 if (version > 20000)
4670 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004672 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004674 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004675# ifdef FEAT_MOUSE_SGR
4676 int is_iterm2 = FALSE;
4677# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004680 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004682 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 check_for_codes = TRUE;
4684 need_gather = TRUE;
4685 req_codes_from_term();
4686 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004687
4688 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004689 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004690 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004691 {
4692 /* If run from Vim $COLORS is set to the number of
4693 * colors the terminal supports. Otherwise assume
4694 * 256, libvterm supports even more. */
4695 if (mch_getenv((char_u *)"COLORS") == NULL)
4696 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004697# ifdef FEAT_MOUSE_SGR
4698 /* Libvterm can handle SGR mouse reporting. */
4699 if (!option_was_set((char_u *)"ttym"))
4700 set_option_value((char_u *)"ttym", 0L,
4701 (char_u *)"sgr", 0);
4702# endif
4703 }
4704
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004705 if (version == 95)
4706 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004707 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004708 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4709 {
4710 is_not_xterm = TRUE;
4711 is_mac_terminal = TRUE;
4712 }
4713# ifdef FEAT_MOUSE_SGR
Bram Moolenaare330ef42018-07-06 23:11:40 +02004714 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004715 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4716 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004717 else
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004718# endif
Bram Moolenaare330ef42018-07-06 23:11:40 +02004719 // old iTerm2 sends 0;95;
4720 if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
4721 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004722 }
4723
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004724 /* Only set 'ttymouse' automatically if it was not set
4725 * by the user already. */
4726 if (!option_was_set((char_u *)"ttym"))
4727 {
4728# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004729 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004730 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004731 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004732 set_option_value((char_u *)"ttym", 0L,
4733 (char_u *)"sgr", 0);
4734 else
4735# endif
4736 /* if xterm version >= 95 use mouse dragging */
4737 if (version >= 95)
4738 set_option_value((char_u *)"ttym", 0L,
4739 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004740 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004741
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004742 /* Detect terminals that set $TERM to something like
4743 * "xterm-256colors" but are not fully xterm
4744 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004745
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004746 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004747 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004748 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004749 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004750 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004751 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004752 is_not_xterm = TRUE;
4753
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004754 /* PuTTY sends 0;136;0
4755 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004756 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004757 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004758 is_not_xterm = TRUE;
4759
4760 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004761 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004762 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004763 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004764
Bram Moolenaar668324e2018-06-30 17:09:26 +02004765 // Xterm first responded to this request at patch level
4766 // 95, so assume anything below 95 is not xterm.
4767 if (version < 95)
4768 is_not_xterm = TRUE;
4769
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004770 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004771 * set. Only supported properly by xterm since version
4772 * 279 (otherwise it returns 0x18).
4773 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004774 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004775 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004776 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004777 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004778 && *T_CSH != NUL
4779 && *T_CRS != NUL)
4780 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004781 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004782 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004783 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004784 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004785 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004786
4787 /* Only request the cursor blink mode if t_RC set. Not
4788 * for Gnome terminal, it can't handle t_RC, it
4789 * echoes the characters to the screen. */
4790 if (rbm_status == STATUS_GET
4791 && !is_not_xterm
4792 && *T_CRC != NUL)
4793 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004794 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004795 out_str(T_CRC);
4796 rbm_status = STATUS_SENT;
4797 need_flush = TRUE;
4798 }
4799
4800 if (need_flush)
4801 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004803 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004805 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 apply_autocmds(EVENT_TERMRESPONSE,
4808 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 key_name[0] = (int)KS_EXTRA;
4810 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004812
Bram Moolenaar4db25542017-08-28 22:43:05 +02004813 /* Check blinking cursor from xterm:
4814 * {lead}?12;1$y set
4815 * {lead}?12;2$y not set
4816 *
4817 * {lead} can be <Esc>[ or CSI
4818 */
4819 else if (rbm_status == STATUS_SENT
4820 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4821 && i == j + 6
4822 && tp[j + 1] == '1'
4823 && tp[j + 2] == '2'
4824 && tp[j + 3] == ';'
4825 && tp[i - 1] == '$'
4826 && tp[i] == 'y')
4827 {
4828 initial_cursor_blink = (tp[j + 4] == '1');
4829 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004830 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004831 key_name[0] = (int)KS_EXTRA;
4832 key_name[1] = (int)KE_IGNORE;
4833 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004834# ifdef FEAT_EVAL
4835 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4836# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004837 }
4838
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004839 /*
4840 * Check for a window position response from the terminal:
4841 * {lead}3;{x}:{y}t
4842 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004843 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004844 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4845 || (len >= 3 && tp[0] == CSI))
4846 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4847 && tp[j + 1] == ';')
4848 {
4849 j += 2;
4850 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4851 ;
4852 if (i < len && tp[i] == ';')
4853 {
4854 winpos_x = atoi((char *)tp + j);
4855 j = i + 1;
4856 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4857 ;
4858 if (i < len && tp[i] == 't')
4859 {
4860 winpos_y = atoi((char *)tp + j);
4861 /* got finished code: consume it */
4862 key_name[0] = (int)KS_EXTRA;
4863 key_name[1] = (int)KE_IGNORE;
4864 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004865
4866 if (--did_request_winpos <= 0)
4867 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004868 }
4869 }
4870 if (i == len)
4871 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004872 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004873 return -1;
4874 }
4875 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004876 }
4877
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004878 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004879 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004880 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004881 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004882 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004883 * {lead} can be <Esc>] or OSC
4884 * {tail} can be '\007', <Esc>\ or STERM.
4885 *
4886 * Consume any code that starts with "{lead}11;", it's also
4887 * possible that "rgba" is following.
4888 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004889 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004890 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4891 || tp[0] == OSC))
4892 {
4893 j = 1 + (tp[0] == ESC);
4894 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004895 || (argp[1] != '1' && argp[1] != '0')
4896 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004897 i = 0; /* no match */
4898 else
4899 for (i = j; i < len; ++i)
4900 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4901 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004902 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004903 int is_bg = argp[1] == '1';
4904
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004905 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004906 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004907 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004908#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004909 int rval = hexhex2nr(tp + j + 7);
4910 int gval = hexhex2nr(tp + j + 12);
4911 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004912#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004913 if (is_bg)
4914 {
4915 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004916 + tp[j+17]) ? "light" : "dark";
4917
Bram Moolenaarb255b902018-04-24 21:40:10 +02004918 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004919 rbg_status = STATUS_GOT;
4920#ifdef FEAT_TERMINAL
4921 bg_r = rval;
4922 bg_g = gval;
4923 bg_b = bval;
4924#endif
4925 if (!option_was_set((char_u *)"bg")
4926 && STRCMP(p_bg, newval) != 0)
4927 {
4928 /* value differs, apply it */
4929 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004930 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004931 reset_option_was_set((char_u *)"bg");
4932 redraw_asap(CLEAR);
4933 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004934 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004935#ifdef FEAT_TERMINAL
4936 else
4937 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004938 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004939 rfg_status = STATUS_GOT;
4940 fg_r = rval;
4941 fg_g = gval;
4942 fg_b = bval;
4943 }
4944#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004945 }
4946
4947 /* got finished code: consume it */
4948 key_name[0] = (int)KS_EXTRA;
4949 key_name[1] = (int)KE_IGNORE;
4950 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004951# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004952 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4953 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004954# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004955 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004956 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004957 if (i == len)
4958 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004959 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004960 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004964 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004965 * {lead}{flag}+r<hex bytes><{tail}
4966 *
4967 * {lead} can be <Esc>P or DCS
4968 * {flag} can be '0' or '1'
4969 * {tail} can be Esc>\ or STERM
4970 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004971 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004972 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004973 *
4974 * {lead} can be <Esc>P or DCS
4975 * {tail} can be Esc>\ or STERM
4976 *
4977 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004978 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004979 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004980 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 || tp[0] == DCS))
4982 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004983 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004984 if (len < j + 3)
4985 i = len; /* need more chars */
4986 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004987 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004988 else if (argp[1] == '+')
4989 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004990 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004991 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004992 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 || tp[i] == STERM)
4994 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004995 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 got_code_from_term(tp + j, i);
4997 key_name[0] = (int)KS_EXTRA;
4998 key_name[1] = (int)KE_IGNORE;
4999 slen = i + 1 + (tp[i] == ESC);
5000 break;
5001 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005002 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005003 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005004 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005005 /* Probably the cursor shape response. Make sure that "i"
5006 * is equal to "len" when there are not sufficient
5007 * characters. */
5008 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005009 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005010 if (i - j == 3 && !isdigit(tp[i]))
5011 break;
5012 if (i - j == 4 && tp[i] != ' ')
5013 break;
5014 if (i - j == 5 && tp[i] != 'q')
5015 break;
5016 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5017 break;
5018 if ((i - j == 6 && tp[i] == STERM)
5019 || (i - j == 7 && tp[i] == '\\'))
5020 {
5021 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005022
Bram Moolenaar590ec872018-03-02 20:58:42 +01005023 /* 0, 1 = block blink, 2 = block
5024 * 3 = underline blink, 4 = underline
5025 * 5 = vertical bar blink, 6 = vertical bar */
5026 number = number == 0 ? 1 : number;
5027 initial_cursor_shape = (number + 1) / 2;
5028 /* The blink flag is actually inverted, compared to
5029 * the value set with T_SH. */
5030 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005031 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01005032 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005033 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005034
Bram Moolenaar590ec872018-03-02 20:58:42 +01005035 key_name[0] = (int)KS_EXTRA;
5036 key_name[1] = (int)KE_IGNORE;
5037 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005038# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005039 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005040# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005041 break;
5042 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005043 }
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
5046 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005047 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005048 /* These codes arrive many together, each code can be
5049 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005050 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005051 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 }
5055#endif
5056
5057 if (key_name[0] == NUL)
5058 continue; /* No match at this position, try next one */
5059
5060 /* We only get here when we have a complete termcode match */
5061
5062#ifdef FEAT_MOUSE
5063# ifdef FEAT_GUI
5064 /*
5065 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5066 * so that we know which window to scroll later.
5067 */
5068 if (gui.in_use
5069 && key_name[0] == (int)KS_EXTRA
5070 && (key_name[1] == (int)KE_X1MOUSE
5071 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005072 || key_name[1] == (int)KE_MOUSELEFT
5073 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 || key_name[1] == (int)KE_MOUSEDOWN
5075 || key_name[1] == (int)KE_MOUSEUP))
5076 {
5077 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5078 if (num_bytes == -1) /* not enough coordinates */
5079 return -1;
5080 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5081 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5082 slen += num_bytes;
5083 }
5084 else
5085# endif
5086 /*
5087 * If it is a mouse click, get the coordinates.
5088 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005089 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005091 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092# endif
5093# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005094 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095# endif
5096# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005097 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098# endif
5099# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005100 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005102# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005103 || key_name[0] == KS_URXVT_MOUSE
5104# endif
5105# ifdef FEAT_MOUSE_SGR
5106 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005107 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 )
5110 {
5111 is_click = is_drag = FALSE;
5112
Bram Moolenaar864207d2008-06-24 22:14:38 +00005113# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5114 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (key_name[0] == (int)KS_MOUSE)
5116 {
5117 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005118 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * s == encoded button state:
5120 * 0x20 = left button down
5121 * 0x21 = middle button down
5122 * 0x22 = right button down
5123 * 0x23 = any button release
5124 * 0x60 = button 4 down (scroll wheel down)
5125 * 0x61 = button 5 down (scroll wheel up)
5126 * add 0x04 for SHIFT
5127 * add 0x08 for ALT
5128 * add 0x10 for CTRL
5129 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005130 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5131 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 * c == column + ' ' + 1 == column + 33
5133 * r == row + ' ' + 1 == row + 33
5134 *
5135 * The coordinates are passed on through global variables.
5136 * Ugly, but this avoids trouble with mouse clicks at an
5137 * unexpected moment and allows for mapping them.
5138 */
5139 for (;;)
5140 {
5141#ifdef FEAT_GUI
5142 if (gui.in_use)
5143 {
5144 /* GUI uses more bits for columns > 223 */
5145 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5146 if (num_bytes == -1) /* not enough coordinates */
5147 return -1;
5148 mouse_code = bytes[0];
5149 mouse_col = 128 * (bytes[1] - ' ' - 1)
5150 + bytes[2] - ' ' - 1;
5151 mouse_row = 128 * (bytes[3] - ' ' - 1)
5152 + bytes[4] - ' ' - 1;
5153 }
5154 else
5155#endif
5156 {
5157 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5158 if (num_bytes == -1) /* not enough coordinates */
5159 return -1;
5160 mouse_code = bytes[0];
5161 mouse_col = bytes[1] - ' ' - 1;
5162 mouse_row = bytes[2] - ' ' - 1;
5163 }
5164 slen += num_bytes;
5165
5166 /* If the following bytes is also a mouse code and it has
5167 * the same code, dump this one and get the next. This
5168 * makes dragging a whole lot faster. */
5169#ifdef FEAT_GUI
5170 if (gui.in_use)
5171 j = 3;
5172 else
5173#endif
5174 j = termcodes[idx].len;
5175 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5176 && tp[slen + j] == mouse_code
5177 && tp[slen + j + 1] != NUL
5178 && tp[slen + j + 2] != NUL
5179#ifdef FEAT_GUI
5180 && (!gui.in_use
5181 || (tp[slen + j + 3] != NUL
5182 && tp[slen + j + 4] != NUL))
5183#endif
5184 )
5185 slen += j;
5186 else
5187 break;
5188 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005191# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5192 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005193 || key_name[0] == KS_SGR_MOUSE
5194 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005195 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005196 /* URXVT 1015 mouse reporting mode:
5197 * Almost identical to xterm mouse mode, except the values
5198 * are decimal instead of bytes.
5199 *
5200 * \033[%d;%d;%dM
5201 * ^-- row
5202 * ^----- column
5203 * ^-------- code
5204 *
5205 * SGR 1006 mouse reporting mode:
5206 * Almost identical to xterm mouse mode, except the values
5207 * are decimal instead of bytes.
5208 *
5209 * \033[<%d;%d;%dM
5210 * ^-- row
5211 * ^----- column
5212 * ^-------- code
5213 *
5214 * \033[<%d;%d;%dm : mouse release event
5215 * ^-- row
5216 * ^----- column
5217 * ^-------- code
5218 */
5219 p = modifiers_start;
5220 if (p == NULL)
5221 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005222
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005223 mouse_code = getdigits(&p);
5224 if (*p++ != ';')
5225 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005226
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005227 /* when mouse reporting is SGR, add 32 to mouse code */
5228 if (key_name[0] == KS_SGR_MOUSE
5229 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5230 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005231
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005232 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5233 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005234
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005235 mouse_col = getdigits(&p) - 1;
5236 if (*p++ != ';')
5237 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005238
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005239 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005240
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005241 /* The modifiers were the mouse coordinates, not the
5242 * modifier keys (alt/shift/ctrl/meta) state. */
5243 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005244 }
5245# endif
5246
5247 if (key_name[0] == (int)KS_MOUSE
5248#ifdef FEAT_MOUSE_URXVT
5249 || key_name[0] == (int)KS_URXVT_MOUSE
5250#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005251#ifdef FEAT_MOUSE_SGR
5252 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005253 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005254#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005255 )
5256 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005257# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 /*
5259 * Handle mouse events.
5260 * Recognize the xterm mouse wheel, but not in the GUI, the
5261 * Linux console with GPM and the MS-DOS or Win32 console
5262 * (multi-clicks use >= 0x60).
5263 */
5264 if (mouse_code >= MOUSEWHEEL_LOW
5265# ifdef FEAT_GUI
5266 && !gui.in_use
5267# endif
5268# ifdef FEAT_MOUSE_GPM
5269 && gpm_flag == 0
5270# endif
5271 )
5272 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005273# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5274 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5275 /* mouse-move event, using MOUSE_DRAG works */
5276 mouse_code = MOUSE_DRAG;
5277 else
5278# endif
5279 /* Keep the mouse_code before it's changed, so that we
5280 * remember that it was a mouse wheel click. */
5281 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283# ifdef FEAT_MOUSE_XTERM
5284 else if (held_button == MOUSE_RELEASE
5285# ifdef FEAT_GUI
5286 && !gui.in_use
5287# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005288 && (mouse_code == 0x23 || mouse_code == 0x24
5289 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005291 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5292 * And 0x40 and 0x41 are used by some xterm emulator. */
5293 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5294 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296# endif
5297
5298# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5299 else if (use_xterm_mouse() > 1)
5300 {
5301 if (mouse_code & MOUSE_DRAG_XTERM)
5302 mouse_code |= MOUSE_DRAG;
5303 }
5304# endif
5305# ifdef FEAT_XCLIPBOARD
5306 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5307 {
5308 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5309 stop_xterm_trace();
5310 else
5311 start_xterm_trace(mouse_code);
5312 }
5313# endif
5314# endif
5315 }
5316# endif /* !UNIX || FEAT_MOUSE_XTERM */
5317# ifdef FEAT_MOUSE_NET
5318 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5319 {
5320 int mc, mr;
5321
5322 /* expect a rather limited sequence like: balancing {
5323 * \033}6,45\r
5324 * '6' is the row, 45 is the column
5325 */
5326 p = tp + slen;
5327 mr = getdigits(&p);
5328 if (*p++ != ',')
5329 return -1;
5330 mc = getdigits(&p);
5331 if (*p++ != '\r')
5332 return -1;
5333
5334 mouse_col = mc - 1;
5335 mouse_row = mr - 1;
5336 mouse_code = MOUSE_LEFT;
5337 slen += (int)(p - (tp + slen));
5338 }
5339# endif /* FEAT_MOUSE_NET */
5340# ifdef FEAT_MOUSE_JSB
5341 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5342 {
5343 int mult, val, iter, button, status;
5344
5345 /* JSBTERM Input Model
5346 * \033[0~zw uniq escape sequence
5347 * (L-x) Left button pressed - not pressed x not reporting
5348 * (M-x) Middle button pressed - not pressed x not reporting
5349 * (R-x) Right button pressed - not pressed x not reporting
5350 * (SDmdu) Single , Double click, m mouse move d button down
5351 * u button up
5352 * ### X cursor position padded to 3 digits
5353 * ### Y cursor position padded to 3 digits
5354 * (s-x) SHIFT key pressed - not pressed x not reporting
5355 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005356 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 */
5358
5359 p = tp + slen;
5360 button = mouse_code = 0;
5361 switch (*p++)
5362 {
5363 case 'L': button = 1; break;
5364 case '-': break;
5365 case 'x': break; /* ignore sequence */
5366 default: return -1; /* Unknown Result */
5367 }
5368 switch (*p++)
5369 {
5370 case 'M': button |= 2; break;
5371 case '-': break;
5372 case 'x': break; /* ignore sequence */
5373 default: return -1; /* Unknown Result */
5374 }
5375 switch (*p++)
5376 {
5377 case 'R': button |= 4; break;
5378 case '-': break;
5379 case 'x': break; /* ignore sequence */
5380 default: return -1; /* Unknown Result */
5381 }
5382 status = *p++;
5383 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5384 mult /= 10, p++)
5385 if (*p >= '0' && *p <= '9')
5386 val += (*p - '0') * mult;
5387 else
5388 return -1;
5389 mouse_col = val;
5390 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5391 mult /= 10, p++)
5392 if (*p >= '0' && *p <= '9')
5393 val += (*p - '0') * mult;
5394 else
5395 return -1;
5396 mouse_row = val;
5397 switch (*p++)
5398 {
5399 case 's': button |= 8; break; /* SHIFT key Pressed */
5400 case '-': break; /* Not Pressed */
5401 case 'x': break; /* Not Reporting */
5402 default: return -1; /* Unknown Result */
5403 }
5404 switch (*p++)
5405 {
5406 case 'c': button |= 16; break; /* CTRL key Pressed */
5407 case '-': break; /* Not Pressed */
5408 case 'x': break; /* Not Reporting */
5409 default: return -1; /* Unknown Result */
5410 }
5411 if (*p++ != '\033')
5412 return -1;
5413 if (*p++ != '\\')
5414 return -1;
5415 switch (status)
5416 {
5417 case 'D': /* Double Click */
5418 case 'S': /* Single Click */
5419 if (button & 1) mouse_code |= MOUSE_LEFT;
5420 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5421 if (button & 4) mouse_code |= MOUSE_RIGHT;
5422 if (button & 8) mouse_code |= MOUSE_SHIFT;
5423 if (button & 16) mouse_code |= MOUSE_CTRL;
5424 break;
5425 case 'm': /* Mouse move */
5426 if (button & 1) mouse_code |= MOUSE_LEFT;
5427 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5428 if (button & 4) mouse_code |= MOUSE_RIGHT;
5429 if (button & 8) mouse_code |= MOUSE_SHIFT;
5430 if (button & 16) mouse_code |= MOUSE_CTRL;
5431 if ((button & 7) != 0)
5432 {
5433 held_button = mouse_code;
5434 mouse_code |= MOUSE_DRAG;
5435 }
5436 is_drag = TRUE;
5437 showmode();
5438 break;
5439 case 'd': /* Button Down */
5440 if (button & 1) mouse_code |= MOUSE_LEFT;
5441 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5442 if (button & 4) mouse_code |= MOUSE_RIGHT;
5443 if (button & 8) mouse_code |= MOUSE_SHIFT;
5444 if (button & 16) mouse_code |= MOUSE_CTRL;
5445 break;
5446 case 'u': /* Button Up */
5447 if (button & 1)
5448 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5449 if (button & 2)
5450 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5451 if (button & 4)
5452 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5453 if (button & 8)
5454 mouse_code |= MOUSE_SHIFT;
5455 if (button & 16)
5456 mouse_code |= MOUSE_CTRL;
5457 break;
5458 default: return -1; /* Unknown Result */
5459 }
5460
5461 slen += (p - (tp + slen));
5462 }
5463# endif /* FEAT_MOUSE_JSB */
5464# ifdef FEAT_MOUSE_DEC
5465 if (key_name[0] == (int)KS_DEC_MOUSE)
5466 {
5467 /* The DEC Locator Input Model
5468 * Netterm delivers the code sequence:
5469 * \033[2;4;24;80&w (left button down)
5470 * \033[3;0;24;80&w (left button up)
5471 * \033[6;1;24;80&w (right button down)
5472 * \033[7;0;24;80&w (right button up)
5473 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5474 * Pe is the event code
5475 * Pb is the button code
5476 * Pr is the row coordinate
5477 * Pc is the column coordinate
5478 * Pp is the third coordinate (page number)
5479 * Pe, the event code indicates what event caused this report
5480 * The following event codes are defined:
5481 * 0 - request, the terminal received an explicit request
5482 * for a locator report, but the locator is unavailable
5483 * 1 - request, the terminal received an explicit request
5484 * for a locator report
5485 * 2 - left button down
5486 * 3 - left button up
5487 * 4 - middle button down
5488 * 5 - middle button up
5489 * 6 - right button down
5490 * 7 - right button up
5491 * 8 - fourth button down
5492 * 9 - fourth button up
5493 * 10 - locator outside filter rectangle
5494 * Pb, the button code, ASCII decimal 0-15 indicating which
5495 * buttons are down if any. The state of the four buttons
5496 * on the locator correspond to the low four bits of the
5497 * decimal value,
5498 * "1" means button depressed
5499 * 0 - no buttons down,
5500 * 1 - right,
5501 * 2 - middle,
5502 * 4 - left,
5503 * 8 - fourth
5504 * Pr is the row coordinate of the locator position in the page,
5505 * encoded as an ASCII decimal value.
5506 * If Pr is omitted, the locator position is undefined
5507 * (outside the terminal window for example).
5508 * Pc is the column coordinate of the locator position in the
5509 * page, encoded as an ASCII decimal value.
5510 * If Pc is omitted, the locator position is undefined
5511 * (outside the terminal window for example).
5512 * Pp is the page coordinate of the locator position
5513 * encoded as an ASCII decimal value.
5514 * The page coordinate may be omitted if the locator is on
5515 * page one (the default). We ignore it anyway.
5516 */
5517 int Pe, Pb, Pr, Pc;
5518
5519 p = tp + slen;
5520
5521 /* get event status */
5522 Pe = getdigits(&p);
5523 if (*p++ != ';')
5524 return -1;
5525
5526 /* get button status */
5527 Pb = getdigits(&p);
5528 if (*p++ != ';')
5529 return -1;
5530
5531 /* get row status */
5532 Pr = getdigits(&p);
5533 if (*p++ != ';')
5534 return -1;
5535
5536 /* get column status */
5537 Pc = getdigits(&p);
5538
5539 /* the page parameter is optional */
5540 if (*p == ';')
5541 {
5542 p++;
5543 (void)getdigits(&p);
5544 }
5545 if (*p++ != '&')
5546 return -1;
5547 if (*p++ != 'w')
5548 return -1;
5549
5550 mouse_code = 0;
5551 switch (Pe)
5552 {
5553 case 0: return -1; /* position request while unavailable */
5554 case 1: /* a response to a locator position request includes
5555 the status of all buttons */
5556 Pb &= 7; /* mask off and ignore fourth button */
5557 if (Pb & 4)
5558 mouse_code = MOUSE_LEFT;
5559 if (Pb & 2)
5560 mouse_code = MOUSE_MIDDLE;
5561 if (Pb & 1)
5562 mouse_code = MOUSE_RIGHT;
5563 if (Pb)
5564 {
5565 held_button = mouse_code;
5566 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005567 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 is_drag = TRUE;
5570 showmode();
5571 break;
5572 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005573 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 break;
5575 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5576 break;
5577 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005578 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 break;
5580 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5581 break;
5582 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005583 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 break;
5585 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5586 break;
5587 case 8: return -1; /* fourth button down */
5588 case 9: return -1; /* fourth button up */
5589 case 10: return -1; /* mouse outside of filter rectangle */
5590 default: return -1; /* should never occur */
5591 }
5592
5593 mouse_col = Pc - 1;
5594 mouse_row = Pr - 1;
5595
5596 slen += (int)(p - (tp + slen));
5597 }
5598# endif /* FEAT_MOUSE_DEC */
5599# ifdef FEAT_MOUSE_PTERM
5600 if (key_name[0] == (int)KS_PTERM_MOUSE)
5601 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005602 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 p = tp + slen;
5605
5606 action = getdigits(&p);
5607 if (*p++ != ';')
5608 return -1;
5609
5610 mouse_row = getdigits(&p);
5611 if (*p++ != ';')
5612 return -1;
5613 mouse_col = getdigits(&p);
5614 if (*p++ != ';')
5615 return -1;
5616
5617 button = getdigits(&p);
5618 mouse_code = 0;
5619
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005620 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 case 4: mouse_code = MOUSE_LEFT; break;
5623 case 1: mouse_code = MOUSE_RIGHT; break;
5624 case 2: mouse_code = MOUSE_MIDDLE; break;
5625 default: return -1;
5626 }
5627
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005628 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
5630 case 31: /* Initial press */
5631 if (*p++ != ';')
5632 return -1;
5633
5634 num_clicks = getdigits(&p); /* Not used */
5635 break;
5636
5637 case 32: /* Release */
5638 mouse_code |= MOUSE_RELEASE;
5639 break;
5640
5641 case 33: /* Drag */
5642 held_button = mouse_code;
5643 mouse_code |= MOUSE_DRAG;
5644 break;
5645
5646 default:
5647 return -1;
5648 }
5649
5650 if (*p++ != 't')
5651 return -1;
5652
5653 slen += (p - (tp + slen));
5654 }
5655# endif /* FEAT_MOUSE_PTERM */
5656
5657 /* Interpret the mouse code */
5658 current_button = (mouse_code & MOUSE_CLICK_MASK);
5659 if (current_button == MOUSE_RELEASE
5660# ifdef FEAT_MOUSE_XTERM
5661 && wheel_code == 0
5662# endif
5663 )
5664 {
5665 /*
5666 * If we get a mouse drag or release event when
5667 * there is no mouse button held down (held_button ==
5668 * MOUSE_RELEASE), produce a K_IGNORE below.
5669 * (can happen when you hold down two buttons
5670 * and then let them go, or click in the menu bar, but not
5671 * on a menu, and drag into the text).
5672 */
5673 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5674 is_drag = TRUE;
5675 current_button = held_button;
5676 }
5677 else if (wheel_code == 0)
5678 {
5679# ifdef CHECK_DOUBLE_CLICK
5680# ifdef FEAT_MOUSE_GPM
5681# ifdef FEAT_GUI
5682 /*
5683 * Only for Unix, when GUI or gpm is not active, we handle
5684 * multi-clicks here.
5685 */
5686 if (gpm_flag == 0 && !gui.in_use)
5687# else
5688 if (gpm_flag == 0)
5689# endif
5690# else
5691# ifdef FEAT_GUI
5692 if (!gui.in_use)
5693# endif
5694# endif
5695 {
5696 /*
5697 * Compute the time elapsed since the previous mouse click.
5698 */
5699 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005700 if (orig_mouse_time.tv_sec == 0)
5701 {
5702 /*
5703 * Avoid computing the difference between mouse_time
5704 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005705 * difference would be huge and would cause
5706 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005707 */
5708 timediff = p_mouset;
5709 }
5710 else
5711 {
5712 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005713 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005714 if (timediff < 0)
5715 --orig_mouse_time.tv_sec;
5716 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005717 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 orig_mouse_time = mouse_time;
5720 if (mouse_code == orig_mouse_code
5721 && timediff < p_mouset
5722 && orig_num_clicks != 4
5723 && orig_mouse_col == mouse_col
5724 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005725 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005727 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005729 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005730 /* Double click in tab pages line also works
5731 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005732 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005733 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 ++orig_num_clicks;
5735 else
5736 orig_num_clicks = 1;
5737 orig_mouse_col = mouse_col;
5738 orig_mouse_row = mouse_row;
5739 orig_topline = curwin->w_topline;
5740#ifdef FEAT_DIFF
5741 orig_topfill = curwin->w_topfill;
5742#endif
5743 }
5744# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5745 else
5746 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5747# endif
5748# else
5749 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5750# endif
5751 is_click = TRUE;
5752 orig_mouse_code = mouse_code;
5753 }
5754 if (!is_drag)
5755 held_button = mouse_code & MOUSE_CLICK_MASK;
5756
5757 /*
5758 * Translate the actual mouse event into a pseudo mouse event.
5759 * First work out what modifiers are to be used.
5760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (orig_mouse_code & MOUSE_SHIFT)
5762 modifiers |= MOD_MASK_SHIFT;
5763 if (orig_mouse_code & MOUSE_CTRL)
5764 modifiers |= MOD_MASK_CTRL;
5765 if (orig_mouse_code & MOUSE_ALT)
5766 modifiers |= MOD_MASK_ALT;
5767 if (orig_num_clicks == 2)
5768 modifiers |= MOD_MASK_2CLICK;
5769 else if (orig_num_clicks == 3)
5770 modifiers |= MOD_MASK_3CLICK;
5771 else if (orig_num_clicks == 4)
5772 modifiers |= MOD_MASK_4CLICK;
5773
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005774 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5775 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005777 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005778 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005779 {
5780 if (wheel_code & MOUSE_CTRL)
5781 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005782 if (wheel_code & MOUSE_ALT)
5783 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 key_name[1] = (wheel_code & 1)
5785 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005786 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
5789 key_name[1] = get_pseudo_mouse_code(current_button,
5790 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005791
5792 /* Make sure the mouse position is valid. Some terminals may
5793 * return weird values. */
5794 if (mouse_col >= Columns)
5795 mouse_col = Columns - 1;
5796 if (mouse_row >= Rows)
5797 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799#endif /* FEAT_MOUSE */
5800
5801#ifdef FEAT_GUI
5802 /*
5803 * If using the GUI, then we get menu and scrollbar events.
5804 *
5805 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5806 * four bytes which are to be taken as a pointer to the vimmenu_T
5807 * structure.
5808 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005809 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005810 * is one byte with the tab index.
5811 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5813 * by one byte representing the scrollbar number, and then four bytes
5814 * representing a long_u which is the new value of the scrollbar.
5815 *
5816 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5817 * KE_FILLER followed by four bytes representing a long_u which is the
5818 * new value of the scrollbar.
5819 */
5820# ifdef FEAT_MENU
5821 else if (key_name[0] == (int)KS_MENU)
5822 {
5823 long_u val;
5824
5825 num_bytes = get_long_from_buf(tp + slen, &val);
5826 if (num_bytes == -1)
5827 return -1;
5828 current_menu = (vimmenu_T *)val;
5829 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005830
5831 /* The menu may have been deleted right after it was used, check
5832 * for that. */
5833 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5834 {
5835 key_name[0] = KS_EXTRA;
5836 key_name[1] = (int)KE_IGNORE;
5837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005840# ifdef FEAT_GUI_TABLINE
5841 else if (key_name[0] == (int)KS_TABLINE)
5842 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005843 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005844 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5845 if (num_bytes == -1)
5846 return -1;
5847 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005848 if (current_tab == 255) /* -1 in a byte gives 255 */
5849 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005850 slen += num_bytes;
5851 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005852 else if (key_name[0] == (int)KS_TABMENU)
5853 {
5854 /* Selecting tabline tab or using its menu. */
5855 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5856 if (num_bytes == -1)
5857 return -1;
5858 current_tab = (int)bytes[0];
5859 current_tabmenu = (int)bytes[1];
5860 slen += num_bytes;
5861 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863# ifndef USE_ON_FLY_SCROLL
5864 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5865 {
5866 long_u val;
5867
5868 /* Get the last scrollbar event in the queue of the same type */
5869 j = 0;
5870 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5871 && tp[j + 2] != NUL; ++i)
5872 {
5873 j += 3;
5874 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5875 if (num_bytes == -1)
5876 break;
5877 if (i == 0)
5878 current_scrollbar = (int)bytes[0];
5879 else if (current_scrollbar != (int)bytes[0])
5880 break;
5881 j += num_bytes;
5882 num_bytes = get_long_from_buf(tp + j, &val);
5883 if (num_bytes == -1)
5884 break;
5885 scrollbar_value = val;
5886 j += num_bytes;
5887 slen = j;
5888 }
5889 if (i == 0) /* not enough characters to make one */
5890 return -1;
5891 }
5892 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5893 {
5894 long_u val;
5895
5896 /* Get the last horiz. scrollbar event in the queue */
5897 j = 0;
5898 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5899 && tp[j + 2] != NUL; ++i)
5900 {
5901 j += 3;
5902 num_bytes = get_long_from_buf(tp + j, &val);
5903 if (num_bytes == -1)
5904 break;
5905 scrollbar_value = val;
5906 j += num_bytes;
5907 slen = j;
5908 }
5909 if (i == 0) /* not enough characters to make one */
5910 return -1;
5911 }
5912# endif /* !USE_ON_FLY_SCROLL */
5913#endif /* FEAT_GUI */
5914
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005915 /*
5916 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5917 */
5918 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5919
5920 /*
5921 * Add any modifier codes to our string.
5922 */
5923 new_slen = 0; /* Length of what will replace the termcode */
5924 if (modifiers != 0)
5925 {
5926 /* Some keys have the modifier included. Need to handle that here
5927 * to make mappings work. */
5928 key = simplify_key(key, &modifiers);
5929 if (modifiers != 0)
5930 {
5931 string[new_slen++] = K_SPECIAL;
5932 string[new_slen++] = (int)KS_MODIFIER;
5933 string[new_slen++] = modifiers;
5934 }
5935 }
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005938 key_name[0] = KEY2TERMCAP0(key);
5939 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005941 {
5942 /* from ":set <M-b>=xx" */
5943#ifdef FEAT_MBYTE
5944 if (has_mbyte)
5945 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5946 else
5947#endif
5948 string[new_slen++] = key_name[1];
5949 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005950 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5951 && key_name[1] == KE_IGNORE)
5952 {
5953 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5954 * to indicate what happened. */
5955 retval = KEYLEN_REMOVED;
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 else
5958 {
5959 string[new_slen++] = K_SPECIAL;
5960 string[new_slen++] = key_name[0];
5961 string[new_slen++] = key_name[1];
5962 }
5963 string[new_slen] = NUL;
5964 extra = new_slen - slen;
5965 if (buf == NULL)
5966 {
5967 if (extra < 0)
5968 /* remove matched chars, taking care of noremap */
5969 del_typebuf(-extra, offset);
5970 else if (extra > 0)
5971 /* insert the extra space we need */
5972 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5973
5974 /*
5975 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5976 * typebuf.tb_buf[]!
5977 */
5978 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5979 (size_t)new_slen);
5980 }
5981 else
5982 {
5983 if (extra < 0)
5984 /* remove matched characters */
5985 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005986 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005988 {
5989 /* Insert the extra space we need. If there is insufficient
5990 * space return -1. */
5991 if (*buflen + extra + new_slen >= bufsize)
5992 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005994 (size_t)(*buflen - offset));
5995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005997 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005999 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001
Bram Moolenaar2951b772013-07-03 12:45:31 +02006002#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006003 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006004#endif
6005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 return 0; /* no match found */
6007}
6008
Bram Moolenaar9377df32017-10-15 13:22:01 +02006009#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006010/*
6011 * Get the text foreground color, if known.
6012 */
6013 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006014term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006015{
6016 if (rfg_status == STATUS_GOT)
6017 {
6018 *r = fg_r;
6019 *g = fg_g;
6020 *b = fg_b;
6021 }
6022}
6023
6024/*
6025 * Get the text background color, if known.
6026 */
6027 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006028term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006029{
6030 if (rbg_status == STATUS_GOT)
6031 {
6032 *r = bg_r;
6033 *g = bg_g;
6034 *b = bg_b;
6035 }
6036}
6037#endif
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039/*
6040 * Replace any terminal code strings in from[] with the equivalent internal
6041 * vim representation. This is used for the "from" and "to" part of a
6042 * mapping, and the "to" part of a menu command.
6043 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6044 * '<'.
6045 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6046 *
6047 * The replacement is done in result[] and finally copied into allocated
6048 * memory. If this all works well *bufp is set to the allocated memory and a
6049 * pointer to it is returned. If something fails *bufp is set to NULL and from
6050 * is returned.
6051 *
6052 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6053 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6054 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6055 * instead of a CTRL-V.
6056 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006057 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006058replace_termcodes(
6059 char_u *from,
6060 char_u **bufp,
6061 int from_part,
6062 int do_lt, /* also translate <lt> */
6063 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 int i;
6066 int slen;
6067 int key;
6068 int dlen = 0;
6069 char_u *src;
6070 int do_backslash; /* backslash is a special character */
6071 int do_special; /* recognize <> key codes */
6072 int do_key_code; /* recognize raw key codes */
6073 char_u *result; /* buffer for resulting string */
6074
6075 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006076 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6078
6079 /*
6080 * Allocate space for the translation. Worst case a single character is
6081 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6082 */
6083 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6084 if (result == NULL) /* out of memory */
6085 {
6086 *bufp = NULL;
6087 return from;
6088 }
6089
6090 src = from;
6091
6092 /*
6093 * Check for #n at start only: function key n
6094 */
6095 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6096 {
6097 result[dlen++] = K_SPECIAL;
6098 result[dlen++] = 'k';
6099 if (src[1] == '0')
6100 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6101 else
6102 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6103 src += 2;
6104 }
6105
6106 /*
6107 * Copy each byte from *from to result[dlen]
6108 */
6109 while (*src != NUL)
6110 {
6111 /*
6112 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006113 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 */
6115 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6116 {
6117#ifdef FEAT_EVAL
6118 /*
6119 * Replace <SID> by K_SNR <script-nr> _.
6120 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6121 */
6122 if (STRNICMP(src, "<SID>", 5) == 0)
6123 {
6124 if (current_SID <= 0)
6125 EMSG(_(e_usingsid));
6126 else
6127 {
6128 src += 5;
6129 result[dlen++] = K_SPECIAL;
6130 result[dlen++] = (int)KS_EXTRA;
6131 result[dlen++] = (int)KE_SNR;
6132 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6133 dlen += (int)STRLEN(result + dlen);
6134 result[dlen++] = '_';
6135 continue;
6136 }
6137 }
6138#endif
6139
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006140 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (slen)
6142 {
6143 dlen += slen;
6144 continue;
6145 }
6146 }
6147
6148 /*
6149 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6150 * Note that this is also checked after replacing the <> form.
6151 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6152 * it could be a character in the file.
6153 */
6154 if (do_key_code)
6155 {
6156 i = find_term_bykeys(src);
6157 if (i >= 0)
6158 {
6159 result[dlen++] = K_SPECIAL;
6160 result[dlen++] = termcodes[i].name[0];
6161 result[dlen++] = termcodes[i].name[1];
6162 src += termcodes[i].len;
6163 /* If terminal code matched, continue after it. */
6164 continue;
6165 }
6166 }
6167
6168#ifdef FEAT_EVAL
6169 if (do_special)
6170 {
6171 char_u *p, *s, len;
6172
6173 /*
6174 * Replace <Leader> by the value of "mapleader".
6175 * Replace <LocalLeader> by the value of "maplocalleader".
6176 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6177 */
6178 if (STRNICMP(src, "<Leader>", 8) == 0)
6179 {
6180 len = 8;
6181 p = get_var_value((char_u *)"g:mapleader");
6182 }
6183 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6184 {
6185 len = 13;
6186 p = get_var_value((char_u *)"g:maplocalleader");
6187 }
6188 else
6189 {
6190 len = 0;
6191 p = NULL;
6192 }
6193 if (len != 0)
6194 {
6195 /* Allow up to 8 * 6 characters for "mapleader". */
6196 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6197 s = (char_u *)"\\";
6198 else
6199 s = p;
6200 while (*s != NUL)
6201 result[dlen++] = *s++;
6202 src += len;
6203 continue;
6204 }
6205 }
6206#endif
6207
6208 /*
6209 * Remove CTRL-V and ignore the next character.
6210 * For "from" side the CTRL-V at the end is included, for the "to"
6211 * part it is removed.
6212 * If 'cpoptions' does not contain 'B', also accept a backslash.
6213 */
6214 key = *src;
6215 if (key == Ctrl_V || (do_backslash && key == '\\'))
6216 {
6217 ++src; /* skip CTRL-V or backslash */
6218 if (*src == NUL)
6219 {
6220 if (from_part)
6221 result[dlen++] = key;
6222 break;
6223 }
6224 }
6225
6226#ifdef FEAT_MBYTE
6227 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006228 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229#endif
6230 {
6231 /*
6232 * If the character is K_SPECIAL, replace it with K_SPECIAL
6233 * KS_SPECIAL KE_FILLER.
6234 * If compiled with the GUI replace CSI with K_CSI.
6235 */
6236 if (*src == K_SPECIAL)
6237 {
6238 result[dlen++] = K_SPECIAL;
6239 result[dlen++] = KS_SPECIAL;
6240 result[dlen++] = KE_FILLER;
6241 }
6242# ifdef FEAT_GUI
6243 else if (*src == CSI)
6244 {
6245 result[dlen++] = K_SPECIAL;
6246 result[dlen++] = KS_EXTRA;
6247 result[dlen++] = (int)KE_CSI;
6248 }
6249# endif
6250 else
6251 result[dlen++] = *src;
6252 ++src;
6253 }
6254 }
6255 result[dlen] = NUL;
6256
6257 /*
6258 * Copy the new string to allocated memory.
6259 * If this fails, just return from.
6260 */
6261 if ((*bufp = vim_strsave(result)) != NULL)
6262 from = *bufp;
6263 vim_free(result);
6264 return from;
6265}
6266
6267/*
6268 * Find a termcode with keys 'src' (must be NUL terminated).
6269 * Return the index in termcodes[], or -1 if not found.
6270 */
6271 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006272find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006275 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 for (i = 0; i < tc_len; ++i)
6278 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006279 if (slen == termcodes[i].len
6280 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 return i;
6282 }
6283 return -1;
6284}
6285
6286/*
6287 * Gather the first characters in the terminal key codes into a string.
6288 * Used to speed up check_termcode().
6289 */
6290 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006291gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 int i;
6294 int len = 0;
6295
6296#ifdef FEAT_GUI
6297 if (gui.in_use)
6298 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6299#endif
6300#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006301 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 termleader[len++] = DCS; /* the termcode response starts with DCS
6303 in 8-bit mode */
6304#endif
6305 termleader[len] = NUL;
6306
6307 for (i = 0; i < tc_len; ++i)
6308 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6309 {
6310 termleader[len++] = termcodes[i].code[0];
6311 termleader[len] = NUL;
6312 }
6313
6314 need_gather = FALSE;
6315}
6316
6317/*
6318 * Show all termcodes (for ":set termcap")
6319 * This code looks a lot like showoptions(), but is different.
6320 */
6321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006322show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 int col;
6325 int *items;
6326 int item_count;
6327 int run;
6328 int row, rows;
6329 int cols;
6330 int i;
6331 int len;
6332
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006333#define INC3 27 /* try to make three columns */
6334#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335#define GAP 2 /* spaces between columns */
6336
6337 if (tc_len == 0) /* no terminal codes (must be GUI) */
6338 return;
6339 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6340 if (items == NULL)
6341 return;
6342
6343 /* Highlight title */
6344 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6345
6346 /*
6347 * do the loop two times:
6348 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006349 * 2. display the medium items (medium length strings)
6350 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006352 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 /*
6355 * collect the items in items[]
6356 */
6357 item_count = 0;
6358 for (i = 0; i < tc_len; i++)
6359 {
6360 len = show_one_termcode(termcodes[i].name,
6361 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006362 if (len <= INC3 - GAP ? run == 1
6363 : len <= INC2 - GAP ? run == 2
6364 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 items[item_count++] = i;
6366 }
6367
6368 /*
6369 * display the items
6370 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006371 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006373 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (cols == 0)
6375 cols = 1;
6376 rows = (item_count + cols - 1) / cols;
6377 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006378 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 rows = item_count;
6380 for (row = 0; row < rows && !got_int; ++row)
6381 {
6382 msg_putchar('\n'); /* go to next line */
6383 if (got_int) /* 'q' typed in more */
6384 break;
6385 col = 0;
6386 for (i = row; i < item_count; i += rows)
6387 {
6388 msg_col = col; /* make columns */
6389 show_one_termcode(termcodes[items[i]].name,
6390 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006391 if (run == 2)
6392 col += INC2;
6393 else
6394 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396 out_flush();
6397 ui_breakcheck();
6398 }
6399 }
6400 vim_free(items);
6401}
6402
6403/*
6404 * Show one termcode entry.
6405 * Output goes into IObuff[]
6406 */
6407 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006408show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 char_u *p;
6411 int len;
6412
6413 if (name[0] > '~')
6414 {
6415 IObuff[0] = ' ';
6416 IObuff[1] = ' ';
6417 IObuff[2] = ' ';
6418 IObuff[3] = ' ';
6419 }
6420 else
6421 {
6422 IObuff[0] = 't';
6423 IObuff[1] = '_';
6424 IObuff[2] = name[0];
6425 IObuff[3] = name[1];
6426 }
6427 IObuff[4] = ' ';
6428
6429 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6430 if (p[1] != 't')
6431 STRCPY(IObuff + 5, p);
6432 else
6433 IObuff[5] = NUL;
6434 len = (int)STRLEN(IObuff);
6435 do
6436 IObuff[len++] = ' ';
6437 while (len < 17);
6438 IObuff[len] = NUL;
6439 if (code == NULL)
6440 len += 4;
6441 else
6442 len += vim_strsize(code);
6443
6444 if (printit)
6445 {
6446 msg_puts(IObuff);
6447 if (code == NULL)
6448 msg_puts((char_u *)"NULL");
6449 else
6450 msg_outtrans(code);
6451 }
6452 return len;
6453}
6454
6455#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6456/*
6457 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6458 * termcap codes from the terminal itself.
6459 * We get them one by one to avoid a very long response string.
6460 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006461static int xt_index_in = 0;
6462static int xt_index_out = 0;
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006465req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 xt_index_out = 0;
6468 xt_index_in = 0;
6469 req_more_codes_from_term();
6470}
6471
6472 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006473req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 char buf[11];
6476 int old_idx = xt_index_out;
6477
6478 /* Don't do anything when going to exit. */
6479 if (exiting)
6480 return;
6481
6482 /* Send up to 10 more requests out than we received. Avoid sending too
6483 * many, there can be a buffer overflow somewhere. */
6484 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6485 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006486 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006487
Bram Moolenaarb255b902018-04-24 21:40:10 +02006488 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6489 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 out_str_nf((char_u *)buf);
6491 ++xt_index_out;
6492 }
6493
6494 /* Send the codes out right away. */
6495 if (xt_index_out != old_idx)
6496 out_flush();
6497}
6498
6499/*
6500 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6501 * A "0" instead of the "1" indicates a code that isn't supported.
6502 * Both <name> and <string> are encoded in hex.
6503 * "code" points to the "0" or "1".
6504 */
6505 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006506got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508#define XT_LEN 100
6509 char_u name[3];
6510 char_u str[XT_LEN];
6511 int i;
6512 int j = 0;
6513 int c;
6514
6515 /* A '1' means the code is supported, a '0' means it isn't.
6516 * When half the length is > XT_LEN we can't use it.
6517 * Our names are currently all 2 characters. */
6518 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6519 {
6520 /* Get the name from the response and find it in the table. */
6521 name[0] = hexhex2nr(code + 3);
6522 name[1] = hexhex2nr(code + 5);
6523 name[2] = NUL;
6524 for (i = 0; key_names[i] != NULL; ++i)
6525 {
6526 if (STRCMP(key_names[i], name) == 0)
6527 {
6528 xt_index_in = i;
6529 break;
6530 }
6531 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006532
Bram Moolenaarb255b902018-04-24 21:40:10 +02006533 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (key_names[i] != NULL)
6536 {
6537 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6538 str[j++] = c;
6539 str[j] = NUL;
6540 if (name[0] == 'C' && name[1] == 'o')
6541 {
6542 /* Color count is not a key code. */
6543 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006544 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546 else
6547 {
6548 /* First delete any existing entry with the same code. */
6549 i = find_term_bykeys(str);
6550 if (i >= 0)
6551 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006552 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
6554 }
6555 }
6556
6557 /* May request more codes now that we received one. */
6558 ++xt_index_in;
6559 req_more_codes_from_term();
6560}
6561
6562/*
6563 * Check if there are any unanswered requests and deal with them.
6564 * This is called before starting an external program or getting direct
6565 * keyboard input. We don't want responses to be send to that program or
6566 * handled as typed text.
6567 */
6568 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006569check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 int c;
6572
6573 /* If no codes requested or all are answered, no need to wait. */
6574 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6575 return;
6576
6577 /* Vgetc() will check for and handle any response.
6578 * Keep calling vpeekc() until we don't get any responses. */
6579 ++no_mapping;
6580 ++allow_keys;
6581 for (;;)
6582 {
6583 c = vpeekc();
6584 if (c == NUL) /* nothing available */
6585 break;
6586
6587 /* If a response is recognized it's replaced with K_IGNORE, must read
6588 * it from the input stream. If there is no K_IGNORE we can't do
6589 * anything, break here (there might be some responses further on, but
6590 * we don't want to throw away any typed chars). */
6591 if (c != K_SPECIAL && c != K_IGNORE)
6592 break;
6593 c = vgetc();
6594 if (c != K_IGNORE)
6595 {
6596 vungetc(c);
6597 break;
6598 }
6599 }
6600 --no_mapping;
6601 --allow_keys;
6602}
6603#endif
6604
6605#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6606/*
6607 * Translate an internal mapping/abbreviation representation into the
6608 * corresponding external one recognized by :map/:abbrev commands;
6609 * respects the current B/k/< settings of 'cpoption'.
6610 *
6611 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006612 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 *
6614 * It uses a growarray to build the translation string since the
6615 * latter can be wider than the original description. The caller has to
6616 * free the string afterwards.
6617 *
6618 * Returns NULL when there is a problem.
6619 */
6620 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006621translate_mapping(
6622 char_u *str,
6623 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 garray_T ga;
6626 int c;
6627 int modifiers;
6628 int cpo_bslash;
6629 int cpo_special;
6630 int cpo_keycode;
6631
6632 ga_init(&ga);
6633 ga.ga_itemsize = 1;
6634 ga.ga_growsize = 40;
6635
6636 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6637 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6638 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6639
6640 for (; *str; ++str)
6641 {
6642 c = *str;
6643 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6644 {
6645 modifiers = 0;
6646 if (str[1] == KS_MODIFIER)
6647 {
6648 str++;
6649 modifiers = *++str;
6650 c = *++str;
6651 }
6652 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6653 {
6654 int i;
6655
6656 /* try to find special key in termcodes */
6657 for (i = 0; i < tc_len; ++i)
6658 if (termcodes[i].name[0] == str[1]
6659 && termcodes[i].name[1] == str[2])
6660 break;
6661 if (i < tc_len)
6662 {
6663 ga_concat(&ga, termcodes[i].code);
6664 str += 2;
6665 continue; /* for (str) */
6666 }
6667 }
6668 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6669 {
6670 if (expmap && cpo_special)
6671 {
6672 ga_clear(&ga);
6673 return NULL;
6674 }
6675 c = TO_SPECIAL(str[1], str[2]);
6676 if (c == K_ZERO) /* display <Nul> as ^@ */
6677 c = NUL;
6678 str += 2;
6679 }
6680 if (IS_SPECIAL(c) || modifiers) /* special key */
6681 {
6682 if (expmap && cpo_special)
6683 {
6684 ga_clear(&ga);
6685 return NULL;
6686 }
6687 ga_concat(&ga, get_special_key_name(c, modifiers));
6688 continue; /* for (str) */
6689 }
6690 }
6691 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6692 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6693 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6694 if (c)
6695 ga_append(&ga, c);
6696 }
6697 ga_append(&ga, NUL);
6698 return (char_u *)(ga.ga_data);
6699}
6700#endif
6701
6702#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6703static char ksme_str[20];
6704static char ksmr_str[20];
6705static char ksmd_str[20];
6706
6707/*
6708 * For Win32 console: update termcap codes for existing console attributes.
6709 */
6710 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006711update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 struct builtin_term *p;
6714
6715 p = find_builtin_term(DEFAULT_TERM);
6716 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6717 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6718 attr | 0x08); /* FOREGROUND_INTENSITY */
6719 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6720 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6721
6722 while (p->bt_string != NULL)
6723 {
6724 if (p->bt_entry == (int)KS_ME)
6725 p->bt_string = &ksme_str[0];
6726 else if (p->bt_entry == (int)KS_MR)
6727 p->bt_string = &ksmr_str[0];
6728 else if (p->bt_entry == (int)KS_MD)
6729 p->bt_string = &ksmd_str[0];
6730 ++p;
6731 }
6732}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006733
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006734# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006735# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006736struct ks_tbl_s
6737{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006738 int code; /* value of KS_ */
6739 char *vtp; /* code in vtp mode */
6740 char *vtp2; /* code in vtp2 mode */
6741 char buf[KSSIZE]; /* save buffer in non-vtp mode */
6742 char vbuf[KSSIZE]; /* save buffer in vtp mode */
6743 char v2buf[KSSIZE]; /* save buffer in vtp2 mode */
6744 char arr[KSSIZE]; /* real buffer */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006745};
6746
6747static struct ks_tbl_s ks_tbl[] =
6748{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006749 {(int)KS_ME, "\033|0m", "\033|0m"}, /* normal */
6750 {(int)KS_MR, "\033|7m", "\033|7m"}, /* reverse */
6751 {(int)KS_MD, "\033|1m", "\033|1m"}, /* bold */
6752 {(int)KS_SO, "\033|91m", "\033|91m"}, /* standout: bright red text */
6753 {(int)KS_SE, "\033|39m", "\033|39m"}, /* standout end: default color */
6754 {(int)KS_CZH, "\033|95m", "\033|95m"}, /* italic: bright magenta text */
6755 {(int)KS_CZR, "\033|0m", "\033|0m"}, /* italic end */
6756 {(int)KS_US, "\033|4m", "\033|4m"}, /* underscore */
6757 {(int)KS_UE, "\033|24m", "\033|24m"}, /* underscore end */
6758# ifdef TERMINFO
6759 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, /* set background color */
6760 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, /* set foreground color */
6761# else
6762 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, /* set background color */
6763 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, /* set foreground color */
6764# endif
6765 {(int)KS_CCO, "16", "256"}, /* colors */
6766 {(int)KS_NAME} /* terminator */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006767};
6768
6769 static struct builtin_term *
6770find_first_tcap(
6771 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006772 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006773{
6774 struct builtin_term *p;
6775
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006776 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006777 if (p->bt_entry == code)
6778 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006779 return NULL;
6780}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006781# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006782
6783/*
6784 * For Win32 console: replace the sequence immediately after termguicolors.
6785 */
6786 void
6787swap_tcap(void)
6788{
6789# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006790 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006792 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006793 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006794 int mode;
6795 enum
6796 {
6797 CMODEINDEX,
6798 CMODE24,
6799 CMODE256
6800 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006801
6802 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006803 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006804 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006805 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006806 {
6807 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006808 if (bt != NULL)
6809 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006810 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6811 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6812 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6813
6814 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6815 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006816 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006817 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006818 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006819 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006820 }
6821
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006822 if (p_tgc)
6823 mode = CMODE24;
6824 else if (t_colors >= 256)
6825 mode = CMODE256;
6826 else
6827 mode = CMODEINDEX;
6828
6829 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006830 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006831 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6832 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006833 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006834 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006835 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006836 case CMODEINDEX:
6837 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6838 break;
6839 case CMODE24:
6840 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6841 break;
6842 default:
6843 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006844 }
6845 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006846 }
6847
6848 if (mode != curr_mode)
6849 {
6850 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006851 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006852 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6853 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006854 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006855 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006856 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006857 case CMODEINDEX:
6858 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6859 break;
6860 case CMODE24:
6861 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6862 break;
6863 default:
6864 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006865 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006866 }
6867 }
6868
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006869 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006870 }
6871# endif
6872}
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006875
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006876#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006877 static int
6878hex_digit(int c)
6879{
6880 if (isdigit(c))
6881 return c - '0';
6882 c = TOLOWER_ASC(c);
6883 if (c >= 'a' && c <= 'f')
6884 return c - 'a' + 10;
6885 return 0x1ffffff;
6886}
6887
6888 guicolor_T
6889gui_get_color_cmn(char_u *name)
6890{
Bram Moolenaar28726652017-01-06 18:16:19 +01006891 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6892 * values as used by the MS-Windows GDI api. It should be used only for
6893 * MS-Windows GDI builds. */
6894# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6895# undef RGB
6896# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006897# ifndef RGB
6898# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6899# endif
6900# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006901 FILE *fd;
6902 char line[LINE_LEN];
6903 char_u *fname;
6904 int r, g, b, i;
6905 guicolor_T color;
6906
6907 struct rgbcolor_table_S {
6908 char_u *color_name;
6909 guicolor_T color;
6910 };
6911
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006912 /* Only non X11 colors (not present in rgb.txt) and colors in
6913 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006914 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006915 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6916 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6917 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6918 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6919 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6920 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6921 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6922 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6923 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6924 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6925 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6926 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6927 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006928 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6929 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006930 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006931 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006932 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006933 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6934 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6935 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6936 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6937 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6938 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6939 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6940 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6941 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006942 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006943 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006944 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6945 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006946 };
6947
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006948 static struct rgbcolor_table_S *colornames_table;
6949 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006950
6951 if (name[0] == '#' && STRLEN(name) == 7)
6952 {
6953 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006954 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006955 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6956 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6957 if (color > 0xffffff)
6958 return INVALCOLOR;
6959 return color;
6960 }
6961
6962 /* Check if the name is one of the colors we know */
6963 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6964 if (STRICMP(name, rgb_table[i].color_name) == 0)
6965 return rgb_table[i].color;
6966
6967 /*
6968 * Last attempt. Look in the file "$VIM/rgb.txt".
6969 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006970 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006971 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006972 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006973
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006974 /* colornames_table not yet initialized */
6975 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6976 if (fname == NULL)
6977 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006978
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006979 fd = fopen((char *)fname, "rt");
6980 vim_free(fname);
6981 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006982 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006983 if (p_verbose > 1)
6984 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6985 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006986 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006987
6988 for (counting = 1; counting >= 0; --counting)
6989 {
6990 if (!counting)
6991 {
6992 colornames_table = (struct rgbcolor_table_S *)alloc(
6993 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6994 if (colornames_table == NULL)
6995 {
6996 fclose(fd);
6997 return INVALCOLOR;
6998 }
6999 rewind(fd);
7000 }
7001 size = 0;
7002
7003 while (!feof(fd))
7004 {
7005 size_t len;
7006 int pos;
7007
7008 ignoredp = fgets(line, LINE_LEN, fd);
7009 len = strlen(line);
7010
7011 if (len <= 1 || line[len - 1] != '\n')
7012 continue;
7013
7014 line[len - 1] = '\0';
7015
7016 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
7017 if (i != 3)
7018 continue;
7019
7020 if (!counting)
7021 {
7022 char_u *s = vim_strsave((char_u *)line + pos);
7023
7024 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007025 {
7026 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007027 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02007028 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007029 colornames_table[size].color_name = s;
7030 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
7031 }
7032 size++;
7033 }
7034 }
7035 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02007036 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02007037
7038 for (i = 0; i < size; i++)
7039 if (STRICMP(name, colornames_table[i].color_name) == 0)
7040 return colornames_table[i].color;
7041
Bram Moolenaarab302212016-04-26 20:59:29 +02007042 return INVALCOLOR;
7043}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007044
7045 guicolor_T
7046gui_get_rgb_color_cmn(int r, int g, int b)
7047{
7048 guicolor_T color = RGB(r, g, b);
7049
7050 if (color > 0xffffff)
7051 return INVALCOLOR;
7052 return color;
7053}
Bram Moolenaarab302212016-04-26 20:59:29 +02007054#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007055
7056#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
7057 || defined(PROTO)
7058static int cube_value[] = {
7059 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7060};
7061
7062static int grey_ramp[] = {
7063 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7064 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7065};
7066
7067# ifdef FEAT_TERMINAL
7068# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007069# else
7070# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007071# endif
7072
Bram Moolenaar9894e392018-05-05 14:29:06 +02007073static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007074// R G B idx
7075 { 0, 0, 0, 1}, // black
7076 {224, 0, 0, 2}, // dark red
7077 { 0, 224, 0, 3}, // dark green
7078 {224, 224, 0, 4}, // dark yellow / brown
7079 { 0, 0, 224, 5}, // dark blue
7080 {224, 0, 224, 6}, // dark magenta
7081 { 0, 224, 224, 7}, // dark cyan
7082 {224, 224, 224, 8}, // light grey
7083
7084 {128, 128, 128, 9}, // dark grey
7085 {255, 64, 64, 10}, // light red
7086 { 64, 255, 64, 11}, // light green
7087 {255, 255, 64, 12}, // yellow
7088 { 64, 64, 255, 13}, // light blue
7089 {255, 64, 255, 14}, // light magenta
7090 { 64, 255, 255, 15}, // light cyan
7091 {255, 255, 255, 16}, // white
7092};
7093
7094 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007095cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007096{
7097 int idx;
7098
7099 if (nr < 16)
7100 {
7101 *r = ansi_table[nr][0];
7102 *g = ansi_table[nr][1];
7103 *b = ansi_table[nr][2];
7104 *ansi_idx = ansi_table[nr][3];
7105 }
7106 else if (nr < 232)
7107 {
7108 /* 216 color cube */
7109 idx = nr - 16;
7110 *r = cube_value[idx / 36 % 6];
7111 *g = cube_value[idx / 6 % 6];
7112 *b = cube_value[idx % 6];
7113 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7114 }
7115 else if (nr < 256)
7116 {
7117 /* 24 grey scale ramp */
7118 idx = nr - 232;
7119 *r = grey_ramp[idx];
7120 *g = grey_ramp[idx];
7121 *b = grey_ramp[idx];
7122 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7123 }
7124 else
7125 {
7126 *r = 0;
7127 *g = 0;
7128 *b = 0;
7129 *ansi_idx = 0;
7130 }
7131}
7132#endif