blob: 21f9a3cfa70ce2ad401b21ddf74d1b1bfdaad3e2 [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);
1486 STRCPY(t + 1, t + 2);
1487 }
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/*
3792 * Enable the cursor.
3793 */
3794 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003795cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 if (cursor_is_off)
3798 {
3799 out_str(T_VE);
3800 cursor_is_off = FALSE;
3801 }
3802}
3803
3804/*
3805 * Disable the cursor.
3806 */
3807 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003808cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003810 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003812 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 cursor_is_off = TRUE;
3814 }
3815}
3816
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003817#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003819 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003820 */
3821 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003822term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003823{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003824 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003825 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003826
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003827 /* Only do something when redrawing the screen and we can restore the
3828 * mode. */
3829 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003830 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003831# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003832 if (forced && initial_cursor_shape > 0)
3833 /* Restore to initial values. */
3834 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003835# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003836 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003837 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003838
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003839 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003840 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003841 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003842 {
3843 if (*T_CSR != NUL)
3844 p = T_CSR; /* Replace mode cursor */
3845 else
3846 p = T_CSI; /* fall back to Insert mode cursor */
3847 if (*p != NUL)
3848 {
3849 out_str(p);
3850 showing_mode = REPLACE;
3851 }
3852 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003853 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003854 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003855 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003856 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003857 {
3858 out_str(T_CSI); /* Insert mode cursor */
3859 showing_mode = INSERT;
3860 }
3861 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003862 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003863 {
3864 out_str(T_CEI); /* non-Insert mode cursor */
3865 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003866 }
3867}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003868
3869# if defined(FEAT_TERMINAL) || defined(PROTO)
3870 void
3871term_cursor_color(char_u *color)
3872{
3873 if (*T_CSC != NUL)
3874 {
3875 out_str(T_CSC); /* set cursor color start */
3876 out_str_nf(color);
3877 out_str(T_CEC); /* set cursor color end */
3878 out_flush();
3879 }
3880}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003881# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003882
Bram Moolenaar4db25542017-08-28 22:43:05 +02003883 int
3884blink_state_is_inverted()
3885{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003886#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003887 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3888 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003889#else
3890 return FALSE;
3891#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003892}
3893
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003894/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003895 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003896 */
3897 void
3898term_cursor_shape(int shape, int blink)
3899{
3900 if (*T_CSH != NUL)
3901 {
3902 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3903 out_flush();
3904 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003905 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003906 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003907 int do_blink = blink;
3908
3909 /* t_SH is empty: try setting just the blink state.
3910 * The blink flags are XORed together, if the initial blinking from
3911 * style and shape differs, we need to invert the flag here. */
3912 if (blink_state_is_inverted())
3913 do_blink = !blink;
3914
3915 if (do_blink && *T_VS != NUL)
3916 {
3917 out_str(T_VS);
3918 out_flush();
3919 }
3920 else if (!do_blink && *T_CVS != NUL)
3921 {
3922 out_str(T_CVS);
3923 out_flush();
3924 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003925 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003926}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003927#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003928
3929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * Set scrolling region for window 'wp'.
3931 * The region starts 'off' lines from the start of the window.
3932 * Also set the vertical scroll region for a vertically split window. Always
3933 * the full width of the window, excluding the vertical separator.
3934 */
3935 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003936scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3939 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003941 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3942 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 screen_start(); /* don't know where cursor is now */
3944}
3945
3946/*
3947 * Reset scrolling region to the whole screen.
3948 */
3949 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003950scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
3952 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (*T_CSV != NUL)
3954 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 screen_start(); /* don't know where cursor is now */
3956}
3957
3958
3959/*
3960 * List of terminal codes that are currently recognized.
3961 */
3962
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003963static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 char_u name[2]; /* termcap name of entry */
3966 char_u *code; /* terminal code (in allocated memory) */
3967 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003968 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969} *termcodes = NULL;
3970
3971static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3972static int tc_len = 0; /* current number of entries in termcodes[] */
3973
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003974static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003977clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 while (tc_len > 0)
3980 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003981 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 tc_max_len = 0;
3983
3984#ifdef HAVE_TGETENT
3985 BC = (char *)empty_option;
3986 UP = (char *)empty_option;
3987 PC = NUL; /* set pad character to NUL */
3988 ospeed = 0;
3989#endif
3990
3991 need_gather = TRUE; /* need to fill termleader[] */
3992}
3993
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994#define ATC_FROM_TERM 55
3995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996/*
3997 * Add a new entry to the list of terminal codes.
3998 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4000 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 */
4002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004003add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 struct termcode *new_tc;
4006 int i, j;
4007 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004008 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (string == NULL || *string == NUL)
4011 {
4012 del_termcode(name);
4013 return;
4014 }
4015
Bram Moolenaar45500912014-07-09 20:51:07 +02004016#if defined(WIN3264) && !defined(FEAT_GUI)
4017 s = vim_strnsave(string, (int)STRLEN(string) + 1);
4018#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (s == NULL)
4022 return;
4023
4024 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004025 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004027 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 s[0] = term_7to8bit(string);
4029 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004030
4031#if defined(WIN3264) && !defined(FEAT_GUI)
4032 if (s[0] == K_NUL)
4033 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004034 STRMOVE(s + 1, s);
4035 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004036 }
4037#endif
4038
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004039 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 need_gather = TRUE; /* need to fill termleader[] */
4042
4043 /*
4044 * need to make space for more entries
4045 */
4046 if (tc_len == tc_max_len)
4047 {
4048 tc_max_len += 20;
4049 new_tc = (struct termcode *)alloc(
4050 (unsigned)(tc_max_len * sizeof(struct termcode)));
4051 if (new_tc == NULL)
4052 {
4053 tc_max_len -= 20;
4054 return;
4055 }
4056 for (i = 0; i < tc_len; ++i)
4057 new_tc[i] = termcodes[i];
4058 vim_free(termcodes);
4059 termcodes = new_tc;
4060 }
4061
4062 /*
4063 * Look for existing entry with the same name, it is replaced.
4064 * Look for an existing entry that is alphabetical higher, the new entry
4065 * is inserted in front of it.
4066 */
4067 for (i = 0; i < tc_len; ++i)
4068 {
4069 if (termcodes[i].name[0] < name[0])
4070 continue;
4071 if (termcodes[i].name[0] == name[0])
4072 {
4073 if (termcodes[i].name[1] < name[1])
4074 continue;
4075 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004076 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 */
4078 if (termcodes[i].name[1] == name[1])
4079 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004080 if (flags == ATC_FROM_TERM && (j = termcode_star(
4081 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004082 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 /* Don't replace ESC[123;*X or ESC O*X with another when
4084 * invoked from got_code_from_term(). */
4085 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004086 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004087 && s[len - 1]
4088 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004089 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 vim_free(s);
4092 return;
4093 }
4094 }
4095 else
4096 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004098 vim_free(termcodes[i].code);
4099 --tc_len;
4100 break;
4101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 }
4104 /*
4105 * Found alphabetical larger entry, move rest to insert new entry
4106 */
4107 for (j = tc_len; j > i; --j)
4108 termcodes[j] = termcodes[j - 1];
4109 break;
4110 }
4111
4112 termcodes[i].name[0] = name[0];
4113 termcodes[i].name[1] = name[1];
4114 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004116
4117 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4118 * accept modifiers. */
4119 termcodes[i].modlen = 0;
4120 j = termcode_star(s, len);
4121 if (j > 0)
4122 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 ++tc_len;
4124}
4125
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004126/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004127 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004128 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004129 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 */
4131 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004132termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133{
4134 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4135 if (len >= 3 && code[len - 2] == '*')
4136 {
4137 if (len >= 5 && code[len - 3] == ';')
4138 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004139 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004140 return 1;
4141 }
4142 return 0;
4143}
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004146find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 int i;
4149
4150 for (i = 0; i < tc_len; ++i)
4151 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4152 return termcodes[i].code;
4153 return NULL;
4154}
4155
4156#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4157 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004158get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 if (i >= tc_len)
4161 return NULL;
4162 return &termcodes[i].name[0];
4163}
4164#endif
4165
4166 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004167del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 int i;
4170
4171 if (termcodes == NULL) /* nothing there yet */
4172 return;
4173
4174 need_gather = TRUE; /* need to fill termleader[] */
4175
4176 for (i = 0; i < tc_len; ++i)
4177 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4178 {
4179 del_termcode_idx(i);
4180 return;
4181 }
4182 /* not found. Give error message? */
4183}
4184
4185 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004186del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 int i;
4189
4190 vim_free(termcodes[idx].code);
4191 --tc_len;
4192 for (i = idx; i < tc_len; ++i)
4193 termcodes[i] = termcodes[i + 1];
4194}
4195
4196#ifdef FEAT_TERMRESPONSE
4197/*
4198 * Called when detected that the terminal sends 8-bit codes.
4199 * Convert all 7-bit codes to their 8-bit equivalent.
4200 */
4201 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004202switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 int i;
4205 int c;
4206
4207 /* Only need to do something when not already using 8-bit codes. */
4208 if (!term_is_8bit(T_NAME))
4209 {
4210 for (i = 0; i < tc_len; ++i)
4211 {
4212 c = term_7to8bit(termcodes[i].code);
4213 if (c != 0)
4214 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004215 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 termcodes[i].code[0] = c;
4217 }
4218 }
4219 need_gather = TRUE; /* need to fill termleader[] */
4220 }
4221 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004222 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223}
4224#endif
4225
4226#ifdef CHECK_DOUBLE_CLICK
4227static linenr_T orig_topline = 0;
4228# ifdef FEAT_DIFF
4229static int orig_topfill = 0;
4230# endif
4231#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004232#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233/*
4234 * Checking for double clicks ourselves.
4235 * "orig_topline" is used to avoid detecting a double-click when the window
4236 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4237 */
4238/*
4239 * Set orig_topline. Used when jumping to another window, so that a double
4240 * click still works.
4241 */
4242 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004243set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 orig_topline = wp->w_topline;
4246# ifdef FEAT_DIFF
4247 orig_topfill = wp->w_topfill;
4248# endif
4249}
4250#endif
4251
4252/*
4253 * Check if typebuf.tb_buf[] contains a terminal key code.
4254 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4255 * + max_offset].
4256 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004257 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 * With a match, the match is removed, the replacement code is inserted in
4259 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4260 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004261 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4262 * "buflen" is then the length of the string in buf[] and is updated for
4263 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
4265 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004266check_termcode(
4267 int max_offset,
4268 char_u *buf,
4269 int bufsize,
4270 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 char_u *tp;
4273 char_u *p;
4274 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004275 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004277 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int offset;
4279 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004281 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int new_slen;
4284 int extra;
4285 char_u string[MAX_KEY_CODE_LEN + 1];
4286 int i, j;
4287 int idx = 0;
4288#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004289# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4290 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 char_u bytes[6];
4292 int num_bytes;
4293# endif
4294 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 int is_click, is_drag;
4296 int wheel_code = 0;
4297 int current_button;
4298 static int held_button = MOUSE_RELEASE;
4299 static int orig_num_clicks = 1;
4300 static int orig_mouse_code = 0x0;
4301# ifdef CHECK_DOUBLE_CLICK
4302 static int orig_mouse_col = 0;
4303 static int orig_mouse_row = 0;
4304 static struct timeval orig_mouse_time = {0, 0};
4305 /* time of previous mouse click */
4306 struct timeval mouse_time; /* time of current mouse click */
4307 long timediff; /* elapsed time in msec */
4308# endif
4309#endif
4310 int cpo_koffset;
4311#ifdef FEAT_MOUSE_GPM
4312 extern int gpm_flag; /* gpm library variable */
4313#endif
4314
4315 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4316
4317 /*
4318 * Speed up the checks for terminal codes by gathering all first bytes
4319 * used in termleader[]. Often this is just a single <Esc>.
4320 */
4321 if (need_gather)
4322 gather_termleader();
4323
4324 /*
4325 * Check at several positions in typebuf.tb_buf[], to catch something like
4326 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4327 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004328 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 * This is used often, KEEP IT FAST!
4330 */
4331 for (offset = 0; offset < max_offset; ++offset)
4332 {
4333 if (buf == NULL)
4334 {
4335 if (offset >= typebuf.tb_len)
4336 break;
4337 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4338 len = typebuf.tb_len - offset; /* length of the input */
4339 }
4340 else
4341 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004342 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004345 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347
4348 /*
4349 * Don't check characters after K_SPECIAL, those are already
4350 * translated terminal chars (avoid translating ~@^Hx).
4351 */
4352 if (*tp == K_SPECIAL)
4353 {
4354 offset += 2; /* there are always 2 extra characters */
4355 continue;
4356 }
4357
4358 /*
4359 * Skip this position if the character does not appear as the first
4360 * character in term_strings. This speeds up a lot, since most
4361 * termcodes start with the same character (ESC or CSI).
4362 */
4363 i = *tp;
4364 for (p = termleader; *p && *p != i; ++p)
4365 ;
4366 if (*p == NUL)
4367 continue;
4368
4369 /*
4370 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4371 * are in Insert mode.
4372 */
4373 if (*tp == ESC && !p_ek && (State & INSERT))
4374 continue;
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004377 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004378 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380#ifdef FEAT_GUI
4381 if (gui.in_use)
4382 {
4383 /*
4384 * GUI special key codes are all of the form [CSI xx].
4385 */
4386 if (*tp == CSI) /* Special key from GUI */
4387 {
4388 if (len < 3)
4389 return -1; /* Shouldn't happen */
4390 slen = 3;
4391 key_name[0] = tp[1];
4392 key_name[1] = tp[2];
4393 }
4394 }
4395 else
4396#endif /* FEAT_GUI */
4397 {
4398 for (idx = 0; idx < tc_len; ++idx)
4399 {
4400 /*
4401 * Ignore the entry if we are not at the start of
4402 * typebuf.tb_buf[]
4403 * and there are not enough characters to make a match.
4404 * But only when the 'K' flag is in 'cpoptions'.
4405 */
4406 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004407 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (cpo_koffset && offset && len < slen)
4409 continue;
4410 if (STRNCMP(termcodes[idx].code, tp,
4411 (size_t)(slen > len ? len : slen)) == 0)
4412 {
4413 if (len < slen) /* got a partial sequence */
4414 return -1; /* need to get more chars */
4415
4416 /*
4417 * When found a keypad key, check if there is another key
4418 * that matches and use that one. This makes <Home> to be
4419 * found instead of <kHome> when they produce the same
4420 * key code.
4421 */
4422 if (termcodes[idx].name[0] == 'K'
4423 && VIM_ISDIGIT(termcodes[idx].name[1]))
4424 {
4425 for (j = idx + 1; j < tc_len; ++j)
4426 if (termcodes[j].len == slen &&
4427 STRNCMP(termcodes[idx].code,
4428 termcodes[j].code, slen) == 0)
4429 {
4430 idx = j;
4431 break;
4432 }
4433 }
4434
4435 key_name[0] = termcodes[idx].name[0];
4436 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 break;
4438 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004439
4440 /*
4441 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004442 * <Esc>[123;*X (modslen == slen - 3)
4443 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4444 * When there is a modifier the * matches a number.
4445 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004446 */
4447 if (termcodes[idx].modlen > 0)
4448 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 modslen = termcodes[idx].modlen;
4450 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004451 continue;
4452 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004453 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004454 {
4455 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004456
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004457 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004458 return -1; /* need to get more chars */
4459
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 if (tp[modslen] == termcodes[idx].code[slen - 1])
4461 slen = modslen + 1; /* no modifiers */
4462 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004463 continue; /* no match */
4464 else
4465 {
4466 /* Skip over the digits, the final char must
4467 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004468 for (j = slen - 2; j < len && (isdigit(tp[j])
4469 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004470 ;
4471 ++j;
4472 if (len < j) /* got a partial sequence */
4473 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004474 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4475 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004476
Bram Moolenaara529ce02017-06-22 22:37:57 +02004477 modifiers_start = tp + slen - 2;
4478
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004479 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004480 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004481 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004482 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004483 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004484 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004485 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004487 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004488 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004489
4490 slen = j;
4491 }
4492 key_name[0] = termcodes[idx].name[0];
4493 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004494 break;
4495 }
4496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 }
4499
4500#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004501 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004502 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004503 * detecting the start of these mouse codes they might as well be
4504 * another key code or terminal response. */
4505# ifdef FEAT_MOUSE_DEC
4506 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004507# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004508# ifdef FEAT_MOUSE_PTERM
4509 || key_name[0] == KS_PTERM_MOUSE
4510# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004511 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004513 /* Check for some responses from the terminal starting with
4514 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004515 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004516 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004517 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004518 * Also eat other possible responses to t_RV, rxvt returns
4519 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4520 * mrxvt has been reported to have "+" in the version. Assume
4521 * the escape sequence ends with a letter or one of "{|}~".
4522 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004523 * - Cursor position report: <Esc>[{row};{col}R
4524 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004525 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004526 *
4527 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004528 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004529 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004530
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004531 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004532 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004533 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004534 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004536 int col = 0;
4537 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004538#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004539 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004540#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004543 for (i = 2 + (tp[0] != CSI); i < len
4544 && !(tp[i] >= '{' && tp[i] <= '~')
4545 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004546 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004547 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004548 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004549#ifdef FEAT_MBYTE
4550 row_char = tp[i - 1];
4551#endif
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004554 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004555 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004556 return -1;
4557 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004558 if (extra > 0)
4559 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004560
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004561#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004562 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4563 * u7_status is not "sent", it may be from a previous Vim that
4564 * just exited. But not for <S-F3>, it sends something
4565 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004566 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004567 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004568 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004569 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004570 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004571
Bram Moolenaarb255b902018-04-24 21:40:10 +02004572 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004573 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004574 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004575 if (col == 2)
4576 aw = "single";
4577 else if (col == 3)
4578 aw = "double";
4579 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4580 {
4581 /* Setting the option causes a screen redraw. Do
4582 * that right away if possible, keeping any
4583 * messages. */
4584 set_option_value((char_u *)"ambw", 0L,
4585 (char_u *)aw, 0);
4586# ifdef DEBUG_TERMRESPONSE
4587 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004588 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004589
Bram Moolenaarb255b902018-04-24 21:40:10 +02004590 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004591 }
4592# else
4593 redraw_asap(CLEAR);
4594# endif
4595 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004596 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004597 key_name[0] = (int)KS_EXTRA;
4598 key_name[1] = (int)KE_IGNORE;
4599 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004600# ifdef FEAT_EVAL
4601 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4602# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004603 }
4604 else
4605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004607 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004609 int version = col;
4610
Bram Moolenaarb255b902018-04-24 21:40:10 +02004611 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004612 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004613 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 /* If this code starts with CSI, you can bet that the
4616 * terminal uses 8-bit codes. */
4617 if (tp[0] == CSI)
4618 switch_to_8bit();
4619
4620 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004621 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 * Ignore it for when the user has set 'term' to xterm,
4623 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004624 if (version > 20000)
4625 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004627 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004629 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004630# ifdef FEAT_MOUSE_SGR
4631 int is_iterm2 = FALSE;
4632# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004635 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004637 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 check_for_codes = TRUE;
4639 need_gather = TRUE;
4640 req_codes_from_term();
4641 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004642
4643 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004644 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004645 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004646 {
4647 /* If run from Vim $COLORS is set to the number of
4648 * colors the terminal supports. Otherwise assume
4649 * 256, libvterm supports even more. */
4650 if (mch_getenv((char_u *)"COLORS") == NULL)
4651 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004652# ifdef FEAT_MOUSE_SGR
4653 /* Libvterm can handle SGR mouse reporting. */
4654 if (!option_was_set((char_u *)"ttym"))
4655 set_option_value((char_u *)"ttym", 0L,
4656 (char_u *)"sgr", 0);
4657# endif
4658 }
4659
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004660 if (version == 95)
4661 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004662 // Mac Terminal.app sends 1;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004663 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4664 {
4665 is_not_xterm = TRUE;
4666 is_mac_terminal = TRUE;
4667 }
4668# ifdef FEAT_MOUSE_SGR
Bram Moolenaare330ef42018-07-06 23:11:40 +02004669 // iTerm2 sends 0;95;0
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004670 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4671 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004672 else
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004673# endif
Bram Moolenaare330ef42018-07-06 23:11:40 +02004674 // old iTerm2 sends 0;95;
4675 if (STRNCMP(tp + extra - 2, "0;95;c", 6) == 0)
4676 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004677 }
4678
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004679 /* Only set 'ttymouse' automatically if it was not set
4680 * by the user already. */
4681 if (!option_was_set((char_u *)"ttym"))
4682 {
4683# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004684 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004685 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004686 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004687 set_option_value((char_u *)"ttym", 0L,
4688 (char_u *)"sgr", 0);
4689 else
4690# endif
4691 /* if xterm version >= 95 use mouse dragging */
4692 if (version >= 95)
4693 set_option_value((char_u *)"ttym", 0L,
4694 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004695 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004696
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004697 /* Detect terminals that set $TERM to something like
4698 * "xterm-256colors" but are not fully xterm
4699 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004700
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004701 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004702 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004703 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004704 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004705 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004706 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004707 is_not_xterm = TRUE;
4708
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004709 /* PuTTY sends 0;136;0
4710 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004711 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004712 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004713 is_not_xterm = TRUE;
4714
4715 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004716 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004717 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004718 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004719
Bram Moolenaar668324e2018-06-30 17:09:26 +02004720 // Xterm first responded to this request at patch level
4721 // 95, so assume anything below 95 is not xterm.
4722 if (version < 95)
4723 is_not_xterm = TRUE;
4724
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004725 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004726 * set. Only supported properly by xterm since version
4727 * 279 (otherwise it returns 0x18).
4728 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004729 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004730 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004731 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004732 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004733 && *T_CSH != NUL
4734 && *T_CRS != NUL)
4735 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004736 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004737 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004738 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004739 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004740 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004741
4742 /* Only request the cursor blink mode if t_RC set. Not
4743 * for Gnome terminal, it can't handle t_RC, it
4744 * echoes the characters to the screen. */
4745 if (rbm_status == STATUS_GET
4746 && !is_not_xterm
4747 && *T_CRC != NUL)
4748 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004749 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004750 out_str(T_CRC);
4751 rbm_status = STATUS_SENT;
4752 need_flush = TRUE;
4753 }
4754
4755 if (need_flush)
4756 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004758 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004760 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 apply_autocmds(EVENT_TERMRESPONSE,
4763 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 key_name[0] = (int)KS_EXTRA;
4765 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004767
Bram Moolenaar4db25542017-08-28 22:43:05 +02004768 /* Check blinking cursor from xterm:
4769 * {lead}?12;1$y set
4770 * {lead}?12;2$y not set
4771 *
4772 * {lead} can be <Esc>[ or CSI
4773 */
4774 else if (rbm_status == STATUS_SENT
4775 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4776 && i == j + 6
4777 && tp[j + 1] == '1'
4778 && tp[j + 2] == '2'
4779 && tp[j + 3] == ';'
4780 && tp[i - 1] == '$'
4781 && tp[i] == 'y')
4782 {
4783 initial_cursor_blink = (tp[j + 4] == '1');
4784 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004785 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004786 key_name[0] = (int)KS_EXTRA;
4787 key_name[1] = (int)KE_IGNORE;
4788 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004789# ifdef FEAT_EVAL
4790 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4791# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004792 }
4793
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004794 /*
4795 * Check for a window position response from the terminal:
4796 * {lead}3;{x}:{y}t
4797 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004798 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004799 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4800 || (len >= 3 && tp[0] == CSI))
4801 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4802 && tp[j + 1] == ';')
4803 {
4804 j += 2;
4805 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4806 ;
4807 if (i < len && tp[i] == ';')
4808 {
4809 winpos_x = atoi((char *)tp + j);
4810 j = i + 1;
4811 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4812 ;
4813 if (i < len && tp[i] == 't')
4814 {
4815 winpos_y = atoi((char *)tp + j);
4816 /* got finished code: consume it */
4817 key_name[0] = (int)KS_EXTRA;
4818 key_name[1] = (int)KE_IGNORE;
4819 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004820
4821 if (--did_request_winpos <= 0)
4822 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004823 }
4824 }
4825 if (i == len)
4826 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004827 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004828 return -1;
4829 }
4830 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004831 }
4832
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004833 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004834 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004835 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004836 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004837 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004838 * {lead} can be <Esc>] or OSC
4839 * {tail} can be '\007', <Esc>\ or STERM.
4840 *
4841 * Consume any code that starts with "{lead}11;", it's also
4842 * possible that "rgba" is following.
4843 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004844 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004845 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4846 || tp[0] == OSC))
4847 {
4848 j = 1 + (tp[0] == ESC);
4849 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004850 || (argp[1] != '1' && argp[1] != '0')
4851 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004852 i = 0; /* no match */
4853 else
4854 for (i = j; i < len; ++i)
4855 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4856 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004857 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004858 int is_bg = argp[1] == '1';
4859
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004860 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004861 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004862 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004863#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004864 int rval = hexhex2nr(tp + j + 7);
4865 int gval = hexhex2nr(tp + j + 12);
4866 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004867#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004868 if (is_bg)
4869 {
4870 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004871 + tp[j+17]) ? "light" : "dark";
4872
Bram Moolenaarb255b902018-04-24 21:40:10 +02004873 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004874 rbg_status = STATUS_GOT;
4875#ifdef FEAT_TERMINAL
4876 bg_r = rval;
4877 bg_g = gval;
4878 bg_b = bval;
4879#endif
4880 if (!option_was_set((char_u *)"bg")
4881 && STRCMP(p_bg, newval) != 0)
4882 {
4883 /* value differs, apply it */
4884 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004885 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004886 reset_option_was_set((char_u *)"bg");
4887 redraw_asap(CLEAR);
4888 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004889 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004890#ifdef FEAT_TERMINAL
4891 else
4892 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004893 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004894 rfg_status = STATUS_GOT;
4895 fg_r = rval;
4896 fg_g = gval;
4897 fg_b = bval;
4898 }
4899#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004900 }
4901
4902 /* got finished code: consume it */
4903 key_name[0] = (int)KS_EXTRA;
4904 key_name[1] = (int)KE_IGNORE;
4905 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004906# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004907 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4908 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004909# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004910 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004911 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004912 if (i == len)
4913 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004914 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004915 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004919 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004920 * {lead}{flag}+r<hex bytes><{tail}
4921 *
4922 * {lead} can be <Esc>P or DCS
4923 * {flag} can be '0' or '1'
4924 * {tail} can be Esc>\ or STERM
4925 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004926 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004927 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004928 *
4929 * {lead} can be <Esc>P or DCS
4930 * {tail} can be Esc>\ or STERM
4931 *
4932 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004933 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004934 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004935 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 || tp[0] == DCS))
4937 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004938 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004939 if (len < j + 3)
4940 i = len; /* need more chars */
4941 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004942 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004943 else if (argp[1] == '+')
4944 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004945 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004946 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004947 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 || tp[i] == STERM)
4949 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004950 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 got_code_from_term(tp + j, i);
4952 key_name[0] = (int)KS_EXTRA;
4953 key_name[1] = (int)KE_IGNORE;
4954 slen = i + 1 + (tp[i] == ESC);
4955 break;
4956 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004957 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004958 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004959 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004960 /* Probably the cursor shape response. Make sure that "i"
4961 * is equal to "len" when there are not sufficient
4962 * characters. */
4963 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004964 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004965 if (i - j == 3 && !isdigit(tp[i]))
4966 break;
4967 if (i - j == 4 && tp[i] != ' ')
4968 break;
4969 if (i - j == 5 && tp[i] != 'q')
4970 break;
4971 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4972 break;
4973 if ((i - j == 6 && tp[i] == STERM)
4974 || (i - j == 7 && tp[i] == '\\'))
4975 {
4976 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004977
Bram Moolenaar590ec872018-03-02 20:58:42 +01004978 /* 0, 1 = block blink, 2 = block
4979 * 3 = underline blink, 4 = underline
4980 * 5 = vertical bar blink, 6 = vertical bar */
4981 number = number == 0 ? 1 : number;
4982 initial_cursor_shape = (number + 1) / 2;
4983 /* The blink flag is actually inverted, compared to
4984 * the value set with T_SH. */
4985 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004986 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004987 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004988 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004989
Bram Moolenaar590ec872018-03-02 20:58:42 +01004990 key_name[0] = (int)KS_EXTRA;
4991 key_name[1] = (int)KE_IGNORE;
4992 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004993# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004994 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004995# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004996 break;
4997 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004998 }
4999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005002 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005003 /* These codes arrive many together, each code can be
5004 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005005 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005006 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 }
5010#endif
5011
5012 if (key_name[0] == NUL)
5013 continue; /* No match at this position, try next one */
5014
5015 /* We only get here when we have a complete termcode match */
5016
5017#ifdef FEAT_MOUSE
5018# ifdef FEAT_GUI
5019 /*
5020 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5021 * so that we know which window to scroll later.
5022 */
5023 if (gui.in_use
5024 && key_name[0] == (int)KS_EXTRA
5025 && (key_name[1] == (int)KE_X1MOUSE
5026 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005027 || key_name[1] == (int)KE_MOUSELEFT
5028 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 || key_name[1] == (int)KE_MOUSEDOWN
5030 || key_name[1] == (int)KE_MOUSEUP))
5031 {
5032 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5033 if (num_bytes == -1) /* not enough coordinates */
5034 return -1;
5035 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5036 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5037 slen += num_bytes;
5038 }
5039 else
5040# endif
5041 /*
5042 * If it is a mouse click, get the coordinates.
5043 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005044 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005046 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047# endif
5048# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005049 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050# endif
5051# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005052 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053# endif
5054# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005055 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005057# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005058 || key_name[0] == KS_URXVT_MOUSE
5059# endif
5060# ifdef FEAT_MOUSE_SGR
5061 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005062 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 )
5065 {
5066 is_click = is_drag = FALSE;
5067
Bram Moolenaar864207d2008-06-24 22:14:38 +00005068# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5069 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (key_name[0] == (int)KS_MOUSE)
5071 {
5072 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005073 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 * s == encoded button state:
5075 * 0x20 = left button down
5076 * 0x21 = middle button down
5077 * 0x22 = right button down
5078 * 0x23 = any button release
5079 * 0x60 = button 4 down (scroll wheel down)
5080 * 0x61 = button 5 down (scroll wheel up)
5081 * add 0x04 for SHIFT
5082 * add 0x08 for ALT
5083 * add 0x10 for CTRL
5084 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005085 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5086 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 * c == column + ' ' + 1 == column + 33
5088 * r == row + ' ' + 1 == row + 33
5089 *
5090 * The coordinates are passed on through global variables.
5091 * Ugly, but this avoids trouble with mouse clicks at an
5092 * unexpected moment and allows for mapping them.
5093 */
5094 for (;;)
5095 {
5096#ifdef FEAT_GUI
5097 if (gui.in_use)
5098 {
5099 /* GUI uses more bits for columns > 223 */
5100 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5101 if (num_bytes == -1) /* not enough coordinates */
5102 return -1;
5103 mouse_code = bytes[0];
5104 mouse_col = 128 * (bytes[1] - ' ' - 1)
5105 + bytes[2] - ' ' - 1;
5106 mouse_row = 128 * (bytes[3] - ' ' - 1)
5107 + bytes[4] - ' ' - 1;
5108 }
5109 else
5110#endif
5111 {
5112 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5113 if (num_bytes == -1) /* not enough coordinates */
5114 return -1;
5115 mouse_code = bytes[0];
5116 mouse_col = bytes[1] - ' ' - 1;
5117 mouse_row = bytes[2] - ' ' - 1;
5118 }
5119 slen += num_bytes;
5120
5121 /* If the following bytes is also a mouse code and it has
5122 * the same code, dump this one and get the next. This
5123 * makes dragging a whole lot faster. */
5124#ifdef FEAT_GUI
5125 if (gui.in_use)
5126 j = 3;
5127 else
5128#endif
5129 j = termcodes[idx].len;
5130 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5131 && tp[slen + j] == mouse_code
5132 && tp[slen + j + 1] != NUL
5133 && tp[slen + j + 2] != NUL
5134#ifdef FEAT_GUI
5135 && (!gui.in_use
5136 || (tp[slen + j + 3] != NUL
5137 && tp[slen + j + 4] != NUL))
5138#endif
5139 )
5140 slen += j;
5141 else
5142 break;
5143 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005146# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5147 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005148 || key_name[0] == KS_SGR_MOUSE
5149 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005150 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005151 /* URXVT 1015 mouse reporting mode:
5152 * Almost identical to xterm mouse mode, except the values
5153 * are decimal instead of bytes.
5154 *
5155 * \033[%d;%d;%dM
5156 * ^-- row
5157 * ^----- column
5158 * ^-------- code
5159 *
5160 * SGR 1006 mouse reporting mode:
5161 * Almost identical to xterm mouse mode, except the values
5162 * are decimal instead of bytes.
5163 *
5164 * \033[<%d;%d;%dM
5165 * ^-- row
5166 * ^----- column
5167 * ^-------- code
5168 *
5169 * \033[<%d;%d;%dm : mouse release event
5170 * ^-- row
5171 * ^----- column
5172 * ^-------- code
5173 */
5174 p = modifiers_start;
5175 if (p == NULL)
5176 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005177
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005178 mouse_code = getdigits(&p);
5179 if (*p++ != ';')
5180 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005181
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005182 /* when mouse reporting is SGR, add 32 to mouse code */
5183 if (key_name[0] == KS_SGR_MOUSE
5184 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5185 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005186
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005187 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5188 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005189
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005190 mouse_col = getdigits(&p) - 1;
5191 if (*p++ != ';')
5192 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005193
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005194 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005195
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005196 /* The modifiers were the mouse coordinates, not the
5197 * modifier keys (alt/shift/ctrl/meta) state. */
5198 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005199 }
5200# endif
5201
5202 if (key_name[0] == (int)KS_MOUSE
5203#ifdef FEAT_MOUSE_URXVT
5204 || key_name[0] == (int)KS_URXVT_MOUSE
5205#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005206#ifdef FEAT_MOUSE_SGR
5207 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005208 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005209#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005210 )
5211 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005212# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 /*
5214 * Handle mouse events.
5215 * Recognize the xterm mouse wheel, but not in the GUI, the
5216 * Linux console with GPM and the MS-DOS or Win32 console
5217 * (multi-clicks use >= 0x60).
5218 */
5219 if (mouse_code >= MOUSEWHEEL_LOW
5220# ifdef FEAT_GUI
5221 && !gui.in_use
5222# endif
5223# ifdef FEAT_MOUSE_GPM
5224 && gpm_flag == 0
5225# endif
5226 )
5227 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005228# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5229 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5230 /* mouse-move event, using MOUSE_DRAG works */
5231 mouse_code = MOUSE_DRAG;
5232 else
5233# endif
5234 /* Keep the mouse_code before it's changed, so that we
5235 * remember that it was a mouse wheel click. */
5236 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238# ifdef FEAT_MOUSE_XTERM
5239 else if (held_button == MOUSE_RELEASE
5240# ifdef FEAT_GUI
5241 && !gui.in_use
5242# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005243 && (mouse_code == 0x23 || mouse_code == 0x24
5244 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005246 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5247 * And 0x40 and 0x41 are used by some xterm emulator. */
5248 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5249 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251# endif
5252
5253# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5254 else if (use_xterm_mouse() > 1)
5255 {
5256 if (mouse_code & MOUSE_DRAG_XTERM)
5257 mouse_code |= MOUSE_DRAG;
5258 }
5259# endif
5260# ifdef FEAT_XCLIPBOARD
5261 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5262 {
5263 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5264 stop_xterm_trace();
5265 else
5266 start_xterm_trace(mouse_code);
5267 }
5268# endif
5269# endif
5270 }
5271# endif /* !UNIX || FEAT_MOUSE_XTERM */
5272# ifdef FEAT_MOUSE_NET
5273 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5274 {
5275 int mc, mr;
5276
5277 /* expect a rather limited sequence like: balancing {
5278 * \033}6,45\r
5279 * '6' is the row, 45 is the column
5280 */
5281 p = tp + slen;
5282 mr = getdigits(&p);
5283 if (*p++ != ',')
5284 return -1;
5285 mc = getdigits(&p);
5286 if (*p++ != '\r')
5287 return -1;
5288
5289 mouse_col = mc - 1;
5290 mouse_row = mr - 1;
5291 mouse_code = MOUSE_LEFT;
5292 slen += (int)(p - (tp + slen));
5293 }
5294# endif /* FEAT_MOUSE_NET */
5295# ifdef FEAT_MOUSE_JSB
5296 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5297 {
5298 int mult, val, iter, button, status;
5299
5300 /* JSBTERM Input Model
5301 * \033[0~zw uniq escape sequence
5302 * (L-x) Left button pressed - not pressed x not reporting
5303 * (M-x) Middle button pressed - not pressed x not reporting
5304 * (R-x) Right button pressed - not pressed x not reporting
5305 * (SDmdu) Single , Double click, m mouse move d button down
5306 * u button up
5307 * ### X cursor position padded to 3 digits
5308 * ### Y cursor position padded to 3 digits
5309 * (s-x) SHIFT key pressed - not pressed x not reporting
5310 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005311 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 */
5313
5314 p = tp + slen;
5315 button = mouse_code = 0;
5316 switch (*p++)
5317 {
5318 case 'L': button = 1; break;
5319 case '-': break;
5320 case 'x': break; /* ignore sequence */
5321 default: return -1; /* Unknown Result */
5322 }
5323 switch (*p++)
5324 {
5325 case 'M': button |= 2; break;
5326 case '-': break;
5327 case 'x': break; /* ignore sequence */
5328 default: return -1; /* Unknown Result */
5329 }
5330 switch (*p++)
5331 {
5332 case 'R': button |= 4; break;
5333 case '-': break;
5334 case 'x': break; /* ignore sequence */
5335 default: return -1; /* Unknown Result */
5336 }
5337 status = *p++;
5338 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5339 mult /= 10, p++)
5340 if (*p >= '0' && *p <= '9')
5341 val += (*p - '0') * mult;
5342 else
5343 return -1;
5344 mouse_col = val;
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_row = val;
5352 switch (*p++)
5353 {
5354 case 's': button |= 8; break; /* SHIFT key Pressed */
5355 case '-': break; /* Not Pressed */
5356 case 'x': break; /* Not Reporting */
5357 default: return -1; /* Unknown Result */
5358 }
5359 switch (*p++)
5360 {
5361 case 'c': button |= 16; break; /* CTRL key Pressed */
5362 case '-': break; /* Not Pressed */
5363 case 'x': break; /* Not Reporting */
5364 default: return -1; /* Unknown Result */
5365 }
5366 if (*p++ != '\033')
5367 return -1;
5368 if (*p++ != '\\')
5369 return -1;
5370 switch (status)
5371 {
5372 case 'D': /* Double Click */
5373 case 'S': /* Single Click */
5374 if (button & 1) mouse_code |= MOUSE_LEFT;
5375 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5376 if (button & 4) mouse_code |= MOUSE_RIGHT;
5377 if (button & 8) mouse_code |= MOUSE_SHIFT;
5378 if (button & 16) mouse_code |= MOUSE_CTRL;
5379 break;
5380 case 'm': /* Mouse move */
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 if ((button & 7) != 0)
5387 {
5388 held_button = mouse_code;
5389 mouse_code |= MOUSE_DRAG;
5390 }
5391 is_drag = TRUE;
5392 showmode();
5393 break;
5394 case 'd': /* Button Down */
5395 if (button & 1) mouse_code |= MOUSE_LEFT;
5396 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5397 if (button & 4) mouse_code |= MOUSE_RIGHT;
5398 if (button & 8) mouse_code |= MOUSE_SHIFT;
5399 if (button & 16) mouse_code |= MOUSE_CTRL;
5400 break;
5401 case 'u': /* Button Up */
5402 if (button & 1)
5403 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5404 if (button & 2)
5405 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5406 if (button & 4)
5407 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5408 if (button & 8)
5409 mouse_code |= MOUSE_SHIFT;
5410 if (button & 16)
5411 mouse_code |= MOUSE_CTRL;
5412 break;
5413 default: return -1; /* Unknown Result */
5414 }
5415
5416 slen += (p - (tp + slen));
5417 }
5418# endif /* FEAT_MOUSE_JSB */
5419# ifdef FEAT_MOUSE_DEC
5420 if (key_name[0] == (int)KS_DEC_MOUSE)
5421 {
5422 /* The DEC Locator Input Model
5423 * Netterm delivers the code sequence:
5424 * \033[2;4;24;80&w (left button down)
5425 * \033[3;0;24;80&w (left button up)
5426 * \033[6;1;24;80&w (right button down)
5427 * \033[7;0;24;80&w (right button up)
5428 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5429 * Pe is the event code
5430 * Pb is the button code
5431 * Pr is the row coordinate
5432 * Pc is the column coordinate
5433 * Pp is the third coordinate (page number)
5434 * Pe, the event code indicates what event caused this report
5435 * The following event codes are defined:
5436 * 0 - request, the terminal received an explicit request
5437 * for a locator report, but the locator is unavailable
5438 * 1 - request, the terminal received an explicit request
5439 * for a locator report
5440 * 2 - left button down
5441 * 3 - left button up
5442 * 4 - middle button down
5443 * 5 - middle button up
5444 * 6 - right button down
5445 * 7 - right button up
5446 * 8 - fourth button down
5447 * 9 - fourth button up
5448 * 10 - locator outside filter rectangle
5449 * Pb, the button code, ASCII decimal 0-15 indicating which
5450 * buttons are down if any. The state of the four buttons
5451 * on the locator correspond to the low four bits of the
5452 * decimal value,
5453 * "1" means button depressed
5454 * 0 - no buttons down,
5455 * 1 - right,
5456 * 2 - middle,
5457 * 4 - left,
5458 * 8 - fourth
5459 * Pr is the row coordinate of the locator position in the page,
5460 * encoded as an ASCII decimal value.
5461 * If Pr is omitted, the locator position is undefined
5462 * (outside the terminal window for example).
5463 * Pc is the column coordinate of the locator position in the
5464 * page, encoded as an ASCII decimal value.
5465 * If Pc is omitted, the locator position is undefined
5466 * (outside the terminal window for example).
5467 * Pp is the page coordinate of the locator position
5468 * encoded as an ASCII decimal value.
5469 * The page coordinate may be omitted if the locator is on
5470 * page one (the default). We ignore it anyway.
5471 */
5472 int Pe, Pb, Pr, Pc;
5473
5474 p = tp + slen;
5475
5476 /* get event status */
5477 Pe = getdigits(&p);
5478 if (*p++ != ';')
5479 return -1;
5480
5481 /* get button status */
5482 Pb = getdigits(&p);
5483 if (*p++ != ';')
5484 return -1;
5485
5486 /* get row status */
5487 Pr = getdigits(&p);
5488 if (*p++ != ';')
5489 return -1;
5490
5491 /* get column status */
5492 Pc = getdigits(&p);
5493
5494 /* the page parameter is optional */
5495 if (*p == ';')
5496 {
5497 p++;
5498 (void)getdigits(&p);
5499 }
5500 if (*p++ != '&')
5501 return -1;
5502 if (*p++ != 'w')
5503 return -1;
5504
5505 mouse_code = 0;
5506 switch (Pe)
5507 {
5508 case 0: return -1; /* position request while unavailable */
5509 case 1: /* a response to a locator position request includes
5510 the status of all buttons */
5511 Pb &= 7; /* mask off and ignore fourth button */
5512 if (Pb & 4)
5513 mouse_code = MOUSE_LEFT;
5514 if (Pb & 2)
5515 mouse_code = MOUSE_MIDDLE;
5516 if (Pb & 1)
5517 mouse_code = MOUSE_RIGHT;
5518 if (Pb)
5519 {
5520 held_button = mouse_code;
5521 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005522 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 is_drag = TRUE;
5525 showmode();
5526 break;
5527 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005528 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 break;
5530 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5531 break;
5532 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005533 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 break;
5535 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5536 break;
5537 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005538 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 break;
5540 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5541 break;
5542 case 8: return -1; /* fourth button down */
5543 case 9: return -1; /* fourth button up */
5544 case 10: return -1; /* mouse outside of filter rectangle */
5545 default: return -1; /* should never occur */
5546 }
5547
5548 mouse_col = Pc - 1;
5549 mouse_row = Pr - 1;
5550
5551 slen += (int)(p - (tp + slen));
5552 }
5553# endif /* FEAT_MOUSE_DEC */
5554# ifdef FEAT_MOUSE_PTERM
5555 if (key_name[0] == (int)KS_PTERM_MOUSE)
5556 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005557 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 p = tp + slen;
5560
5561 action = getdigits(&p);
5562 if (*p++ != ';')
5563 return -1;
5564
5565 mouse_row = getdigits(&p);
5566 if (*p++ != ';')
5567 return -1;
5568 mouse_col = getdigits(&p);
5569 if (*p++ != ';')
5570 return -1;
5571
5572 button = getdigits(&p);
5573 mouse_code = 0;
5574
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005575 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 case 4: mouse_code = MOUSE_LEFT; break;
5578 case 1: mouse_code = MOUSE_RIGHT; break;
5579 case 2: mouse_code = MOUSE_MIDDLE; break;
5580 default: return -1;
5581 }
5582
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005583 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 case 31: /* Initial press */
5586 if (*p++ != ';')
5587 return -1;
5588
5589 num_clicks = getdigits(&p); /* Not used */
5590 break;
5591
5592 case 32: /* Release */
5593 mouse_code |= MOUSE_RELEASE;
5594 break;
5595
5596 case 33: /* Drag */
5597 held_button = mouse_code;
5598 mouse_code |= MOUSE_DRAG;
5599 break;
5600
5601 default:
5602 return -1;
5603 }
5604
5605 if (*p++ != 't')
5606 return -1;
5607
5608 slen += (p - (tp + slen));
5609 }
5610# endif /* FEAT_MOUSE_PTERM */
5611
5612 /* Interpret the mouse code */
5613 current_button = (mouse_code & MOUSE_CLICK_MASK);
5614 if (current_button == MOUSE_RELEASE
5615# ifdef FEAT_MOUSE_XTERM
5616 && wheel_code == 0
5617# endif
5618 )
5619 {
5620 /*
5621 * If we get a mouse drag or release event when
5622 * there is no mouse button held down (held_button ==
5623 * MOUSE_RELEASE), produce a K_IGNORE below.
5624 * (can happen when you hold down two buttons
5625 * and then let them go, or click in the menu bar, but not
5626 * on a menu, and drag into the text).
5627 */
5628 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5629 is_drag = TRUE;
5630 current_button = held_button;
5631 }
5632 else if (wheel_code == 0)
5633 {
5634# ifdef CHECK_DOUBLE_CLICK
5635# ifdef FEAT_MOUSE_GPM
5636# ifdef FEAT_GUI
5637 /*
5638 * Only for Unix, when GUI or gpm is not active, we handle
5639 * multi-clicks here.
5640 */
5641 if (gpm_flag == 0 && !gui.in_use)
5642# else
5643 if (gpm_flag == 0)
5644# endif
5645# else
5646# ifdef FEAT_GUI
5647 if (!gui.in_use)
5648# endif
5649# endif
5650 {
5651 /*
5652 * Compute the time elapsed since the previous mouse click.
5653 */
5654 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005655 if (orig_mouse_time.tv_sec == 0)
5656 {
5657 /*
5658 * Avoid computing the difference between mouse_time
5659 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005660 * difference would be huge and would cause
5661 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005662 */
5663 timediff = p_mouset;
5664 }
5665 else
5666 {
5667 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005668 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005669 if (timediff < 0)
5670 --orig_mouse_time.tv_sec;
5671 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005672 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 orig_mouse_time = mouse_time;
5675 if (mouse_code == orig_mouse_code
5676 && timediff < p_mouset
5677 && orig_num_clicks != 4
5678 && orig_mouse_col == mouse_col
5679 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005680 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005682 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005684 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005685 /* Double click in tab pages line also works
5686 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 ++orig_num_clicks;
5690 else
5691 orig_num_clicks = 1;
5692 orig_mouse_col = mouse_col;
5693 orig_mouse_row = mouse_row;
5694 orig_topline = curwin->w_topline;
5695#ifdef FEAT_DIFF
5696 orig_topfill = curwin->w_topfill;
5697#endif
5698 }
5699# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5700 else
5701 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5702# endif
5703# else
5704 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5705# endif
5706 is_click = TRUE;
5707 orig_mouse_code = mouse_code;
5708 }
5709 if (!is_drag)
5710 held_button = mouse_code & MOUSE_CLICK_MASK;
5711
5712 /*
5713 * Translate the actual mouse event into a pseudo mouse event.
5714 * First work out what modifiers are to be used.
5715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (orig_mouse_code & MOUSE_SHIFT)
5717 modifiers |= MOD_MASK_SHIFT;
5718 if (orig_mouse_code & MOUSE_CTRL)
5719 modifiers |= MOD_MASK_CTRL;
5720 if (orig_mouse_code & MOUSE_ALT)
5721 modifiers |= MOD_MASK_ALT;
5722 if (orig_num_clicks == 2)
5723 modifiers |= MOD_MASK_2CLICK;
5724 else if (orig_num_clicks == 3)
5725 modifiers |= MOD_MASK_3CLICK;
5726 else if (orig_num_clicks == 4)
5727 modifiers |= MOD_MASK_4CLICK;
5728
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005729 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5730 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005732 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005733 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005734 {
5735 if (wheel_code & MOUSE_CTRL)
5736 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005737 if (wheel_code & MOUSE_ALT)
5738 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 key_name[1] = (wheel_code & 1)
5740 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005741 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 else
5744 key_name[1] = get_pseudo_mouse_code(current_button,
5745 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005746
5747 /* Make sure the mouse position is valid. Some terminals may
5748 * return weird values. */
5749 if (mouse_col >= Columns)
5750 mouse_col = Columns - 1;
5751 if (mouse_row >= Rows)
5752 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754#endif /* FEAT_MOUSE */
5755
5756#ifdef FEAT_GUI
5757 /*
5758 * If using the GUI, then we get menu and scrollbar events.
5759 *
5760 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5761 * four bytes which are to be taken as a pointer to the vimmenu_T
5762 * structure.
5763 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005764 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005765 * is one byte with the tab index.
5766 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5768 * by one byte representing the scrollbar number, and then four bytes
5769 * representing a long_u which is the new value of the scrollbar.
5770 *
5771 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5772 * KE_FILLER followed by four bytes representing a long_u which is the
5773 * new value of the scrollbar.
5774 */
5775# ifdef FEAT_MENU
5776 else if (key_name[0] == (int)KS_MENU)
5777 {
5778 long_u val;
5779
5780 num_bytes = get_long_from_buf(tp + slen, &val);
5781 if (num_bytes == -1)
5782 return -1;
5783 current_menu = (vimmenu_T *)val;
5784 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005785
5786 /* The menu may have been deleted right after it was used, check
5787 * for that. */
5788 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5789 {
5790 key_name[0] = KS_EXTRA;
5791 key_name[1] = (int)KE_IGNORE;
5792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
5794# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005795# ifdef FEAT_GUI_TABLINE
5796 else if (key_name[0] == (int)KS_TABLINE)
5797 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005798 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005799 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5800 if (num_bytes == -1)
5801 return -1;
5802 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005803 if (current_tab == 255) /* -1 in a byte gives 255 */
5804 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005805 slen += num_bytes;
5806 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005807 else if (key_name[0] == (int)KS_TABMENU)
5808 {
5809 /* Selecting tabline tab or using its menu. */
5810 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5811 if (num_bytes == -1)
5812 return -1;
5813 current_tab = (int)bytes[0];
5814 current_tabmenu = (int)bytes[1];
5815 slen += num_bytes;
5816 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818# ifndef USE_ON_FLY_SCROLL
5819 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5820 {
5821 long_u val;
5822
5823 /* Get the last scrollbar event in the queue of the same type */
5824 j = 0;
5825 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5826 && tp[j + 2] != NUL; ++i)
5827 {
5828 j += 3;
5829 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5830 if (num_bytes == -1)
5831 break;
5832 if (i == 0)
5833 current_scrollbar = (int)bytes[0];
5834 else if (current_scrollbar != (int)bytes[0])
5835 break;
5836 j += num_bytes;
5837 num_bytes = get_long_from_buf(tp + j, &val);
5838 if (num_bytes == -1)
5839 break;
5840 scrollbar_value = val;
5841 j += num_bytes;
5842 slen = j;
5843 }
5844 if (i == 0) /* not enough characters to make one */
5845 return -1;
5846 }
5847 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5848 {
5849 long_u val;
5850
5851 /* Get the last horiz. scrollbar event in the queue */
5852 j = 0;
5853 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5854 && tp[j + 2] != NUL; ++i)
5855 {
5856 j += 3;
5857 num_bytes = get_long_from_buf(tp + j, &val);
5858 if (num_bytes == -1)
5859 break;
5860 scrollbar_value = val;
5861 j += num_bytes;
5862 slen = j;
5863 }
5864 if (i == 0) /* not enough characters to make one */
5865 return -1;
5866 }
5867# endif /* !USE_ON_FLY_SCROLL */
5868#endif /* FEAT_GUI */
5869
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005870 /*
5871 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5872 */
5873 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5874
5875 /*
5876 * Add any modifier codes to our string.
5877 */
5878 new_slen = 0; /* Length of what will replace the termcode */
5879 if (modifiers != 0)
5880 {
5881 /* Some keys have the modifier included. Need to handle that here
5882 * to make mappings work. */
5883 key = simplify_key(key, &modifiers);
5884 if (modifiers != 0)
5885 {
5886 string[new_slen++] = K_SPECIAL;
5887 string[new_slen++] = (int)KS_MODIFIER;
5888 string[new_slen++] = modifiers;
5889 }
5890 }
5891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005893 key_name[0] = KEY2TERMCAP0(key);
5894 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005896 {
5897 /* from ":set <M-b>=xx" */
5898#ifdef FEAT_MBYTE
5899 if (has_mbyte)
5900 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5901 else
5902#endif
5903 string[new_slen++] = key_name[1];
5904 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005905 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5906 && key_name[1] == KE_IGNORE)
5907 {
5908 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5909 * to indicate what happened. */
5910 retval = KEYLEN_REMOVED;
5911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 else
5913 {
5914 string[new_slen++] = K_SPECIAL;
5915 string[new_slen++] = key_name[0];
5916 string[new_slen++] = key_name[1];
5917 }
5918 string[new_slen] = NUL;
5919 extra = new_slen - slen;
5920 if (buf == NULL)
5921 {
5922 if (extra < 0)
5923 /* remove matched chars, taking care of noremap */
5924 del_typebuf(-extra, offset);
5925 else if (extra > 0)
5926 /* insert the extra space we need */
5927 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5928
5929 /*
5930 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5931 * typebuf.tb_buf[]!
5932 */
5933 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5934 (size_t)new_slen);
5935 }
5936 else
5937 {
5938 if (extra < 0)
5939 /* remove matched characters */
5940 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005941 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005943 {
5944 /* Insert the extra space we need. If there is insufficient
5945 * space return -1. */
5946 if (*buflen + extra + new_slen >= bufsize)
5947 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005949 (size_t)(*buflen - offset));
5950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005952 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005954 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 }
5956
Bram Moolenaar2951b772013-07-03 12:45:31 +02005957#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005958 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005959#endif
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 return 0; /* no match found */
5962}
5963
Bram Moolenaar9377df32017-10-15 13:22:01 +02005964#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005965/*
5966 * Get the text foreground color, if known.
5967 */
5968 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005969term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005970{
5971 if (rfg_status == STATUS_GOT)
5972 {
5973 *r = fg_r;
5974 *g = fg_g;
5975 *b = fg_b;
5976 }
5977}
5978
5979/*
5980 * Get the text background color, if known.
5981 */
5982 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005983term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005984{
5985 if (rbg_status == STATUS_GOT)
5986 {
5987 *r = bg_r;
5988 *g = bg_g;
5989 *b = bg_b;
5990 }
5991}
5992#endif
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994/*
5995 * Replace any terminal code strings in from[] with the equivalent internal
5996 * vim representation. This is used for the "from" and "to" part of a
5997 * mapping, and the "to" part of a menu command.
5998 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5999 * '<'.
6000 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6001 *
6002 * The replacement is done in result[] and finally copied into allocated
6003 * memory. If this all works well *bufp is set to the allocated memory and a
6004 * pointer to it is returned. If something fails *bufp is set to NULL and from
6005 * is returned.
6006 *
6007 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
6008 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
6009 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
6010 * instead of a CTRL-V.
6011 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006012 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006013replace_termcodes(
6014 char_u *from,
6015 char_u **bufp,
6016 int from_part,
6017 int do_lt, /* also translate <lt> */
6018 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 int i;
6021 int slen;
6022 int key;
6023 int dlen = 0;
6024 char_u *src;
6025 int do_backslash; /* backslash is a special character */
6026 int do_special; /* recognize <> key codes */
6027 int do_key_code; /* recognize raw key codes */
6028 char_u *result; /* buffer for resulting string */
6029
6030 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00006031 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6033
6034 /*
6035 * Allocate space for the translation. Worst case a single character is
6036 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6037 */
6038 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6039 if (result == NULL) /* out of memory */
6040 {
6041 *bufp = NULL;
6042 return from;
6043 }
6044
6045 src = from;
6046
6047 /*
6048 * Check for #n at start only: function key n
6049 */
6050 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6051 {
6052 result[dlen++] = K_SPECIAL;
6053 result[dlen++] = 'k';
6054 if (src[1] == '0')
6055 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6056 else
6057 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6058 src += 2;
6059 }
6060
6061 /*
6062 * Copy each byte from *from to result[dlen]
6063 */
6064 while (*src != NUL)
6065 {
6066 /*
6067 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006068 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 */
6070 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6071 {
6072#ifdef FEAT_EVAL
6073 /*
6074 * Replace <SID> by K_SNR <script-nr> _.
6075 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6076 */
6077 if (STRNICMP(src, "<SID>", 5) == 0)
6078 {
6079 if (current_SID <= 0)
6080 EMSG(_(e_usingsid));
6081 else
6082 {
6083 src += 5;
6084 result[dlen++] = K_SPECIAL;
6085 result[dlen++] = (int)KS_EXTRA;
6086 result[dlen++] = (int)KE_SNR;
6087 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6088 dlen += (int)STRLEN(result + dlen);
6089 result[dlen++] = '_';
6090 continue;
6091 }
6092 }
6093#endif
6094
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006095 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (slen)
6097 {
6098 dlen += slen;
6099 continue;
6100 }
6101 }
6102
6103 /*
6104 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6105 * Note that this is also checked after replacing the <> form.
6106 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6107 * it could be a character in the file.
6108 */
6109 if (do_key_code)
6110 {
6111 i = find_term_bykeys(src);
6112 if (i >= 0)
6113 {
6114 result[dlen++] = K_SPECIAL;
6115 result[dlen++] = termcodes[i].name[0];
6116 result[dlen++] = termcodes[i].name[1];
6117 src += termcodes[i].len;
6118 /* If terminal code matched, continue after it. */
6119 continue;
6120 }
6121 }
6122
6123#ifdef FEAT_EVAL
6124 if (do_special)
6125 {
6126 char_u *p, *s, len;
6127
6128 /*
6129 * Replace <Leader> by the value of "mapleader".
6130 * Replace <LocalLeader> by the value of "maplocalleader".
6131 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6132 */
6133 if (STRNICMP(src, "<Leader>", 8) == 0)
6134 {
6135 len = 8;
6136 p = get_var_value((char_u *)"g:mapleader");
6137 }
6138 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6139 {
6140 len = 13;
6141 p = get_var_value((char_u *)"g:maplocalleader");
6142 }
6143 else
6144 {
6145 len = 0;
6146 p = NULL;
6147 }
6148 if (len != 0)
6149 {
6150 /* Allow up to 8 * 6 characters for "mapleader". */
6151 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6152 s = (char_u *)"\\";
6153 else
6154 s = p;
6155 while (*s != NUL)
6156 result[dlen++] = *s++;
6157 src += len;
6158 continue;
6159 }
6160 }
6161#endif
6162
6163 /*
6164 * Remove CTRL-V and ignore the next character.
6165 * For "from" side the CTRL-V at the end is included, for the "to"
6166 * part it is removed.
6167 * If 'cpoptions' does not contain 'B', also accept a backslash.
6168 */
6169 key = *src;
6170 if (key == Ctrl_V || (do_backslash && key == '\\'))
6171 {
6172 ++src; /* skip CTRL-V or backslash */
6173 if (*src == NUL)
6174 {
6175 if (from_part)
6176 result[dlen++] = key;
6177 break;
6178 }
6179 }
6180
6181#ifdef FEAT_MBYTE
6182 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006183 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185 {
6186 /*
6187 * If the character is K_SPECIAL, replace it with K_SPECIAL
6188 * KS_SPECIAL KE_FILLER.
6189 * If compiled with the GUI replace CSI with K_CSI.
6190 */
6191 if (*src == K_SPECIAL)
6192 {
6193 result[dlen++] = K_SPECIAL;
6194 result[dlen++] = KS_SPECIAL;
6195 result[dlen++] = KE_FILLER;
6196 }
6197# ifdef FEAT_GUI
6198 else if (*src == CSI)
6199 {
6200 result[dlen++] = K_SPECIAL;
6201 result[dlen++] = KS_EXTRA;
6202 result[dlen++] = (int)KE_CSI;
6203 }
6204# endif
6205 else
6206 result[dlen++] = *src;
6207 ++src;
6208 }
6209 }
6210 result[dlen] = NUL;
6211
6212 /*
6213 * Copy the new string to allocated memory.
6214 * If this fails, just return from.
6215 */
6216 if ((*bufp = vim_strsave(result)) != NULL)
6217 from = *bufp;
6218 vim_free(result);
6219 return from;
6220}
6221
6222/*
6223 * Find a termcode with keys 'src' (must be NUL terminated).
6224 * Return the index in termcodes[], or -1 if not found.
6225 */
6226 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006227find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006230 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 for (i = 0; i < tc_len; ++i)
6233 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006234 if (slen == termcodes[i].len
6235 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 return i;
6237 }
6238 return -1;
6239}
6240
6241/*
6242 * Gather the first characters in the terminal key codes into a string.
6243 * Used to speed up check_termcode().
6244 */
6245 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006246gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 int i;
6249 int len = 0;
6250
6251#ifdef FEAT_GUI
6252 if (gui.in_use)
6253 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6254#endif
6255#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006256 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 termleader[len++] = DCS; /* the termcode response starts with DCS
6258 in 8-bit mode */
6259#endif
6260 termleader[len] = NUL;
6261
6262 for (i = 0; i < tc_len; ++i)
6263 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6264 {
6265 termleader[len++] = termcodes[i].code[0];
6266 termleader[len] = NUL;
6267 }
6268
6269 need_gather = FALSE;
6270}
6271
6272/*
6273 * Show all termcodes (for ":set termcap")
6274 * This code looks a lot like showoptions(), but is different.
6275 */
6276 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006277show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 int col;
6280 int *items;
6281 int item_count;
6282 int run;
6283 int row, rows;
6284 int cols;
6285 int i;
6286 int len;
6287
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006288#define INC3 27 /* try to make three columns */
6289#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290#define GAP 2 /* spaces between columns */
6291
6292 if (tc_len == 0) /* no terminal codes (must be GUI) */
6293 return;
6294 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6295 if (items == NULL)
6296 return;
6297
6298 /* Highlight title */
6299 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6300
6301 /*
6302 * do the loop two times:
6303 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006304 * 2. display the medium items (medium length strings)
6305 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006307 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
6309 /*
6310 * collect the items in items[]
6311 */
6312 item_count = 0;
6313 for (i = 0; i < tc_len; i++)
6314 {
6315 len = show_one_termcode(termcodes[i].name,
6316 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006317 if (len <= INC3 - GAP ? run == 1
6318 : len <= INC2 - GAP ? run == 2
6319 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 items[item_count++] = i;
6321 }
6322
6323 /*
6324 * display the items
6325 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006326 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006328 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (cols == 0)
6330 cols = 1;
6331 rows = (item_count + cols - 1) / cols;
6332 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006333 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 rows = item_count;
6335 for (row = 0; row < rows && !got_int; ++row)
6336 {
6337 msg_putchar('\n'); /* go to next line */
6338 if (got_int) /* 'q' typed in more */
6339 break;
6340 col = 0;
6341 for (i = row; i < item_count; i += rows)
6342 {
6343 msg_col = col; /* make columns */
6344 show_one_termcode(termcodes[items[i]].name,
6345 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006346 if (run == 2)
6347 col += INC2;
6348 else
6349 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351 out_flush();
6352 ui_breakcheck();
6353 }
6354 }
6355 vim_free(items);
6356}
6357
6358/*
6359 * Show one termcode entry.
6360 * Output goes into IObuff[]
6361 */
6362 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006363show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 char_u *p;
6366 int len;
6367
6368 if (name[0] > '~')
6369 {
6370 IObuff[0] = ' ';
6371 IObuff[1] = ' ';
6372 IObuff[2] = ' ';
6373 IObuff[3] = ' ';
6374 }
6375 else
6376 {
6377 IObuff[0] = 't';
6378 IObuff[1] = '_';
6379 IObuff[2] = name[0];
6380 IObuff[3] = name[1];
6381 }
6382 IObuff[4] = ' ';
6383
6384 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6385 if (p[1] != 't')
6386 STRCPY(IObuff + 5, p);
6387 else
6388 IObuff[5] = NUL;
6389 len = (int)STRLEN(IObuff);
6390 do
6391 IObuff[len++] = ' ';
6392 while (len < 17);
6393 IObuff[len] = NUL;
6394 if (code == NULL)
6395 len += 4;
6396 else
6397 len += vim_strsize(code);
6398
6399 if (printit)
6400 {
6401 msg_puts(IObuff);
6402 if (code == NULL)
6403 msg_puts((char_u *)"NULL");
6404 else
6405 msg_outtrans(code);
6406 }
6407 return len;
6408}
6409
6410#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6411/*
6412 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6413 * termcap codes from the terminal itself.
6414 * We get them one by one to avoid a very long response string.
6415 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006416static int xt_index_in = 0;
6417static int xt_index_out = 0;
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006420req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 xt_index_out = 0;
6423 xt_index_in = 0;
6424 req_more_codes_from_term();
6425}
6426
6427 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006428req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 char buf[11];
6431 int old_idx = xt_index_out;
6432
6433 /* Don't do anything when going to exit. */
6434 if (exiting)
6435 return;
6436
6437 /* Send up to 10 more requests out than we received. Avoid sending too
6438 * many, there can be a buffer overflow somewhere. */
6439 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6440 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006441 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006442
Bram Moolenaarb255b902018-04-24 21:40:10 +02006443 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6444 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 out_str_nf((char_u *)buf);
6446 ++xt_index_out;
6447 }
6448
6449 /* Send the codes out right away. */
6450 if (xt_index_out != old_idx)
6451 out_flush();
6452}
6453
6454/*
6455 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6456 * A "0" instead of the "1" indicates a code that isn't supported.
6457 * Both <name> and <string> are encoded in hex.
6458 * "code" points to the "0" or "1".
6459 */
6460 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006461got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463#define XT_LEN 100
6464 char_u name[3];
6465 char_u str[XT_LEN];
6466 int i;
6467 int j = 0;
6468 int c;
6469
6470 /* A '1' means the code is supported, a '0' means it isn't.
6471 * When half the length is > XT_LEN we can't use it.
6472 * Our names are currently all 2 characters. */
6473 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6474 {
6475 /* Get the name from the response and find it in the table. */
6476 name[0] = hexhex2nr(code + 3);
6477 name[1] = hexhex2nr(code + 5);
6478 name[2] = NUL;
6479 for (i = 0; key_names[i] != NULL; ++i)
6480 {
6481 if (STRCMP(key_names[i], name) == 0)
6482 {
6483 xt_index_in = i;
6484 break;
6485 }
6486 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006487
Bram Moolenaarb255b902018-04-24 21:40:10 +02006488 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (key_names[i] != NULL)
6491 {
6492 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6493 str[j++] = c;
6494 str[j] = NUL;
6495 if (name[0] == 'C' && name[1] == 'o')
6496 {
6497 /* Color count is not a key code. */
6498 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006499 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 }
6501 else
6502 {
6503 /* First delete any existing entry with the same code. */
6504 i = find_term_bykeys(str);
6505 if (i >= 0)
6506 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006507 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 }
6509 }
6510 }
6511
6512 /* May request more codes now that we received one. */
6513 ++xt_index_in;
6514 req_more_codes_from_term();
6515}
6516
6517/*
6518 * Check if there are any unanswered requests and deal with them.
6519 * This is called before starting an external program or getting direct
6520 * keyboard input. We don't want responses to be send to that program or
6521 * handled as typed text.
6522 */
6523 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006524check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int c;
6527
6528 /* If no codes requested or all are answered, no need to wait. */
6529 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6530 return;
6531
6532 /* Vgetc() will check for and handle any response.
6533 * Keep calling vpeekc() until we don't get any responses. */
6534 ++no_mapping;
6535 ++allow_keys;
6536 for (;;)
6537 {
6538 c = vpeekc();
6539 if (c == NUL) /* nothing available */
6540 break;
6541
6542 /* If a response is recognized it's replaced with K_IGNORE, must read
6543 * it from the input stream. If there is no K_IGNORE we can't do
6544 * anything, break here (there might be some responses further on, but
6545 * we don't want to throw away any typed chars). */
6546 if (c != K_SPECIAL && c != K_IGNORE)
6547 break;
6548 c = vgetc();
6549 if (c != K_IGNORE)
6550 {
6551 vungetc(c);
6552 break;
6553 }
6554 }
6555 --no_mapping;
6556 --allow_keys;
6557}
6558#endif
6559
6560#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6561/*
6562 * Translate an internal mapping/abbreviation representation into the
6563 * corresponding external one recognized by :map/:abbrev commands;
6564 * respects the current B/k/< settings of 'cpoption'.
6565 *
6566 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006567 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 *
6569 * It uses a growarray to build the translation string since the
6570 * latter can be wider than the original description. The caller has to
6571 * free the string afterwards.
6572 *
6573 * Returns NULL when there is a problem.
6574 */
6575 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006576translate_mapping(
6577 char_u *str,
6578 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 garray_T ga;
6581 int c;
6582 int modifiers;
6583 int cpo_bslash;
6584 int cpo_special;
6585 int cpo_keycode;
6586
6587 ga_init(&ga);
6588 ga.ga_itemsize = 1;
6589 ga.ga_growsize = 40;
6590
6591 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6592 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6593 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6594
6595 for (; *str; ++str)
6596 {
6597 c = *str;
6598 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6599 {
6600 modifiers = 0;
6601 if (str[1] == KS_MODIFIER)
6602 {
6603 str++;
6604 modifiers = *++str;
6605 c = *++str;
6606 }
6607 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6608 {
6609 int i;
6610
6611 /* try to find special key in termcodes */
6612 for (i = 0; i < tc_len; ++i)
6613 if (termcodes[i].name[0] == str[1]
6614 && termcodes[i].name[1] == str[2])
6615 break;
6616 if (i < tc_len)
6617 {
6618 ga_concat(&ga, termcodes[i].code);
6619 str += 2;
6620 continue; /* for (str) */
6621 }
6622 }
6623 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6624 {
6625 if (expmap && cpo_special)
6626 {
6627 ga_clear(&ga);
6628 return NULL;
6629 }
6630 c = TO_SPECIAL(str[1], str[2]);
6631 if (c == K_ZERO) /* display <Nul> as ^@ */
6632 c = NUL;
6633 str += 2;
6634 }
6635 if (IS_SPECIAL(c) || modifiers) /* special key */
6636 {
6637 if (expmap && cpo_special)
6638 {
6639 ga_clear(&ga);
6640 return NULL;
6641 }
6642 ga_concat(&ga, get_special_key_name(c, modifiers));
6643 continue; /* for (str) */
6644 }
6645 }
6646 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6647 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6648 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6649 if (c)
6650 ga_append(&ga, c);
6651 }
6652 ga_append(&ga, NUL);
6653 return (char_u *)(ga.ga_data);
6654}
6655#endif
6656
6657#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6658static char ksme_str[20];
6659static char ksmr_str[20];
6660static char ksmd_str[20];
6661
6662/*
6663 * For Win32 console: update termcap codes for existing console attributes.
6664 */
6665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006666update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 struct builtin_term *p;
6669
6670 p = find_builtin_term(DEFAULT_TERM);
6671 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6672 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6673 attr | 0x08); /* FOREGROUND_INTENSITY */
6674 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6675 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6676
6677 while (p->bt_string != NULL)
6678 {
6679 if (p->bt_entry == (int)KS_ME)
6680 p->bt_string = &ksme_str[0];
6681 else if (p->bt_entry == (int)KS_MR)
6682 p->bt_string = &ksmr_str[0];
6683 else if (p->bt_entry == (int)KS_MD)
6684 p->bt_string = &ksmd_str[0];
6685 ++p;
6686 }
6687}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006688
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006689# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006690# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006691struct ks_tbl_s
6692{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006693 int code; /* value of KS_ */
6694 char *vtp; /* code in vtp mode */
6695 char *vtp2; /* code in vtp2 mode */
6696 char buf[KSSIZE]; /* save buffer in non-vtp mode */
6697 char vbuf[KSSIZE]; /* save buffer in vtp mode */
6698 char v2buf[KSSIZE]; /* save buffer in vtp2 mode */
6699 char arr[KSSIZE]; /* real buffer */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006700};
6701
6702static struct ks_tbl_s ks_tbl[] =
6703{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006704 {(int)KS_ME, "\033|0m", "\033|0m"}, /* normal */
6705 {(int)KS_MR, "\033|7m", "\033|7m"}, /* reverse */
6706 {(int)KS_MD, "\033|1m", "\033|1m"}, /* bold */
6707 {(int)KS_SO, "\033|91m", "\033|91m"}, /* standout: bright red text */
6708 {(int)KS_SE, "\033|39m", "\033|39m"}, /* standout end: default color */
6709 {(int)KS_CZH, "\033|95m", "\033|95m"}, /* italic: bright magenta text */
6710 {(int)KS_CZR, "\033|0m", "\033|0m"}, /* italic end */
6711 {(int)KS_US, "\033|4m", "\033|4m"}, /* underscore */
6712 {(int)KS_UE, "\033|24m", "\033|24m"}, /* underscore end */
6713# ifdef TERMINFO
6714 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, /* set background color */
6715 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, /* set foreground color */
6716# else
6717 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, /* set background color */
6718 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, /* set foreground color */
6719# endif
6720 {(int)KS_CCO, "16", "256"}, /* colors */
6721 {(int)KS_NAME} /* terminator */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006722};
6723
6724 static struct builtin_term *
6725find_first_tcap(
6726 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006727 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006728{
6729 struct builtin_term *p;
6730
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006731 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006732 if (p->bt_entry == code)
6733 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006734 return NULL;
6735}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006736# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006737
6738/*
6739 * For Win32 console: replace the sequence immediately after termguicolors.
6740 */
6741 void
6742swap_tcap(void)
6743{
6744# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006745 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006746 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006747 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006748 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006749 int mode;
6750 enum
6751 {
6752 CMODEINDEX,
6753 CMODE24,
6754 CMODE256
6755 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006756
6757 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006758 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006759 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006760 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006761 {
6762 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006763 if (bt != NULL)
6764 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006765 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6766 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6767 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6768
6769 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6770 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006771 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006772 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006773 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006774 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006775 }
6776
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006777 if (p_tgc)
6778 mode = CMODE24;
6779 else if (t_colors >= 256)
6780 mode = CMODE256;
6781 else
6782 mode = CMODEINDEX;
6783
6784 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006785 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006786 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6787 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006788 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006789 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006790 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 case CMODEINDEX:
6792 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6793 break;
6794 case CMODE24:
6795 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6796 break;
6797 default:
6798 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006799 }
6800 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006801 }
6802
6803 if (mode != curr_mode)
6804 {
6805 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006806 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006807 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6808 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006809 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006810 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006811 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006812 case CMODEINDEX:
6813 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6814 break;
6815 case CMODE24:
6816 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6817 break;
6818 default:
6819 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006820 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006821 }
6822 }
6823
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006824 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006825 }
6826# endif
6827}
6828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006830
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006831#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006832 static int
6833hex_digit(int c)
6834{
6835 if (isdigit(c))
6836 return c - '0';
6837 c = TOLOWER_ASC(c);
6838 if (c >= 'a' && c <= 'f')
6839 return c - 'a' + 10;
6840 return 0x1ffffff;
6841}
6842
6843 guicolor_T
6844gui_get_color_cmn(char_u *name)
6845{
Bram Moolenaar28726652017-01-06 18:16:19 +01006846 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6847 * values as used by the MS-Windows GDI api. It should be used only for
6848 * MS-Windows GDI builds. */
6849# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6850# undef RGB
6851# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006852# ifndef RGB
6853# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6854# endif
6855# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006856 FILE *fd;
6857 char line[LINE_LEN];
6858 char_u *fname;
6859 int r, g, b, i;
6860 guicolor_T color;
6861
6862 struct rgbcolor_table_S {
6863 char_u *color_name;
6864 guicolor_T color;
6865 };
6866
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006867 /* Only non X11 colors (not present in rgb.txt) and colors in
6868 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006869 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006870 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6871 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6872 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6873 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6874 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6875 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6876 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6877 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6878 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6879 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6880 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6881 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6882 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006883 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6884 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006885 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006886 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006887 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006888 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6889 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6890 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6891 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6892 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6893 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6894 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6895 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6896 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006897 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006898 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006899 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6900 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006901 };
6902
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006903 static struct rgbcolor_table_S *colornames_table;
6904 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006905
6906 if (name[0] == '#' && STRLEN(name) == 7)
6907 {
6908 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006909 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006910 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6911 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6912 if (color > 0xffffff)
6913 return INVALCOLOR;
6914 return color;
6915 }
6916
6917 /* Check if the name is one of the colors we know */
6918 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6919 if (STRICMP(name, rgb_table[i].color_name) == 0)
6920 return rgb_table[i].color;
6921
6922 /*
6923 * Last attempt. Look in the file "$VIM/rgb.txt".
6924 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006925 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006926 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006927 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006928
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006929 /* colornames_table not yet initialized */
6930 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6931 if (fname == NULL)
6932 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006933
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006934 fd = fopen((char *)fname, "rt");
6935 vim_free(fname);
6936 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006937 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006938 if (p_verbose > 1)
6939 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6940 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006941 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006942
6943 for (counting = 1; counting >= 0; --counting)
6944 {
6945 if (!counting)
6946 {
6947 colornames_table = (struct rgbcolor_table_S *)alloc(
6948 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6949 if (colornames_table == NULL)
6950 {
6951 fclose(fd);
6952 return INVALCOLOR;
6953 }
6954 rewind(fd);
6955 }
6956 size = 0;
6957
6958 while (!feof(fd))
6959 {
6960 size_t len;
6961 int pos;
6962
6963 ignoredp = fgets(line, LINE_LEN, fd);
6964 len = strlen(line);
6965
6966 if (len <= 1 || line[len - 1] != '\n')
6967 continue;
6968
6969 line[len - 1] = '\0';
6970
6971 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6972 if (i != 3)
6973 continue;
6974
6975 if (!counting)
6976 {
6977 char_u *s = vim_strsave((char_u *)line + pos);
6978
6979 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006980 {
6981 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006982 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006983 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006984 colornames_table[size].color_name = s;
6985 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6986 }
6987 size++;
6988 }
6989 }
6990 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006991 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006992
6993 for (i = 0; i < size; i++)
6994 if (STRICMP(name, colornames_table[i].color_name) == 0)
6995 return colornames_table[i].color;
6996
Bram Moolenaarab302212016-04-26 20:59:29 +02006997 return INVALCOLOR;
6998}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006999
7000 guicolor_T
7001gui_get_rgb_color_cmn(int r, int g, int b)
7002{
7003 guicolor_T color = RGB(r, g, b);
7004
7005 if (color > 0xffffff)
7006 return INVALCOLOR;
7007 return color;
7008}
Bram Moolenaarab302212016-04-26 20:59:29 +02007009#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007010
7011#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
7012 || defined(PROTO)
7013static int cube_value[] = {
7014 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7015};
7016
7017static int grey_ramp[] = {
7018 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7019 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7020};
7021
7022# ifdef FEAT_TERMINAL
7023# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007024# else
7025# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007026# endif
7027
Bram Moolenaar9894e392018-05-05 14:29:06 +02007028static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007029// R G B idx
7030 { 0, 0, 0, 1}, // black
7031 {224, 0, 0, 2}, // dark red
7032 { 0, 224, 0, 3}, // dark green
7033 {224, 224, 0, 4}, // dark yellow / brown
7034 { 0, 0, 224, 5}, // dark blue
7035 {224, 0, 224, 6}, // dark magenta
7036 { 0, 224, 224, 7}, // dark cyan
7037 {224, 224, 224, 8}, // light grey
7038
7039 {128, 128, 128, 9}, // dark grey
7040 {255, 64, 64, 10}, // light red
7041 { 64, 255, 64, 11}, // light green
7042 {255, 255, 64, 12}, // yellow
7043 { 64, 64, 255, 13}, // light blue
7044 {255, 64, 255, 14}, // light magenta
7045 { 64, 255, 255, 15}, // light cyan
7046 {255, 255, 255, 16}, // white
7047};
7048
7049 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007050cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007051{
7052 int idx;
7053
7054 if (nr < 16)
7055 {
7056 *r = ansi_table[nr][0];
7057 *g = ansi_table[nr][1];
7058 *b = ansi_table[nr][2];
7059 *ansi_idx = ansi_table[nr][3];
7060 }
7061 else if (nr < 232)
7062 {
7063 /* 216 color cube */
7064 idx = nr - 16;
7065 *r = cube_value[idx / 36 % 6];
7066 *g = cube_value[idx / 6 % 6];
7067 *b = cube_value[idx % 6];
7068 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7069 }
7070 else if (nr < 256)
7071 {
7072 /* 24 grey scale ramp */
7073 idx = nr - 232;
7074 *r = grey_ramp[idx];
7075 *g = grey_ramp[idx];
7076 *b = grey_ramp[idx];
7077 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7078 }
7079 else
7080 {
7081 *r = 0;
7082 *g = 0;
7083 *b = 0;
7084 *ansi_idx = 0;
7085 }
7086}
7087#endif