blob: ace467f6df51f21c23f88615e557c29b3b6ea5b2 [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 Moolenaarbc7aa852005-03-06 23:38:09 +0000925
926 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
927 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
928 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
929 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
930 /* An extra set of cursor keys for vt100 mode */
931 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
932 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
933 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
934 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000936 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
937 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
938 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
939 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000940 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
941 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
942 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
943 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
944 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
945 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
946 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
947 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
948 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
949 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
950 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
951 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000953 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
954 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
955 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
956 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000957 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
958 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000959 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000960 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000961 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000962 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000963 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
964 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000965 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000966 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000967 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
969 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100970 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
971 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
972 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
973 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
974 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
975 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200976 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
977 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
978 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
979 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
980 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
981 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
982 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
983 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
984 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
985 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +0100986 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
987 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
988 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000991 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
992 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
993 /* F14 and F15 are missing, because they send the same codes as the undo
994 * and help key, although they don't work on all keyboards. */
995 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
996 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
997 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
998 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
999 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1000
1001 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1002 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1003 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1004 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1005 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1006 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1007 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1008 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1009 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1010 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1011
1012 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1013 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1014 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1015 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1016 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1017 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1018 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019# endif
1020
1021# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1022/*
1023 * iris-ansi for Silicon Graphics machines.
1024 */
1025 {(int)KS_NAME, "iris-ansi"},
1026 {(int)KS_CE, "\033[K"},
1027 {(int)KS_CD, "\033[J"},
1028 {(int)KS_AL, "\033[L"},
1029# ifdef TERMINFO
1030 {(int)KS_CAL, "\033[%p1%dL"},
1031# else
1032 {(int)KS_CAL, "\033[%dL"},
1033# endif
1034 {(int)KS_DL, "\033[M"},
1035# ifdef TERMINFO
1036 {(int)KS_CDL, "\033[%p1%dM"},
1037# else
1038 {(int)KS_CDL, "\033[%dM"},
1039# endif
1040#if 0 /* The scroll region is not working as Vim expects. */
1041# ifdef TERMINFO
1042 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1043# else
1044 {(int)KS_CS, "\033[%i%d;%dr"},
1045# endif
1046#endif
1047 {(int)KS_CL, "\033[H\033[2J"},
1048 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1049 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1050 {(int)KS_TI, "\033[=6h"},
1051 {(int)KS_TE, "\033[=6l"},
1052 {(int)KS_SE, "\033[21;27m"},
1053 {(int)KS_SO, "\033[1;7m"},
1054 {(int)KS_ME, "\033[m"},
1055 {(int)KS_MR, "\033[7m"},
1056 {(int)KS_MD, "\033[1m"},
1057 {(int)KS_CCO, "8"}, /* allow 8 colors */
1058 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1059 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1060 {(int)KS_US, "\033[4m"}, /* underline on */
1061 {(int)KS_UE, "\033[24m"}, /* underline off */
1062# ifdef TERMINFO
1063 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1064 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1065 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1066 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1067# else
1068 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1069 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1070 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1071 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1072# endif
1073 {(int)KS_MS, "y"}, /* guessed */
1074 {(int)KS_UT, "y"}, /* guessed */
1075 {(int)KS_LE, "\b"},
1076# ifdef TERMINFO
1077 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1078# else
1079 {(int)KS_CM, "\033[%i%d;%dH"},
1080# endif
1081 {(int)KS_SR, "\033M"},
1082# ifdef TERMINFO
1083 {(int)KS_CRI, "\033[%p1%dC"},
1084# else
1085 {(int)KS_CRI, "\033[%dC"},
1086# endif
1087 {(int)KS_CIS, "\033P3.y"},
1088 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1089 {(int)KS_TS, "\033P1.y"},
1090 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1091# ifdef TERMINFO
1092 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1093 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1094# else
1095 {(int)KS_CWS, "\033[203;%d;%d/y"},
1096 {(int)KS_CWP, "\033[205;%d;%d/y"},
1097# endif
1098 {K_UP, "\033[A"},
1099 {K_DOWN, "\033[B"},
1100 {K_LEFT, "\033[D"},
1101 {K_RIGHT, "\033[C"},
1102 {K_S_UP, "\033[161q"},
1103 {K_S_DOWN, "\033[164q"},
1104 {K_S_LEFT, "\033[158q"},
1105 {K_S_RIGHT, "\033[167q"},
1106 {K_F1, "\033[001q"},
1107 {K_F2, "\033[002q"},
1108 {K_F3, "\033[003q"},
1109 {K_F4, "\033[004q"},
1110 {K_F5, "\033[005q"},
1111 {K_F6, "\033[006q"},
1112 {K_F7, "\033[007q"},
1113 {K_F8, "\033[008q"},
1114 {K_F9, "\033[009q"},
1115 {K_F10, "\033[010q"},
1116 {K_F11, "\033[011q"},
1117 {K_F12, "\033[012q"},
1118 {K_S_F1, "\033[013q"},
1119 {K_S_F2, "\033[014q"},
1120 {K_S_F3, "\033[015q"},
1121 {K_S_F4, "\033[016q"},
1122 {K_S_F5, "\033[017q"},
1123 {K_S_F6, "\033[018q"},
1124 {K_S_F7, "\033[019q"},
1125 {K_S_F8, "\033[020q"},
1126 {K_S_F9, "\033[021q"},
1127 {K_S_F10, "\033[022q"},
1128 {K_S_F11, "\033[023q"},
1129 {K_S_F12, "\033[024q"},
1130 {K_INS, "\033[139q"},
1131 {K_HOME, "\033[H"},
1132 {K_END, "\033[146q"},
1133 {K_PAGEUP, "\033[150q"},
1134 {K_PAGEDOWN, "\033[154q"},
1135# endif
1136
1137# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1138/*
1139 * for debugging
1140 */
1141 {(int)KS_NAME, "debug"},
1142 {(int)KS_CE, "[CE]"},
1143 {(int)KS_CD, "[CD]"},
1144 {(int)KS_AL, "[AL]"},
1145# ifdef TERMINFO
1146 {(int)KS_CAL, "[CAL%p1%d]"},
1147# else
1148 {(int)KS_CAL, "[CAL%d]"},
1149# endif
1150 {(int)KS_DL, "[DL]"},
1151# ifdef TERMINFO
1152 {(int)KS_CDL, "[CDL%p1%d]"},
1153# else
1154 {(int)KS_CDL, "[CDL%d]"},
1155# endif
1156# ifdef TERMINFO
1157 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1158# else
1159 {(int)KS_CS, "[%dCS%d]"},
1160# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001161# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# endif
1166# ifdef TERMINFO
1167 {(int)KS_CAB, "[CAB%p1%d]"},
1168 {(int)KS_CAF, "[CAF%p1%d]"},
1169 {(int)KS_CSB, "[CSB%p1%d]"},
1170 {(int)KS_CSF, "[CSF%p1%d]"},
1171# else
1172 {(int)KS_CAB, "[CAB%d]"},
1173 {(int)KS_CAF, "[CAF%d]"},
1174 {(int)KS_CSB, "[CSB%d]"},
1175 {(int)KS_CSF, "[CSF%d]"},
1176# endif
1177 {(int)KS_OP, "[OP]"},
1178 {(int)KS_LE, "[LE]"},
1179 {(int)KS_CL, "[CL]"},
1180 {(int)KS_VI, "[VI]"},
1181 {(int)KS_VE, "[VE]"},
1182 {(int)KS_VS, "[VS]"},
1183 {(int)KS_ME, "[ME]"},
1184 {(int)KS_MR, "[MR]"},
1185 {(int)KS_MB, "[MB]"},
1186 {(int)KS_MD, "[MD]"},
1187 {(int)KS_SE, "[SE]"},
1188 {(int)KS_SO, "[SO]"},
1189 {(int)KS_UE, "[UE]"},
1190 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001191 {(int)KS_UCE, "[UCE]"},
1192 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001193 {(int)KS_STE, "[STE]"},
1194 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_MS, "[MS]"},
1196 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001197 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# ifdef TERMINFO
1199 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1200# else
1201 {(int)KS_CM, "[%dCM%d]"},
1202# endif
1203 {(int)KS_SR, "[SR]"},
1204# ifdef TERMINFO
1205 {(int)KS_CRI, "[CRI%p1%d]"},
1206# else
1207 {(int)KS_CRI, "[CRI%d]"},
1208# endif
1209 {(int)KS_VB, "[VB]"},
1210 {(int)KS_KS, "[KS]"},
1211 {(int)KS_KE, "[KE]"},
1212 {(int)KS_TI, "[TI]"},
1213 {(int)KS_TE, "[TE]"},
1214 {(int)KS_CIS, "[CIS]"},
1215 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001216 {(int)KS_CSC, "[CSC]"},
1217 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {(int)KS_TS, "[TS]"},
1219 {(int)KS_FS, "[FS]"},
1220# ifdef TERMINFO
1221 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1222 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1223# else
1224 {(int)KS_CWS, "[%dCWS%d]"},
1225 {(int)KS_CWP, "[%dCWP%d]"},
1226# endif
1227 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001228 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001229 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001230 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {K_UP, "[KU]"},
1232 {K_DOWN, "[KD]"},
1233 {K_LEFT, "[KL]"},
1234 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 {K_XUP, "[xKU]"},
1236 {K_XDOWN, "[xKD]"},
1237 {K_XLEFT, "[xKL]"},
1238 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_S_UP, "[S-KU]"},
1240 {K_S_DOWN, "[S-KD]"},
1241 {K_S_LEFT, "[S-KL]"},
1242 {K_C_LEFT, "[C-KL]"},
1243 {K_S_RIGHT, "[S-KR]"},
1244 {K_C_RIGHT, "[C-KR]"},
1245 {K_F1, "[F1]"},
1246 {K_XF1, "[xF1]"},
1247 {K_F2, "[F2]"},
1248 {K_XF2, "[xF2]"},
1249 {K_F3, "[F3]"},
1250 {K_XF3, "[xF3]"},
1251 {K_F4, "[F4]"},
1252 {K_XF4, "[xF4]"},
1253 {K_F5, "[F5]"},
1254 {K_F6, "[F6]"},
1255 {K_F7, "[F7]"},
1256 {K_F8, "[F8]"},
1257 {K_F9, "[F9]"},
1258 {K_F10, "[F10]"},
1259 {K_F11, "[F11]"},
1260 {K_F12, "[F12]"},
1261 {K_S_F1, "[S-F1]"},
1262 {K_S_XF1, "[S-xF1]"},
1263 {K_S_F2, "[S-F2]"},
1264 {K_S_XF2, "[S-xF2]"},
1265 {K_S_F3, "[S-F3]"},
1266 {K_S_XF3, "[S-xF3]"},
1267 {K_S_F4, "[S-F4]"},
1268 {K_S_XF4, "[S-xF4]"},
1269 {K_S_F5, "[S-F5]"},
1270 {K_S_F6, "[S-F6]"},
1271 {K_S_F7, "[S-F7]"},
1272 {K_S_F8, "[S-F8]"},
1273 {K_S_F9, "[S-F9]"},
1274 {K_S_F10, "[S-F10]"},
1275 {K_S_F11, "[S-F11]"},
1276 {K_S_F12, "[S-F12]"},
1277 {K_HELP, "[HELP]"},
1278 {K_UNDO, "[UNDO]"},
1279 {K_BS, "[BS]"},
1280 {K_INS, "[INS]"},
1281 {K_KINS, "[KINS]"},
1282 {K_DEL, "[DEL]"},
1283 {K_KDEL, "[KDEL]"},
1284 {K_HOME, "[HOME]"},
1285 {K_S_HOME, "[C-HOME]"},
1286 {K_C_HOME, "[C-HOME]"},
1287 {K_KHOME, "[KHOME]"},
1288 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001289 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {K_END, "[END]"},
1291 {K_S_END, "[C-END]"},
1292 {K_C_END, "[C-END]"},
1293 {K_KEND, "[KEND]"},
1294 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_PAGEUP, "[PAGEUP]"},
1297 {K_PAGEDOWN, "[PAGEDOWN]"},
1298 {K_KPAGEUP, "[KPAGEUP]"},
1299 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1300 {K_MOUSE, "[MOUSE]"},
1301 {K_KPLUS, "[KPLUS]"},
1302 {K_KMINUS, "[KMINUS]"},
1303 {K_KDIVIDE, "[KDIVIDE]"},
1304 {K_KMULTIPLY, "[KMULTIPLY]"},
1305 {K_KENTER, "[KENTER]"},
1306 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001307 {K_PS, "[PASTE-START]"},
1308 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_K0, "[K0]"},
1310 {K_K1, "[K1]"},
1311 {K_K2, "[K2]"},
1312 {K_K3, "[K3]"},
1313 {K_K4, "[K4]"},
1314 {K_K5, "[K5]"},
1315 {K_K6, "[K6]"},
1316 {K_K7, "[K7]"},
1317 {K_K8, "[K8]"},
1318 {K_K9, "[K9]"},
1319# endif
1320
1321#endif /* NO_BUILTIN_TCAPS */
1322
1323/*
1324 * The most minimal terminal: only clear screen and cursor positioning
1325 * Always included.
1326 */
1327 {(int)KS_NAME, "dumb"},
1328 {(int)KS_CL, "\014"},
1329#ifdef TERMINFO
1330 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1331 ESC_STR "[%i%p1%d;%p2%dH")},
1332#else
1333 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1334#endif
1335
1336/*
1337 * end marker
1338 */
1339 {(int)KS_NAME, NULL}
1340
1341}; /* end of builtin_termcaps */
1342
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001343#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001345termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001346{
Bram Moolenaarab302212016-04-26 20:59:29 +02001347 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348}
1349
1350 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001351termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001352{
1353 guicolor_T t;
1354
1355 if (*name == NUL)
1356 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001357 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001358
1359 if (t == INVALCOLOR)
1360 EMSG2(_("E254: Cannot allocate color %s"), name);
1361 return t;
1362}
1363
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001364 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001365termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001366{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001367 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368}
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374#ifdef AMIGA
1375# define DEFAULT_TERM (char_u *)"amiga"
1376#endif
1377
1378#ifdef MSWIN
1379# define DEFAULT_TERM (char_u *)"win32"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#if defined(UNIX) && !defined(__MINT__)
1383# define DEFAULT_TERM (char_u *)"ansi"
1384#endif
1385
1386#ifdef __MINT__
1387# define DEFAULT_TERM (char_u *)"vt52"
1388#endif
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef VMS
1391# define DEFAULT_TERM (char_u *)"vt320"
1392#endif
1393
1394#ifdef __BEOS__
1395# undef DEFAULT_TERM
1396# define DEFAULT_TERM (char_u *)"beos-ansi"
1397#endif
1398
1399#ifndef DEFAULT_TERM
1400# define DEFAULT_TERM (char_u *)"dumb"
1401#endif
1402
1403/*
1404 * Term_strings contains currently used terminal output strings.
1405 * It is initialized with the default values by parse_builtin_tcap().
1406 * The values can be changed by setting the option with the same name.
1407 */
1408char_u *(term_strings[(int)KS_LAST + 1]);
1409
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001410static int need_gather = FALSE; /* need to fill termleader[] */
1411static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001413static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001414static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#endif
1416
1417 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001418find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 struct builtin_term *p;
1421
1422 p = builtin_termcaps;
1423 while (p->bt_string != NULL)
1424 {
1425 if (p->bt_entry == (int)KS_NAME)
1426 {
1427#ifdef UNIX
1428 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1429 return p;
1430 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1431 return p;
1432 else
1433#endif
1434#ifdef VMS
1435 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1436 return p;
1437 else
1438#endif
1439 if (STRCMP(term, p->bt_string) == 0)
1440 return p;
1441 }
1442 ++p;
1443 }
1444 return p;
1445}
1446
1447/*
1448 * Parsing of the builtin termcap entries.
1449 * Caller should check if 'name' is a valid builtin term.
1450 * The terminal's name is not set, as this is already done in termcapinit().
1451 */
1452 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001453parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 struct builtin_term *p;
1456 char_u name[2];
1457 int term_8bit;
1458
1459 p = find_builtin_term(term);
1460 term_8bit = term_is_8bit(term);
1461
1462 /* Do not parse if builtin term not found */
1463 if (p->bt_string == NULL)
1464 return;
1465
1466 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1467 {
1468 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1469 {
1470 /* Only set the value if it wasn't set yet. */
1471 if (term_strings[p->bt_entry] == NULL
1472 || term_strings[p->bt_entry] == empty_option)
1473 {
1474 /* 8bit terminal: use CSI instead of <Esc>[ */
1475 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1476 {
1477 char_u *s, *t;
1478
1479 s = vim_strsave((char_u *)p->bt_string);
1480 if (s != NULL)
1481 {
1482 for (t = s; *t; ++t)
1483 if (term_7to8bit(t))
1484 {
1485 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001486 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 term_strings[p->bt_entry] = s;
1489 set_term_option_alloced(&term_strings[p->bt_entry]);
1490 }
1491 }
1492 else
1493 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1494 }
1495 }
1496 else
1497 {
1498 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1499 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1500 if (find_termcode(name) == NULL)
1501 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1502 }
1503 }
1504}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
1507 * Set number of colors.
1508 * Store it as a number in t_colors.
1509 * Store it as a string in T_CCO (using nr_colors[]).
1510 */
1511 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001512set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 char_u nr_colors[20]; /* string for number of colors */
1515
1516 t_colors = nr;
1517 if (t_colors > 1)
1518 sprintf((char *)nr_colors, "%d", t_colors);
1519 else
1520 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001521 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001523
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001524#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001525/*
1526 * Set the color count to "val" and redraw if it changed.
1527 */
1528 static void
1529may_adjust_color_count(int val)
1530{
1531 if (val != t_colors)
1532 {
1533 /* Nr of colors changed, initialize highlighting and
1534 * redraw everything. This causes a redraw, which usually
1535 * clears the message. Try keeping the message if it
1536 * might work. */
1537 set_keep_msg_from_hist();
1538 set_color_count(val);
1539 init_highlight(TRUE, FALSE);
1540# ifdef DEBUG_TERMRESPONSE
1541 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001542 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001543
Bram Moolenaarb255b902018-04-24 21:40:10 +02001544 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001545 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001546#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001547 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001548#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001549 }
1550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551#endif
1552
1553#ifdef HAVE_TGETENT
1554static char *(key_names[]) =
1555{
1556#ifdef FEAT_TERMRESPONSE
1557 /* Do this one first, it may cause a screen redraw. */
1558 "Co",
1559#endif
1560 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 "#2", "#4", "%i", "*7",
1562 "k1", "k2", "k3", "k4", "k5", "k6",
1563 "k7", "k8", "k9", "k;", "F1", "F2",
1564 "%1", "&8", "kb", "kI", "kD", "kh",
1565 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1566 NULL
1567};
1568#endif
1569
Bram Moolenaar9289df52018-05-10 14:40:57 +02001570#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001571 static void
1572get_term_entries(int *height, int *width)
1573{
1574 static struct {
1575 enum SpecialKey dest; /* index in term_strings[] */
1576 char *name; /* termcap name for string */
1577 } string_names[] =
1578 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1579 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1580 {KS_CL, "cl"}, {KS_CD, "cd"},
1581 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1582 {KS_ME, "me"}, {KS_MR, "mr"},
1583 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1584 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1585 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1586 {KS_STE,"Te"}, {KS_STS,"Ts"},
1587 {KS_CM, "cm"}, {KS_SR, "sr"},
1588 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1589 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1590 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1591 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1592 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1593 {KS_VS, "vs"}, {KS_CVS, "VS"},
1594 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1595 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1596 {KS_TS, "ts"}, {KS_FS, "fs"},
1597 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1598 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1599 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1600 {KS_8F, "8f"}, {KS_8B, "8b"},
1601 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1602 {KS_CPS, "PS"}, {KS_CPE, "PE"},
1603 {(enum SpecialKey)0, NULL}
1604 };
1605 int i;
1606 char_u *p;
1607 static char_u tstrbuf[TBUFSZ];
1608 char_u *tp = tstrbuf;
1609
1610 /*
1611 * get output strings
1612 */
1613 for (i = 0; string_names[i].name != NULL; ++i)
1614 {
1615 if (TERM_STR(string_names[i].dest) == NULL
1616 || TERM_STR(string_names[i].dest) == empty_option)
1617 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
1618 }
1619
1620 /* tgetflag() returns 1 if the flag is present, 0 if not and
1621 * possibly -1 if the flag doesn't exist. */
1622 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1623 T_MS = (char_u *)"y";
1624 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1625 T_XS = (char_u *)"y";
1626 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1627 T_XN = (char_u *)"y";
1628 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1629 T_DB = (char_u *)"y";
1630 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1631 T_DA = (char_u *)"y";
1632 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1633 T_UT = (char_u *)"y";
1634
1635 /*
1636 * get key codes
1637 */
1638 for (i = 0; key_names[i] != NULL; ++i)
1639 if (find_termcode((char_u *)key_names[i]) == NULL)
1640 {
1641 p = TGETSTR(key_names[i], &tp);
1642 /* if cursor-left == backspace, ignore it (televideo 925) */
1643 if (p != NULL
1644 && (*p != Ctrl_H
1645 || key_names[i][0] != 'k'
1646 || key_names[i][1] != 'l'))
1647 add_termcode((char_u *)key_names[i], p, FALSE);
1648 }
1649
1650 if (*height == 0)
1651 *height = tgetnum("li");
1652 if (*width == 0)
1653 *width = tgetnum("co");
1654
1655 /*
1656 * Get number of colors (if not done already).
1657 */
1658 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
1659 set_color_count(tgetnum("Co"));
1660
1661# ifndef hpux
1662 BC = (char *)TGETSTR("bc", &tp);
1663 UP = (char *)TGETSTR("up", &tp);
1664 p = TGETSTR("pc", &tp);
1665 if (p)
1666 PC = *p;
1667# endif
1668}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001669#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001670
1671 static void
1672report_term_error(char_u *error_msg, char_u *term)
1673{
1674 struct builtin_term *termp;
1675
1676 mch_errmsg("\r\n");
1677 if (error_msg != NULL)
1678 {
1679 mch_errmsg((char *)error_msg);
1680 mch_errmsg("\r\n");
1681 }
1682 mch_errmsg("'");
1683 mch_errmsg((char *)term);
1684 mch_errmsg(_("' not known. Available builtin terminals are:"));
1685 mch_errmsg("\r\n");
1686 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1687 {
1688 if (termp->bt_entry == (int)KS_NAME)
1689 {
1690#ifdef HAVE_TGETENT
1691 mch_errmsg(" builtin_");
1692#else
1693 mch_errmsg(" ");
1694#endif
1695 mch_errmsg(termp->bt_string);
1696 mch_errmsg("\r\n");
1697 }
1698 }
1699}
1700
1701 static void
1702report_default_term(char_u *term)
1703{
1704 mch_errmsg(_("defaulting to '"));
1705 mch_errmsg((char *)term);
1706 mch_errmsg("'\r\n");
1707 if (emsg_silent == 0)
1708 {
1709 screen_start(); /* don't know where cursor is now */
1710 out_flush();
1711 if (!is_not_a_term())
1712 ui_delay(2000L, TRUE);
1713 }
1714}
1715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716/*
1717 * Set terminal options for terminal "term".
1718 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1719 *
1720 * While doing this, until ttest(), some options may be NULL, be careful.
1721 */
1722 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001723set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 struct builtin_term *termp;
1726#ifdef HAVE_TGETENT
1727 int builtin_first = p_tbi;
1728 int try;
1729 int termcap_cleared = FALSE;
1730#endif
1731 int width = 0, height = 0;
1732 char_u *error_msg = NULL;
1733 char_u *bs_p, *del_p;
1734
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001735 /* In silect mode (ex -s) we don't use the 'term' option. */
1736 if (silent_mode)
1737 return OK;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 detected_8bit = FALSE; /* reset 8-bit detection */
1740
1741 if (term_is_builtin(term))
1742 {
1743 term += 8;
1744#ifdef HAVE_TGETENT
1745 builtin_first = 1;
1746#endif
1747 }
1748
1749/*
1750 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1751 * If builtin_first is TRUE:
1752 * 0. try builtin termcap
1753 * 1. try external termcap
1754 * 2. if both fail default to a builtin terminal
1755 * If builtin_first is FALSE:
1756 * 1. try external termcap
1757 * 2. try builtin termcap, if both fail default to a builtin terminal
1758 */
1759#ifdef HAVE_TGETENT
1760 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1761 {
1762 /*
1763 * Use external termcap
1764 */
1765 if (try == 1)
1766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 /*
1770 * If the external termcap does not have a matching entry, try the
1771 * builtin ones.
1772 */
1773 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (!termcap_cleared)
1776 {
1777 clear_termoptions(); /* clear old options */
1778 termcap_cleared = TRUE;
1779 }
1780
Bram Moolenaar69e05692018-05-10 14:11:52 +02001781 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
1783 }
1784 else /* try == 0 || try == 2 */
1785#endif /* HAVE_TGETENT */
1786 /*
1787 * Use builtin termcap
1788 */
1789 {
1790#ifdef HAVE_TGETENT
1791 /*
1792 * If builtin termcap was already used, there is no need to search
1793 * for the builtin termcap again, quit now.
1794 */
1795 if (try == 2 && builtin_first && termcap_cleared)
1796 break;
1797#endif
1798 /*
1799 * search for 'term' in builtin_termcaps[]
1800 */
1801 termp = find_builtin_term(term);
1802 if (termp->bt_string == NULL) /* did not find it */
1803 {
1804#ifdef HAVE_TGETENT
1805 /*
1806 * If try == 0, first try the external termcap. If that is not
1807 * found we'll get back here with try == 2.
1808 * If termcap_cleared is set we used the external termcap,
1809 * don't complain about not finding the term in the builtin
1810 * termcap.
1811 */
1812 if (try == 0) /* try external one */
1813 continue;
1814 if (termcap_cleared) /* found in external termcap */
1815 break;
1816#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001817 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 /* when user typed :set term=xxx, quit here */
1820 if (starting != NO_SCREEN)
1821 {
1822 screen_start(); /* don't know where cursor is now */
1823 wait_return(TRUE);
1824 return FAIL;
1825 }
1826 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001827 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001828 set_string_option_direct((char_u *)"term", -1, term,
1829 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 display_errors();
1831 }
1832 out_flush();
1833#ifdef HAVE_TGETENT
1834 if (!termcap_cleared)
1835 {
1836#endif
1837 clear_termoptions(); /* clear old options */
1838#ifdef HAVE_TGETENT
1839 termcap_cleared = TRUE;
1840 }
1841#endif
1842 parse_builtin_tcap(term);
1843#ifdef FEAT_GUI
1844 if (term_is_gui(term))
1845 {
1846 out_flush();
1847 gui_init();
1848 /* If starting the GUI failed, don't do any of the other
1849 * things for this terminal */
1850 if (!gui.in_use)
1851 return FAIL;
1852#ifdef HAVE_TGETENT
1853 break; /* don't try using external termcap */
1854#endif
1855 }
1856#endif /* FEAT_GUI */
1857 }
1858#ifdef HAVE_TGETENT
1859 }
1860#endif
1861
1862/*
1863 * special: There is no info in the termcap about whether the cursor
1864 * positioning is relative to the start of the screen or to the start of the
1865 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1866 * relative.
1867 */
1868 if (STRCMP(term, "pcterm") == 0)
1869 T_CCS = (char_u *)"yes";
1870 else
1871 T_CCS = empty_option;
1872
1873#ifdef UNIX
1874/*
1875 * Any "stty" settings override the default for t_kb from the termcap.
1876 * This is in os_unix.c, because it depends a lot on the version of unix that
1877 * is being used.
1878 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1879 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001880# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 get_stty();
1884#endif
1885
1886/*
1887 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1888 * didn't work, use the default CTRL-H
1889 * The default for t_kD is DEL, unless t_kb is DEL.
1890 * The vim_strsave'd strings are probably lost forever, well it's only two
1891 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1892 * directly.
1893 */
1894#ifdef FEAT_GUI
1895 if (!gui.in_use)
1896#endif
1897 {
1898 bs_p = find_termcode((char_u *)"kb");
1899 del_p = find_termcode((char_u *)"kD");
1900 if (bs_p == NULL || *bs_p == NUL)
1901 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1902 if ((del_p == NULL || *del_p == NUL) &&
1903 (bs_p == NULL || *bs_p != DEL))
1904 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1905 }
1906
1907#if defined(UNIX) || defined(VMS)
1908 term_is_xterm = vim_is_xterm(term);
1909#endif
1910
1911#ifdef FEAT_MOUSE
1912# if defined(UNIX) || defined(VMS)
1913# ifdef FEAT_MOUSE_TTY
1914 /*
1915 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1916 * The termcode for the mouse is added as a side effect in option.c.
1917 */
1918 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001919 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001922 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 if (use_xterm_mouse())
1925 p = NULL; /* keep existing value, might be "xterm2" */
1926 else
1927 p = (char_u *)"xterm";
1928 }
1929# endif
1930 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001933 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1934 * "xterm2" in check_termcode(). */
1935 reset_option_was_set((char_u *)"ttym");
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (p == NULL
1938# ifdef FEAT_GUI
1939 || gui.in_use
1940# endif
1941 )
1942 check_mouse_termcode(); /* set mouse termcode anyway */
1943 }
1944# endif
1945# else
1946 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1947# endif
1948#endif /* FEAT_MOUSE */
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#ifdef USE_TERM_CONSOLE
1951 /* DEFAULT_TERM indicates that it is the machine console. */
1952 if (STRCMP(term, DEFAULT_TERM) != 0)
1953 term_console = FALSE;
1954 else
1955 {
1956 term_console = TRUE;
1957# ifdef AMIGA
1958 win_resize_on(); /* enable window resizing reports */
1959# endif
1960 }
1961#endif
1962
1963#if defined(UNIX) || defined(VMS)
1964 /*
1965 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1966 */
1967 if (vim_is_fastterm(term))
1968 p_tf = TRUE;
1969#endif
1970#ifdef USE_TERM_CONSOLE
1971 /*
1972 * 'ttyfast' is default on consoles
1973 */
1974 if (term_console)
1975 p_tf = TRUE;
1976#endif
1977
1978 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1979
1980 full_screen = TRUE; /* we can use termcap codes from now on */
1981 set_term_defaults(); /* use current values as defaults */
1982#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02001983 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001984 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985#endif
1986
1987 /*
1988 * Initialize the terminal with the appropriate termcap codes.
1989 * Set the mouse and window title if possible.
1990 * Don't do this when starting, need to parse the .vimrc first, because it
1991 * may redefine t_TI etc.
1992 */
1993 if (starting != NO_SCREEN)
1994 {
1995 starttermcap(); /* may change terminal mode */
1996#ifdef FEAT_MOUSE
1997 setmouse(); /* may start using the mouse */
1998#endif
1999#ifdef FEAT_TITLE
2000 maketitle(); /* may display window title */
2001#endif
2002 }
2003
2004 /* display initial screen after ttest() checking. jw. */
2005 if (width <= 0 || height <= 0)
2006 {
2007 /* termcap failed to report size */
2008 /* set defaults, in case ui_get_shellsize() also fails */
2009 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002010#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 height = 25; /* console is often 25 lines */
2012#else
2013 height = 24; /* most terminals are 24 lines */
2014#endif
2015 }
2016 set_shellsize(width, height, FALSE); /* may change Rows */
2017 if (starting != NO_SCREEN)
2018 {
2019 if (scroll_region)
2020 scroll_region_reset(); /* In case Rows changed */
2021 check_map_keycodes(); /* check mappings for terminal codes used */
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002024 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /*
2027 * Execute the TermChanged autocommands for each buffer that is
2028 * loaded.
2029 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002030 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002031 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
2033 if (curbuf->b_ml.ml_mfp != NULL)
2034 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2035 curbuf);
2036 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002037 if (bufref_valid(&old_curbuf))
2038 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041
2042#ifdef FEAT_TERMRESPONSE
2043 may_req_termresponse();
2044#endif
2045
2046 return OK;
2047}
2048
2049#if defined(FEAT_MOUSE) || defined(PROTO)
2050
2051# ifdef FEAT_MOUSE_TTY
2052# define HMT_NORMAL 1
2053# define HMT_NETTERM 2
2054# define HMT_DEC 4
2055# define HMT_JSBTERM 8
2056# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002057# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002058# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002059# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060static int has_mouse_termcode = 0;
2061# endif
2062
Bram Moolenaar864207d2008-06-24 22:14:38 +00002063# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002065set_mouse_termcode(
2066 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2067 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 char_u name[2];
2070
2071 name[0] = n;
2072 name[1] = KE_FILLER;
2073 add_termcode(name, s, FALSE);
2074# ifdef FEAT_MOUSE_TTY
2075# ifdef FEAT_MOUSE_JSB
2076 if (n == KS_JSBTERM_MOUSE)
2077 has_mouse_termcode |= HMT_JSBTERM;
2078 else
2079# endif
2080# ifdef FEAT_MOUSE_NET
2081 if (n == KS_NETTERM_MOUSE)
2082 has_mouse_termcode |= HMT_NETTERM;
2083 else
2084# endif
2085# ifdef FEAT_MOUSE_DEC
2086 if (n == KS_DEC_MOUSE)
2087 has_mouse_termcode |= HMT_DEC;
2088 else
2089# endif
2090# ifdef FEAT_MOUSE_PTERM
2091 if (n == KS_PTERM_MOUSE)
2092 has_mouse_termcode |= HMT_PTERM;
2093 else
2094# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002095# ifdef FEAT_MOUSE_URXVT
2096 if (n == KS_URXVT_MOUSE)
2097 has_mouse_termcode |= HMT_URXVT;
2098 else
2099# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002100# ifdef FEAT_MOUSE_SGR
2101 if (n == KS_SGR_MOUSE)
2102 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002103 else if (n == KS_SGR_MOUSE_RELEASE)
2104 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002105 else
2106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 has_mouse_termcode |= HMT_NORMAL;
2108# endif
2109}
2110# endif
2111
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002112# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002113 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002115del_mouse_termcode(
2116 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
2118 char_u name[2];
2119
2120 name[0] = n;
2121 name[1] = KE_FILLER;
2122 del_termcode(name);
2123# ifdef FEAT_MOUSE_TTY
2124# ifdef FEAT_MOUSE_JSB
2125 if (n == KS_JSBTERM_MOUSE)
2126 has_mouse_termcode &= ~HMT_JSBTERM;
2127 else
2128# endif
2129# ifdef FEAT_MOUSE_NET
2130 if (n == KS_NETTERM_MOUSE)
2131 has_mouse_termcode &= ~HMT_NETTERM;
2132 else
2133# endif
2134# ifdef FEAT_MOUSE_DEC
2135 if (n == KS_DEC_MOUSE)
2136 has_mouse_termcode &= ~HMT_DEC;
2137 else
2138# endif
2139# ifdef FEAT_MOUSE_PTERM
2140 if (n == KS_PTERM_MOUSE)
2141 has_mouse_termcode &= ~HMT_PTERM;
2142 else
2143# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002144# ifdef FEAT_MOUSE_URXVT
2145 if (n == KS_URXVT_MOUSE)
2146 has_mouse_termcode &= ~HMT_URXVT;
2147 else
2148# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002149# ifdef FEAT_MOUSE_SGR
2150 if (n == KS_SGR_MOUSE)
2151 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002152 else if (n == KS_SGR_MOUSE_RELEASE)
2153 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002154 else
2155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 has_mouse_termcode &= ~HMT_NORMAL;
2157# endif
2158}
2159# endif
2160#endif
2161
2162#ifdef HAVE_TGETENT
2163/*
2164 * Call tgetent()
2165 * Return error message if it fails, NULL if it's OK.
2166 */
2167 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002168tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 int i;
2171
2172 i = TGETENT(tbuf, term);
2173 if (i < 0 /* -1 is always an error */
2174# ifdef TGETENT_ZERO_ERR
2175 || i == 0 /* sometimes zero is also an error */
2176# endif
2177 )
2178 {
2179 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2180 * tgetent() with the always existing "dumb" entry to avoid a crash or
2181 * hang. */
2182 (void)TGETENT(tbuf, "dumb");
2183
2184 if (i < 0)
2185# ifdef TGETENT_ZERO_ERR
2186 return (char_u *)_("E557: Cannot open termcap file");
2187 if (i == 0)
2188# endif
2189#ifdef TERMINFO
2190 return (char_u *)_("E558: Terminal entry not found in terminfo");
2191#else
2192 return (char_u *)_("E559: Terminal entry not found in termcap");
2193#endif
2194 }
2195 return NULL;
2196}
2197
2198/*
2199 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2200 * Fix that here.
2201 */
2202 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002203vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 char *p;
2206
2207 p = tgetstr(s, (char **)pp);
2208 if (p == (char *)-1)
2209 p = NULL;
2210 return (char_u *)p;
2211}
2212#endif /* HAVE_TGETENT */
2213
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002214#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215/*
2216 * Get Columns and Rows from the termcap. Used after a window signal if the
2217 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2218 * and "li" entries never change. But on some systems this works.
2219 * Errors while getting the entries are ignored.
2220 */
2221 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002222getlinecol(
2223 long *cp, /* pointer to columns */
2224 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 char_u tbuf[TBUFSZ];
2227
2228 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2229 {
2230 if (*cp == 0)
2231 *cp = tgetnum("co");
2232 if (*rp == 0)
2233 *rp = tgetnum("li");
2234 }
2235}
2236#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2237
2238/*
2239 * Get a string entry from the termcap and add it to the list of termcodes.
2240 * Used for <t_xx> special keys.
2241 * Give an error message for failure when not sourcing.
2242 * If force given, replace an existing entry.
2243 * Return FAIL if the entry was not found, OK if the entry was added.
2244 */
2245 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002246add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
2248 char_u *term;
2249 int key;
2250 struct builtin_term *termp;
2251#ifdef HAVE_TGETENT
2252 char_u *string;
2253 int i;
2254 int builtin_first;
2255 char_u tbuf[TBUFSZ];
2256 char_u tstrbuf[TBUFSZ];
2257 char_u *tp = tstrbuf;
2258 char_u *error_msg = NULL;
2259#endif
2260
2261/*
2262 * If the GUI is running or will start in a moment, we only support the keys
2263 * that the GUI can produce.
2264 */
2265#ifdef FEAT_GUI
2266 if (gui.in_use || gui.starting)
2267 return gui_mch_haskey(name);
2268#endif
2269
2270 if (!force && find_termcode(name) != NULL) /* it's already there */
2271 return OK;
2272
2273 term = T_NAME;
2274 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2275 return FAIL;
2276
2277 if (term_is_builtin(term)) /* name starts with "builtin_" */
2278 {
2279 term += 8;
2280#ifdef HAVE_TGETENT
2281 builtin_first = TRUE;
2282#endif
2283 }
2284#ifdef HAVE_TGETENT
2285 else
2286 builtin_first = p_tbi;
2287#endif
2288
2289#ifdef HAVE_TGETENT
2290/*
2291 * We can get the entry from the builtin termcap and from the external one.
2292 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2293 * builtin termcap first.
2294 * If 'ttybuiltin' is off, try external termcap first.
2295 */
2296 for (i = 0; i < 2; ++i)
2297 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002298 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#endif
2300 /*
2301 * Search in builtin termcap
2302 */
2303 {
2304 termp = find_builtin_term(term);
2305 if (termp->bt_string != NULL) /* found it */
2306 {
2307 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002308 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 while (termp->bt_entry != (int)KS_NAME)
2310 {
2311 if ((int)termp->bt_entry == key)
2312 {
2313 add_termcode(name, (char_u *)termp->bt_string,
2314 term_is_8bit(term));
2315 return OK;
2316 }
2317 ++termp;
2318 }
2319 }
2320 }
2321#ifdef HAVE_TGETENT
2322 else
2323 /*
2324 * Search in external termcap
2325 */
2326 {
2327 error_msg = tgetent_error(tbuf, term);
2328 if (error_msg == NULL)
2329 {
2330 string = TGETSTR((char *)name, &tp);
2331 if (string != NULL && *string != NUL)
2332 {
2333 add_termcode(name, string, FALSE);
2334 return OK;
2335 }
2336 }
2337 }
2338 }
2339#endif
2340
2341 if (sourcing_name == NULL)
2342 {
2343#ifdef HAVE_TGETENT
2344 if (error_msg != NULL)
2345 EMSG(error_msg);
2346 else
2347#endif
2348 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2349 }
2350 return FAIL;
2351}
2352
2353 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002354term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2357}
2358
2359/*
2360 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2361 * Assume that the terminal is using 8-bit controls when the name contains
2362 * "8bit", like in "xterm-8bit".
2363 */
2364 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002365term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2368}
2369
2370/*
2371 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002372 * <Esc>[ -> CSI <M_C_[>
2373 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * <Esc>O -> <M-C-O>
2375 */
2376 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002377term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 if (*p == ESC)
2380 {
2381 if (p[1] == '[')
2382 return CSI;
2383 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002384 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (p[1] == 'O')
2386 return 0x8f;
2387 }
2388 return 0;
2389}
2390
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002391#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002393term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
2395 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2396}
2397#endif
2398
2399#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2400
2401 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002402tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404 static char_u buf[16];
2405 char_u *p;
2406
2407 p = buf + 15;
2408 *p = '\0';
2409 do
2410 {
2411 --p;
2412 *p = (char_u) (i % 10 + '0');
2413 i /= 10;
2414 }
2415 while (i > 0 && p > buf);
2416 return p;
2417}
2418#endif
2419
2420#ifndef HAVE_TGETENT
2421
2422/*
2423 * minimal tgoto() implementation.
2424 * no padding and we only parse for %i %d and %+char
2425 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002426static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002428 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002429tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 static char buf[30];
2432 char *p, *s, *e;
2433
2434 if (!cm)
2435 return "OOPS";
2436 e = buf + 29;
2437 for (s = buf; s < e && *cm; cm++)
2438 {
2439 if (*cm != '%')
2440 {
2441 *s++ = *cm;
2442 continue;
2443 }
2444 switch (*++cm)
2445 {
2446 case 'd':
2447 p = (char *)tltoa((unsigned long)y);
2448 y = x;
2449 while (*p)
2450 *s++ = *p++;
2451 break;
2452 case 'i':
2453 x++;
2454 y++;
2455 break;
2456 case '+':
2457 *s++ = (char)(*++cm + y);
2458 y = x;
2459 break;
2460 case '%':
2461 *s++ = *cm;
2462 break;
2463 default:
2464 return "OOPS";
2465 }
2466 }
2467 *s = '\0';
2468 return buf;
2469}
2470
2471#endif /* HAVE_TGETENT */
2472
2473/*
2474 * Set the terminal name and initialize the terminal options.
2475 * If "name" is NULL or empty, get the terminal name from the environment.
2476 * If that fails, use the default terminal name.
2477 */
2478 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002479termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 char_u *term;
2482
2483 if (name != NULL && *name == NUL)
2484 name = NULL; /* empty name is equal to no name */
2485 term = name;
2486
2487#ifdef __BEOS__
2488 /*
2489 * TERM environment variable is normally set to 'ansi' on the Bebox;
2490 * Since the BeBox doesn't quite support full ANSI yet, we use our
2491 * own custom 'ansi-beos' termcap instead, unless the -T option has
2492 * been given on the command line.
2493 */
2494 if (term == NULL
2495 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2496 term = DEFAULT_TERM;
2497#endif
2498#ifndef MSWIN
2499 if (term == NULL)
2500 term = mch_getenv((char_u *)"TERM");
2501#endif
2502 if (term == NULL || *term == NUL)
2503 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002504 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 /* Set the default terminal name. */
2507 set_string_default("term", term);
2508 set_string_default("ttytype", term);
2509
2510 /*
2511 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2512 */
2513 set_termname(T_NAME != NULL ? T_NAME : term);
2514}
2515
2516/*
2517 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2518 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002519#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2521static char_u out_buf[OUT_SIZE + 1];
2522static int out_pos = 0; /* number of chars in out_buf */
2523
2524/*
2525 * out_flush(): flush the output buffer
2526 */
2527 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002528out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 int len;
2531
2532 if (out_pos != 0)
2533 {
2534 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2535 len = out_pos;
2536 out_pos = 0;
2537 ui_write(out_buf, len);
2538 }
2539}
2540
Bram Moolenaara338adc2018-01-31 20:51:47 +01002541/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002542 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2543 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002544 */
2545 void
2546out_flush_cursor(
2547 int force UNUSED, /* when TRUE, update cursor even when not moved */
2548 int clear_selection UNUSED) /* clear selection under cursor */
2549{
2550 mch_disable_flush();
2551 out_flush();
2552 mch_enable_flush();
2553#ifdef FEAT_GUI
2554 if (gui.in_use)
2555 {
2556 gui_update_cursor(force, clear_selection);
2557 gui_may_flush();
2558 }
2559#endif
2560}
2561
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#if defined(FEAT_MBYTE) || defined(PROTO)
2564/*
2565 * Sometimes a byte out of a multi-byte character is written with out_char().
2566 * To avoid flushing half of the character, call this function first.
2567 */
2568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002569out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2572 out_flush();
2573}
2574#endif
2575
2576#ifdef FEAT_GUI
2577/*
2578 * out_trash(): Throw away the contents of the output buffer
2579 */
2580 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002581out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 out_pos = 0;
2584}
2585#endif
2586
2587/*
2588 * out_char(c): put a byte into the output buffer.
2589 * Flush it if it becomes full.
2590 * This should not be used for outputting text on the screen (use functions
2591 * like msg_puts() and screen_putchar() for that).
2592 */
2593 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002594out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
Bram Moolenaard0573012017-10-28 21:11:06 +02002596#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2598 out_char('\r');
2599#endif
2600
2601 out_buf[out_pos++] = c;
2602
2603 /* For testing we flush each time. */
2604 if (out_pos >= OUT_SIZE || p_wd)
2605 out_flush();
2606}
2607
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002608static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610/*
2611 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2612 */
2613 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002614out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaard0573012017-10-28 21:11:06 +02002616#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2618 out_char_nf('\r');
2619#endif
2620
2621 out_buf[out_pos++] = c;
2622
2623 if (out_pos >= OUT_SIZE)
2624 out_flush();
2625}
2626
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002627#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2628 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
2630 * A never-padding out_str.
2631 * use this whenever you don't want to run the string through tputs.
2632 * tputs above is harmless, but tputs from the termcap library
2633 * is likely to strip off leading digits, that it mistakes for padding
2634 * information, and "%i", "%d", etc.
2635 * This should only be used for writing terminal codes, not for outputting
2636 * normal text (use functions like msg_puts() and screen_putchar() for that).
2637 */
2638 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002639out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2642 out_flush();
2643 while (*s)
2644 out_char_nf(*s++);
2645
2646 /* For testing we write one string at a time. */
2647 if (p_wd)
2648 out_flush();
2649}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002653 * A conditional-flushing out_str, mainly for visualbell.
2654 * Handles a delay internally, because termlib may not respect the delay or do
2655 * it at the wrong time.
2656 * Note: Only for terminal strings.
2657 */
2658 void
2659out_str_cf(char_u *s)
2660{
2661 if (s != NULL && *s)
2662 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002663#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002664 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002665#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002666
2667#ifdef FEAT_GUI
2668 /* Don't use tputs() when GUI is used, ncurses crashes. */
2669 if (gui.in_use)
2670 {
2671 out_str_nf(s);
2672 return;
2673 }
2674#endif
2675 if (out_pos > OUT_SIZE - 20)
2676 out_flush();
2677#ifdef HAVE_TGETENT
2678 for (p = s; *s; ++s)
2679 {
2680 /* flush just before delay command */
2681 if (*s == '$' && *(s + 1) == '<')
2682 {
2683 char_u save_c = *s;
2684 int duration = atoi((char *)s + 2);
2685
2686 *s = NUL;
2687 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2688 *s = save_c;
2689 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002690# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002691 /* Only sleep here if we can limit this happening in
2692 * vim_beep(). */
2693 p = vim_strchr(s, '>');
2694 if (p == NULL || duration <= 0)
2695 {
2696 /* can't parse the time, don't sleep here */
2697 p = s;
2698 }
2699 else
2700 {
2701 ++p;
2702 do_sleep(duration);
2703 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002704# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002705 /* Rely on the terminal library to sleep. */
2706 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002707# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002708 break;
2709 }
2710 }
2711 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2712#else
2713 while (*s)
2714 out_char_nf(*s++);
2715#endif
2716
2717 /* For testing we write one string at a time. */
2718 if (p_wd)
2719 out_flush();
2720 }
2721}
2722
2723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * out_str(s): Put a character string a byte at a time into the output buffer.
2725 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2726 * This should only be used for writing terminal codes, not for outputting
2727 * normal text (use functions like msg_puts() and screen_putchar() for that).
2728 */
2729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 if (s != NULL && *s)
2733 {
2734#ifdef FEAT_GUI
2735 /* Don't use tputs() when GUI is used, ncurses crashes. */
2736 if (gui.in_use)
2737 {
2738 out_str_nf(s);
2739 return;
2740 }
2741#endif
2742 /* avoid terminal strings being split up */
2743 if (out_pos > OUT_SIZE - 20)
2744 out_flush();
2745#ifdef HAVE_TGETENT
2746 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2747#else
2748 while (*s)
2749 out_char_nf(*s++);
2750#endif
2751
2752 /* For testing we write one string at a time. */
2753 if (p_wd)
2754 out_flush();
2755 }
2756}
2757
2758/*
2759 * cursor positioning using termcap parser. (jw)
2760 */
2761 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002762term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 OUT_STR(tgoto((char *)T_CM, col, row));
2765}
2766
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 OUT_STR(tgoto((char *)T_CRI, 0, i));
2771}
2772
2773 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002774term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2777}
2778
2779 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002780term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2783}
2784
2785#if defined(HAVE_TGETENT) || defined(PROTO)
2786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002787term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 /* Can't handle a negative value here */
2790 if (x < 0)
2791 x = 0;
2792 if (y < 0)
2793 y = 0;
2794 OUT_STR(tgoto((char *)T_CWP, y, x));
2795}
2796
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002797# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2798/*
2799 * Return TRUE if we can request the terminal for a response.
2800 */
2801 static int
2802can_get_termresponse()
2803{
2804 return cur_tmode == TMODE_RAW
2805 && termcap_active
2806# ifdef UNIX
2807 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2808# endif
2809 && p_ek;
2810}
2811
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002812static int winpos_x = -1;
2813static int winpos_y = -1;
2814static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002815
2816/*
2817 * Try getting the Vim window position from the terminal.
2818 * Returns OK or FAIL.
2819 */
2820 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002821term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002822{
2823 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002824 int prev_winpos_x = winpos_x;
2825 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002826
2827 if (*T_CGP == NUL || !can_get_termresponse())
2828 return FAIL;
2829 winpos_x = -1;
2830 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002831 ++did_request_winpos;
2832 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002833 OUT_STR(T_CGP);
2834 out_flush();
2835
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002836 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002837 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838 {
2839 (void)vpeekc_nomap();
2840 if (winpos_x >= 0 && winpos_y >= 0)
2841 {
2842 *x = winpos_x;
2843 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002844 return OK;
2845 }
2846 ui_delay(10, FALSE);
2847 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002848 /* Do not reset "did_request_winpos", if we timed out the response might
2849 * still come later and we must consume it. */
2850
2851 winpos_x = prev_winpos_x;
2852 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002853 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002854 {
2855 /* Polling: return previous values if we have them. */
2856 *x = winpos_x;
2857 *y = winpos_y;
2858 return OK;
2859 }
2860
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002861 return FALSE;
2862}
2863# endif
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002866term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002868 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869}
2870#endif
2871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002873term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002876 int i = *s == CSI ? 1 : 2;
2877 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 /* Special handling of 16 colors, because termcap can't handle it */
2880 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2881 /* Also accept CSI instead of <Esc>[ */
2882 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002883 && ((s[0] == ESC && s[1] == '[')
2884#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2885 || (s[0] == ESC && s[1] == '|')
2886#endif
2887 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 && s[i] != NUL
2889 && (STRCMP(s + i + 1, "%p1%dm") == 0
2890 || STRCMP(s + i + 1, "%dm") == 0)
2891 && (s[i] == '3' || s[i] == '4'))
2892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002894 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002896 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002898 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002899#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002900 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002901#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002902 IF_EB("\033[", ESC_STR "[")) : "\233";
2903 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2904 : (n >= 16 ? "48;5;" : "10");
2905
2906 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2908 }
2909 else
2910 OUT_STR(tgoto((char *)s, 0, n));
2911}
2912
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002913 void
2914term_fg_color(int n)
2915{
2916 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2917 if (*T_CAF)
2918 term_color(T_CAF, n);
2919 else if (*T_CSF)
2920 term_color(T_CSF, n);
2921}
2922
2923 void
2924term_bg_color(int n)
2925{
2926 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2927 if (*T_CAB)
2928 term_color(T_CAB, n);
2929 else if (*T_CSB)
2930 term_color(T_CSB, n);
2931}
2932
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002933#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002934
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002935#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2936#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2937#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002938
2939 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002940term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002941{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002942#define MAX_COLOR_STR_LEN 100
2943 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002944
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002945 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002946 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002947 OUT_STR(buf);
2948}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002949
2950 void
2951term_fg_rgb_color(guicolor_T rgb)
2952{
2953 term_rgb_color(T_8F, rgb);
2954}
2955
2956 void
2957term_bg_rgb_color(guicolor_T rgb)
2958{
2959 term_rgb_color(T_8B, rgb);
2960}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002961#endif
2962
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002963#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2964 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965/*
2966 * Generic function to set window title, using t_ts and t_fs.
2967 */
2968 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002969term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 /* t_ts takes one argument: column in status line */
2972 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2973 out_str_nf(title);
2974 out_str(T_FS); /* set title end */
2975 out_flush();
2976}
2977#endif
2978
2979/*
2980 * Make sure we have a valid set or terminal options.
2981 * Replace all entries that are NULL by empty_option
2982 */
2983 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002984ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002986 char_u *env_colors;
2987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 check_options(); /* make sure no options are NULL */
2989
2990 /*
2991 * MUST have "cm": cursor motion.
2992 */
2993 if (*T_CM == NUL)
2994 EMSG(_("E437: terminal capability \"cm\" required"));
2995
2996 /*
2997 * if "cs" defined, use a scroll region, it's faster.
2998 */
2999 if (*T_CS != NUL)
3000 scroll_region = TRUE;
3001 else
3002 scroll_region = FALSE;
3003
3004 if (pairs)
3005 {
3006 /*
3007 * optional pairs
3008 */
3009 /* TP goes to normal mode for TI (invert) and TB (bold) */
3010 if (*T_ME == NUL)
3011 T_ME = T_MR = T_MD = T_MB = empty_option;
3012 if (*T_SO == NUL || *T_SE == NUL)
3013 T_SO = T_SE = empty_option;
3014 if (*T_US == NUL || *T_UE == NUL)
3015 T_US = T_UE = empty_option;
3016 if (*T_CZH == NUL || *T_CZR == NUL)
3017 T_CZH = T_CZR = empty_option;
3018
3019 /* T_VE is needed even though T_VI is not defined */
3020 if (*T_VE == NUL)
3021 T_VI = empty_option;
3022
3023 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3024 if (*T_ME == NUL)
3025 {
3026 T_ME = T_SE;
3027 T_MR = T_SO;
3028 T_MD = T_SO;
3029 }
3030
3031 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3032 if (*T_SO == NUL)
3033 {
3034 T_SE = T_ME;
3035 if (*T_MR == NUL)
3036 T_SO = T_MD;
3037 else
3038 T_SO = T_MR;
3039 }
3040
3041 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3042 if (*T_CZH == NUL)
3043 {
3044 T_CZR = T_ME;
3045 if (*T_MR == NUL)
3046 T_CZH = T_MD;
3047 else
3048 T_CZH = T_MR;
3049 }
3050
3051 /* "Sb" and "Sf" come in pairs */
3052 if (*T_CSB == NUL || *T_CSF == NUL)
3053 {
3054 T_CSB = empty_option;
3055 T_CSF = empty_option;
3056 }
3057
3058 /* "AB" and "AF" come in pairs */
3059 if (*T_CAB == NUL || *T_CAF == NUL)
3060 {
3061 T_CAB = empty_option;
3062 T_CAF = empty_option;
3063 }
3064
3065 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3066 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003067 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /* Set 'weirdinvert' according to value of 't_xs' */
3070 p_wiv = (*T_XS != NUL);
3071 }
3072 need_gather = TRUE;
3073
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003074 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003076 env_colors = mch_getenv((char_u *)"COLORS");
3077 if (env_colors != NULL && isdigit(*env_colors))
3078 {
3079 int colors = atoi((char *)env_colors);
3080
3081 if (colors != t_colors)
3082 set_color_count(colors);
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084}
3085
3086#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3087 || defined(PROTO)
3088/*
3089 * Represent the given long_u as individual bytes, with the most significant
3090 * byte first, and store them in dst.
3091 */
3092 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003093add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 int i;
3096 int shift;
3097
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003098 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
3100 shift = 8 * (sizeof(long_u) - i);
3101 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3102 }
3103}
3104
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003105static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107/*
3108 * Interpret the next string of bytes in buf as a long integer, with the most
3109 * significant byte first. Note that it is assumed that buf has been through
3110 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3111 * Puts result in val, and returns the number of bytes read from buf
3112 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3113 * were present.
3114 */
3115 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003116get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 int len;
3119 char_u bytes[sizeof(long_u)];
3120 int i;
3121 int shift;
3122
3123 *val = 0;
3124 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3125 if (len != -1)
3126 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003127 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 shift = 8 * (sizeof(long_u) - 1 - i);
3130 *val += (long_u)bytes[i] << shift;
3131 }
3132 }
3133 return len;
3134}
3135#endif
3136
3137#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003138 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3139 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
3141 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3142 * that buf has been through inchar(). Returns the actual number of bytes used
3143 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3144 * available.
3145 */
3146 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003147get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 int len = 0;
3150 int i;
3151 char_u c;
3152
3153 for (i = 0; i < num_bytes; i++)
3154 {
3155 if ((c = buf[len++]) == NUL)
3156 return -1;
3157 if (c == K_SPECIAL)
3158 {
3159 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3160 return -1;
3161 if (buf[len++] == (int)KS_ZERO)
3162 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003163 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3164 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3165 if (buf[len++] == (int)KE_CSI)
3166 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003168 else if (c == CSI && buf[len] == KS_EXTRA
3169 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003170 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3171 * the start of a special key, see add_to_input_buf_csi(). */
3172 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 bytes[i] = c;
3174 }
3175 return len;
3176}
3177#endif
3178
3179/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003180 * Check if the new shell size is valid, correct it if it's too small or way
3181 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 */
3183 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003184check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (Rows < min_rows()) /* need room for one window and command line */
3187 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003188 limit_screen_size();
3189}
3190
3191/*
3192 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3193 */
3194 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003195limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003196{
3197 if (Columns < MIN_COLUMNS)
3198 Columns = MIN_COLUMNS;
3199 else if (Columns > 10000)
3200 Columns = 10000;
3201 if (Rows > 1000)
3202 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203}
3204
Bram Moolenaar86b68352004-12-27 21:59:20 +00003205/*
3206 * Invoked just before the screen structures are going to be (re)allocated.
3207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003209win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 static int old_Rows = 0;
3212 static int old_Columns = 0;
3213
3214 if (old_Rows != Rows || old_Columns != Columns)
3215 ui_new_shellsize();
3216 if (old_Rows != Rows)
3217 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003218 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003219 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003220 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 old_Rows = Rows;
3222 shell_new_rows(); /* update window sizes */
3223 }
3224 if (old_Columns != Columns)
3225 {
3226 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229}
3230
3231/*
3232 * Call this function when the Vim shell has been resized in any way.
3233 * Will obtain the current size and redraw (also when size didn't change).
3234 */
3235 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003236shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 set_shellsize(0, 0, FALSE);
3239}
3240
3241/*
3242 * Check if the shell size changed. Handle a resize.
3243 * When the size didn't change, nothing happens.
3244 */
3245 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003246shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 int old_Rows = Rows;
3249 int old_Columns = Columns;
3250
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003251 if (!exiting
3252#ifdef FEAT_GUI
3253 /* Do not get the size when executing a shell command during
3254 * startup. */
3255 && !gui.starting
3256#endif
3257 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003258 {
3259 (void)ui_get_shellsize();
3260 check_shellsize();
3261 if (old_Rows != Rows || old_Columns != Columns)
3262 shell_resized();
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
3266/*
3267 * Set size of the Vim shell.
3268 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3269 * window size (this is used for the :win command).
3270 * If 'mustset' is FALSE, we may try to get the real window size and if
3271 * it fails use 'width' and 'height'.
3272 */
3273 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003274set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 static int busy = FALSE;
3277
3278 /*
3279 * Avoid recursiveness, can happen when setting the window size causes
3280 * another window-changed signal.
3281 */
3282 if (busy)
3283 return;
3284
3285 if (width < 0 || height < 0) /* just checking... */
3286 return;
3287
Bram Moolenaara971b822011-09-14 14:43:25 +02003288 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003290 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 State = SETWSIZE;
3292 return;
3293 }
3294
Bram Moolenaara971b822011-09-14 14:43:25 +02003295 /* curwin->w_buffer can be NULL when we are closing a window and the
3296 * buffer has already been closed and removing a scrollbar causes a resize
3297 * event. Don't resize then, it will happen after entering another buffer.
3298 */
3299 if (curwin->w_buffer == NULL)
3300 return;
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 ++busy;
3303
3304#ifdef AMIGA
3305 out_flush(); /* must do this before mch_get_shellsize() for
3306 some obscure reason */
3307#endif
3308
3309 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3310 {
3311 Rows = height;
3312 Columns = width;
3313 check_shellsize();
3314 ui_set_shellsize(mustset);
3315 }
3316 else
3317 check_shellsize();
3318
3319 /* The window layout used to be adjusted here, but it now happens in
3320 * screenalloc() (also invoked from screenclear()). That is because the
3321 * "busy" check above may skip this, but not screenalloc(). */
3322
3323 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3324 screenclear();
3325 else
3326 screen_start(); /* don't know where cursor is now */
3327
3328 if (starting != NO_SCREEN)
3329 {
3330#ifdef FEAT_TITLE
3331 maketitle();
3332#endif
3333 changed_line_abv_curs();
3334 invalidate_botline();
3335
3336 /*
3337 * We only redraw when it's needed:
3338 * - While at the more prompt or executing an external command, don't
3339 * redraw, but position the cursor.
3340 * - While editing the command line, only redraw that.
3341 * - in Ex mode, don't redraw anything.
3342 * - Otherwise, redraw right now, and position the cursor.
3343 * Always need to call update_screen() or screenalloc(), to make
3344 * sure Rows/Columns and the size of ScreenLines[] is correct!
3345 */
3346 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3347 || exmode_active)
3348 {
3349 screenalloc(FALSE);
3350 repeat_message();
3351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
3353 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003354 if (curwin->w_p_scb)
3355 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003356 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003357 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003358 update_screen(NOT_VALID);
3359 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003360 }
3361 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003362 {
3363 update_topline();
3364#if defined(FEAT_INS_EXPAND)
3365 if (pum_visible())
3366 {
3367 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003368 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003369 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003370#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003371 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003372 if (redrawing())
3373 setcursor();
3374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 cursor_on(); /* redrawing may have switched it off */
3377 }
3378 out_flush();
3379 --busy;
3380}
3381
3382/*
3383 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3384 * commands and Ex mode).
3385 */
3386 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003387settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389#ifdef FEAT_GUI
3390 /* don't set the term where gvim was started to any mode */
3391 if (gui.in_use)
3392 return;
3393#endif
3394
3395 if (full_screen)
3396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 /*
3398 * When returning after calling a shell we want to really set the
3399 * terminal to raw mode, even though we think it already is, because
3400 * the shell program may have reset the terminal mode.
3401 * When we think the terminal is normal, don't try to set it to
3402 * normal again, because that causes problems (logout!) on some
3403 * machines.
3404 */
3405 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3406 {
3407#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003408# ifdef FEAT_GUI
3409 if (!gui.in_use && !gui.starting)
3410# endif
3411 {
3412 /* May need to check for T_CRV response and termcodes, it
3413 * doesn't work in Cooked mode, an external program may get
3414 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003415 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3416 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003417#ifdef FEAT_TERMINAL
3418 || rfg_status == STATUS_SENT
3419#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003420 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003421 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003422 || rcs_status == STATUS_SENT
3423 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003424 (void)vpeekc_nomap();
3425 check_for_codes_from_term();
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#endif
3428#ifdef FEAT_MOUSE_TTY
3429 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003430 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003432 if (tmode != TMODE_RAW)
3433 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003435 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 cur_tmode = tmode;
3437#ifdef FEAT_MOUSE
3438 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003439 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003441 if (tmode == TMODE_RAW)
3442 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 out_flush();
3444 }
3445#ifdef FEAT_TERMRESPONSE
3446 may_req_termresponse();
3447#endif
3448 }
3449}
3450
3451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 if (full_screen && !termcap_active)
3455 {
3456 out_str(T_TI); /* start termcap mode */
3457 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003458 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 out_flush();
3460 termcap_active = TRUE;
3461 screen_start(); /* don't know where cursor is now */
3462#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003463# ifdef FEAT_GUI
3464 if (!gui.in_use && !gui.starting)
3465# endif
3466 {
3467 may_req_termresponse();
3468 /* Immediately check for a response. If t_Co changes, we don't
3469 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003470 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003471 check_for_codes_from_term();
3472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474 }
3475}
3476
3477 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003478stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 screen_stop_highlight();
3481 reset_cterm_colors();
3482 if (termcap_active)
3483 {
3484#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003485# ifdef FEAT_GUI
3486 if (!gui.in_use && !gui.starting)
3487# endif
3488 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003489 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003490 if (crv_status == STATUS_SENT
3491 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003492# ifdef FEAT_TERMINAL
3493 || rfg_status == STATUS_SENT
3494# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003495 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003496 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003497 || rcs_status == STATUS_SENT
3498 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003499 {
3500# ifdef UNIX
3501 /* Give the terminal a chance to respond. */
3502 mch_delay(100L, FALSE);
3503# endif
3504# ifdef TCIFLUSH
3505 /* Discard data received but not read. */
3506 if (exiting)
3507 tcflush(fileno(stdin), TCIFLUSH);
3508# endif
3509 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003510 /* Check for termcodes first, otherwise an external program may
3511 * get them. */
3512 check_for_codes_from_term();
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003515 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 out_str(T_KE); /* stop "keypad transmit" mode */
3517 out_flush();
3518 termcap_active = FALSE;
3519 cursor_on(); /* just in case it is still off */
3520 out_str(T_TE); /* stop termcap mode */
3521 screen_start(); /* don't know where cursor is now */
3522 out_flush();
3523 }
3524}
3525
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003526#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527/*
3528 * Request version string (for xterm) when needed.
3529 * Only do this after switching to raw mode, otherwise the result will be
3530 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003531 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003532 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Only do this after termcap mode has been started, otherwise the codes for
3534 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003535 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3536 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003537 * On Unix only do it when both output and input are a tty (avoid writing
3538 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 * The result is caught in check_termcode().
3540 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003542may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003544 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003545 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003546 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 && *T_CRV != NUL)
3548 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003549 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003551 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* check for the characters now, otherwise they might be eaten by
3553 * get_keystroke() */
3554 out_flush();
3555 (void)vpeekc_nomap();
3556 }
3557}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003558
3559# if defined(FEAT_MBYTE) || defined(PROTO)
3560/*
3561 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003562 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003563 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003564 * If the terminal treats \u25bd as single width, the position is (1, 1),
3565 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003566 * This function has the side effect that changes cursor position, so
3567 * it must be called immediately after entering termcap mode.
3568 */
3569 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003570may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003571{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003572 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003573 && can_get_termresponse()
3574 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003575 && *T_U7 != NUL
3576 && !option_was_set((char_u *)"ambiwidth"))
3577 {
3578 char_u buf[16];
3579
Bram Moolenaarb255b902018-04-24 21:40:10 +02003580 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003581 /* Do this in the second row. In the first row the returned sequence
3582 * may be CSI 1;2R, which is the same as <S-F3>. */
3583 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003584 buf[mb_char2bytes(0x25bd, buf)] = 0;
3585 out_str(buf);
3586 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003587 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003588 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003589
3590 /* This overwrites a few characters on the screen, a redraw is needed
3591 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003592 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003593 out_str((char_u *)" ");
3594 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003595
Bram Moolenaar05684312017-12-09 15:11:24 +01003596 /* Need to reset the known cursor position. */
3597 screen_start();
3598
Bram Moolenaar9584b312013-03-13 19:29:28 +01003599 /* check for the characters now, otherwise they might be eaten by
3600 * get_keystroke() */
3601 out_flush();
3602 (void)vpeekc_nomap();
3603 }
3604}
3605# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003606
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003607/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003608 * Similar to requesting the version string: Request the terminal background
3609 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003610 */
3611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003612may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003613{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003614 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003615 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003616 int didit = FALSE;
3617
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003618# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003619 /* Only request foreground if t_RF is set. */
3620 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3621 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003622 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003623 out_str(T_RFG);
3624 rfg_status = STATUS_SENT;
3625 didit = TRUE;
3626 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003627# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003628
3629 /* Only request background if t_RB is set. */
3630 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003631 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003632 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003633 out_str(T_RBG);
3634 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003635 didit = TRUE;
3636 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003637
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003638 if (didit)
3639 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003640 /* check for the characters now, otherwise they might be eaten by
3641 * get_keystroke() */
3642 out_flush();
3643 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003644 }
3645 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003646}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003647
Bram Moolenaar2951b772013-07-03 12:45:31 +02003648# ifdef DEBUG_TERMRESPONSE
3649 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003650log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003651{
3652 static FILE *fd_tr = NULL;
3653 static proftime_T start;
3654 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003655 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003656
3657 if (fd_tr == NULL)
3658 {
3659 fd_tr = fopen("termresponse.log", "w");
3660 profile_start(&start);
3661 }
3662 now = start;
3663 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003664 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3665 must_redraw == NOT_VALID ? "NV"
3666 : must_redraw == CLEAR ? "CL" : " ");
3667 va_start(ap, fmt);
3668 vfprintf(fd_tr, fmt, ap);
3669 va_end(ap);
3670 fputc('\n', fd_tr);
3671 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003672}
3673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674#endif
3675
3676/*
3677 * Return TRUE when saving and restoring the screen.
3678 */
3679 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003680swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 return (full_screen && *T_TI != NUL);
3683}
3684
Bram Moolenaarecadf432018-03-20 11:17:04 +01003685#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686/*
3687 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3688 */
3689 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003690setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692# ifdef FEAT_MOUSE_TTY
3693 int checkfor;
3694# endif
3695
3696# ifdef FEAT_MOUSESHAPE
3697 update_mouseshape(-1);
3698# endif
3699
3700# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3701# ifdef FEAT_GUI
3702 /* In the GUI the mouse is always enabled. */
3703 if (gui.in_use)
3704 return;
3705# endif
3706 /* be quick when mouse is off */
3707 if (*p_mouse == NUL || has_mouse_termcode == 0)
3708 return;
3709
3710 /* don't switch mouse on when not in raw mode (Ex mode) */
3711 if (cur_tmode != TMODE_RAW)
3712 {
3713 mch_setmouse(FALSE);
3714 return;
3715 }
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (VIsual_active)
3718 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003719 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 checkfor = MOUSE_RETURN;
3721 else if (State & INSERT)
3722 checkfor = MOUSE_INSERT;
3723 else if (State & CMDLINE)
3724 checkfor = MOUSE_COMMAND;
3725 else if (State == CONFIRM || State == EXTERNCMD)
3726 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3727 else
3728 checkfor = MOUSE_NORMAL; /* assume normal mode */
3729
3730 if (mouse_has(checkfor))
3731 mch_setmouse(TRUE);
3732 else
3733 mch_setmouse(FALSE);
3734# endif
3735}
3736
3737/*
3738 * Return TRUE if
3739 * - "c" is in 'mouse', or
3740 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3741 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3742 * normal editing mode (not at hit-return message).
3743 */
3744 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003745mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 char_u *p;
3748
3749 for (p = p_mouse; *p; ++p)
3750 switch (*p)
3751 {
3752 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3753 return TRUE;
3754 break;
3755 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3756 return TRUE;
3757 break;
3758 default: if (c == *p) return TRUE; break;
3759 }
3760 return FALSE;
3761}
3762
3763/*
3764 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3765 */
3766 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003767mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 return (p_mousem[0] == 'p');
3770}
3771#endif
3772
3773/*
3774 * By outputting the 'cursor very visible' termcap code, for some windowed
3775 * terminals this makes the screen scrolled to the correct position.
3776 * Used when starting Vim or returning from a shell.
3777 */
3778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003779scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003781 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003784 out_str(T_CVS);
3785 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787}
3788
3789static int cursor_is_off = FALSE;
3790
3791/*
Bram Moolenaar917e32b2018-08-07 17:38:41 +02003792 * Enable the cursor without checking if it's already enabled.
3793 */
3794 void
3795cursor_on_force(void)
3796{
3797 out_str(T_VE);
3798 cursor_is_off = FALSE;
3799}
3800
3801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 * Enable the cursor.
3803 */
3804 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003805cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 if (cursor_is_off)
Bram Moolenaar917e32b2018-08-07 17:38:41 +02003808 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809}
3810
3811/*
3812 * Disable the cursor.
3813 */
3814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003815cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003817 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003819 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 cursor_is_off = TRUE;
3821 }
3822}
3823
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003824#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003826 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 */
3828 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003829term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003830{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003831 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003832 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003833
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003834 /* Only do something when redrawing the screen and we can restore the
3835 * mode. */
3836 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003837 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003838# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003839 if (forced && initial_cursor_shape > 0)
3840 /* Restore to initial values. */
3841 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003842# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003843 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003844 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003845
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003846 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003847 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003848 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003849 {
3850 if (*T_CSR != NUL)
3851 p = T_CSR; /* Replace mode cursor */
3852 else
3853 p = T_CSI; /* fall back to Insert mode cursor */
3854 if (*p != NUL)
3855 {
3856 out_str(p);
3857 showing_mode = REPLACE;
3858 }
3859 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003860 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003861 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003862 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003863 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003864 {
3865 out_str(T_CSI); /* Insert mode cursor */
3866 showing_mode = INSERT;
3867 }
3868 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003869 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003870 {
3871 out_str(T_CEI); /* non-Insert mode cursor */
3872 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003873 }
3874}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003875
3876# if defined(FEAT_TERMINAL) || defined(PROTO)
3877 void
3878term_cursor_color(char_u *color)
3879{
3880 if (*T_CSC != NUL)
3881 {
3882 out_str(T_CSC); /* set cursor color start */
3883 out_str_nf(color);
3884 out_str(T_CEC); /* set cursor color end */
3885 out_flush();
3886 }
3887}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003888# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003889
Bram Moolenaar4db25542017-08-28 22:43:05 +02003890 int
3891blink_state_is_inverted()
3892{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003893#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003894 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3895 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003896#else
3897 return FALSE;
3898#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003899}
3900
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003901/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003902 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003903 */
3904 void
3905term_cursor_shape(int shape, int blink)
3906{
3907 if (*T_CSH != NUL)
3908 {
3909 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3910 out_flush();
3911 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003912 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003913 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003914 int do_blink = blink;
3915
3916 /* t_SH is empty: try setting just the blink state.
3917 * The blink flags are XORed together, if the initial blinking from
3918 * style and shape differs, we need to invert the flag here. */
3919 if (blink_state_is_inverted())
3920 do_blink = !blink;
3921
3922 if (do_blink && *T_VS != NUL)
3923 {
3924 out_str(T_VS);
3925 out_flush();
3926 }
3927 else if (!do_blink && *T_CVS != NUL)
3928 {
3929 out_str(T_CVS);
3930 out_flush();
3931 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003932 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003933}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003934#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003935
3936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 * Set scrolling region for window 'wp'.
3938 * The region starts 'off' lines from the start of the window.
3939 * Also set the vertical scroll region for a vertically split window. Always
3940 * the full width of the window, excluding the vertical separator.
3941 */
3942 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003943scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3946 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003948 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3949 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 screen_start(); /* don't know where cursor is now */
3951}
3952
3953/*
3954 * Reset scrolling region to the whole screen.
3955 */
3956 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003957scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (*T_CSV != NUL)
3961 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 screen_start(); /* don't know where cursor is now */
3963}
3964
3965
3966/*
3967 * List of terminal codes that are currently recognized.
3968 */
3969
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003970static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
3972 char_u name[2]; /* termcap name of entry */
3973 char_u *code; /* terminal code (in allocated memory) */
3974 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003975 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976} *termcodes = NULL;
3977
3978static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3979static int tc_len = 0; /* current number of entries in termcodes[] */
3980
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003981static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003984clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
3986 while (tc_len > 0)
3987 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003988 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 tc_max_len = 0;
3990
3991#ifdef HAVE_TGETENT
3992 BC = (char *)empty_option;
3993 UP = (char *)empty_option;
3994 PC = NUL; /* set pad character to NUL */
3995 ospeed = 0;
3996#endif
3997
3998 need_gather = TRUE; /* need to fill termleader[] */
3999}
4000
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001#define ATC_FROM_TERM 55
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003/*
4004 * Add a new entry to the list of terminal codes.
4005 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4007 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
4009 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004010add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
4012 struct termcode *new_tc;
4013 int i, j;
4014 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004015 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (string == NULL || *string == NUL)
4018 {
4019 del_termcode(name);
4020 return;
4021 }
4022
Bram Moolenaar45500912014-07-09 20:51:07 +02004023#if defined(WIN3264) && !defined(FEAT_GUI)
4024 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4025#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (s == NULL)
4029 return;
4030
4031 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004032 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004034 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 s[0] = term_7to8bit(string);
4036 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004037
4038#if defined(WIN3264) && !defined(FEAT_GUI)
4039 if (s[0] == K_NUL)
4040 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004041 STRMOVE(s + 1, s);
4042 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004043 }
4044#endif
4045
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004046 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 need_gather = TRUE; /* need to fill termleader[] */
4049
4050 /*
4051 * need to make space for more entries
4052 */
4053 if (tc_len == tc_max_len)
4054 {
4055 tc_max_len += 20;
4056 new_tc = (struct termcode *)alloc(
4057 (unsigned)(tc_max_len * sizeof(struct termcode)));
4058 if (new_tc == NULL)
4059 {
4060 tc_max_len -= 20;
4061 return;
4062 }
4063 for (i = 0; i < tc_len; ++i)
4064 new_tc[i] = termcodes[i];
4065 vim_free(termcodes);
4066 termcodes = new_tc;
4067 }
4068
4069 /*
4070 * Look for existing entry with the same name, it is replaced.
4071 * Look for an existing entry that is alphabetical higher, the new entry
4072 * is inserted in front of it.
4073 */
4074 for (i = 0; i < tc_len; ++i)
4075 {
4076 if (termcodes[i].name[0] < name[0])
4077 continue;
4078 if (termcodes[i].name[0] == name[0])
4079 {
4080 if (termcodes[i].name[1] < name[1])
4081 continue;
4082 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 */
4085 if (termcodes[i].name[1] == name[1])
4086 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004087 if (flags == ATC_FROM_TERM && (j = termcode_star(
4088 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004089 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090 /* Don't replace ESC[123;*X or ESC O*X with another when
4091 * invoked from got_code_from_term(). */
4092 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004093 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 && s[len - 1]
4095 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004096 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004098 vim_free(s);
4099 return;
4100 }
4101 }
4102 else
4103 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004104 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004105 vim_free(termcodes[i].code);
4106 --tc_len;
4107 break;
4108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 }
4111 /*
4112 * Found alphabetical larger entry, move rest to insert new entry
4113 */
4114 for (j = tc_len; j > i; --j)
4115 termcodes[j] = termcodes[j - 1];
4116 break;
4117 }
4118
4119 termcodes[i].name[0] = name[0];
4120 termcodes[i].name[1] = name[1];
4121 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004122 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004123
4124 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4125 * accept modifiers. */
4126 termcodes[i].modlen = 0;
4127 j = termcode_star(s, len);
4128 if (j > 0)
4129 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 ++tc_len;
4131}
4132
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004134 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004135 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004136 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 */
4138 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004139termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004140{
4141 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4142 if (len >= 3 && code[len - 2] == '*')
4143 {
4144 if (len >= 5 && code[len - 3] == ';')
4145 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004146 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 return 1;
4148 }
4149 return 0;
4150}
4151
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004153find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 int i;
4156
4157 for (i = 0; i < tc_len; ++i)
4158 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4159 return termcodes[i].code;
4160 return NULL;
4161}
4162
4163#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4164 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004165get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 if (i >= tc_len)
4168 return NULL;
4169 return &termcodes[i].name[0];
4170}
4171#endif
4172
4173 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004174del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 int i;
4177
4178 if (termcodes == NULL) /* nothing there yet */
4179 return;
4180
4181 need_gather = TRUE; /* need to fill termleader[] */
4182
4183 for (i = 0; i < tc_len; ++i)
4184 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4185 {
4186 del_termcode_idx(i);
4187 return;
4188 }
4189 /* not found. Give error message? */
4190}
4191
4192 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004193del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
4195 int i;
4196
4197 vim_free(termcodes[idx].code);
4198 --tc_len;
4199 for (i = idx; i < tc_len; ++i)
4200 termcodes[i] = termcodes[i + 1];
4201}
4202
4203#ifdef FEAT_TERMRESPONSE
4204/*
4205 * Called when detected that the terminal sends 8-bit codes.
4206 * Convert all 7-bit codes to their 8-bit equivalent.
4207 */
4208 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004209switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 int i;
4212 int c;
4213
4214 /* Only need to do something when not already using 8-bit codes. */
4215 if (!term_is_8bit(T_NAME))
4216 {
4217 for (i = 0; i < tc_len; ++i)
4218 {
4219 c = term_7to8bit(termcodes[i].code);
4220 if (c != 0)
4221 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004222 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 termcodes[i].code[0] = c;
4224 }
4225 }
4226 need_gather = TRUE; /* need to fill termleader[] */
4227 }
4228 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004229 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231#endif
4232
4233#ifdef CHECK_DOUBLE_CLICK
4234static linenr_T orig_topline = 0;
4235# ifdef FEAT_DIFF
4236static int orig_topfill = 0;
4237# endif
4238#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004239#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240/*
4241 * Checking for double clicks ourselves.
4242 * "orig_topline" is used to avoid detecting a double-click when the window
4243 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4244 */
4245/*
4246 * Set orig_topline. Used when jumping to another window, so that a double
4247 * click still works.
4248 */
4249 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004250set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 orig_topline = wp->w_topline;
4253# ifdef FEAT_DIFF
4254 orig_topfill = wp->w_topfill;
4255# endif
4256}
4257#endif
4258
4259/*
4260 * Check if typebuf.tb_buf[] contains a terminal key code.
4261 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4262 * + max_offset].
4263 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004264 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 * With a match, the match is removed, the replacement code is inserted in
4266 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4267 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004268 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4269 * "buflen" is then the length of the string in buf[] and is updated for
4270 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 */
4272 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004273check_termcode(
4274 int max_offset,
4275 char_u *buf,
4276 int bufsize,
4277 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 char_u *tp;
4280 char_u *p;
4281 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004284 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int offset;
4286 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004288 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004289 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 int new_slen;
4291 int extra;
4292 char_u string[MAX_KEY_CODE_LEN + 1];
4293 int i, j;
4294 int idx = 0;
4295#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004296# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4297 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u bytes[6];
4299 int num_bytes;
4300# endif
4301 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int is_click, is_drag;
4303 int wheel_code = 0;
4304 int current_button;
4305 static int held_button = MOUSE_RELEASE;
4306 static int orig_num_clicks = 1;
4307 static int orig_mouse_code = 0x0;
4308# ifdef CHECK_DOUBLE_CLICK
4309 static int orig_mouse_col = 0;
4310 static int orig_mouse_row = 0;
4311 static struct timeval orig_mouse_time = {0, 0};
4312 /* time of previous mouse click */
4313 struct timeval mouse_time; /* time of current mouse click */
4314 long timediff; /* elapsed time in msec */
4315# endif
4316#endif
4317 int cpo_koffset;
4318#ifdef FEAT_MOUSE_GPM
4319 extern int gpm_flag; /* gpm library variable */
4320#endif
4321
4322 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4323
4324 /*
4325 * Speed up the checks for terminal codes by gathering all first bytes
4326 * used in termleader[]. Often this is just a single <Esc>.
4327 */
4328 if (need_gather)
4329 gather_termleader();
4330
4331 /*
4332 * Check at several positions in typebuf.tb_buf[], to catch something like
4333 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4334 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004335 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 * This is used often, KEEP IT FAST!
4337 */
4338 for (offset = 0; offset < max_offset; ++offset)
4339 {
4340 if (buf == NULL)
4341 {
4342 if (offset >= typebuf.tb_len)
4343 break;
4344 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4345 len = typebuf.tb_len - offset; /* length of the input */
4346 }
4347 else
4348 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004349 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004352 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354
4355 /*
4356 * Don't check characters after K_SPECIAL, those are already
4357 * translated terminal chars (avoid translating ~@^Hx).
4358 */
4359 if (*tp == K_SPECIAL)
4360 {
4361 offset += 2; /* there are always 2 extra characters */
4362 continue;
4363 }
4364
4365 /*
4366 * Skip this position if the character does not appear as the first
4367 * character in term_strings. This speeds up a lot, since most
4368 * termcodes start with the same character (ESC or CSI).
4369 */
4370 i = *tp;
4371 for (p = termleader; *p && *p != i; ++p)
4372 ;
4373 if (*p == NUL)
4374 continue;
4375
4376 /*
4377 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4378 * are in Insert mode.
4379 */
4380 if (*tp == ESC && !p_ek && (State & INSERT))
4381 continue;
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004384 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004385 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387#ifdef FEAT_GUI
4388 if (gui.in_use)
4389 {
4390 /*
4391 * GUI special key codes are all of the form [CSI xx].
4392 */
4393 if (*tp == CSI) /* Special key from GUI */
4394 {
4395 if (len < 3)
4396 return -1; /* Shouldn't happen */
4397 slen = 3;
4398 key_name[0] = tp[1];
4399 key_name[1] = tp[2];
4400 }
4401 }
4402 else
4403#endif /* FEAT_GUI */
4404 {
4405 for (idx = 0; idx < tc_len; ++idx)
4406 {
4407 /*
4408 * Ignore the entry if we are not at the start of
4409 * typebuf.tb_buf[]
4410 * and there are not enough characters to make a match.
4411 * But only when the 'K' flag is in 'cpoptions'.
4412 */
4413 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004414 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (cpo_koffset && offset && len < slen)
4416 continue;
4417 if (STRNCMP(termcodes[idx].code, tp,
4418 (size_t)(slen > len ? len : slen)) == 0)
4419 {
4420 if (len < slen) /* got a partial sequence */
4421 return -1; /* need to get more chars */
4422
4423 /*
4424 * When found a keypad key, check if there is another key
4425 * that matches and use that one. This makes <Home> to be
4426 * found instead of <kHome> when they produce the same
4427 * key code.
4428 */
4429 if (termcodes[idx].name[0] == 'K'
4430 && VIM_ISDIGIT(termcodes[idx].name[1]))
4431 {
4432 for (j = idx + 1; j < tc_len; ++j)
4433 if (termcodes[j].len == slen &&
4434 STRNCMP(termcodes[idx].code,
4435 termcodes[j].code, slen) == 0)
4436 {
4437 idx = j;
4438 break;
4439 }
4440 }
4441
4442 key_name[0] = termcodes[idx].name[0];
4443 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004446
4447 /*
4448 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 * <Esc>[123;*X (modslen == slen - 3)
4450 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4451 * When there is a modifier the * matches a number.
4452 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004453 */
4454 if (termcodes[idx].modlen > 0)
4455 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 modslen = termcodes[idx].modlen;
4457 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004458 continue;
4459 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004461 {
4462 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004463
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004464 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004465 return -1; /* need to get more chars */
4466
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004467 if (tp[modslen] == termcodes[idx].code[slen - 1])
4468 slen = modslen + 1; /* no modifiers */
4469 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004470 continue; /* no match */
4471 else
4472 {
4473 /* Skip over the digits, the final char must
4474 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004475 for (j = slen - 2; j < len && (isdigit(tp[j])
4476 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004477 ;
4478 ++j;
4479 if (len < j) /* got a partial sequence */
4480 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004481 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4482 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004483
Bram Moolenaara529ce02017-06-22 22:37:57 +02004484 modifiers_start = tp + slen - 2;
4485
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004486 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004487 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004488 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004489 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004490 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004492 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004493 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004494 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004495 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004496
4497 slen = j;
4498 }
4499 key_name[0] = termcodes[idx].name[0];
4500 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004501 break;
4502 }
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 }
4506
4507#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004508 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004509 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004510 * detecting the start of these mouse codes they might as well be
4511 * another key code or terminal response. */
4512# ifdef FEAT_MOUSE_DEC
4513 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004514# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004515# ifdef FEAT_MOUSE_PTERM
4516 || key_name[0] == KS_PTERM_MOUSE
4517# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004518 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004520 /* Check for some responses from the terminal starting with
4521 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004522 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004523 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004524 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004525 * Also eat other possible responses to t_RV, rxvt returns
4526 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4527 * mrxvt has been reported to have "+" in the version. Assume
4528 * the escape sequence ends with a letter or one of "{|}~".
4529 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004530 * - Cursor position report: <Esc>[{row};{col}R
4531 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004532 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004533 *
4534 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004535 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004536 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004537
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004538 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004539 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004540 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004541 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004543 int col = 0;
4544 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004545#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004546 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004547#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004550 for (i = 2 + (tp[0] != CSI); i < len
4551 && !(tp[i] >= '{' && tp[i] <= '~')
4552 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004553 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004554 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004555 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004556#ifdef FEAT_MBYTE
4557 row_char = tp[i - 1];
4558#endif
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004561 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004562 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004563 return -1;
4564 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004565 if (extra > 0)
4566 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004567
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004568#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004569 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4570 * u7_status is not "sent", it may be from a previous Vim that
4571 * just exited. But not for <S-F3>, it sends something
4572 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004573 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004574 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004575 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004576 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004577 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004578
Bram Moolenaarb255b902018-04-24 21:40:10 +02004579 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004580 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004581 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004582 if (col == 2)
4583 aw = "single";
4584 else if (col == 3)
4585 aw = "double";
4586 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4587 {
4588 /* Setting the option causes a screen redraw. Do
4589 * that right away if possible, keeping any
4590 * messages. */
4591 set_option_value((char_u *)"ambw", 0L,
4592 (char_u *)aw, 0);
4593# ifdef DEBUG_TERMRESPONSE
4594 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004595 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004596
Bram Moolenaarb255b902018-04-24 21:40:10 +02004597 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004598 }
4599# else
4600 redraw_asap(CLEAR);
4601# endif
4602 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004603 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004604 key_name[0] = (int)KS_EXTRA;
4605 key_name[1] = (int)KE_IGNORE;
4606 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004607# ifdef FEAT_EVAL
4608 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4609# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004610 }
4611 else
4612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004614 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004616 int version = col;
4617
Bram Moolenaarb255b902018-04-24 21:40:10 +02004618 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004619 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004620 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 /* If this code starts with CSI, you can bet that the
4623 * terminal uses 8-bit codes. */
4624 if (tp[0] == CSI)
4625 switch_to_8bit();
4626
4627 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004628 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 * Ignore it for when the user has set 'term' to xterm,
4630 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004631 if (version > 20000)
4632 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004634 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004636 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004637# ifdef FEAT_MOUSE_SGR
4638 int is_iterm2 = FALSE;
4639# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004642 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004644 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 check_for_codes = TRUE;
4646 need_gather = TRUE;
4647 req_codes_from_term();
4648 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004649
4650 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004651 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004652 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004653 {
4654 /* If run from Vim $COLORS is set to the number of
4655 * colors the terminal supports. Otherwise assume
4656 * 256, libvterm supports even more. */
4657 if (mch_getenv((char_u *)"COLORS") == NULL)
4658 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004659# ifdef FEAT_MOUSE_SGR
4660 /* Libvterm can handle SGR mouse reporting. */
4661 if (!option_was_set((char_u *)"ttym"))
4662 set_option_value((char_u *)"ttym", 0L,
4663 (char_u *)"sgr", 0);
4664# endif
4665 }
4666
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004667 if (version == 95)
4668 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004669 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004670 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4671 {
4672 is_not_xterm = TRUE;
4673 is_mac_terminal = TRUE;
4674 }
4675# ifdef FEAT_MOUSE_SGR
Bram Moolenaare330ef42018-07-06 23:11:40 +02004676 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004677 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4678 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004679 else
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004680# endif
Bram Moolenaare330ef42018-07-06 23:11:40 +02004681 // old iTerm2 sends 0;95;
4682 if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
4683 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004684 }
4685
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004686 /* Only set 'ttymouse' automatically if it was not set
4687 * by the user already. */
4688 if (!option_was_set((char_u *)"ttym"))
4689 {
4690# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004691 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004692 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004693 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004694 set_option_value((char_u *)"ttym", 0L,
4695 (char_u *)"sgr", 0);
4696 else
4697# endif
4698 /* if xterm version >= 95 use mouse dragging */
4699 if (version >= 95)
4700 set_option_value((char_u *)"ttym", 0L,
4701 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004702 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004703
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004704 /* Detect terminals that set $TERM to something like
4705 * "xterm-256colors" but are not fully xterm
4706 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004707
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004708 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004709 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004710 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004711 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004712 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004713 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004714 is_not_xterm = TRUE;
4715
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004716 /* PuTTY sends 0;136;0
4717 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004718 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004719 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004720 is_not_xterm = TRUE;
4721
4722 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004723 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004724 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004725 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004726
Bram Moolenaar668324e2018-06-30 17:09:26 +02004727 // Xterm first responded to this request at patch level
4728 // 95, so assume anything below 95 is not xterm.
4729 if (version < 95)
4730 is_not_xterm = TRUE;
4731
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004732 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004733 * set. Only supported properly by xterm since version
4734 * 279 (otherwise it returns 0x18).
4735 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004736 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004737 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004738 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004739 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004740 && *T_CSH != NUL
4741 && *T_CRS != NUL)
4742 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004743 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004744 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004745 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004746 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004747 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004748
4749 /* Only request the cursor blink mode if t_RC set. Not
4750 * for Gnome terminal, it can't handle t_RC, it
4751 * echoes the characters to the screen. */
4752 if (rbm_status == STATUS_GET
4753 && !is_not_xterm
4754 && *T_CRC != NUL)
4755 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004756 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004757 out_str(T_CRC);
4758 rbm_status = STATUS_SENT;
4759 need_flush = TRUE;
4760 }
4761
4762 if (need_flush)
4763 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004765 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004767 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 apply_autocmds(EVENT_TERMRESPONSE,
4770 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 key_name[0] = (int)KS_EXTRA;
4772 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004774
Bram Moolenaar4db25542017-08-28 22:43:05 +02004775 /* Check blinking cursor from xterm:
4776 * {lead}?12;1$y set
4777 * {lead}?12;2$y not set
4778 *
4779 * {lead} can be <Esc>[ or CSI
4780 */
4781 else if (rbm_status == STATUS_SENT
4782 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4783 && i == j + 6
4784 && tp[j + 1] == '1'
4785 && tp[j + 2] == '2'
4786 && tp[j + 3] == ';'
4787 && tp[i - 1] == '$'
4788 && tp[i] == 'y')
4789 {
4790 initial_cursor_blink = (tp[j + 4] == '1');
4791 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004792 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004793 key_name[0] = (int)KS_EXTRA;
4794 key_name[1] = (int)KE_IGNORE;
4795 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004796# ifdef FEAT_EVAL
4797 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4798# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004799 }
4800
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004801 /*
4802 * Check for a window position response from the terminal:
4803 * {lead}3;{x}:{y}t
4804 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004805 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004806 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4807 || (len >= 3 && tp[0] == CSI))
4808 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4809 && tp[j + 1] == ';')
4810 {
4811 j += 2;
4812 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4813 ;
4814 if (i < len && tp[i] == ';')
4815 {
4816 winpos_x = atoi((char *)tp + j);
4817 j = i + 1;
4818 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4819 ;
4820 if (i < len && tp[i] == 't')
4821 {
4822 winpos_y = atoi((char *)tp + j);
4823 /* got finished code: consume it */
4824 key_name[0] = (int)KS_EXTRA;
4825 key_name[1] = (int)KE_IGNORE;
4826 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004827
4828 if (--did_request_winpos <= 0)
4829 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004830 }
4831 }
4832 if (i == len)
4833 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004834 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004835 return -1;
4836 }
4837 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004838 }
4839
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004840 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004841 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004842 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004843 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004844 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004845 * {lead} can be <Esc>] or OSC
4846 * {tail} can be '\007', <Esc>\ or STERM.
4847 *
4848 * Consume any code that starts with "{lead}11;", it's also
4849 * possible that "rgba" is following.
4850 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004851 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004852 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4853 || tp[0] == OSC))
4854 {
4855 j = 1 + (tp[0] == ESC);
4856 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004857 || (argp[1] != '1' && argp[1] != '0')
4858 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004859 i = 0; /* no match */
4860 else
4861 for (i = j; i < len; ++i)
4862 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4863 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004864 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004865 int is_bg = argp[1] == '1';
4866
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004867 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004868 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004869 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004870#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004871 int rval = hexhex2nr(tp + j + 7);
4872 int gval = hexhex2nr(tp + j + 12);
4873 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004874#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004875 if (is_bg)
4876 {
4877 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004878 + tp[j+17]) ? "light" : "dark";
4879
Bram Moolenaarb255b902018-04-24 21:40:10 +02004880 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004881 rbg_status = STATUS_GOT;
4882#ifdef FEAT_TERMINAL
4883 bg_r = rval;
4884 bg_g = gval;
4885 bg_b = bval;
4886#endif
4887 if (!option_was_set((char_u *)"bg")
4888 && STRCMP(p_bg, newval) != 0)
4889 {
4890 /* value differs, apply it */
4891 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004892 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004893 reset_option_was_set((char_u *)"bg");
4894 redraw_asap(CLEAR);
4895 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004896 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004897#ifdef FEAT_TERMINAL
4898 else
4899 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004900 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004901 rfg_status = STATUS_GOT;
4902 fg_r = rval;
4903 fg_g = gval;
4904 fg_b = bval;
4905 }
4906#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004907 }
4908
4909 /* got finished code: consume it */
4910 key_name[0] = (int)KS_EXTRA;
4911 key_name[1] = (int)KE_IGNORE;
4912 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004913# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004914 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4915 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004916# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004917 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004918 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004919 if (i == len)
4920 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004921 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004922 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004926 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004927 * {lead}{flag}+r<hex bytes><{tail}
4928 *
4929 * {lead} can be <Esc>P or DCS
4930 * {flag} can be '0' or '1'
4931 * {tail} can be Esc>\ or STERM
4932 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004933 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004934 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004935 *
4936 * {lead} can be <Esc>P or DCS
4937 * {tail} can be Esc>\ or STERM
4938 *
4939 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004940 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004941 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004942 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 || tp[0] == DCS))
4944 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004945 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004946 if (len < j + 3)
4947 i = len; /* need more chars */
4948 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004949 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004950 else if (argp[1] == '+')
4951 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004952 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004953 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004954 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 || tp[i] == STERM)
4956 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004957 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 got_code_from_term(tp + j, i);
4959 key_name[0] = (int)KS_EXTRA;
4960 key_name[1] = (int)KE_IGNORE;
4961 slen = i + 1 + (tp[i] == ESC);
4962 break;
4963 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004964 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004965 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004966 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004967 /* Probably the cursor shape response. Make sure that "i"
4968 * is equal to "len" when there are not sufficient
4969 * characters. */
4970 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004971 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004972 if (i - j == 3 && !isdigit(tp[i]))
4973 break;
4974 if (i - j == 4 && tp[i] != ' ')
4975 break;
4976 if (i - j == 5 && tp[i] != 'q')
4977 break;
4978 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4979 break;
4980 if ((i - j == 6 && tp[i] == STERM)
4981 || (i - j == 7 && tp[i] == '\\'))
4982 {
4983 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004984
Bram Moolenaar590ec872018-03-02 20:58:42 +01004985 /* 0, 1 = block blink, 2 = block
4986 * 3 = underline blink, 4 = underline
4987 * 5 = vertical bar blink, 6 = vertical bar */
4988 number = number == 0 ? 1 : number;
4989 initial_cursor_shape = (number + 1) / 2;
4990 /* The blink flag is actually inverted, compared to
4991 * the value set with T_SH. */
4992 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004993 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004994 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004995 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004996
Bram Moolenaar590ec872018-03-02 20:58:42 +01004997 key_name[0] = (int)KS_EXTRA;
4998 key_name[1] = (int)KE_IGNORE;
4999 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005000# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005001 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005002# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005003 break;
5004 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005005 }
5006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005009 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005010 /* These codes arrive many together, each code can be
5011 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005012 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005013 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 }
5017#endif
5018
5019 if (key_name[0] == NUL)
5020 continue; /* No match at this position, try next one */
5021
5022 /* We only get here when we have a complete termcode match */
5023
5024#ifdef FEAT_MOUSE
5025# ifdef FEAT_GUI
5026 /*
5027 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5028 * so that we know which window to scroll later.
5029 */
5030 if (gui.in_use
5031 && key_name[0] == (int)KS_EXTRA
5032 && (key_name[1] == (int)KE_X1MOUSE
5033 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005034 || key_name[1] == (int)KE_MOUSELEFT
5035 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 || key_name[1] == (int)KE_MOUSEDOWN
5037 || key_name[1] == (int)KE_MOUSEUP))
5038 {
5039 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5040 if (num_bytes == -1) /* not enough coordinates */
5041 return -1;
5042 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5043 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5044 slen += num_bytes;
5045 }
5046 else
5047# endif
5048 /*
5049 * If it is a mouse click, get the coordinates.
5050 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005051 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005053 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054# endif
5055# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005056 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057# endif
5058# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005059 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060# endif
5061# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005062 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005064# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005065 || key_name[0] == KS_URXVT_MOUSE
5066# endif
5067# ifdef FEAT_MOUSE_SGR
5068 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005069 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 )
5072 {
5073 is_click = is_drag = FALSE;
5074
Bram Moolenaar864207d2008-06-24 22:14:38 +00005075# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5076 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (key_name[0] == (int)KS_MOUSE)
5078 {
5079 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005080 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 * s == encoded button state:
5082 * 0x20 = left button down
5083 * 0x21 = middle button down
5084 * 0x22 = right button down
5085 * 0x23 = any button release
5086 * 0x60 = button 4 down (scroll wheel down)
5087 * 0x61 = button 5 down (scroll wheel up)
5088 * add 0x04 for SHIFT
5089 * add 0x08 for ALT
5090 * add 0x10 for CTRL
5091 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005092 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5093 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * c == column + ' ' + 1 == column + 33
5095 * r == row + ' ' + 1 == row + 33
5096 *
5097 * The coordinates are passed on through global variables.
5098 * Ugly, but this avoids trouble with mouse clicks at an
5099 * unexpected moment and allows for mapping them.
5100 */
5101 for (;;)
5102 {
5103#ifdef FEAT_GUI
5104 if (gui.in_use)
5105 {
5106 /* GUI uses more bits for columns > 223 */
5107 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5108 if (num_bytes == -1) /* not enough coordinates */
5109 return -1;
5110 mouse_code = bytes[0];
5111 mouse_col = 128 * (bytes[1] - ' ' - 1)
5112 + bytes[2] - ' ' - 1;
5113 mouse_row = 128 * (bytes[3] - ' ' - 1)
5114 + bytes[4] - ' ' - 1;
5115 }
5116 else
5117#endif
5118 {
5119 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5120 if (num_bytes == -1) /* not enough coordinates */
5121 return -1;
5122 mouse_code = bytes[0];
5123 mouse_col = bytes[1] - ' ' - 1;
5124 mouse_row = bytes[2] - ' ' - 1;
5125 }
5126 slen += num_bytes;
5127
5128 /* If the following bytes is also a mouse code and it has
5129 * the same code, dump this one and get the next. This
5130 * makes dragging a whole lot faster. */
5131#ifdef FEAT_GUI
5132 if (gui.in_use)
5133 j = 3;
5134 else
5135#endif
5136 j = termcodes[idx].len;
5137 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5138 && tp[slen + j] == mouse_code
5139 && tp[slen + j + 1] != NUL
5140 && tp[slen + j + 2] != NUL
5141#ifdef FEAT_GUI
5142 && (!gui.in_use
5143 || (tp[slen + j + 3] != NUL
5144 && tp[slen + j + 4] != NUL))
5145#endif
5146 )
5147 slen += j;
5148 else
5149 break;
5150 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005153# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5154 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005155 || key_name[0] == KS_SGR_MOUSE
5156 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005157 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005158 /* URXVT 1015 mouse reporting mode:
5159 * Almost identical to xterm mouse mode, except the values
5160 * are decimal instead of bytes.
5161 *
5162 * \033[%d;%d;%dM
5163 * ^-- row
5164 * ^----- column
5165 * ^-------- code
5166 *
5167 * SGR 1006 mouse reporting mode:
5168 * Almost identical to xterm mouse mode, except the values
5169 * are decimal instead of bytes.
5170 *
5171 * \033[<%d;%d;%dM
5172 * ^-- row
5173 * ^----- column
5174 * ^-------- code
5175 *
5176 * \033[<%d;%d;%dm : mouse release event
5177 * ^-- row
5178 * ^----- column
5179 * ^-------- code
5180 */
5181 p = modifiers_start;
5182 if (p == NULL)
5183 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005184
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005185 mouse_code = getdigits(&p);
5186 if (*p++ != ';')
5187 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005188
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005189 /* when mouse reporting is SGR, add 32 to mouse code */
5190 if (key_name[0] == KS_SGR_MOUSE
5191 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5192 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005193
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005194 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5195 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005196
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005197 mouse_col = getdigits(&p) - 1;
5198 if (*p++ != ';')
5199 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005200
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005201 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005202
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005203 /* The modifiers were the mouse coordinates, not the
5204 * modifier keys (alt/shift/ctrl/meta) state. */
5205 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005206 }
5207# endif
5208
5209 if (key_name[0] == (int)KS_MOUSE
5210#ifdef FEAT_MOUSE_URXVT
5211 || key_name[0] == (int)KS_URXVT_MOUSE
5212#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005213#ifdef FEAT_MOUSE_SGR
5214 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005215 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005216#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005217 )
5218 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005219# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /*
5221 * Handle mouse events.
5222 * Recognize the xterm mouse wheel, but not in the GUI, the
5223 * Linux console with GPM and the MS-DOS or Win32 console
5224 * (multi-clicks use >= 0x60).
5225 */
5226 if (mouse_code >= MOUSEWHEEL_LOW
5227# ifdef FEAT_GUI
5228 && !gui.in_use
5229# endif
5230# ifdef FEAT_MOUSE_GPM
5231 && gpm_flag == 0
5232# endif
5233 )
5234 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005235# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5236 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5237 /* mouse-move event, using MOUSE_DRAG works */
5238 mouse_code = MOUSE_DRAG;
5239 else
5240# endif
5241 /* Keep the mouse_code before it's changed, so that we
5242 * remember that it was a mouse wheel click. */
5243 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245# ifdef FEAT_MOUSE_XTERM
5246 else if (held_button == MOUSE_RELEASE
5247# ifdef FEAT_GUI
5248 && !gui.in_use
5249# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005250 && (mouse_code == 0x23 || mouse_code == 0x24
5251 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005253 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5254 * And 0x40 and 0x41 are used by some xterm emulator. */
5255 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5256 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258# endif
5259
5260# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5261 else if (use_xterm_mouse() > 1)
5262 {
5263 if (mouse_code & MOUSE_DRAG_XTERM)
5264 mouse_code |= MOUSE_DRAG;
5265 }
5266# endif
5267# ifdef FEAT_XCLIPBOARD
5268 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5269 {
5270 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5271 stop_xterm_trace();
5272 else
5273 start_xterm_trace(mouse_code);
5274 }
5275# endif
5276# endif
5277 }
5278# endif /* !UNIX || FEAT_MOUSE_XTERM */
5279# ifdef FEAT_MOUSE_NET
5280 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5281 {
5282 int mc, mr;
5283
5284 /* expect a rather limited sequence like: balancing {
5285 * \033}6,45\r
5286 * '6' is the row, 45 is the column
5287 */
5288 p = tp + slen;
5289 mr = getdigits(&p);
5290 if (*p++ != ',')
5291 return -1;
5292 mc = getdigits(&p);
5293 if (*p++ != '\r')
5294 return -1;
5295
5296 mouse_col = mc - 1;
5297 mouse_row = mr - 1;
5298 mouse_code = MOUSE_LEFT;
5299 slen += (int)(p - (tp + slen));
5300 }
5301# endif /* FEAT_MOUSE_NET */
5302# ifdef FEAT_MOUSE_JSB
5303 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5304 {
5305 int mult, val, iter, button, status;
5306
5307 /* JSBTERM Input Model
5308 * \033[0~zw uniq escape sequence
5309 * (L-x) Left button pressed - not pressed x not reporting
5310 * (M-x) Middle button pressed - not pressed x not reporting
5311 * (R-x) Right button pressed - not pressed x not reporting
5312 * (SDmdu) Single , Double click, m mouse move d button down
5313 * u button up
5314 * ### X cursor position padded to 3 digits
5315 * ### Y cursor position padded to 3 digits
5316 * (s-x) SHIFT key pressed - not pressed x not reporting
5317 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005318 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 */
5320
5321 p = tp + slen;
5322 button = mouse_code = 0;
5323 switch (*p++)
5324 {
5325 case 'L': button = 1; break;
5326 case '-': break;
5327 case 'x': break; /* ignore sequence */
5328 default: return -1; /* Unknown Result */
5329 }
5330 switch (*p++)
5331 {
5332 case 'M': button |= 2; break;
5333 case '-': break;
5334 case 'x': break; /* ignore sequence */
5335 default: return -1; /* Unknown Result */
5336 }
5337 switch (*p++)
5338 {
5339 case 'R': button |= 4; break;
5340 case '-': break;
5341 case 'x': break; /* ignore sequence */
5342 default: return -1; /* Unknown Result */
5343 }
5344 status = *p++;
5345 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5346 mult /= 10, p++)
5347 if (*p >= '0' && *p <= '9')
5348 val += (*p - '0') * mult;
5349 else
5350 return -1;
5351 mouse_col = val;
5352 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5353 mult /= 10, p++)
5354 if (*p >= '0' && *p <= '9')
5355 val += (*p - '0') * mult;
5356 else
5357 return -1;
5358 mouse_row = val;
5359 switch (*p++)
5360 {
5361 case 's': button |= 8; break; /* SHIFT key Pressed */
5362 case '-': break; /* Not Pressed */
5363 case 'x': break; /* Not Reporting */
5364 default: return -1; /* Unknown Result */
5365 }
5366 switch (*p++)
5367 {
5368 case 'c': button |= 16; break; /* CTRL key Pressed */
5369 case '-': break; /* Not Pressed */
5370 case 'x': break; /* Not Reporting */
5371 default: return -1; /* Unknown Result */
5372 }
5373 if (*p++ != '\033')
5374 return -1;
5375 if (*p++ != '\\')
5376 return -1;
5377 switch (status)
5378 {
5379 case 'D': /* Double Click */
5380 case 'S': /* Single Click */
5381 if (button & 1) mouse_code |= MOUSE_LEFT;
5382 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5383 if (button & 4) mouse_code |= MOUSE_RIGHT;
5384 if (button & 8) mouse_code |= MOUSE_SHIFT;
5385 if (button & 16) mouse_code |= MOUSE_CTRL;
5386 break;
5387 case 'm': /* Mouse move */
5388 if (button & 1) mouse_code |= MOUSE_LEFT;
5389 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5390 if (button & 4) mouse_code |= MOUSE_RIGHT;
5391 if (button & 8) mouse_code |= MOUSE_SHIFT;
5392 if (button & 16) mouse_code |= MOUSE_CTRL;
5393 if ((button & 7) != 0)
5394 {
5395 held_button = mouse_code;
5396 mouse_code |= MOUSE_DRAG;
5397 }
5398 is_drag = TRUE;
5399 showmode();
5400 break;
5401 case 'd': /* Button Down */
5402 if (button & 1) mouse_code |= MOUSE_LEFT;
5403 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5404 if (button & 4) mouse_code |= MOUSE_RIGHT;
5405 if (button & 8) mouse_code |= MOUSE_SHIFT;
5406 if (button & 16) mouse_code |= MOUSE_CTRL;
5407 break;
5408 case 'u': /* Button Up */
5409 if (button & 1)
5410 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5411 if (button & 2)
5412 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5413 if (button & 4)
5414 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5415 if (button & 8)
5416 mouse_code |= MOUSE_SHIFT;
5417 if (button & 16)
5418 mouse_code |= MOUSE_CTRL;
5419 break;
5420 default: return -1; /* Unknown Result */
5421 }
5422
5423 slen += (p - (tp + slen));
5424 }
5425# endif /* FEAT_MOUSE_JSB */
5426# ifdef FEAT_MOUSE_DEC
5427 if (key_name[0] == (int)KS_DEC_MOUSE)
5428 {
5429 /* The DEC Locator Input Model
5430 * Netterm delivers the code sequence:
5431 * \033[2;4;24;80&w (left button down)
5432 * \033[3;0;24;80&w (left button up)
5433 * \033[6;1;24;80&w (right button down)
5434 * \033[7;0;24;80&w (right button up)
5435 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5436 * Pe is the event code
5437 * Pb is the button code
5438 * Pr is the row coordinate
5439 * Pc is the column coordinate
5440 * Pp is the third coordinate (page number)
5441 * Pe, the event code indicates what event caused this report
5442 * The following event codes are defined:
5443 * 0 - request, the terminal received an explicit request
5444 * for a locator report, but the locator is unavailable
5445 * 1 - request, the terminal received an explicit request
5446 * for a locator report
5447 * 2 - left button down
5448 * 3 - left button up
5449 * 4 - middle button down
5450 * 5 - middle button up
5451 * 6 - right button down
5452 * 7 - right button up
5453 * 8 - fourth button down
5454 * 9 - fourth button up
5455 * 10 - locator outside filter rectangle
5456 * Pb, the button code, ASCII decimal 0-15 indicating which
5457 * buttons are down if any. The state of the four buttons
5458 * on the locator correspond to the low four bits of the
5459 * decimal value,
5460 * "1" means button depressed
5461 * 0 - no buttons down,
5462 * 1 - right,
5463 * 2 - middle,
5464 * 4 - left,
5465 * 8 - fourth
5466 * Pr is the row coordinate of the locator position in the page,
5467 * encoded as an ASCII decimal value.
5468 * If Pr is omitted, the locator position is undefined
5469 * (outside the terminal window for example).
5470 * Pc is the column coordinate of the locator position in the
5471 * page, encoded as an ASCII decimal value.
5472 * If Pc is omitted, the locator position is undefined
5473 * (outside the terminal window for example).
5474 * Pp is the page coordinate of the locator position
5475 * encoded as an ASCII decimal value.
5476 * The page coordinate may be omitted if the locator is on
5477 * page one (the default). We ignore it anyway.
5478 */
5479 int Pe, Pb, Pr, Pc;
5480
5481 p = tp + slen;
5482
5483 /* get event status */
5484 Pe = getdigits(&p);
5485 if (*p++ != ';')
5486 return -1;
5487
5488 /* get button status */
5489 Pb = getdigits(&p);
5490 if (*p++ != ';')
5491 return -1;
5492
5493 /* get row status */
5494 Pr = getdigits(&p);
5495 if (*p++ != ';')
5496 return -1;
5497
5498 /* get column status */
5499 Pc = getdigits(&p);
5500
5501 /* the page parameter is optional */
5502 if (*p == ';')
5503 {
5504 p++;
5505 (void)getdigits(&p);
5506 }
5507 if (*p++ != '&')
5508 return -1;
5509 if (*p++ != 'w')
5510 return -1;
5511
5512 mouse_code = 0;
5513 switch (Pe)
5514 {
5515 case 0: return -1; /* position request while unavailable */
5516 case 1: /* a response to a locator position request includes
5517 the status of all buttons */
5518 Pb &= 7; /* mask off and ignore fourth button */
5519 if (Pb & 4)
5520 mouse_code = MOUSE_LEFT;
5521 if (Pb & 2)
5522 mouse_code = MOUSE_MIDDLE;
5523 if (Pb & 1)
5524 mouse_code = MOUSE_RIGHT;
5525 if (Pb)
5526 {
5527 held_button = mouse_code;
5528 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005529 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531 is_drag = TRUE;
5532 showmode();
5533 break;
5534 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005535 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 break;
5537 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5538 break;
5539 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005540 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 break;
5542 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5543 break;
5544 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005545 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 break;
5547 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5548 break;
5549 case 8: return -1; /* fourth button down */
5550 case 9: return -1; /* fourth button up */
5551 case 10: return -1; /* mouse outside of filter rectangle */
5552 default: return -1; /* should never occur */
5553 }
5554
5555 mouse_col = Pc - 1;
5556 mouse_row = Pr - 1;
5557
5558 slen += (int)(p - (tp + slen));
5559 }
5560# endif /* FEAT_MOUSE_DEC */
5561# ifdef FEAT_MOUSE_PTERM
5562 if (key_name[0] == (int)KS_PTERM_MOUSE)
5563 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005564 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 p = tp + slen;
5567
5568 action = getdigits(&p);
5569 if (*p++ != ';')
5570 return -1;
5571
5572 mouse_row = getdigits(&p);
5573 if (*p++ != ';')
5574 return -1;
5575 mouse_col = getdigits(&p);
5576 if (*p++ != ';')
5577 return -1;
5578
5579 button = getdigits(&p);
5580 mouse_code = 0;
5581
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005582 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 case 4: mouse_code = MOUSE_LEFT; break;
5585 case 1: mouse_code = MOUSE_RIGHT; break;
5586 case 2: mouse_code = MOUSE_MIDDLE; break;
5587 default: return -1;
5588 }
5589
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005590 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 case 31: /* Initial press */
5593 if (*p++ != ';')
5594 return -1;
5595
5596 num_clicks = getdigits(&p); /* Not used */
5597 break;
5598
5599 case 32: /* Release */
5600 mouse_code |= MOUSE_RELEASE;
5601 break;
5602
5603 case 33: /* Drag */
5604 held_button = mouse_code;
5605 mouse_code |= MOUSE_DRAG;
5606 break;
5607
5608 default:
5609 return -1;
5610 }
5611
5612 if (*p++ != 't')
5613 return -1;
5614
5615 slen += (p - (tp + slen));
5616 }
5617# endif /* FEAT_MOUSE_PTERM */
5618
5619 /* Interpret the mouse code */
5620 current_button = (mouse_code & MOUSE_CLICK_MASK);
5621 if (current_button == MOUSE_RELEASE
5622# ifdef FEAT_MOUSE_XTERM
5623 && wheel_code == 0
5624# endif
5625 )
5626 {
5627 /*
5628 * If we get a mouse drag or release event when
5629 * there is no mouse button held down (held_button ==
5630 * MOUSE_RELEASE), produce a K_IGNORE below.
5631 * (can happen when you hold down two buttons
5632 * and then let them go, or click in the menu bar, but not
5633 * on a menu, and drag into the text).
5634 */
5635 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5636 is_drag = TRUE;
5637 current_button = held_button;
5638 }
5639 else if (wheel_code == 0)
5640 {
5641# ifdef CHECK_DOUBLE_CLICK
5642# ifdef FEAT_MOUSE_GPM
5643# ifdef FEAT_GUI
5644 /*
5645 * Only for Unix, when GUI or gpm is not active, we handle
5646 * multi-clicks here.
5647 */
5648 if (gpm_flag == 0 && !gui.in_use)
5649# else
5650 if (gpm_flag == 0)
5651# endif
5652# else
5653# ifdef FEAT_GUI
5654 if (!gui.in_use)
5655# endif
5656# endif
5657 {
5658 /*
5659 * Compute the time elapsed since the previous mouse click.
5660 */
5661 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005662 if (orig_mouse_time.tv_sec == 0)
5663 {
5664 /*
5665 * Avoid computing the difference between mouse_time
5666 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005667 * difference would be huge and would cause
5668 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005669 */
5670 timediff = p_mouset;
5671 }
5672 else
5673 {
5674 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005675 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005676 if (timediff < 0)
5677 --orig_mouse_time.tv_sec;
5678 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005679 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 orig_mouse_time = mouse_time;
5682 if (mouse_code == orig_mouse_code
5683 && timediff < p_mouset
5684 && orig_num_clicks != 4
5685 && orig_mouse_col == mouse_col
5686 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005687 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005689 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005691 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005692 /* Double click in tab pages line also works
5693 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005694 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005695 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 ++orig_num_clicks;
5697 else
5698 orig_num_clicks = 1;
5699 orig_mouse_col = mouse_col;
5700 orig_mouse_row = mouse_row;
5701 orig_topline = curwin->w_topline;
5702#ifdef FEAT_DIFF
5703 orig_topfill = curwin->w_topfill;
5704#endif
5705 }
5706# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5707 else
5708 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5709# endif
5710# else
5711 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5712# endif
5713 is_click = TRUE;
5714 orig_mouse_code = mouse_code;
5715 }
5716 if (!is_drag)
5717 held_button = mouse_code & MOUSE_CLICK_MASK;
5718
5719 /*
5720 * Translate the actual mouse event into a pseudo mouse event.
5721 * First work out what modifiers are to be used.
5722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (orig_mouse_code & MOUSE_SHIFT)
5724 modifiers |= MOD_MASK_SHIFT;
5725 if (orig_mouse_code & MOUSE_CTRL)
5726 modifiers |= MOD_MASK_CTRL;
5727 if (orig_mouse_code & MOUSE_ALT)
5728 modifiers |= MOD_MASK_ALT;
5729 if (orig_num_clicks == 2)
5730 modifiers |= MOD_MASK_2CLICK;
5731 else if (orig_num_clicks == 3)
5732 modifiers |= MOD_MASK_3CLICK;
5733 else if (orig_num_clicks == 4)
5734 modifiers |= MOD_MASK_4CLICK;
5735
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005736 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5737 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005739 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005740 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005741 {
5742 if (wheel_code & MOUSE_CTRL)
5743 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005744 if (wheel_code & MOUSE_ALT)
5745 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 key_name[1] = (wheel_code & 1)
5747 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005748 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 else
5751 key_name[1] = get_pseudo_mouse_code(current_button,
5752 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005753
5754 /* Make sure the mouse position is valid. Some terminals may
5755 * return weird values. */
5756 if (mouse_col >= Columns)
5757 mouse_col = Columns - 1;
5758 if (mouse_row >= Rows)
5759 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761#endif /* FEAT_MOUSE */
5762
5763#ifdef FEAT_GUI
5764 /*
5765 * If using the GUI, then we get menu and scrollbar events.
5766 *
5767 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5768 * four bytes which are to be taken as a pointer to the vimmenu_T
5769 * structure.
5770 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005771 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005772 * is one byte with the tab index.
5773 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5775 * by one byte representing the scrollbar number, and then four bytes
5776 * representing a long_u which is the new value of the scrollbar.
5777 *
5778 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5779 * KE_FILLER followed by four bytes representing a long_u which is the
5780 * new value of the scrollbar.
5781 */
5782# ifdef FEAT_MENU
5783 else if (key_name[0] == (int)KS_MENU)
5784 {
5785 long_u val;
5786
5787 num_bytes = get_long_from_buf(tp + slen, &val);
5788 if (num_bytes == -1)
5789 return -1;
5790 current_menu = (vimmenu_T *)val;
5791 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005792
5793 /* The menu may have been deleted right after it was used, check
5794 * for that. */
5795 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5796 {
5797 key_name[0] = KS_EXTRA;
5798 key_name[1] = (int)KE_IGNORE;
5799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
5801# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005802# ifdef FEAT_GUI_TABLINE
5803 else if (key_name[0] == (int)KS_TABLINE)
5804 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005805 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005806 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5807 if (num_bytes == -1)
5808 return -1;
5809 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005810 if (current_tab == 255) /* -1 in a byte gives 255 */
5811 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005812 slen += num_bytes;
5813 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005814 else if (key_name[0] == (int)KS_TABMENU)
5815 {
5816 /* Selecting tabline tab or using its menu. */
5817 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5818 if (num_bytes == -1)
5819 return -1;
5820 current_tab = (int)bytes[0];
5821 current_tabmenu = (int)bytes[1];
5822 slen += num_bytes;
5823 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825# ifndef USE_ON_FLY_SCROLL
5826 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5827 {
5828 long_u val;
5829
5830 /* Get the last scrollbar event in the queue of the same type */
5831 j = 0;
5832 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5833 && tp[j + 2] != NUL; ++i)
5834 {
5835 j += 3;
5836 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5837 if (num_bytes == -1)
5838 break;
5839 if (i == 0)
5840 current_scrollbar = (int)bytes[0];
5841 else if (current_scrollbar != (int)bytes[0])
5842 break;
5843 j += num_bytes;
5844 num_bytes = get_long_from_buf(tp + j, &val);
5845 if (num_bytes == -1)
5846 break;
5847 scrollbar_value = val;
5848 j += num_bytes;
5849 slen = j;
5850 }
5851 if (i == 0) /* not enough characters to make one */
5852 return -1;
5853 }
5854 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5855 {
5856 long_u val;
5857
5858 /* Get the last horiz. scrollbar event in the queue */
5859 j = 0;
5860 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5861 && tp[j + 2] != NUL; ++i)
5862 {
5863 j += 3;
5864 num_bytes = get_long_from_buf(tp + j, &val);
5865 if (num_bytes == -1)
5866 break;
5867 scrollbar_value = val;
5868 j += num_bytes;
5869 slen = j;
5870 }
5871 if (i == 0) /* not enough characters to make one */
5872 return -1;
5873 }
5874# endif /* !USE_ON_FLY_SCROLL */
5875#endif /* FEAT_GUI */
5876
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005877 /*
5878 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5879 */
5880 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5881
5882 /*
5883 * Add any modifier codes to our string.
5884 */
5885 new_slen = 0; /* Length of what will replace the termcode */
5886 if (modifiers != 0)
5887 {
5888 /* Some keys have the modifier included. Need to handle that here
5889 * to make mappings work. */
5890 key = simplify_key(key, &modifiers);
5891 if (modifiers != 0)
5892 {
5893 string[new_slen++] = K_SPECIAL;
5894 string[new_slen++] = (int)KS_MODIFIER;
5895 string[new_slen++] = modifiers;
5896 }
5897 }
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005900 key_name[0] = KEY2TERMCAP0(key);
5901 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005903 {
5904 /* from ":set <M-b>=xx" */
5905#ifdef FEAT_MBYTE
5906 if (has_mbyte)
5907 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5908 else
5909#endif
5910 string[new_slen++] = key_name[1];
5911 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005912 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5913 && key_name[1] == KE_IGNORE)
5914 {
5915 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5916 * to indicate what happened. */
5917 retval = KEYLEN_REMOVED;
5918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 else
5920 {
5921 string[new_slen++] = K_SPECIAL;
5922 string[new_slen++] = key_name[0];
5923 string[new_slen++] = key_name[1];
5924 }
5925 string[new_slen] = NUL;
5926 extra = new_slen - slen;
5927 if (buf == NULL)
5928 {
5929 if (extra < 0)
5930 /* remove matched chars, taking care of noremap */
5931 del_typebuf(-extra, offset);
5932 else if (extra > 0)
5933 /* insert the extra space we need */
5934 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5935
5936 /*
5937 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5938 * typebuf.tb_buf[]!
5939 */
5940 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5941 (size_t)new_slen);
5942 }
5943 else
5944 {
5945 if (extra < 0)
5946 /* remove matched characters */
5947 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005948 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005950 {
5951 /* Insert the extra space we need. If there is insufficient
5952 * space return -1. */
5953 if (*buflen + extra + new_slen >= bufsize)
5954 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005956 (size_t)(*buflen - offset));
5957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005959 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005961 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
5963
Bram Moolenaar2951b772013-07-03 12:45:31 +02005964#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005965 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005966#endif
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 return 0; /* no match found */
5969}
5970
Bram Moolenaar9377df32017-10-15 13:22:01 +02005971#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005972/*
5973 * Get the text foreground color, if known.
5974 */
5975 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005976term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005977{
5978 if (rfg_status == STATUS_GOT)
5979 {
5980 *r = fg_r;
5981 *g = fg_g;
5982 *b = fg_b;
5983 }
5984}
5985
5986/*
5987 * Get the text background color, if known.
5988 */
5989 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005990term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005991{
5992 if (rbg_status == STATUS_GOT)
5993 {
5994 *r = bg_r;
5995 *g = bg_g;
5996 *b = bg_b;
5997 }
5998}
5999#endif
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001/*
6002 * Replace any terminal code strings in from[] with the equivalent internal
6003 * vim representation. This is used for the "from" and "to" part of a
6004 * mapping, and the "to" part of a menu command.
6005 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6006 * '<'.
6007 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6008 *
6009 * The replacement is done in result[] and finally copied into allocated
6010 * memory. If this all works well *bufp is set to the allocated memory and a
6011 * pointer to it is returned. If something fails *bufp is set to NULL and from
6012 * is returned.
6013 *
6014 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6015 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6016 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6017 * instead of a CTRL-V.
6018 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006019 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006020replace_termcodes(
6021 char_u *from,
6022 char_u **bufp,
6023 int from_part,
6024 int do_lt, /* also translate <lt> */
6025 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 int i;
6028 int slen;
6029 int key;
6030 int dlen = 0;
6031 char_u *src;
6032 int do_backslash; /* backslash is a special character */
6033 int do_special; /* recognize <> key codes */
6034 int do_key_code; /* recognize raw key codes */
6035 char_u *result; /* buffer for resulting string */
6036
6037 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006038 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6040
6041 /*
6042 * Allocate space for the translation. Worst case a single character is
6043 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6044 */
6045 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6046 if (result == NULL) /* out of memory */
6047 {
6048 *bufp = NULL;
6049 return from;
6050 }
6051
6052 src = from;
6053
6054 /*
6055 * Check for #n at start only: function key n
6056 */
6057 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6058 {
6059 result[dlen++] = K_SPECIAL;
6060 result[dlen++] = 'k';
6061 if (src[1] == '0')
6062 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6063 else
6064 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6065 src += 2;
6066 }
6067
6068 /*
6069 * Copy each byte from *from to result[dlen]
6070 */
6071 while (*src != NUL)
6072 {
6073 /*
6074 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006075 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
6077 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6078 {
6079#ifdef FEAT_EVAL
6080 /*
6081 * Replace <SID> by K_SNR <script-nr> _.
6082 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6083 */
6084 if (STRNICMP(src, "<SID>", 5) == 0)
6085 {
6086 if (current_SID <= 0)
6087 EMSG(_(e_usingsid));
6088 else
6089 {
6090 src += 5;
6091 result[dlen++] = K_SPECIAL;
6092 result[dlen++] = (int)KS_EXTRA;
6093 result[dlen++] = (int)KE_SNR;
6094 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6095 dlen += (int)STRLEN(result + dlen);
6096 result[dlen++] = '_';
6097 continue;
6098 }
6099 }
6100#endif
6101
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006102 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (slen)
6104 {
6105 dlen += slen;
6106 continue;
6107 }
6108 }
6109
6110 /*
6111 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6112 * Note that this is also checked after replacing the <> form.
6113 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6114 * it could be a character in the file.
6115 */
6116 if (do_key_code)
6117 {
6118 i = find_term_bykeys(src);
6119 if (i >= 0)
6120 {
6121 result[dlen++] = K_SPECIAL;
6122 result[dlen++] = termcodes[i].name[0];
6123 result[dlen++] = termcodes[i].name[1];
6124 src += termcodes[i].len;
6125 /* If terminal code matched, continue after it. */
6126 continue;
6127 }
6128 }
6129
6130#ifdef FEAT_EVAL
6131 if (do_special)
6132 {
6133 char_u *p, *s, len;
6134
6135 /*
6136 * Replace <Leader> by the value of "mapleader".
6137 * Replace <LocalLeader> by the value of "maplocalleader".
6138 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6139 */
6140 if (STRNICMP(src, "<Leader>", 8) == 0)
6141 {
6142 len = 8;
6143 p = get_var_value((char_u *)"g:mapleader");
6144 }
6145 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6146 {
6147 len = 13;
6148 p = get_var_value((char_u *)"g:maplocalleader");
6149 }
6150 else
6151 {
6152 len = 0;
6153 p = NULL;
6154 }
6155 if (len != 0)
6156 {
6157 /* Allow up to 8 * 6 characters for "mapleader". */
6158 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6159 s = (char_u *)"\\";
6160 else
6161 s = p;
6162 while (*s != NUL)
6163 result[dlen++] = *s++;
6164 src += len;
6165 continue;
6166 }
6167 }
6168#endif
6169
6170 /*
6171 * Remove CTRL-V and ignore the next character.
6172 * For "from" side the CTRL-V at the end is included, for the "to"
6173 * part it is removed.
6174 * If 'cpoptions' does not contain 'B', also accept a backslash.
6175 */
6176 key = *src;
6177 if (key == Ctrl_V || (do_backslash && key == '\\'))
6178 {
6179 ++src; /* skip CTRL-V or backslash */
6180 if (*src == NUL)
6181 {
6182 if (from_part)
6183 result[dlen++] = key;
6184 break;
6185 }
6186 }
6187
6188#ifdef FEAT_MBYTE
6189 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006190 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
6192 {
6193 /*
6194 * If the character is K_SPECIAL, replace it with K_SPECIAL
6195 * KS_SPECIAL KE_FILLER.
6196 * If compiled with the GUI replace CSI with K_CSI.
6197 */
6198 if (*src == K_SPECIAL)
6199 {
6200 result[dlen++] = K_SPECIAL;
6201 result[dlen++] = KS_SPECIAL;
6202 result[dlen++] = KE_FILLER;
6203 }
6204# ifdef FEAT_GUI
6205 else if (*src == CSI)
6206 {
6207 result[dlen++] = K_SPECIAL;
6208 result[dlen++] = KS_EXTRA;
6209 result[dlen++] = (int)KE_CSI;
6210 }
6211# endif
6212 else
6213 result[dlen++] = *src;
6214 ++src;
6215 }
6216 }
6217 result[dlen] = NUL;
6218
6219 /*
6220 * Copy the new string to allocated memory.
6221 * If this fails, just return from.
6222 */
6223 if ((*bufp = vim_strsave(result)) != NULL)
6224 from = *bufp;
6225 vim_free(result);
6226 return from;
6227}
6228
6229/*
6230 * Find a termcode with keys 'src' (must be NUL terminated).
6231 * Return the index in termcodes[], or -1 if not found.
6232 */
6233 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006234find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006237 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
6239 for (i = 0; i < tc_len; ++i)
6240 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006241 if (slen == termcodes[i].len
6242 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return i;
6244 }
6245 return -1;
6246}
6247
6248/*
6249 * Gather the first characters in the terminal key codes into a string.
6250 * Used to speed up check_termcode().
6251 */
6252 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006253gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 int i;
6256 int len = 0;
6257
6258#ifdef FEAT_GUI
6259 if (gui.in_use)
6260 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6261#endif
6262#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006263 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 termleader[len++] = DCS; /* the termcode response starts with DCS
6265 in 8-bit mode */
6266#endif
6267 termleader[len] = NUL;
6268
6269 for (i = 0; i < tc_len; ++i)
6270 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6271 {
6272 termleader[len++] = termcodes[i].code[0];
6273 termleader[len] = NUL;
6274 }
6275
6276 need_gather = FALSE;
6277}
6278
6279/*
6280 * Show all termcodes (for ":set termcap")
6281 * This code looks a lot like showoptions(), but is different.
6282 */
6283 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006284show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 int col;
6287 int *items;
6288 int item_count;
6289 int run;
6290 int row, rows;
6291 int cols;
6292 int i;
6293 int len;
6294
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006295#define INC3 27 /* try to make three columns */
6296#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297#define GAP 2 /* spaces between columns */
6298
6299 if (tc_len == 0) /* no terminal codes (must be GUI) */
6300 return;
6301 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6302 if (items == NULL)
6303 return;
6304
6305 /* Highlight title */
6306 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6307
6308 /*
6309 * do the loop two times:
6310 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006311 * 2. display the medium items (medium length strings)
6312 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006314 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 /*
6317 * collect the items in items[]
6318 */
6319 item_count = 0;
6320 for (i = 0; i < tc_len; i++)
6321 {
6322 len = show_one_termcode(termcodes[i].name,
6323 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006324 if (len <= INC3 - GAP ? run == 1
6325 : len <= INC2 - GAP ? run == 2
6326 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 items[item_count++] = i;
6328 }
6329
6330 /*
6331 * display the items
6332 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006333 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006335 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (cols == 0)
6337 cols = 1;
6338 rows = (item_count + cols - 1) / cols;
6339 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006340 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 rows = item_count;
6342 for (row = 0; row < rows && !got_int; ++row)
6343 {
6344 msg_putchar('\n'); /* go to next line */
6345 if (got_int) /* 'q' typed in more */
6346 break;
6347 col = 0;
6348 for (i = row; i < item_count; i += rows)
6349 {
6350 msg_col = col; /* make columns */
6351 show_one_termcode(termcodes[items[i]].name,
6352 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006353 if (run == 2)
6354 col += INC2;
6355 else
6356 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 }
6358 out_flush();
6359 ui_breakcheck();
6360 }
6361 }
6362 vim_free(items);
6363}
6364
6365/*
6366 * Show one termcode entry.
6367 * Output goes into IObuff[]
6368 */
6369 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006370show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 char_u *p;
6373 int len;
6374
6375 if (name[0] > '~')
6376 {
6377 IObuff[0] = ' ';
6378 IObuff[1] = ' ';
6379 IObuff[2] = ' ';
6380 IObuff[3] = ' ';
6381 }
6382 else
6383 {
6384 IObuff[0] = 't';
6385 IObuff[1] = '_';
6386 IObuff[2] = name[0];
6387 IObuff[3] = name[1];
6388 }
6389 IObuff[4] = ' ';
6390
6391 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6392 if (p[1] != 't')
6393 STRCPY(IObuff + 5, p);
6394 else
6395 IObuff[5] = NUL;
6396 len = (int)STRLEN(IObuff);
6397 do
6398 IObuff[len++] = ' ';
6399 while (len < 17);
6400 IObuff[len] = NUL;
6401 if (code == NULL)
6402 len += 4;
6403 else
6404 len += vim_strsize(code);
6405
6406 if (printit)
6407 {
6408 msg_puts(IObuff);
6409 if (code == NULL)
6410 msg_puts((char_u *)"NULL");
6411 else
6412 msg_outtrans(code);
6413 }
6414 return len;
6415}
6416
6417#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6418/*
6419 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6420 * termcap codes from the terminal itself.
6421 * We get them one by one to avoid a very long response string.
6422 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006423static int xt_index_in = 0;
6424static int xt_index_out = 0;
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006427req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 xt_index_out = 0;
6430 xt_index_in = 0;
6431 req_more_codes_from_term();
6432}
6433
6434 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006435req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char buf[11];
6438 int old_idx = xt_index_out;
6439
6440 /* Don't do anything when going to exit. */
6441 if (exiting)
6442 return;
6443
6444 /* Send up to 10 more requests out than we received. Avoid sending too
6445 * many, there can be a buffer overflow somewhere. */
6446 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6447 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006448 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006449
Bram Moolenaarb255b902018-04-24 21:40:10 +02006450 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6451 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 out_str_nf((char_u *)buf);
6453 ++xt_index_out;
6454 }
6455
6456 /* Send the codes out right away. */
6457 if (xt_index_out != old_idx)
6458 out_flush();
6459}
6460
6461/*
6462 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6463 * A "0" instead of the "1" indicates a code that isn't supported.
6464 * Both <name> and <string> are encoded in hex.
6465 * "code" points to the "0" or "1".
6466 */
6467 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006468got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470#define XT_LEN 100
6471 char_u name[3];
6472 char_u str[XT_LEN];
6473 int i;
6474 int j = 0;
6475 int c;
6476
6477 /* A '1' means the code is supported, a '0' means it isn't.
6478 * When half the length is > XT_LEN we can't use it.
6479 * Our names are currently all 2 characters. */
6480 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6481 {
6482 /* Get the name from the response and find it in the table. */
6483 name[0] = hexhex2nr(code + 3);
6484 name[1] = hexhex2nr(code + 5);
6485 name[2] = NUL;
6486 for (i = 0; key_names[i] != NULL; ++i)
6487 {
6488 if (STRCMP(key_names[i], name) == 0)
6489 {
6490 xt_index_in = i;
6491 break;
6492 }
6493 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006494
Bram Moolenaarb255b902018-04-24 21:40:10 +02006495 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6496
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (key_names[i] != NULL)
6498 {
6499 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6500 str[j++] = c;
6501 str[j] = NUL;
6502 if (name[0] == 'C' && name[1] == 'o')
6503 {
6504 /* Color count is not a key code. */
6505 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006506 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508 else
6509 {
6510 /* First delete any existing entry with the same code. */
6511 i = find_term_bykeys(str);
6512 if (i >= 0)
6513 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006514 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516 }
6517 }
6518
6519 /* May request more codes now that we received one. */
6520 ++xt_index_in;
6521 req_more_codes_from_term();
6522}
6523
6524/*
6525 * Check if there are any unanswered requests and deal with them.
6526 * This is called before starting an external program or getting direct
6527 * keyboard input. We don't want responses to be send to that program or
6528 * handled as typed text.
6529 */
6530 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006531check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 int c;
6534
6535 /* If no codes requested or all are answered, no need to wait. */
6536 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6537 return;
6538
6539 /* Vgetc() will check for and handle any response.
6540 * Keep calling vpeekc() until we don't get any responses. */
6541 ++no_mapping;
6542 ++allow_keys;
6543 for (;;)
6544 {
6545 c = vpeekc();
6546 if (c == NUL) /* nothing available */
6547 break;
6548
6549 /* If a response is recognized it's replaced with K_IGNORE, must read
6550 * it from the input stream. If there is no K_IGNORE we can't do
6551 * anything, break here (there might be some responses further on, but
6552 * we don't want to throw away any typed chars). */
6553 if (c != K_SPECIAL && c != K_IGNORE)
6554 break;
6555 c = vgetc();
6556 if (c != K_IGNORE)
6557 {
6558 vungetc(c);
6559 break;
6560 }
6561 }
6562 --no_mapping;
6563 --allow_keys;
6564}
6565#endif
6566
6567#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6568/*
6569 * Translate an internal mapping/abbreviation representation into the
6570 * corresponding external one recognized by :map/:abbrev commands;
6571 * respects the current B/k/< settings of 'cpoption'.
6572 *
6573 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006574 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 *
6576 * It uses a growarray to build the translation string since the
6577 * latter can be wider than the original description. The caller has to
6578 * free the string afterwards.
6579 *
6580 * Returns NULL when there is a problem.
6581 */
6582 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006583translate_mapping(
6584 char_u *str,
6585 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 garray_T ga;
6588 int c;
6589 int modifiers;
6590 int cpo_bslash;
6591 int cpo_special;
6592 int cpo_keycode;
6593
6594 ga_init(&ga);
6595 ga.ga_itemsize = 1;
6596 ga.ga_growsize = 40;
6597
6598 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6599 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6600 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6601
6602 for (; *str; ++str)
6603 {
6604 c = *str;
6605 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6606 {
6607 modifiers = 0;
6608 if (str[1] == KS_MODIFIER)
6609 {
6610 str++;
6611 modifiers = *++str;
6612 c = *++str;
6613 }
6614 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6615 {
6616 int i;
6617
6618 /* try to find special key in termcodes */
6619 for (i = 0; i < tc_len; ++i)
6620 if (termcodes[i].name[0] == str[1]
6621 && termcodes[i].name[1] == str[2])
6622 break;
6623 if (i < tc_len)
6624 {
6625 ga_concat(&ga, termcodes[i].code);
6626 str += 2;
6627 continue; /* for (str) */
6628 }
6629 }
6630 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6631 {
6632 if (expmap && cpo_special)
6633 {
6634 ga_clear(&ga);
6635 return NULL;
6636 }
6637 c = TO_SPECIAL(str[1], str[2]);
6638 if (c == K_ZERO) /* display <Nul> as ^@ */
6639 c = NUL;
6640 str += 2;
6641 }
6642 if (IS_SPECIAL(c) || modifiers) /* special key */
6643 {
6644 if (expmap && cpo_special)
6645 {
6646 ga_clear(&ga);
6647 return NULL;
6648 }
6649 ga_concat(&ga, get_special_key_name(c, modifiers));
6650 continue; /* for (str) */
6651 }
6652 }
6653 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6654 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6655 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6656 if (c)
6657 ga_append(&ga, c);
6658 }
6659 ga_append(&ga, NUL);
6660 return (char_u *)(ga.ga_data);
6661}
6662#endif
6663
6664#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6665static char ksme_str[20];
6666static char ksmr_str[20];
6667static char ksmd_str[20];
6668
6669/*
6670 * For Win32 console: update termcap codes for existing console attributes.
6671 */
6672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006673update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675 struct builtin_term *p;
6676
6677 p = find_builtin_term(DEFAULT_TERM);
6678 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6679 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6680 attr | 0x08); /* FOREGROUND_INTENSITY */
6681 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6682 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6683
6684 while (p->bt_string != NULL)
6685 {
6686 if (p->bt_entry == (int)KS_ME)
6687 p->bt_string = &ksme_str[0];
6688 else if (p->bt_entry == (int)KS_MR)
6689 p->bt_string = &ksmr_str[0];
6690 else if (p->bt_entry == (int)KS_MD)
6691 p->bt_string = &ksmd_str[0];
6692 ++p;
6693 }
6694}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006695
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006696# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006697# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006698struct ks_tbl_s
6699{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006700 int code; /* value of KS_ */
6701 char *vtp; /* code in vtp mode */
6702 char *vtp2; /* code in vtp2 mode */
6703 char buf[KSSIZE]; /* save buffer in non-vtp mode */
6704 char vbuf[KSSIZE]; /* save buffer in vtp mode */
6705 char v2buf[KSSIZE]; /* save buffer in vtp2 mode */
6706 char arr[KSSIZE]; /* real buffer */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006707};
6708
6709static struct ks_tbl_s ks_tbl[] =
6710{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711 {(int)KS_ME, "\033|0m", "\033|0m"}, /* normal */
6712 {(int)KS_MR, "\033|7m", "\033|7m"}, /* reverse */
6713 {(int)KS_MD, "\033|1m", "\033|1m"}, /* bold */
6714 {(int)KS_SO, "\033|91m", "\033|91m"}, /* standout: bright red text */
6715 {(int)KS_SE, "\033|39m", "\033|39m"}, /* standout end: default color */
6716 {(int)KS_CZH, "\033|95m", "\033|95m"}, /* italic: bright magenta text */
6717 {(int)KS_CZR, "\033|0m", "\033|0m"}, /* italic end */
6718 {(int)KS_US, "\033|4m", "\033|4m"}, /* underscore */
6719 {(int)KS_UE, "\033|24m", "\033|24m"}, /* underscore end */
6720# ifdef TERMINFO
6721 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, /* set background color */
6722 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, /* set foreground color */
6723# else
6724 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, /* set background color */
6725 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, /* set foreground color */
6726# endif
6727 {(int)KS_CCO, "16", "256"}, /* colors */
6728 {(int)KS_NAME} /* terminator */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006729};
6730
6731 static struct builtin_term *
6732find_first_tcap(
6733 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006734 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006735{
6736 struct builtin_term *p;
6737
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006738 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006739 if (p->bt_entry == code)
6740 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006741 return NULL;
6742}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006743# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006744
6745/*
6746 * For Win32 console: replace the sequence immediately after termguicolors.
6747 */
6748 void
6749swap_tcap(void)
6750{
6751# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006752 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006753 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006754 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006755 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006756 int mode;
6757 enum
6758 {
6759 CMODEINDEX,
6760 CMODE24,
6761 CMODE256
6762 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006763
6764 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006765 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006766 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006767 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006768 {
6769 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006770 if (bt != NULL)
6771 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006772 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6773 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6774 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6775
6776 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6777 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006778 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006779 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006780 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006781 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006782 }
6783
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006784 if (p_tgc)
6785 mode = CMODE24;
6786 else if (t_colors >= 256)
6787 mode = CMODE256;
6788 else
6789 mode = CMODEINDEX;
6790
6791 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006792 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006793 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6794 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006795 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006796 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006797 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006798 case CMODEINDEX:
6799 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6800 break;
6801 case CMODE24:
6802 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6803 break;
6804 default:
6805 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006806 }
6807 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006808 }
6809
6810 if (mode != curr_mode)
6811 {
6812 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006813 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006814 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6815 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006816 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006817 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006818 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006819 case CMODEINDEX:
6820 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6821 break;
6822 case CMODE24:
6823 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6824 break;
6825 default:
6826 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006827 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006828 }
6829 }
6830
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006831 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006832 }
6833# endif
6834}
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006837
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006838#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006839 static int
6840hex_digit(int c)
6841{
6842 if (isdigit(c))
6843 return c - '0';
6844 c = TOLOWER_ASC(c);
6845 if (c >= 'a' && c <= 'f')
6846 return c - 'a' + 10;
6847 return 0x1ffffff;
6848}
6849
6850 guicolor_T
6851gui_get_color_cmn(char_u *name)
6852{
Bram Moolenaar28726652017-01-06 18:16:19 +01006853 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6854 * values as used by the MS-Windows GDI api. It should be used only for
6855 * MS-Windows GDI builds. */
6856# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6857# undef RGB
6858# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006859# ifndef RGB
6860# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6861# endif
6862# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006863 FILE *fd;
6864 char line[LINE_LEN];
6865 char_u *fname;
6866 int r, g, b, i;
6867 guicolor_T color;
6868
6869 struct rgbcolor_table_S {
6870 char_u *color_name;
6871 guicolor_T color;
6872 };
6873
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006874 /* Only non X11 colors (not present in rgb.txt) and colors in
6875 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006876 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006877 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6878 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6879 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6880 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6881 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6882 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6883 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6884 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6885 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6886 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6887 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6888 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6889 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006890 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6891 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006892 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006893 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006894 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006895 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6896 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6897 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6898 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6899 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6900 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6901 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6902 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6903 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006904 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006905 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006906 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6907 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006908 };
6909
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006910 static struct rgbcolor_table_S *colornames_table;
6911 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006912
6913 if (name[0] == '#' && STRLEN(name) == 7)
6914 {
6915 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006916 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006917 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6918 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6919 if (color > 0xffffff)
6920 return INVALCOLOR;
6921 return color;
6922 }
6923
6924 /* Check if the name is one of the colors we know */
6925 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6926 if (STRICMP(name, rgb_table[i].color_name) == 0)
6927 return rgb_table[i].color;
6928
6929 /*
6930 * Last attempt. Look in the file "$VIM/rgb.txt".
6931 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006932 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006933 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006934 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006935
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006936 /* colornames_table not yet initialized */
6937 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6938 if (fname == NULL)
6939 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006940
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006941 fd = fopen((char *)fname, "rt");
6942 vim_free(fname);
6943 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006944 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006945 if (p_verbose > 1)
6946 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6947 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006948 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006949
6950 for (counting = 1; counting >= 0; --counting)
6951 {
6952 if (!counting)
6953 {
6954 colornames_table = (struct rgbcolor_table_S *)alloc(
6955 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6956 if (colornames_table == NULL)
6957 {
6958 fclose(fd);
6959 return INVALCOLOR;
6960 }
6961 rewind(fd);
6962 }
6963 size = 0;
6964
6965 while (!feof(fd))
6966 {
6967 size_t len;
6968 int pos;
6969
6970 ignoredp = fgets(line, LINE_LEN, fd);
6971 len = strlen(line);
6972
6973 if (len <= 1 || line[len - 1] != '\n')
6974 continue;
6975
6976 line[len - 1] = '\0';
6977
6978 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6979 if (i != 3)
6980 continue;
6981
6982 if (!counting)
6983 {
6984 char_u *s = vim_strsave((char_u *)line + pos);
6985
6986 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006987 {
6988 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006989 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006990 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006991 colornames_table[size].color_name = s;
6992 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6993 }
6994 size++;
6995 }
6996 }
6997 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006998 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006999
7000 for (i = 0; i < size; i++)
7001 if (STRICMP(name, colornames_table[i].color_name) == 0)
7002 return colornames_table[i].color;
7003
Bram Moolenaarab302212016-04-26 20:59:29 +02007004 return INVALCOLOR;
7005}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02007006
7007 guicolor_T
7008gui_get_rgb_color_cmn(int r, int g, int b)
7009{
7010 guicolor_T color = RGB(r, g, b);
7011
7012 if (color > 0xffffff)
7013 return INVALCOLOR;
7014 return color;
7015}
Bram Moolenaarab302212016-04-26 20:59:29 +02007016#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007017
7018#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
7019 || defined(PROTO)
7020static int cube_value[] = {
7021 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7022};
7023
7024static int grey_ramp[] = {
7025 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7026 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7027};
7028
7029# ifdef FEAT_TERMINAL
7030# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007031# else
7032# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007033# endif
7034
Bram Moolenaar9894e392018-05-05 14:29:06 +02007035static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007036// R G B idx
7037 { 0, 0, 0, 1}, // black
7038 {224, 0, 0, 2}, // dark red
7039 { 0, 224, 0, 3}, // dark green
7040 {224, 224, 0, 4}, // dark yellow / brown
7041 { 0, 0, 224, 5}, // dark blue
7042 {224, 0, 224, 6}, // dark magenta
7043 { 0, 224, 224, 7}, // dark cyan
7044 {224, 224, 224, 8}, // light grey
7045
7046 {128, 128, 128, 9}, // dark grey
7047 {255, 64, 64, 10}, // light red
7048 { 64, 255, 64, 11}, // light green
7049 {255, 255, 64, 12}, // yellow
7050 { 64, 64, 255, 13}, // light blue
7051 {255, 64, 255, 14}, // light magenta
7052 { 64, 255, 255, 15}, // light cyan
7053 {255, 255, 255, 16}, // white
7054};
7055
7056 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007057cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007058{
7059 int idx;
7060
7061 if (nr < 16)
7062 {
7063 *r = ansi_table[nr][0];
7064 *g = ansi_table[nr][1];
7065 *b = ansi_table[nr][2];
7066 *ansi_idx = ansi_table[nr][3];
7067 }
7068 else if (nr < 232)
7069 {
7070 /* 216 color cube */
7071 idx = nr - 16;
7072 *r = cube_value[idx / 36 % 6];
7073 *g = cube_value[idx / 6 % 6];
7074 *b = cube_value[idx % 6];
7075 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7076 }
7077 else if (nr < 256)
7078 {
7079 /* 24 grey scale ramp */
7080 idx = nr - 232;
7081 *r = grey_ramp[idx];
7082 *g = grey_ramp[idx];
7083 *b = grey_ramp[idx];
7084 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7085 }
7086 else
7087 {
7088 *r = 0;
7089 *g = 0;
7090 *b = 0;
7091 *ansi_idx = 0;
7092 }
7093}
7094#endif