blob: c74e14f44ba18b08e10a724e6e507444d2849286 [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
111 static void log_tr(char *msg);
112# define LOG_TR(msg) log_tr(msg)
113# else
114# define LOG_TR(msg)
115# 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)
674 * Note: K_F1...K_F5 are for internal use, should not be defined.
675 * TODO:- rewrite ESC[ codes to CSI
676 * - keyboard languages (CSI ? 26 n)
677 */
678 {(int)KS_NAME, "vt320"},
679 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
680 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
681# ifdef TERMINFO
682 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
683# else
684 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
685# endif
686 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
687# ifdef TERMINFO
688 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
689# else
690 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
691# endif
692 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000693 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
694 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
696 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
698 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
699 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
700 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
701 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
702 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
703 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
704 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
705 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
706 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_MS, "y"},
708 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100709 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {(int)KS_LE, "\b"},
711# ifdef TERMINFO
712 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
713 ESC_STR "[%i%p1%d;%p2%dH")},
714# else
715 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
716# endif
717# ifdef TERMINFO
718 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
719# else
720 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
721# endif
722 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
723 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
724 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
725 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
727 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
728 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
729 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
730 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
732 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
733 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
734 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
735 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
738 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
739 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
740 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
741 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
742 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
743 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
744 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
745 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
746 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
747 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
748 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
749 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
750 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
751 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
752 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
753 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
754 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
755 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
756 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
757 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
758# endif
759
760# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
761/*
762 * Ordinary vt52
763 */
764 {(int)KS_NAME, "vt52"},
765 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
766 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100767# ifdef TERMINFO
768 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
769 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100774 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
776 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_UP, IF_EB("\033A", ESC_STR "A")},
778 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
779 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
780 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100781 {K_F1, IF_EB("\033P", ESC_STR "P")},
782 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
783 {K_F3, IF_EB("\033R", ESC_STR "R")},
784# ifdef __MINT__
785 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
786 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
787 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
788 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
789 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
791 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
792 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
793 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {K_F4, IF_EB("\033S", ESC_STR "S")},
795 {K_F5, IF_EB("\033T", ESC_STR "T")},
796 {K_F6, IF_EB("\033U", ESC_STR "U")},
797 {K_F7, IF_EB("\033V", ESC_STR "V")},
798 {K_F8, IF_EB("\033W", ESC_STR "W")},
799 {K_F9, IF_EB("\033X", ESC_STR "X")},
800 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
801 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
802 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
803 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
804 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
805 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
806 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
807 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
808 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
809 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
810 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
811 {K_INS, IF_EB("\033I", ESC_STR "I")},
812 {K_HOME, IF_EB("\033E", ESC_STR "E")},
813 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
814 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
815# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {(int)KS_MS, "y"},
818# endif
819# endif
820
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200821# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200822 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
824 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
825# ifdef TERMINFO
826 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
827# else
828 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
829# endif
830 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
831# ifdef TERMINFO
832 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
833# else
834 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
835# endif
836# ifdef TERMINFO
837 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
838 ESC_STR "[%i%p1%d;%p2%dr")},
839# else
840 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
841# endif
842 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
843 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
844 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
845 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
846 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
847 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
848 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200849 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
850 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {(int)KS_MS, "y"},
852 {(int)KS_UT, "y"},
853 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200854 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
855 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200856 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200857 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200858# ifdef TERMINFO
859 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
860# else
861 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
862# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200863 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200864 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# ifdef TERMINFO
866 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
867 ESC_STR "[%i%p1%d;%p2%dH")},
868# else
869 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
870# endif
871 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
872# ifdef TERMINFO
873 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
874# else
875 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
876# endif
877 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
878 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
879# ifdef FEAT_XTERM_SAVE
880 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
881 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
882 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
883# endif
884 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
885 {(int)KS_CIE, "\007"},
886 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
887 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200888 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
889 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890# ifdef TERMINFO
891 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
892 ESC_STR "[8;%p1%d;%p2%dt")},
893 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
894 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200895 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896# else
897 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
898 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200899 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900# endif
901 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200902 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200903 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100904 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200905# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200906 /* These are printf strings, not terminal codes. */
907 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
908 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
909# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100910 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
911 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000912
913 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
914 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
915 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
916 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
917 /* An extra set of cursor keys for vt100 mode */
918 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
919 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
920 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
921 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000923 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
924 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
925 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
926 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000927 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
928 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
929 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
930 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
931 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
932 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
933 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
934 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
935 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
936 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
937 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
938 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000940 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
941 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
942 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
943 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
945 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000946 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000947 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000948 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000949 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000950 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
951 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000952 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000954 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000955 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
956 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100957 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
958 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
959 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
960 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
961 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
962 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
963 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
964 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
965 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
969 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
970 /* F14 and F15 are missing, because they send the same codes as the undo
971 * and help key, although they don't work on all keyboards. */
972 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
973 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
974 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
975 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
976 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
977
978 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
979 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
980 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
981 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
982 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
983 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
984 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
985 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
986 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
987 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
988
989 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
990 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
991 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
992 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
993 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
994 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
995 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# endif
997
998# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
999/*
1000 * iris-ansi for Silicon Graphics machines.
1001 */
1002 {(int)KS_NAME, "iris-ansi"},
1003 {(int)KS_CE, "\033[K"},
1004 {(int)KS_CD, "\033[J"},
1005 {(int)KS_AL, "\033[L"},
1006# ifdef TERMINFO
1007 {(int)KS_CAL, "\033[%p1%dL"},
1008# else
1009 {(int)KS_CAL, "\033[%dL"},
1010# endif
1011 {(int)KS_DL, "\033[M"},
1012# ifdef TERMINFO
1013 {(int)KS_CDL, "\033[%p1%dM"},
1014# else
1015 {(int)KS_CDL, "\033[%dM"},
1016# endif
1017#if 0 /* The scroll region is not working as Vim expects. */
1018# ifdef TERMINFO
1019 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1020# else
1021 {(int)KS_CS, "\033[%i%d;%dr"},
1022# endif
1023#endif
1024 {(int)KS_CL, "\033[H\033[2J"},
1025 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1026 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1027 {(int)KS_TI, "\033[=6h"},
1028 {(int)KS_TE, "\033[=6l"},
1029 {(int)KS_SE, "\033[21;27m"},
1030 {(int)KS_SO, "\033[1;7m"},
1031 {(int)KS_ME, "\033[m"},
1032 {(int)KS_MR, "\033[7m"},
1033 {(int)KS_MD, "\033[1m"},
1034 {(int)KS_CCO, "8"}, /* allow 8 colors */
1035 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1036 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1037 {(int)KS_US, "\033[4m"}, /* underline on */
1038 {(int)KS_UE, "\033[24m"}, /* underline off */
1039# ifdef TERMINFO
1040 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1041 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1042 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1043 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1044# else
1045 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1046 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1047 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1048 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1049# endif
1050 {(int)KS_MS, "y"}, /* guessed */
1051 {(int)KS_UT, "y"}, /* guessed */
1052 {(int)KS_LE, "\b"},
1053# ifdef TERMINFO
1054 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1055# else
1056 {(int)KS_CM, "\033[%i%d;%dH"},
1057# endif
1058 {(int)KS_SR, "\033M"},
1059# ifdef TERMINFO
1060 {(int)KS_CRI, "\033[%p1%dC"},
1061# else
1062 {(int)KS_CRI, "\033[%dC"},
1063# endif
1064 {(int)KS_CIS, "\033P3.y"},
1065 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1066 {(int)KS_TS, "\033P1.y"},
1067 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1068# ifdef TERMINFO
1069 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1070 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1071# else
1072 {(int)KS_CWS, "\033[203;%d;%d/y"},
1073 {(int)KS_CWP, "\033[205;%d;%d/y"},
1074# endif
1075 {K_UP, "\033[A"},
1076 {K_DOWN, "\033[B"},
1077 {K_LEFT, "\033[D"},
1078 {K_RIGHT, "\033[C"},
1079 {K_S_UP, "\033[161q"},
1080 {K_S_DOWN, "\033[164q"},
1081 {K_S_LEFT, "\033[158q"},
1082 {K_S_RIGHT, "\033[167q"},
1083 {K_F1, "\033[001q"},
1084 {K_F2, "\033[002q"},
1085 {K_F3, "\033[003q"},
1086 {K_F4, "\033[004q"},
1087 {K_F5, "\033[005q"},
1088 {K_F6, "\033[006q"},
1089 {K_F7, "\033[007q"},
1090 {K_F8, "\033[008q"},
1091 {K_F9, "\033[009q"},
1092 {K_F10, "\033[010q"},
1093 {K_F11, "\033[011q"},
1094 {K_F12, "\033[012q"},
1095 {K_S_F1, "\033[013q"},
1096 {K_S_F2, "\033[014q"},
1097 {K_S_F3, "\033[015q"},
1098 {K_S_F4, "\033[016q"},
1099 {K_S_F5, "\033[017q"},
1100 {K_S_F6, "\033[018q"},
1101 {K_S_F7, "\033[019q"},
1102 {K_S_F8, "\033[020q"},
1103 {K_S_F9, "\033[021q"},
1104 {K_S_F10, "\033[022q"},
1105 {K_S_F11, "\033[023q"},
1106 {K_S_F12, "\033[024q"},
1107 {K_INS, "\033[139q"},
1108 {K_HOME, "\033[H"},
1109 {K_END, "\033[146q"},
1110 {K_PAGEUP, "\033[150q"},
1111 {K_PAGEDOWN, "\033[154q"},
1112# endif
1113
1114# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1115/*
1116 * for debugging
1117 */
1118 {(int)KS_NAME, "debug"},
1119 {(int)KS_CE, "[CE]"},
1120 {(int)KS_CD, "[CD]"},
1121 {(int)KS_AL, "[AL]"},
1122# ifdef TERMINFO
1123 {(int)KS_CAL, "[CAL%p1%d]"},
1124# else
1125 {(int)KS_CAL, "[CAL%d]"},
1126# endif
1127 {(int)KS_DL, "[DL]"},
1128# ifdef TERMINFO
1129 {(int)KS_CDL, "[CDL%p1%d]"},
1130# else
1131 {(int)KS_CDL, "[CDL%d]"},
1132# endif
1133# ifdef TERMINFO
1134 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1135# else
1136 {(int)KS_CS, "[%dCS%d]"},
1137# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001138# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001140# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142# endif
1143# ifdef TERMINFO
1144 {(int)KS_CAB, "[CAB%p1%d]"},
1145 {(int)KS_CAF, "[CAF%p1%d]"},
1146 {(int)KS_CSB, "[CSB%p1%d]"},
1147 {(int)KS_CSF, "[CSF%p1%d]"},
1148# else
1149 {(int)KS_CAB, "[CAB%d]"},
1150 {(int)KS_CAF, "[CAF%d]"},
1151 {(int)KS_CSB, "[CSB%d]"},
1152 {(int)KS_CSF, "[CSF%d]"},
1153# endif
1154 {(int)KS_OP, "[OP]"},
1155 {(int)KS_LE, "[LE]"},
1156 {(int)KS_CL, "[CL]"},
1157 {(int)KS_VI, "[VI]"},
1158 {(int)KS_VE, "[VE]"},
1159 {(int)KS_VS, "[VS]"},
1160 {(int)KS_ME, "[ME]"},
1161 {(int)KS_MR, "[MR]"},
1162 {(int)KS_MB, "[MB]"},
1163 {(int)KS_MD, "[MD]"},
1164 {(int)KS_SE, "[SE]"},
1165 {(int)KS_SO, "[SO]"},
1166 {(int)KS_UE, "[UE]"},
1167 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001168 {(int)KS_UCE, "[UCE]"},
1169 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001170 {(int)KS_STE, "[STE]"},
1171 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_MS, "[MS]"},
1173 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001174 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# ifdef TERMINFO
1176 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1177# else
1178 {(int)KS_CM, "[%dCM%d]"},
1179# endif
1180 {(int)KS_SR, "[SR]"},
1181# ifdef TERMINFO
1182 {(int)KS_CRI, "[CRI%p1%d]"},
1183# else
1184 {(int)KS_CRI, "[CRI%d]"},
1185# endif
1186 {(int)KS_VB, "[VB]"},
1187 {(int)KS_KS, "[KS]"},
1188 {(int)KS_KE, "[KE]"},
1189 {(int)KS_TI, "[TI]"},
1190 {(int)KS_TE, "[TE]"},
1191 {(int)KS_CIS, "[CIS]"},
1192 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001193 {(int)KS_CSC, "[CSC]"},
1194 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_TS, "[TS]"},
1196 {(int)KS_FS, "[FS]"},
1197# ifdef TERMINFO
1198 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1199 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1200# else
1201 {(int)KS_CWS, "[%dCWS%d]"},
1202 {(int)KS_CWP, "[%dCWP%d]"},
1203# endif
1204 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001205 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001206 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001207 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {K_UP, "[KU]"},
1209 {K_DOWN, "[KD]"},
1210 {K_LEFT, "[KL]"},
1211 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001212 {K_XUP, "[xKU]"},
1213 {K_XDOWN, "[xKD]"},
1214 {K_XLEFT, "[xKL]"},
1215 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {K_S_UP, "[S-KU]"},
1217 {K_S_DOWN, "[S-KD]"},
1218 {K_S_LEFT, "[S-KL]"},
1219 {K_C_LEFT, "[C-KL]"},
1220 {K_S_RIGHT, "[S-KR]"},
1221 {K_C_RIGHT, "[C-KR]"},
1222 {K_F1, "[F1]"},
1223 {K_XF1, "[xF1]"},
1224 {K_F2, "[F2]"},
1225 {K_XF2, "[xF2]"},
1226 {K_F3, "[F3]"},
1227 {K_XF3, "[xF3]"},
1228 {K_F4, "[F4]"},
1229 {K_XF4, "[xF4]"},
1230 {K_F5, "[F5]"},
1231 {K_F6, "[F6]"},
1232 {K_F7, "[F7]"},
1233 {K_F8, "[F8]"},
1234 {K_F9, "[F9]"},
1235 {K_F10, "[F10]"},
1236 {K_F11, "[F11]"},
1237 {K_F12, "[F12]"},
1238 {K_S_F1, "[S-F1]"},
1239 {K_S_XF1, "[S-xF1]"},
1240 {K_S_F2, "[S-F2]"},
1241 {K_S_XF2, "[S-xF2]"},
1242 {K_S_F3, "[S-F3]"},
1243 {K_S_XF3, "[S-xF3]"},
1244 {K_S_F4, "[S-F4]"},
1245 {K_S_XF4, "[S-xF4]"},
1246 {K_S_F5, "[S-F5]"},
1247 {K_S_F6, "[S-F6]"},
1248 {K_S_F7, "[S-F7]"},
1249 {K_S_F8, "[S-F8]"},
1250 {K_S_F9, "[S-F9]"},
1251 {K_S_F10, "[S-F10]"},
1252 {K_S_F11, "[S-F11]"},
1253 {K_S_F12, "[S-F12]"},
1254 {K_HELP, "[HELP]"},
1255 {K_UNDO, "[UNDO]"},
1256 {K_BS, "[BS]"},
1257 {K_INS, "[INS]"},
1258 {K_KINS, "[KINS]"},
1259 {K_DEL, "[DEL]"},
1260 {K_KDEL, "[KDEL]"},
1261 {K_HOME, "[HOME]"},
1262 {K_S_HOME, "[C-HOME]"},
1263 {K_C_HOME, "[C-HOME]"},
1264 {K_KHOME, "[KHOME]"},
1265 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001266 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {K_END, "[END]"},
1268 {K_S_END, "[C-END]"},
1269 {K_C_END, "[C-END]"},
1270 {K_KEND, "[KEND]"},
1271 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001272 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {K_PAGEUP, "[PAGEUP]"},
1274 {K_PAGEDOWN, "[PAGEDOWN]"},
1275 {K_KPAGEUP, "[KPAGEUP]"},
1276 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1277 {K_MOUSE, "[MOUSE]"},
1278 {K_KPLUS, "[KPLUS]"},
1279 {K_KMINUS, "[KMINUS]"},
1280 {K_KDIVIDE, "[KDIVIDE]"},
1281 {K_KMULTIPLY, "[KMULTIPLY]"},
1282 {K_KENTER, "[KENTER]"},
1283 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001284 {K_PS, "[PASTE-START]"},
1285 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {K_K0, "[K0]"},
1287 {K_K1, "[K1]"},
1288 {K_K2, "[K2]"},
1289 {K_K3, "[K3]"},
1290 {K_K4, "[K4]"},
1291 {K_K5, "[K5]"},
1292 {K_K6, "[K6]"},
1293 {K_K7, "[K7]"},
1294 {K_K8, "[K8]"},
1295 {K_K9, "[K9]"},
1296# endif
1297
1298#endif /* NO_BUILTIN_TCAPS */
1299
1300/*
1301 * The most minimal terminal: only clear screen and cursor positioning
1302 * Always included.
1303 */
1304 {(int)KS_NAME, "dumb"},
1305 {(int)KS_CL, "\014"},
1306#ifdef TERMINFO
1307 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1308 ESC_STR "[%i%p1%d;%p2%dH")},
1309#else
1310 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1311#endif
1312
1313/*
1314 * end marker
1315 */
1316 {(int)KS_NAME, NULL}
1317
1318}; /* end of builtin_termcaps */
1319
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001320#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001321 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001322termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001323{
Bram Moolenaarab302212016-04-26 20:59:29 +02001324 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001325}
1326
1327 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001328termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001329{
1330 guicolor_T t;
1331
1332 if (*name == NUL)
1333 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001334 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001335
1336 if (t == INVALCOLOR)
1337 EMSG2(_("E254: Cannot allocate color %s"), name);
1338 return t;
1339}
1340
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001341 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001342termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001343{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001344 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001345}
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
1349 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#ifdef AMIGA
1352# define DEFAULT_TERM (char_u *)"amiga"
1353#endif
1354
1355#ifdef MSWIN
1356# define DEFAULT_TERM (char_u *)"win32"
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#if defined(UNIX) && !defined(__MINT__)
1360# define DEFAULT_TERM (char_u *)"ansi"
1361#endif
1362
1363#ifdef __MINT__
1364# define DEFAULT_TERM (char_u *)"vt52"
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#ifdef VMS
1368# define DEFAULT_TERM (char_u *)"vt320"
1369#endif
1370
1371#ifdef __BEOS__
1372# undef DEFAULT_TERM
1373# define DEFAULT_TERM (char_u *)"beos-ansi"
1374#endif
1375
1376#ifndef DEFAULT_TERM
1377# define DEFAULT_TERM (char_u *)"dumb"
1378#endif
1379
1380/*
1381 * Term_strings contains currently used terminal output strings.
1382 * It is initialized with the default values by parse_builtin_tcap().
1383 * The values can be changed by setting the option with the same name.
1384 */
1385char_u *(term_strings[(int)KS_LAST + 1]);
1386
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001387static int need_gather = FALSE; /* need to fill termleader[] */
1388static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001390static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001391static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
1394 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001395find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
1397 struct builtin_term *p;
1398
1399 p = builtin_termcaps;
1400 while (p->bt_string != NULL)
1401 {
1402 if (p->bt_entry == (int)KS_NAME)
1403 {
1404#ifdef UNIX
1405 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1406 return p;
1407 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1408 return p;
1409 else
1410#endif
1411#ifdef VMS
1412 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1413 return p;
1414 else
1415#endif
1416 if (STRCMP(term, p->bt_string) == 0)
1417 return p;
1418 }
1419 ++p;
1420 }
1421 return p;
1422}
1423
1424/*
1425 * Parsing of the builtin termcap entries.
1426 * Caller should check if 'name' is a valid builtin term.
1427 * The terminal's name is not set, as this is already done in termcapinit().
1428 */
1429 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001430parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 struct builtin_term *p;
1433 char_u name[2];
1434 int term_8bit;
1435
1436 p = find_builtin_term(term);
1437 term_8bit = term_is_8bit(term);
1438
1439 /* Do not parse if builtin term not found */
1440 if (p->bt_string == NULL)
1441 return;
1442
1443 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1444 {
1445 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1446 {
1447 /* Only set the value if it wasn't set yet. */
1448 if (term_strings[p->bt_entry] == NULL
1449 || term_strings[p->bt_entry] == empty_option)
1450 {
1451 /* 8bit terminal: use CSI instead of <Esc>[ */
1452 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1453 {
1454 char_u *s, *t;
1455
1456 s = vim_strsave((char_u *)p->bt_string);
1457 if (s != NULL)
1458 {
1459 for (t = s; *t; ++t)
1460 if (term_7to8bit(t))
1461 {
1462 *t = term_7to8bit(t);
1463 STRCPY(t + 1, t + 2);
1464 }
1465 term_strings[p->bt_entry] = s;
1466 set_term_option_alloced(&term_strings[p->bt_entry]);
1467 }
1468 }
1469 else
1470 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1471 }
1472 }
1473 else
1474 {
1475 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1476 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1477 if (find_termcode(name) == NULL)
1478 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1479 }
1480 }
1481}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Set number of colors.
1485 * Store it as a number in t_colors.
1486 * Store it as a string in T_CCO (using nr_colors[]).
1487 */
1488 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 char_u nr_colors[20]; /* string for number of colors */
1492
1493 t_colors = nr;
1494 if (t_colors > 1)
1495 sprintf((char *)nr_colors, "%d", t_colors);
1496 else
1497 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001498 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001500
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001501#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001502/*
1503 * Set the color count to "val" and redraw if it changed.
1504 */
1505 static void
1506may_adjust_color_count(int val)
1507{
1508 if (val != t_colors)
1509 {
1510 /* Nr of colors changed, initialize highlighting and
1511 * redraw everything. This causes a redraw, which usually
1512 * clears the message. Try keeping the message if it
1513 * might work. */
1514 set_keep_msg_from_hist();
1515 set_color_count(val);
1516 init_highlight(TRUE, FALSE);
1517# ifdef DEBUG_TERMRESPONSE
1518 {
1519 char buf[100];
1520 int r = redraw_asap(CLEAR);
1521
1522 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1523 log_tr(buf);
1524 }
1525# else
1526 redraw_asap(CLEAR);
1527# endif
1528 }
1529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
1531
1532#ifdef HAVE_TGETENT
1533static char *(key_names[]) =
1534{
1535#ifdef FEAT_TERMRESPONSE
1536 /* Do this one first, it may cause a screen redraw. */
1537 "Co",
1538#endif
1539 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 "#2", "#4", "%i", "*7",
1541 "k1", "k2", "k3", "k4", "k5", "k6",
1542 "k7", "k8", "k9", "k;", "F1", "F2",
1543 "%1", "&8", "kb", "kI", "kD", "kh",
1544 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1545 NULL
1546};
1547#endif
1548
1549/*
1550 * Set terminal options for terminal "term".
1551 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1552 *
1553 * While doing this, until ttest(), some options may be NULL, be careful.
1554 */
1555 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001556set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
1558 struct builtin_term *termp;
1559#ifdef HAVE_TGETENT
1560 int builtin_first = p_tbi;
1561 int try;
1562 int termcap_cleared = FALSE;
1563#endif
1564 int width = 0, height = 0;
1565 char_u *error_msg = NULL;
1566 char_u *bs_p, *del_p;
1567
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001568 /* In silect mode (ex -s) we don't use the 'term' option. */
1569 if (silent_mode)
1570 return OK;
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 detected_8bit = FALSE; /* reset 8-bit detection */
1573
1574 if (term_is_builtin(term))
1575 {
1576 term += 8;
1577#ifdef HAVE_TGETENT
1578 builtin_first = 1;
1579#endif
1580 }
1581
1582/*
1583 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1584 * If builtin_first is TRUE:
1585 * 0. try builtin termcap
1586 * 1. try external termcap
1587 * 2. if both fail default to a builtin terminal
1588 * If builtin_first is FALSE:
1589 * 1. try external termcap
1590 * 2. try builtin termcap, if both fail default to a builtin terminal
1591 */
1592#ifdef HAVE_TGETENT
1593 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1594 {
1595 /*
1596 * Use external termcap
1597 */
1598 if (try == 1)
1599 {
1600 char_u *p;
1601 static char_u tstrbuf[TBUFSZ];
1602 int i;
1603 char_u tbuf[TBUFSZ];
1604 char_u *tp;
1605 static struct {
1606 enum SpecialKey dest; /* index in term_strings[] */
1607 char *name; /* termcap name for string */
1608 } string_names[] =
1609 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1610 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1611 {KS_CL, "cl"}, {KS_CD, "cd"},
1612 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001613 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1615 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001616 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001617 {KS_STE,"Te"}, {KS_STS,"Ts"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001618 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1620 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1621 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1622 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1623 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001624 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001626 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {KS_TS, "ts"}, {KS_FS, "fs"},
1628 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001630 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001631 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001632 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1633 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {(enum SpecialKey)0, NULL}
1635 };
1636
1637 /*
1638 * If the external termcap does not have a matching entry, try the
1639 * builtin ones.
1640 */
1641 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1642 {
1643 tp = tstrbuf;
1644 if (!termcap_cleared)
1645 {
1646 clear_termoptions(); /* clear old options */
1647 termcap_cleared = TRUE;
1648 }
1649
1650 /* get output strings */
1651 for (i = 0; string_names[i].name != NULL; ++i)
1652 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001653 if (TERM_STR(string_names[i].dest) == NULL
1654 || TERM_STR(string_names[i].dest) == empty_option)
1655 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 TGETSTR(string_names[i].name, &tp);
1657 }
1658
1659 /* tgetflag() returns 1 if the flag is present, 0 if not and
1660 * possibly -1 if the flag doesn't exist. */
1661 if ((T_MS == NULL || T_MS == empty_option)
1662 && tgetflag("ms") > 0)
1663 T_MS = (char_u *)"y";
1664 if ((T_XS == NULL || T_XS == empty_option)
1665 && tgetflag("xs") > 0)
1666 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001667 if ((T_XN == NULL || T_XN == empty_option)
1668 && tgetflag("xn") > 0)
1669 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if ((T_DB == NULL || T_DB == empty_option)
1671 && tgetflag("db") > 0)
1672 T_DB = (char_u *)"y";
1673 if ((T_DA == NULL || T_DA == empty_option)
1674 && tgetflag("da") > 0)
1675 T_DA = (char_u *)"y";
1676 if ((T_UT == NULL || T_UT == empty_option)
1677 && tgetflag("ut") > 0)
1678 T_UT = (char_u *)"y";
1679
1680
1681 /*
1682 * get key codes
1683 */
1684 for (i = 0; key_names[i] != NULL; ++i)
1685 {
1686 if (find_termcode((char_u *)key_names[i]) == NULL)
1687 {
1688 p = TGETSTR(key_names[i], &tp);
1689 /* if cursor-left == backspace, ignore it (televideo
1690 * 925) */
1691 if (p != NULL
1692 && (*p != Ctrl_H
1693 || key_names[i][0] != 'k'
1694 || key_names[i][1] != 'l'))
1695 add_termcode((char_u *)key_names[i], p, FALSE);
1696 }
1697 }
1698
1699 if (height == 0)
1700 height = tgetnum("li");
1701 if (width == 0)
1702 width = tgetnum("co");
1703
1704 /*
1705 * Get number of colors (if not done already).
1706 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001707 if (TERM_STR(KS_CCO) == NULL
1708 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 set_color_count(tgetnum("Co"));
1710
1711# ifndef hpux
1712 BC = (char *)TGETSTR("bc", &tp);
1713 UP = (char *)TGETSTR("up", &tp);
1714 p = TGETSTR("pc", &tp);
1715 if (p)
1716 PC = *p;
1717# endif /* hpux */
1718 }
1719 }
1720 else /* try == 0 || try == 2 */
1721#endif /* HAVE_TGETENT */
1722 /*
1723 * Use builtin termcap
1724 */
1725 {
1726#ifdef HAVE_TGETENT
1727 /*
1728 * If builtin termcap was already used, there is no need to search
1729 * for the builtin termcap again, quit now.
1730 */
1731 if (try == 2 && builtin_first && termcap_cleared)
1732 break;
1733#endif
1734 /*
1735 * search for 'term' in builtin_termcaps[]
1736 */
1737 termp = find_builtin_term(term);
1738 if (termp->bt_string == NULL) /* did not find it */
1739 {
1740#ifdef HAVE_TGETENT
1741 /*
1742 * If try == 0, first try the external termcap. If that is not
1743 * found we'll get back here with try == 2.
1744 * If termcap_cleared is set we used the external termcap,
1745 * don't complain about not finding the term in the builtin
1746 * termcap.
1747 */
1748 if (try == 0) /* try external one */
1749 continue;
1750 if (termcap_cleared) /* found in external termcap */
1751 break;
1752#endif
1753
1754 mch_errmsg("\r\n");
1755 if (error_msg != NULL)
1756 {
1757 mch_errmsg((char *)error_msg);
1758 mch_errmsg("\r\n");
1759 }
1760 mch_errmsg("'");
1761 mch_errmsg((char *)term);
1762 mch_errmsg(_("' not known. Available builtin terminals are:"));
1763 mch_errmsg("\r\n");
1764 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1765 ++termp)
1766 {
1767 if (termp->bt_entry == (int)KS_NAME)
1768 {
1769#ifdef HAVE_TGETENT
1770 mch_errmsg(" builtin_");
1771#else
1772 mch_errmsg(" ");
1773#endif
1774 mch_errmsg(termp->bt_string);
1775 mch_errmsg("\r\n");
1776 }
1777 }
1778 /* when user typed :set term=xxx, quit here */
1779 if (starting != NO_SCREEN)
1780 {
1781 screen_start(); /* don't know where cursor is now */
1782 wait_return(TRUE);
1783 return FAIL;
1784 }
1785 term = DEFAULT_TERM;
1786 mch_errmsg(_("defaulting to '"));
1787 mch_errmsg((char *)term);
1788 mch_errmsg("'\r\n");
1789 if (emsg_silent == 0)
1790 {
1791 screen_start(); /* don't know where cursor is now */
1792 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001793 if (!is_not_a_term())
1794 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001796 set_string_option_direct((char_u *)"term", -1, term,
1797 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 display_errors();
1799 }
1800 out_flush();
1801#ifdef HAVE_TGETENT
1802 if (!termcap_cleared)
1803 {
1804#endif
1805 clear_termoptions(); /* clear old options */
1806#ifdef HAVE_TGETENT
1807 termcap_cleared = TRUE;
1808 }
1809#endif
1810 parse_builtin_tcap(term);
1811#ifdef FEAT_GUI
1812 if (term_is_gui(term))
1813 {
1814 out_flush();
1815 gui_init();
1816 /* If starting the GUI failed, don't do any of the other
1817 * things for this terminal */
1818 if (!gui.in_use)
1819 return FAIL;
1820#ifdef HAVE_TGETENT
1821 break; /* don't try using external termcap */
1822#endif
1823 }
1824#endif /* FEAT_GUI */
1825 }
1826#ifdef HAVE_TGETENT
1827 }
1828#endif
1829
1830/*
1831 * special: There is no info in the termcap about whether the cursor
1832 * positioning is relative to the start of the screen or to the start of the
1833 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1834 * relative.
1835 */
1836 if (STRCMP(term, "pcterm") == 0)
1837 T_CCS = (char_u *)"yes";
1838 else
1839 T_CCS = empty_option;
1840
1841#ifdef UNIX
1842/*
1843 * Any "stty" settings override the default for t_kb from the termcap.
1844 * This is in os_unix.c, because it depends a lot on the version of unix that
1845 * is being used.
1846 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1847 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001848# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 get_stty();
1852#endif
1853
1854/*
1855 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1856 * didn't work, use the default CTRL-H
1857 * The default for t_kD is DEL, unless t_kb is DEL.
1858 * The vim_strsave'd strings are probably lost forever, well it's only two
1859 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1860 * directly.
1861 */
1862#ifdef FEAT_GUI
1863 if (!gui.in_use)
1864#endif
1865 {
1866 bs_p = find_termcode((char_u *)"kb");
1867 del_p = find_termcode((char_u *)"kD");
1868 if (bs_p == NULL || *bs_p == NUL)
1869 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1870 if ((del_p == NULL || *del_p == NUL) &&
1871 (bs_p == NULL || *bs_p != DEL))
1872 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1873 }
1874
1875#if defined(UNIX) || defined(VMS)
1876 term_is_xterm = vim_is_xterm(term);
1877#endif
1878
1879#ifdef FEAT_MOUSE
1880# if defined(UNIX) || defined(VMS)
1881# ifdef FEAT_MOUSE_TTY
1882 /*
1883 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1884 * The termcode for the mouse is added as a side effect in option.c.
1885 */
1886 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001887 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001890 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 if (use_xterm_mouse())
1893 p = NULL; /* keep existing value, might be "xterm2" */
1894 else
1895 p = (char_u *)"xterm";
1896 }
1897# endif
1898 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001901 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1902 * "xterm2" in check_termcode(). */
1903 reset_option_was_set((char_u *)"ttym");
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (p == NULL
1906# ifdef FEAT_GUI
1907 || gui.in_use
1908# endif
1909 )
1910 check_mouse_termcode(); /* set mouse termcode anyway */
1911 }
1912# endif
1913# else
1914 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1915# endif
1916#endif /* FEAT_MOUSE */
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918#ifdef USE_TERM_CONSOLE
1919 /* DEFAULT_TERM indicates that it is the machine console. */
1920 if (STRCMP(term, DEFAULT_TERM) != 0)
1921 term_console = FALSE;
1922 else
1923 {
1924 term_console = TRUE;
1925# ifdef AMIGA
1926 win_resize_on(); /* enable window resizing reports */
1927# endif
1928 }
1929#endif
1930
1931#if defined(UNIX) || defined(VMS)
1932 /*
1933 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1934 */
1935 if (vim_is_fastterm(term))
1936 p_tf = TRUE;
1937#endif
1938#ifdef USE_TERM_CONSOLE
1939 /*
1940 * 'ttyfast' is default on consoles
1941 */
1942 if (term_console)
1943 p_tf = TRUE;
1944#endif
1945
1946 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1947
1948 full_screen = TRUE; /* we can use termcap codes from now on */
1949 set_term_defaults(); /* use current values as defaults */
1950#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001951 LOG_TR("setting crv_status to STATUS_GET");
1952 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953#endif
1954
1955 /*
1956 * Initialize the terminal with the appropriate termcap codes.
1957 * Set the mouse and window title if possible.
1958 * Don't do this when starting, need to parse the .vimrc first, because it
1959 * may redefine t_TI etc.
1960 */
1961 if (starting != NO_SCREEN)
1962 {
1963 starttermcap(); /* may change terminal mode */
1964#ifdef FEAT_MOUSE
1965 setmouse(); /* may start using the mouse */
1966#endif
1967#ifdef FEAT_TITLE
1968 maketitle(); /* may display window title */
1969#endif
1970 }
1971
1972 /* display initial screen after ttest() checking. jw. */
1973 if (width <= 0 || height <= 0)
1974 {
1975 /* termcap failed to report size */
1976 /* set defaults, in case ui_get_shellsize() also fails */
1977 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001978#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 height = 25; /* console is often 25 lines */
1980#else
1981 height = 24; /* most terminals are 24 lines */
1982#endif
1983 }
1984 set_shellsize(width, height, FALSE); /* may change Rows */
1985 if (starting != NO_SCREEN)
1986 {
1987 if (scroll_region)
1988 scroll_region_reset(); /* In case Rows changed */
1989 check_map_keycodes(); /* check mappings for terminal codes used */
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001992 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 /*
1995 * Execute the TermChanged autocommands for each buffer that is
1996 * loaded.
1997 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001998 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001999 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
2001 if (curbuf->b_ml.ml_mfp != NULL)
2002 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2003 curbuf);
2004 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002005 if (bufref_valid(&old_curbuf))
2006 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010#ifdef FEAT_TERMRESPONSE
2011 may_req_termresponse();
2012#endif
2013
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002014#if defined(WIN3264) && !defined(FEAT_GUI) && defined(FEAT_TERMGUICOLORS)
2015 if (STRCMP(term, "win32") == 0)
2016 set_color_count((p_tgc) ? 256 : 16);
2017#endif
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 return OK;
2020}
2021
2022#if defined(FEAT_MOUSE) || defined(PROTO)
2023
2024# ifdef FEAT_MOUSE_TTY
2025# define HMT_NORMAL 1
2026# define HMT_NETTERM 2
2027# define HMT_DEC 4
2028# define HMT_JSBTERM 8
2029# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002030# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002031# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002032# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033static int has_mouse_termcode = 0;
2034# endif
2035
Bram Moolenaar864207d2008-06-24 22:14:38 +00002036# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002038set_mouse_termcode(
2039 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2040 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
2042 char_u name[2];
2043
2044 name[0] = n;
2045 name[1] = KE_FILLER;
2046 add_termcode(name, s, FALSE);
2047# ifdef FEAT_MOUSE_TTY
2048# ifdef FEAT_MOUSE_JSB
2049 if (n == KS_JSBTERM_MOUSE)
2050 has_mouse_termcode |= HMT_JSBTERM;
2051 else
2052# endif
2053# ifdef FEAT_MOUSE_NET
2054 if (n == KS_NETTERM_MOUSE)
2055 has_mouse_termcode |= HMT_NETTERM;
2056 else
2057# endif
2058# ifdef FEAT_MOUSE_DEC
2059 if (n == KS_DEC_MOUSE)
2060 has_mouse_termcode |= HMT_DEC;
2061 else
2062# endif
2063# ifdef FEAT_MOUSE_PTERM
2064 if (n == KS_PTERM_MOUSE)
2065 has_mouse_termcode |= HMT_PTERM;
2066 else
2067# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002068# ifdef FEAT_MOUSE_URXVT
2069 if (n == KS_URXVT_MOUSE)
2070 has_mouse_termcode |= HMT_URXVT;
2071 else
2072# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002073# ifdef FEAT_MOUSE_SGR
2074 if (n == KS_SGR_MOUSE)
2075 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002076 else if (n == KS_SGR_MOUSE_RELEASE)
2077 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002078 else
2079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 has_mouse_termcode |= HMT_NORMAL;
2081# endif
2082}
2083# endif
2084
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002085# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002086 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002088del_mouse_termcode(
2089 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091 char_u name[2];
2092
2093 name[0] = n;
2094 name[1] = KE_FILLER;
2095 del_termcode(name);
2096# ifdef FEAT_MOUSE_TTY
2097# ifdef FEAT_MOUSE_JSB
2098 if (n == KS_JSBTERM_MOUSE)
2099 has_mouse_termcode &= ~HMT_JSBTERM;
2100 else
2101# endif
2102# ifdef FEAT_MOUSE_NET
2103 if (n == KS_NETTERM_MOUSE)
2104 has_mouse_termcode &= ~HMT_NETTERM;
2105 else
2106# endif
2107# ifdef FEAT_MOUSE_DEC
2108 if (n == KS_DEC_MOUSE)
2109 has_mouse_termcode &= ~HMT_DEC;
2110 else
2111# endif
2112# ifdef FEAT_MOUSE_PTERM
2113 if (n == KS_PTERM_MOUSE)
2114 has_mouse_termcode &= ~HMT_PTERM;
2115 else
2116# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002117# ifdef FEAT_MOUSE_URXVT
2118 if (n == KS_URXVT_MOUSE)
2119 has_mouse_termcode &= ~HMT_URXVT;
2120 else
2121# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002122# ifdef FEAT_MOUSE_SGR
2123 if (n == KS_SGR_MOUSE)
2124 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002125 else if (n == KS_SGR_MOUSE_RELEASE)
2126 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002127 else
2128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 has_mouse_termcode &= ~HMT_NORMAL;
2130# endif
2131}
2132# endif
2133#endif
2134
2135#ifdef HAVE_TGETENT
2136/*
2137 * Call tgetent()
2138 * Return error message if it fails, NULL if it's OK.
2139 */
2140 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002141tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 int i;
2144
2145 i = TGETENT(tbuf, term);
2146 if (i < 0 /* -1 is always an error */
2147# ifdef TGETENT_ZERO_ERR
2148 || i == 0 /* sometimes zero is also an error */
2149# endif
2150 )
2151 {
2152 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2153 * tgetent() with the always existing "dumb" entry to avoid a crash or
2154 * hang. */
2155 (void)TGETENT(tbuf, "dumb");
2156
2157 if (i < 0)
2158# ifdef TGETENT_ZERO_ERR
2159 return (char_u *)_("E557: Cannot open termcap file");
2160 if (i == 0)
2161# endif
2162#ifdef TERMINFO
2163 return (char_u *)_("E558: Terminal entry not found in terminfo");
2164#else
2165 return (char_u *)_("E559: Terminal entry not found in termcap");
2166#endif
2167 }
2168 return NULL;
2169}
2170
2171/*
2172 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2173 * Fix that here.
2174 */
2175 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002176vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 char *p;
2179
2180 p = tgetstr(s, (char **)pp);
2181 if (p == (char *)-1)
2182 p = NULL;
2183 return (char_u *)p;
2184}
2185#endif /* HAVE_TGETENT */
2186
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002187#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188/*
2189 * Get Columns and Rows from the termcap. Used after a window signal if the
2190 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2191 * and "li" entries never change. But on some systems this works.
2192 * Errors while getting the entries are ignored.
2193 */
2194 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002195getlinecol(
2196 long *cp, /* pointer to columns */
2197 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 char_u tbuf[TBUFSZ];
2200
2201 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2202 {
2203 if (*cp == 0)
2204 *cp = tgetnum("co");
2205 if (*rp == 0)
2206 *rp = tgetnum("li");
2207 }
2208}
2209#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2210
2211/*
2212 * Get a string entry from the termcap and add it to the list of termcodes.
2213 * Used for <t_xx> special keys.
2214 * Give an error message for failure when not sourcing.
2215 * If force given, replace an existing entry.
2216 * Return FAIL if the entry was not found, OK if the entry was added.
2217 */
2218 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002219add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 char_u *term;
2222 int key;
2223 struct builtin_term *termp;
2224#ifdef HAVE_TGETENT
2225 char_u *string;
2226 int i;
2227 int builtin_first;
2228 char_u tbuf[TBUFSZ];
2229 char_u tstrbuf[TBUFSZ];
2230 char_u *tp = tstrbuf;
2231 char_u *error_msg = NULL;
2232#endif
2233
2234/*
2235 * If the GUI is running or will start in a moment, we only support the keys
2236 * that the GUI can produce.
2237 */
2238#ifdef FEAT_GUI
2239 if (gui.in_use || gui.starting)
2240 return gui_mch_haskey(name);
2241#endif
2242
2243 if (!force && find_termcode(name) != NULL) /* it's already there */
2244 return OK;
2245
2246 term = T_NAME;
2247 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2248 return FAIL;
2249
2250 if (term_is_builtin(term)) /* name starts with "builtin_" */
2251 {
2252 term += 8;
2253#ifdef HAVE_TGETENT
2254 builtin_first = TRUE;
2255#endif
2256 }
2257#ifdef HAVE_TGETENT
2258 else
2259 builtin_first = p_tbi;
2260#endif
2261
2262#ifdef HAVE_TGETENT
2263/*
2264 * We can get the entry from the builtin termcap and from the external one.
2265 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2266 * builtin termcap first.
2267 * If 'ttybuiltin' is off, try external termcap first.
2268 */
2269 for (i = 0; i < 2; ++i)
2270 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002271 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#endif
2273 /*
2274 * Search in builtin termcap
2275 */
2276 {
2277 termp = find_builtin_term(term);
2278 if (termp->bt_string != NULL) /* found it */
2279 {
2280 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002281 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 while (termp->bt_entry != (int)KS_NAME)
2283 {
2284 if ((int)termp->bt_entry == key)
2285 {
2286 add_termcode(name, (char_u *)termp->bt_string,
2287 term_is_8bit(term));
2288 return OK;
2289 }
2290 ++termp;
2291 }
2292 }
2293 }
2294#ifdef HAVE_TGETENT
2295 else
2296 /*
2297 * Search in external termcap
2298 */
2299 {
2300 error_msg = tgetent_error(tbuf, term);
2301 if (error_msg == NULL)
2302 {
2303 string = TGETSTR((char *)name, &tp);
2304 if (string != NULL && *string != NUL)
2305 {
2306 add_termcode(name, string, FALSE);
2307 return OK;
2308 }
2309 }
2310 }
2311 }
2312#endif
2313
2314 if (sourcing_name == NULL)
2315 {
2316#ifdef HAVE_TGETENT
2317 if (error_msg != NULL)
2318 EMSG(error_msg);
2319 else
2320#endif
2321 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2322 }
2323 return FAIL;
2324}
2325
2326 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002327term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2330}
2331
2332/*
2333 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2334 * Assume that the terminal is using 8-bit controls when the name contains
2335 * "8bit", like in "xterm-8bit".
2336 */
2337 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002338term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2341}
2342
2343/*
2344 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002345 * <Esc>[ -> CSI <M_C_[>
2346 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 * <Esc>O -> <M-C-O>
2348 */
2349 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002350term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 if (*p == ESC)
2353 {
2354 if (p[1] == '[')
2355 return CSI;
2356 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002357 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (p[1] == 'O')
2359 return 0x8f;
2360 }
2361 return 0;
2362}
2363
2364#ifdef FEAT_GUI
2365 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002366term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2369}
2370#endif
2371
2372#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2373
2374 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002375tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377 static char_u buf[16];
2378 char_u *p;
2379
2380 p = buf + 15;
2381 *p = '\0';
2382 do
2383 {
2384 --p;
2385 *p = (char_u) (i % 10 + '0');
2386 i /= 10;
2387 }
2388 while (i > 0 && p > buf);
2389 return p;
2390}
2391#endif
2392
2393#ifndef HAVE_TGETENT
2394
2395/*
2396 * minimal tgoto() implementation.
2397 * no padding and we only parse for %i %d and %+char
2398 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002399static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002401 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002402tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404 static char buf[30];
2405 char *p, *s, *e;
2406
2407 if (!cm)
2408 return "OOPS";
2409 e = buf + 29;
2410 for (s = buf; s < e && *cm; cm++)
2411 {
2412 if (*cm != '%')
2413 {
2414 *s++ = *cm;
2415 continue;
2416 }
2417 switch (*++cm)
2418 {
2419 case 'd':
2420 p = (char *)tltoa((unsigned long)y);
2421 y = x;
2422 while (*p)
2423 *s++ = *p++;
2424 break;
2425 case 'i':
2426 x++;
2427 y++;
2428 break;
2429 case '+':
2430 *s++ = (char)(*++cm + y);
2431 y = x;
2432 break;
2433 case '%':
2434 *s++ = *cm;
2435 break;
2436 default:
2437 return "OOPS";
2438 }
2439 }
2440 *s = '\0';
2441 return buf;
2442}
2443
2444#endif /* HAVE_TGETENT */
2445
2446/*
2447 * Set the terminal name and initialize the terminal options.
2448 * If "name" is NULL or empty, get the terminal name from the environment.
2449 * If that fails, use the default terminal name.
2450 */
2451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002452termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 char_u *term;
2455
2456 if (name != NULL && *name == NUL)
2457 name = NULL; /* empty name is equal to no name */
2458 term = name;
2459
2460#ifdef __BEOS__
2461 /*
2462 * TERM environment variable is normally set to 'ansi' on the Bebox;
2463 * Since the BeBox doesn't quite support full ANSI yet, we use our
2464 * own custom 'ansi-beos' termcap instead, unless the -T option has
2465 * been given on the command line.
2466 */
2467 if (term == NULL
2468 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2469 term = DEFAULT_TERM;
2470#endif
2471#ifndef MSWIN
2472 if (term == NULL)
2473 term = mch_getenv((char_u *)"TERM");
2474#endif
2475 if (term == NULL || *term == NUL)
2476 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002477 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 /* Set the default terminal name. */
2480 set_string_default("term", term);
2481 set_string_default("ttytype", term);
2482
2483 /*
2484 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2485 */
2486 set_termname(T_NAME != NULL ? T_NAME : term);
2487}
2488
2489/*
2490 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2491 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002492#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2494static char_u out_buf[OUT_SIZE + 1];
2495static int out_pos = 0; /* number of chars in out_buf */
2496
2497/*
2498 * out_flush(): flush the output buffer
2499 */
2500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002501out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 int len;
2504
2505 if (out_pos != 0)
2506 {
2507 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2508 len = out_pos;
2509 out_pos = 0;
2510 ui_write(out_buf, len);
2511 }
2512}
2513
Bram Moolenaara338adc2018-01-31 20:51:47 +01002514/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002515 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2516 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002517 */
2518 void
2519out_flush_cursor(
2520 int force UNUSED, /* when TRUE, update cursor even when not moved */
2521 int clear_selection UNUSED) /* clear selection under cursor */
2522{
2523 mch_disable_flush();
2524 out_flush();
2525 mch_enable_flush();
2526#ifdef FEAT_GUI
2527 if (gui.in_use)
2528 {
2529 gui_update_cursor(force, clear_selection);
2530 gui_may_flush();
2531 }
2532#endif
2533}
2534
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#if defined(FEAT_MBYTE) || defined(PROTO)
2537/*
2538 * Sometimes a byte out of a multi-byte character is written with out_char().
2539 * To avoid flushing half of the character, call this function first.
2540 */
2541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002542out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2545 out_flush();
2546}
2547#endif
2548
2549#ifdef FEAT_GUI
2550/*
2551 * out_trash(): Throw away the contents of the output buffer
2552 */
2553 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002554out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 out_pos = 0;
2557}
2558#endif
2559
2560/*
2561 * out_char(c): put a byte into the output buffer.
2562 * Flush it if it becomes full.
2563 * This should not be used for outputting text on the screen (use functions
2564 * like msg_puts() and screen_putchar() for that).
2565 */
2566 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002567out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
Bram Moolenaard0573012017-10-28 21:11:06 +02002569#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2571 out_char('\r');
2572#endif
2573
2574 out_buf[out_pos++] = c;
2575
2576 /* For testing we flush each time. */
2577 if (out_pos >= OUT_SIZE || p_wd)
2578 out_flush();
2579}
2580
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002581static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583/*
2584 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2585 */
2586 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002587out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
Bram Moolenaard0573012017-10-28 21:11:06 +02002589#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2591 out_char_nf('\r');
2592#endif
2593
2594 out_buf[out_pos++] = c;
2595
2596 if (out_pos >= OUT_SIZE)
2597 out_flush();
2598}
2599
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002600#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2601 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602/*
2603 * A never-padding out_str.
2604 * use this whenever you don't want to run the string through tputs.
2605 * tputs above is harmless, but tputs from the termcap library
2606 * is likely to strip off leading digits, that it mistakes for padding
2607 * information, and "%i", "%d", etc.
2608 * This should only be used for writing terminal codes, not for outputting
2609 * normal text (use functions like msg_puts() and screen_putchar() for that).
2610 */
2611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002612out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2615 out_flush();
2616 while (*s)
2617 out_char_nf(*s++);
2618
2619 /* For testing we write one string at a time. */
2620 if (p_wd)
2621 out_flush();
2622}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002626 * A conditional-flushing out_str, mainly for visualbell.
2627 * Handles a delay internally, because termlib may not respect the delay or do
2628 * it at the wrong time.
2629 * Note: Only for terminal strings.
2630 */
2631 void
2632out_str_cf(char_u *s)
2633{
2634 if (s != NULL && *s)
2635 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002636#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002637 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002638#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002639
2640#ifdef FEAT_GUI
2641 /* Don't use tputs() when GUI is used, ncurses crashes. */
2642 if (gui.in_use)
2643 {
2644 out_str_nf(s);
2645 return;
2646 }
2647#endif
2648 if (out_pos > OUT_SIZE - 20)
2649 out_flush();
2650#ifdef HAVE_TGETENT
2651 for (p = s; *s; ++s)
2652 {
2653 /* flush just before delay command */
2654 if (*s == '$' && *(s + 1) == '<')
2655 {
2656 char_u save_c = *s;
2657 int duration = atoi((char *)s + 2);
2658
2659 *s = NUL;
2660 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2661 *s = save_c;
2662 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002663# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002664 /* Only sleep here if we can limit this happening in
2665 * vim_beep(). */
2666 p = vim_strchr(s, '>');
2667 if (p == NULL || duration <= 0)
2668 {
2669 /* can't parse the time, don't sleep here */
2670 p = s;
2671 }
2672 else
2673 {
2674 ++p;
2675 do_sleep(duration);
2676 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002677# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002678 /* Rely on the terminal library to sleep. */
2679 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002680# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002681 break;
2682 }
2683 }
2684 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2685#else
2686 while (*s)
2687 out_char_nf(*s++);
2688#endif
2689
2690 /* For testing we write one string at a time. */
2691 if (p_wd)
2692 out_flush();
2693 }
2694}
2695
2696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 * out_str(s): Put a character string a byte at a time into the output buffer.
2698 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2699 * This should only be used for writing terminal codes, not for outputting
2700 * normal text (use functions like msg_puts() and screen_putchar() for that).
2701 */
2702 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002703out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 if (s != NULL && *s)
2706 {
2707#ifdef FEAT_GUI
2708 /* Don't use tputs() when GUI is used, ncurses crashes. */
2709 if (gui.in_use)
2710 {
2711 out_str_nf(s);
2712 return;
2713 }
2714#endif
2715 /* avoid terminal strings being split up */
2716 if (out_pos > OUT_SIZE - 20)
2717 out_flush();
2718#ifdef HAVE_TGETENT
2719 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2720#else
2721 while (*s)
2722 out_char_nf(*s++);
2723#endif
2724
2725 /* For testing we write one string at a time. */
2726 if (p_wd)
2727 out_flush();
2728 }
2729}
2730
2731/*
2732 * cursor positioning using termcap parser. (jw)
2733 */
2734 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002735term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737 OUT_STR(tgoto((char *)T_CM, col, row));
2738}
2739
2740 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002741term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 OUT_STR(tgoto((char *)T_CRI, 0, i));
2744}
2745
2746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002747term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2750}
2751
2752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002753term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
2755 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2756}
2757
2758#if defined(HAVE_TGETENT) || defined(PROTO)
2759 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002760term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 /* Can't handle a negative value here */
2763 if (x < 0)
2764 x = 0;
2765 if (y < 0)
2766 y = 0;
2767 OUT_STR(tgoto((char *)T_CWP, y, x));
2768}
2769
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002770# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2771/*
2772 * Return TRUE if we can request the terminal for a response.
2773 */
2774 static int
2775can_get_termresponse()
2776{
2777 return cur_tmode == TMODE_RAW
2778 && termcap_active
2779# ifdef UNIX
2780 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2781# endif
2782 && p_ek;
2783}
2784
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002785static int winpos_x = -1;
2786static int winpos_y = -1;
2787static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002788
2789/*
2790 * Try getting the Vim window position from the terminal.
2791 * Returns OK or FAIL.
2792 */
2793 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002794term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002795{
2796 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002797 int prev_winpos_x = winpos_x;
2798 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002799
2800 if (*T_CGP == NUL || !can_get_termresponse())
2801 return FAIL;
2802 winpos_x = -1;
2803 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002804 ++did_request_winpos;
2805 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002806 OUT_STR(T_CGP);
2807 out_flush();
2808
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002809 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002810 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002811 {
2812 (void)vpeekc_nomap();
2813 if (winpos_x >= 0 && winpos_y >= 0)
2814 {
2815 *x = winpos_x;
2816 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002817 return OK;
2818 }
2819 ui_delay(10, FALSE);
2820 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002821 /* Do not reset "did_request_winpos", if we timed out the response might
2822 * still come later and we must consume it. */
2823
2824 winpos_x = prev_winpos_x;
2825 winpos_y = prev_winpos_y;
2826 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_y >= 0)
2827 {
2828 /* Polling: return previous values if we have them. */
2829 *x = winpos_x;
2830 *y = winpos_y;
2831 return OK;
2832 }
2833
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002834 return FALSE;
2835}
2836# endif
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002839term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002841 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842}
2843#endif
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002846term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
2848 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002849 int i = *s == CSI ? 1 : 2;
2850 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 /* Special handling of 16 colors, because termcap can't handle it */
2853 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2854 /* Also accept CSI instead of <Esc>[ */
2855 if (n >= 8 && t_colors >= 16
2856 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2857 && s[i] != NUL
2858 && (STRCMP(s + i + 1, "%p1%dm") == 0
2859 || STRCMP(s + i + 1, "%dm") == 0)
2860 && (s[i] == '3' || s[i] == '4'))
2861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002863 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002865 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002867 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2869 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2870 : (n >= 16 ? "48;5;" : "10"));
2871 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2872 }
2873 else
2874 OUT_STR(tgoto((char *)s, 0, n));
2875}
2876
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002877 void
2878term_fg_color(int n)
2879{
2880 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2881 if (*T_CAF)
2882 term_color(T_CAF, n);
2883 else if (*T_CSF)
2884 term_color(T_CSF, n);
2885}
2886
2887 void
2888term_bg_color(int n)
2889{
2890 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2891 if (*T_CAB)
2892 term_color(T_CAB, n);
2893 else if (*T_CSB)
2894 term_color(T_CSB, n);
2895}
2896
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002897#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002898
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002899#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2900#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2901#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002902
2903 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002904term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002905{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002906#define MAX_COLOR_STR_LEN 100
2907 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002908
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002909 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002910 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002911 OUT_STR(buf);
2912}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002913
2914 void
2915term_fg_rgb_color(guicolor_T rgb)
2916{
2917 term_rgb_color(T_8F, rgb);
2918}
2919
2920 void
2921term_bg_rgb_color(guicolor_T rgb)
2922{
2923 term_rgb_color(T_8B, rgb);
2924}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002925#endif
2926
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002927#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2928 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929/*
2930 * Generic function to set window title, using t_ts and t_fs.
2931 */
2932 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002933term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 /* t_ts takes one argument: column in status line */
2936 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2937 out_str_nf(title);
2938 out_str(T_FS); /* set title end */
2939 out_flush();
2940}
2941#endif
2942
2943/*
2944 * Make sure we have a valid set or terminal options.
2945 * Replace all entries that are NULL by empty_option
2946 */
2947 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002948ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002950 char_u *env_colors;
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 check_options(); /* make sure no options are NULL */
2953
2954 /*
2955 * MUST have "cm": cursor motion.
2956 */
2957 if (*T_CM == NUL)
2958 EMSG(_("E437: terminal capability \"cm\" required"));
2959
2960 /*
2961 * if "cs" defined, use a scroll region, it's faster.
2962 */
2963 if (*T_CS != NUL)
2964 scroll_region = TRUE;
2965 else
2966 scroll_region = FALSE;
2967
2968 if (pairs)
2969 {
2970 /*
2971 * optional pairs
2972 */
2973 /* TP goes to normal mode for TI (invert) and TB (bold) */
2974 if (*T_ME == NUL)
2975 T_ME = T_MR = T_MD = T_MB = empty_option;
2976 if (*T_SO == NUL || *T_SE == NUL)
2977 T_SO = T_SE = empty_option;
2978 if (*T_US == NUL || *T_UE == NUL)
2979 T_US = T_UE = empty_option;
2980 if (*T_CZH == NUL || *T_CZR == NUL)
2981 T_CZH = T_CZR = empty_option;
2982
2983 /* T_VE is needed even though T_VI is not defined */
2984 if (*T_VE == NUL)
2985 T_VI = empty_option;
2986
2987 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2988 if (*T_ME == NUL)
2989 {
2990 T_ME = T_SE;
2991 T_MR = T_SO;
2992 T_MD = T_SO;
2993 }
2994
2995 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2996 if (*T_SO == NUL)
2997 {
2998 T_SE = T_ME;
2999 if (*T_MR == NUL)
3000 T_SO = T_MD;
3001 else
3002 T_SO = T_MR;
3003 }
3004
3005 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3006 if (*T_CZH == NUL)
3007 {
3008 T_CZR = T_ME;
3009 if (*T_MR == NUL)
3010 T_CZH = T_MD;
3011 else
3012 T_CZH = T_MR;
3013 }
3014
3015 /* "Sb" and "Sf" come in pairs */
3016 if (*T_CSB == NUL || *T_CSF == NUL)
3017 {
3018 T_CSB = empty_option;
3019 T_CSF = empty_option;
3020 }
3021
3022 /* "AB" and "AF" come in pairs */
3023 if (*T_CAB == NUL || *T_CAF == NUL)
3024 {
3025 T_CAB = empty_option;
3026 T_CAF = empty_option;
3027 }
3028
3029 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3030 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003031 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 /* Set 'weirdinvert' according to value of 't_xs' */
3034 p_wiv = (*T_XS != NUL);
3035 }
3036 need_gather = TRUE;
3037
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003038 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003040 env_colors = mch_getenv((char_u *)"COLORS");
3041 if (env_colors != NULL && isdigit(*env_colors))
3042 {
3043 int colors = atoi((char *)env_colors);
3044
3045 if (colors != t_colors)
3046 set_color_count(colors);
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048}
3049
3050#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3051 || defined(PROTO)
3052/*
3053 * Represent the given long_u as individual bytes, with the most significant
3054 * byte first, and store them in dst.
3055 */
3056 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003057add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 int i;
3060 int shift;
3061
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003062 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 shift = 8 * (sizeof(long_u) - i);
3065 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3066 }
3067}
3068
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003069static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071/*
3072 * Interpret the next string of bytes in buf as a long integer, with the most
3073 * significant byte first. Note that it is assumed that buf has been through
3074 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3075 * Puts result in val, and returns the number of bytes read from buf
3076 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3077 * were present.
3078 */
3079 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003080get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 int len;
3083 char_u bytes[sizeof(long_u)];
3084 int i;
3085 int shift;
3086
3087 *val = 0;
3088 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3089 if (len != -1)
3090 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003091 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 shift = 8 * (sizeof(long_u) - 1 - i);
3094 *val += (long_u)bytes[i] << shift;
3095 }
3096 }
3097 return len;
3098}
3099#endif
3100
3101#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003102 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3103 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
3105 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3106 * that buf has been through inchar(). Returns the actual number of bytes used
3107 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3108 * available.
3109 */
3110 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003111get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 int len = 0;
3114 int i;
3115 char_u c;
3116
3117 for (i = 0; i < num_bytes; i++)
3118 {
3119 if ((c = buf[len++]) == NUL)
3120 return -1;
3121 if (c == K_SPECIAL)
3122 {
3123 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3124 return -1;
3125 if (buf[len++] == (int)KS_ZERO)
3126 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003127 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3128 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3129 if (buf[len++] == (int)KE_CSI)
3130 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003132 else if (c == CSI && buf[len] == KS_EXTRA
3133 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3135 * the start of a special key, see add_to_input_buf_csi(). */
3136 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 bytes[i] = c;
3138 }
3139 return len;
3140}
3141#endif
3142
3143/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003144 * Check if the new shell size is valid, correct it if it's too small or way
3145 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003148check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (Rows < min_rows()) /* need room for one window and command line */
3151 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003152 limit_screen_size();
3153}
3154
3155/*
3156 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3157 */
3158 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003159limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003160{
3161 if (Columns < MIN_COLUMNS)
3162 Columns = MIN_COLUMNS;
3163 else if (Columns > 10000)
3164 Columns = 10000;
3165 if (Rows > 1000)
3166 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167}
3168
Bram Moolenaar86b68352004-12-27 21:59:20 +00003169/*
3170 * Invoked just before the screen structures are going to be (re)allocated.
3171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003173win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 static int old_Rows = 0;
3176 static int old_Columns = 0;
3177
3178 if (old_Rows != Rows || old_Columns != Columns)
3179 ui_new_shellsize();
3180 if (old_Rows != Rows)
3181 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003182 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003183 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003184 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 old_Rows = Rows;
3186 shell_new_rows(); /* update window sizes */
3187 }
3188 if (old_Columns != Columns)
3189 {
3190 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193}
3194
3195/*
3196 * Call this function when the Vim shell has been resized in any way.
3197 * Will obtain the current size and redraw (also when size didn't change).
3198 */
3199 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003200shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 set_shellsize(0, 0, FALSE);
3203}
3204
3205/*
3206 * Check if the shell size changed. Handle a resize.
3207 * When the size didn't change, nothing happens.
3208 */
3209 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003210shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int old_Rows = Rows;
3213 int old_Columns = Columns;
3214
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003215 if (!exiting
3216#ifdef FEAT_GUI
3217 /* Do not get the size when executing a shell command during
3218 * startup. */
3219 && !gui.starting
3220#endif
3221 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003222 {
3223 (void)ui_get_shellsize();
3224 check_shellsize();
3225 if (old_Rows != Rows || old_Columns != Columns)
3226 shell_resized();
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
3229
3230/*
3231 * Set size of the Vim shell.
3232 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3233 * window size (this is used for the :win command).
3234 * If 'mustset' is FALSE, we may try to get the real window size and if
3235 * it fails use 'width' and 'height'.
3236 */
3237 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003238set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 static int busy = FALSE;
3241
3242 /*
3243 * Avoid recursiveness, can happen when setting the window size causes
3244 * another window-changed signal.
3245 */
3246 if (busy)
3247 return;
3248
3249 if (width < 0 || height < 0) /* just checking... */
3250 return;
3251
Bram Moolenaara971b822011-09-14 14:43:25 +02003252 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003254 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 State = SETWSIZE;
3256 return;
3257 }
3258
Bram Moolenaara971b822011-09-14 14:43:25 +02003259 /* curwin->w_buffer can be NULL when we are closing a window and the
3260 * buffer has already been closed and removing a scrollbar causes a resize
3261 * event. Don't resize then, it will happen after entering another buffer.
3262 */
3263 if (curwin->w_buffer == NULL)
3264 return;
3265
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 ++busy;
3267
3268#ifdef AMIGA
3269 out_flush(); /* must do this before mch_get_shellsize() for
3270 some obscure reason */
3271#endif
3272
3273 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3274 {
3275 Rows = height;
3276 Columns = width;
3277 check_shellsize();
3278 ui_set_shellsize(mustset);
3279 }
3280 else
3281 check_shellsize();
3282
3283 /* The window layout used to be adjusted here, but it now happens in
3284 * screenalloc() (also invoked from screenclear()). That is because the
3285 * "busy" check above may skip this, but not screenalloc(). */
3286
3287 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3288 screenclear();
3289 else
3290 screen_start(); /* don't know where cursor is now */
3291
3292 if (starting != NO_SCREEN)
3293 {
3294#ifdef FEAT_TITLE
3295 maketitle();
3296#endif
3297 changed_line_abv_curs();
3298 invalidate_botline();
3299
3300 /*
3301 * We only redraw when it's needed:
3302 * - While at the more prompt or executing an external command, don't
3303 * redraw, but position the cursor.
3304 * - While editing the command line, only redraw that.
3305 * - in Ex mode, don't redraw anything.
3306 * - Otherwise, redraw right now, and position the cursor.
3307 * Always need to call update_screen() or screenalloc(), to make
3308 * sure Rows/Columns and the size of ScreenLines[] is correct!
3309 */
3310 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3311 || exmode_active)
3312 {
3313 screenalloc(FALSE);
3314 repeat_message();
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 else
3317 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003318 if (curwin->w_p_scb)
3319 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003320 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003321 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003322 update_screen(NOT_VALID);
3323 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003324 }
3325 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003326 {
3327 update_topline();
3328#if defined(FEAT_INS_EXPAND)
3329 if (pum_visible())
3330 {
3331 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003332 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003333 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003334#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003335 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003336 if (redrawing())
3337 setcursor();
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340 cursor_on(); /* redrawing may have switched it off */
3341 }
3342 out_flush();
3343 --busy;
3344}
3345
3346/*
3347 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3348 * commands and Ex mode).
3349 */
3350 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003351settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353#ifdef FEAT_GUI
3354 /* don't set the term where gvim was started to any mode */
3355 if (gui.in_use)
3356 return;
3357#endif
3358
3359 if (full_screen)
3360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 /*
3362 * When returning after calling a shell we want to really set the
3363 * terminal to raw mode, even though we think it already is, because
3364 * the shell program may have reset the terminal mode.
3365 * When we think the terminal is normal, don't try to set it to
3366 * normal again, because that causes problems (logout!) on some
3367 * machines.
3368 */
3369 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3370 {
3371#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003372# ifdef FEAT_GUI
3373 if (!gui.in_use && !gui.starting)
3374# endif
3375 {
3376 /* May need to check for T_CRV response and termcodes, it
3377 * doesn't work in Cooked mode, an external program may get
3378 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003379 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3380 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003381#ifdef FEAT_TERMINAL
3382 || rfg_status == STATUS_SENT
3383#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003384 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003385 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003386 || rcs_status == STATUS_SENT
3387 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003388 (void)vpeekc_nomap();
3389 check_for_codes_from_term();
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
3392#ifdef FEAT_MOUSE_TTY
3393 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003394 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003396 if (tmode != TMODE_RAW)
3397 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003399 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 cur_tmode = tmode;
3401#ifdef FEAT_MOUSE
3402 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003403 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003405 if (tmode == TMODE_RAW)
3406 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 out_flush();
3408 }
3409#ifdef FEAT_TERMRESPONSE
3410 may_req_termresponse();
3411#endif
3412 }
3413}
3414
3415 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003416starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 if (full_screen && !termcap_active)
3419 {
3420 out_str(T_TI); /* start termcap mode */
3421 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003422 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 out_flush();
3424 termcap_active = TRUE;
3425 screen_start(); /* don't know where cursor is now */
3426#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003427# ifdef FEAT_GUI
3428 if (!gui.in_use && !gui.starting)
3429# endif
3430 {
3431 may_req_termresponse();
3432 /* Immediately check for a response. If t_Co changes, we don't
3433 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003434 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003435 check_for_codes_from_term();
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#endif
3438 }
3439}
3440
3441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 screen_stop_highlight();
3445 reset_cterm_colors();
3446 if (termcap_active)
3447 {
3448#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003449# ifdef FEAT_GUI
3450 if (!gui.in_use && !gui.starting)
3451# endif
3452 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003453 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003454 if (crv_status == STATUS_SENT
3455 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003456# ifdef FEAT_TERMINAL
3457 || rfg_status == STATUS_SENT
3458# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003459 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003460 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003461 || rcs_status == STATUS_SENT
3462 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003463 {
3464# ifdef UNIX
3465 /* Give the terminal a chance to respond. */
3466 mch_delay(100L, FALSE);
3467# endif
3468# ifdef TCIFLUSH
3469 /* Discard data received but not read. */
3470 if (exiting)
3471 tcflush(fileno(stdin), TCIFLUSH);
3472# endif
3473 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003474 /* Check for termcodes first, otherwise an external program may
3475 * get them. */
3476 check_for_codes_from_term();
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003479 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 out_str(T_KE); /* stop "keypad transmit" mode */
3481 out_flush();
3482 termcap_active = FALSE;
3483 cursor_on(); /* just in case it is still off */
3484 out_str(T_TE); /* stop termcap mode */
3485 screen_start(); /* don't know where cursor is now */
3486 out_flush();
3487 }
3488}
3489
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003490#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
3492 * Request version string (for xterm) when needed.
3493 * Only do this after switching to raw mode, otherwise the result will be
3494 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003495 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003496 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Only do this after termcap mode has been started, otherwise the codes for
3498 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003499 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3500 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003501 * On Unix only do it when both output and input are a tty (avoid writing
3502 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 * The result is caught in check_termcode().
3504 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003506may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003508 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003509 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003510 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 && *T_CRV != NUL)
3512 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003513 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003515 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* check for the characters now, otherwise they might be eaten by
3517 * get_keystroke() */
3518 out_flush();
3519 (void)vpeekc_nomap();
3520 }
3521}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003522
3523# if defined(FEAT_MBYTE) || defined(PROTO)
3524/*
3525 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003526 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003527 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003528 * If the terminal treats \u25bd as single width, the position is (1, 1),
3529 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003530 * This function has the side effect that changes cursor position, so
3531 * it must be called immediately after entering termcap mode.
3532 */
3533 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003534may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003535{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003536 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003537 && can_get_termresponse()
3538 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003539 && *T_U7 != NUL
3540 && !option_was_set((char_u *)"ambiwidth"))
3541 {
3542 char_u buf[16];
3543
Bram Moolenaar2951b772013-07-03 12:45:31 +02003544 LOG_TR("Sending U7 request");
3545 /* Do this in the second row. In the first row the returned sequence
3546 * may be CSI 1;2R, which is the same as <S-F3>. */
3547 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003548 buf[mb_char2bytes(0x25bd, buf)] = 0;
3549 out_str(buf);
3550 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003551 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003552 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003553
3554 /* This overwrites a few characters on the screen, a redraw is needed
3555 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003556 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003557 out_str((char_u *)" ");
3558 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003559
Bram Moolenaar05684312017-12-09 15:11:24 +01003560 /* Need to reset the known cursor position. */
3561 screen_start();
3562
Bram Moolenaar9584b312013-03-13 19:29:28 +01003563 /* check for the characters now, otherwise they might be eaten by
3564 * get_keystroke() */
3565 out_flush();
3566 (void)vpeekc_nomap();
3567 }
3568}
3569# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003570
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003571/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003572 * Similar to requesting the version string: Request the terminal background
3573 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003574 */
3575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003576may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003577{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003578 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003579 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003580 int didit = FALSE;
3581
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003582# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003583 /* Only request foreground if t_RF is set. */
3584 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3585 {
3586 LOG_TR("Sending FG request");
3587 out_str(T_RFG);
3588 rfg_status = STATUS_SENT;
3589 didit = TRUE;
3590 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003591# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003592
3593 /* Only request background if t_RB is set. */
3594 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003595 {
3596 LOG_TR("Sending BG request");
3597 out_str(T_RBG);
3598 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003599 didit = TRUE;
3600 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003601
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003602 if (didit)
3603 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003604 /* check for the characters now, otherwise they might be eaten by
3605 * get_keystroke() */
3606 out_flush();
3607 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003608 }
3609 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003610}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003611
Bram Moolenaar2951b772013-07-03 12:45:31 +02003612# ifdef DEBUG_TERMRESPONSE
3613 static void
3614log_tr(char *msg)
3615{
3616 static FILE *fd_tr = NULL;
3617 static proftime_T start;
3618 proftime_T now;
3619
3620 if (fd_tr == NULL)
3621 {
3622 fd_tr = fopen("termresponse.log", "w");
3623 profile_start(&start);
3624 }
3625 now = start;
3626 profile_end(&now);
3627 fprintf(fd_tr, "%s: %s %s\n",
3628 profile_msg(&now),
3629 must_redraw == NOT_VALID ? "NV"
3630 : must_redraw == CLEAR ? "CL" : " ",
3631 msg);
3632}
3633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
3635
3636/*
3637 * Return TRUE when saving and restoring the screen.
3638 */
3639 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003640swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 return (full_screen && *T_TI != NUL);
3643}
3644
Bram Moolenaarecadf432018-03-20 11:17:04 +01003645#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646/*
3647 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3648 */
3649 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003650setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652# ifdef FEAT_MOUSE_TTY
3653 int checkfor;
3654# endif
3655
3656# ifdef FEAT_MOUSESHAPE
3657 update_mouseshape(-1);
3658# endif
3659
3660# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3661# ifdef FEAT_GUI
3662 /* In the GUI the mouse is always enabled. */
3663 if (gui.in_use)
3664 return;
3665# endif
3666 /* be quick when mouse is off */
3667 if (*p_mouse == NUL || has_mouse_termcode == 0)
3668 return;
3669
3670 /* don't switch mouse on when not in raw mode (Ex mode) */
3671 if (cur_tmode != TMODE_RAW)
3672 {
3673 mch_setmouse(FALSE);
3674 return;
3675 }
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (VIsual_active)
3678 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003679 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 checkfor = MOUSE_RETURN;
3681 else if (State & INSERT)
3682 checkfor = MOUSE_INSERT;
3683 else if (State & CMDLINE)
3684 checkfor = MOUSE_COMMAND;
3685 else if (State == CONFIRM || State == EXTERNCMD)
3686 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3687 else
3688 checkfor = MOUSE_NORMAL; /* assume normal mode */
3689
3690 if (mouse_has(checkfor))
3691 mch_setmouse(TRUE);
3692 else
3693 mch_setmouse(FALSE);
3694# endif
3695}
3696
3697/*
3698 * Return TRUE if
3699 * - "c" is in 'mouse', or
3700 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3701 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3702 * normal editing mode (not at hit-return message).
3703 */
3704 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003705mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 char_u *p;
3708
3709 for (p = p_mouse; *p; ++p)
3710 switch (*p)
3711 {
3712 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3713 return TRUE;
3714 break;
3715 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3716 return TRUE;
3717 break;
3718 default: if (c == *p) return TRUE; break;
3719 }
3720 return FALSE;
3721}
3722
3723/*
3724 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3725 */
3726 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003727mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 return (p_mousem[0] == 'p');
3730}
3731#endif
3732
3733/*
3734 * By outputting the 'cursor very visible' termcap code, for some windowed
3735 * terminals this makes the screen scrolled to the correct position.
3736 * Used when starting Vim or returning from a shell.
3737 */
3738 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003739scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003741 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
3743 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003744 out_str(T_CVS);
3745 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747}
3748
3749static int cursor_is_off = FALSE;
3750
3751/*
3752 * Enable the cursor.
3753 */
3754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003755cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 if (cursor_is_off)
3758 {
3759 out_str(T_VE);
3760 cursor_is_off = FALSE;
3761 }
3762}
3763
3764/*
3765 * Disable the cursor.
3766 */
3767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003768cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003770 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003772 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 cursor_is_off = TRUE;
3774 }
3775}
3776
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003777#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003779 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003780 */
3781 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003782term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003783{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003784 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003785 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003786
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003787 /* Only do something when redrawing the screen and we can restore the
3788 * mode. */
3789 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003790 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003791# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003792 if (forced && initial_cursor_shape > 0)
3793 /* Restore to initial values. */
3794 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003795# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003796 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003797 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003798
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003799 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003800 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003801 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003802 {
3803 if (*T_CSR != NUL)
3804 p = T_CSR; /* Replace mode cursor */
3805 else
3806 p = T_CSI; /* fall back to Insert mode cursor */
3807 if (*p != NUL)
3808 {
3809 out_str(p);
3810 showing_mode = REPLACE;
3811 }
3812 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003813 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003814 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003815 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003816 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003817 {
3818 out_str(T_CSI); /* Insert mode cursor */
3819 showing_mode = INSERT;
3820 }
3821 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003822 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003823 {
3824 out_str(T_CEI); /* non-Insert mode cursor */
3825 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003826 }
3827}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003828
3829# if defined(FEAT_TERMINAL) || defined(PROTO)
3830 void
3831term_cursor_color(char_u *color)
3832{
3833 if (*T_CSC != NUL)
3834 {
3835 out_str(T_CSC); /* set cursor color start */
3836 out_str_nf(color);
3837 out_str(T_CEC); /* set cursor color end */
3838 out_flush();
3839 }
3840}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003841# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003842
Bram Moolenaar4db25542017-08-28 22:43:05 +02003843 int
3844blink_state_is_inverted()
3845{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003846#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003847 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3848 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003849#else
3850 return FALSE;
3851#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003852}
3853
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003854/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003855 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003856 */
3857 void
3858term_cursor_shape(int shape, int blink)
3859{
3860 if (*T_CSH != NUL)
3861 {
3862 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3863 out_flush();
3864 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003865 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003866 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003867 int do_blink = blink;
3868
3869 /* t_SH is empty: try setting just the blink state.
3870 * The blink flags are XORed together, if the initial blinking from
3871 * style and shape differs, we need to invert the flag here. */
3872 if (blink_state_is_inverted())
3873 do_blink = !blink;
3874
3875 if (do_blink && *T_VS != NUL)
3876 {
3877 out_str(T_VS);
3878 out_flush();
3879 }
3880 else if (!do_blink && *T_CVS != NUL)
3881 {
3882 out_str(T_CVS);
3883 out_flush();
3884 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003885 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003886}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003887#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * Set scrolling region for window 'wp'.
3891 * The region starts 'off' lines from the start of the window.
3892 * Also set the vertical scroll region for a vertically split window. Always
3893 * the full width of the window, excluding the vertical separator.
3894 */
3895 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003896scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3899 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003901 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3902 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 screen_start(); /* don't know where cursor is now */
3904}
3905
3906/*
3907 * Reset scrolling region to the whole screen.
3908 */
3909 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003910scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (*T_CSV != NUL)
3914 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 screen_start(); /* don't know where cursor is now */
3916}
3917
3918
3919/*
3920 * List of terminal codes that are currently recognized.
3921 */
3922
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003923static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 char_u name[2]; /* termcap name of entry */
3926 char_u *code; /* terminal code (in allocated memory) */
3927 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003928 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929} *termcodes = NULL;
3930
3931static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3932static int tc_len = 0; /* current number of entries in termcodes[] */
3933
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003934static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003937clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 while (tc_len > 0)
3940 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003941 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 tc_max_len = 0;
3943
3944#ifdef HAVE_TGETENT
3945 BC = (char *)empty_option;
3946 UP = (char *)empty_option;
3947 PC = NUL; /* set pad character to NUL */
3948 ospeed = 0;
3949#endif
3950
3951 need_gather = TRUE; /* need to fill termleader[] */
3952}
3953
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954#define ATC_FROM_TERM 55
3955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956/*
3957 * Add a new entry to the list of terminal codes.
3958 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003959 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3960 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
3962 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003963add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 struct termcode *new_tc;
3966 int i, j;
3967 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003968 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 if (string == NULL || *string == NUL)
3971 {
3972 del_termcode(name);
3973 return;
3974 }
3975
Bram Moolenaar45500912014-07-09 20:51:07 +02003976#if defined(WIN3264) && !defined(FEAT_GUI)
3977 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3978#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (s == NULL)
3982 return;
3983
3984 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003987 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 s[0] = term_7to8bit(string);
3989 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003990
3991#if defined(WIN3264) && !defined(FEAT_GUI)
3992 if (s[0] == K_NUL)
3993 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003994 STRMOVE(s + 1, s);
3995 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003996 }
3997#endif
3998
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003999 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 need_gather = TRUE; /* need to fill termleader[] */
4002
4003 /*
4004 * need to make space for more entries
4005 */
4006 if (tc_len == tc_max_len)
4007 {
4008 tc_max_len += 20;
4009 new_tc = (struct termcode *)alloc(
4010 (unsigned)(tc_max_len * sizeof(struct termcode)));
4011 if (new_tc == NULL)
4012 {
4013 tc_max_len -= 20;
4014 return;
4015 }
4016 for (i = 0; i < tc_len; ++i)
4017 new_tc[i] = termcodes[i];
4018 vim_free(termcodes);
4019 termcodes = new_tc;
4020 }
4021
4022 /*
4023 * Look for existing entry with the same name, it is replaced.
4024 * Look for an existing entry that is alphabetical higher, the new entry
4025 * is inserted in front of it.
4026 */
4027 for (i = 0; i < tc_len; ++i)
4028 {
4029 if (termcodes[i].name[0] < name[0])
4030 continue;
4031 if (termcodes[i].name[0] == name[0])
4032 {
4033 if (termcodes[i].name[1] < name[1])
4034 continue;
4035 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 */
4038 if (termcodes[i].name[1] == name[1])
4039 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004040 if (flags == ATC_FROM_TERM && (j = termcode_star(
4041 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004042 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004043 /* Don't replace ESC[123;*X or ESC O*X with another when
4044 * invoked from got_code_from_term(). */
4045 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004046 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004047 && s[len - 1]
4048 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004049 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004051 vim_free(s);
4052 return;
4053 }
4054 }
4055 else
4056 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004058 vim_free(termcodes[i].code);
4059 --tc_len;
4060 break;
4061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 }
4064 /*
4065 * Found alphabetical larger entry, move rest to insert new entry
4066 */
4067 for (j = tc_len; j > i; --j)
4068 termcodes[j] = termcodes[j - 1];
4069 break;
4070 }
4071
4072 termcodes[i].name[0] = name[0];
4073 termcodes[i].name[1] = name[1];
4074 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004075 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004076
4077 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4078 * accept modifiers. */
4079 termcodes[i].modlen = 0;
4080 j = termcode_star(s, len);
4081 if (j > 0)
4082 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 ++tc_len;
4084}
4085
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004086/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004087 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004088 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004089 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090 */
4091 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004092termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004093{
4094 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4095 if (len >= 3 && code[len - 2] == '*')
4096 {
4097 if (len >= 5 && code[len - 3] == ';')
4098 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004099 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100 return 1;
4101 }
4102 return 0;
4103}
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004106find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107{
4108 int i;
4109
4110 for (i = 0; i < tc_len; ++i)
4111 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4112 return termcodes[i].code;
4113 return NULL;
4114}
4115
4116#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4117 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004118get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 if (i >= tc_len)
4121 return NULL;
4122 return &termcodes[i].name[0];
4123}
4124#endif
4125
4126 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004127del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 int i;
4130
4131 if (termcodes == NULL) /* nothing there yet */
4132 return;
4133
4134 need_gather = TRUE; /* need to fill termleader[] */
4135
4136 for (i = 0; i < tc_len; ++i)
4137 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4138 {
4139 del_termcode_idx(i);
4140 return;
4141 }
4142 /* not found. Give error message? */
4143}
4144
4145 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004146del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 int i;
4149
4150 vim_free(termcodes[idx].code);
4151 --tc_len;
4152 for (i = idx; i < tc_len; ++i)
4153 termcodes[i] = termcodes[i + 1];
4154}
4155
4156#ifdef FEAT_TERMRESPONSE
4157/*
4158 * Called when detected that the terminal sends 8-bit codes.
4159 * Convert all 7-bit codes to their 8-bit equivalent.
4160 */
4161 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004162switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 int i;
4165 int c;
4166
4167 /* Only need to do something when not already using 8-bit codes. */
4168 if (!term_is_8bit(T_NAME))
4169 {
4170 for (i = 0; i < tc_len; ++i)
4171 {
4172 c = term_7to8bit(termcodes[i].code);
4173 if (c != 0)
4174 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004175 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 termcodes[i].code[0] = c;
4177 }
4178 }
4179 need_gather = TRUE; /* need to fill termleader[] */
4180 }
4181 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004182 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183}
4184#endif
4185
4186#ifdef CHECK_DOUBLE_CLICK
4187static linenr_T orig_topline = 0;
4188# ifdef FEAT_DIFF
4189static int orig_topfill = 0;
4190# endif
4191#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004192#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193/*
4194 * Checking for double clicks ourselves.
4195 * "orig_topline" is used to avoid detecting a double-click when the window
4196 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4197 */
4198/*
4199 * Set orig_topline. Used when jumping to another window, so that a double
4200 * click still works.
4201 */
4202 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004203set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 orig_topline = wp->w_topline;
4206# ifdef FEAT_DIFF
4207 orig_topfill = wp->w_topfill;
4208# endif
4209}
4210#endif
4211
4212/*
4213 * Check if typebuf.tb_buf[] contains a terminal key code.
4214 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4215 * + max_offset].
4216 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004217 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * With a match, the match is removed, the replacement code is inserted in
4219 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4220 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004221 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4222 * "buflen" is then the length of the string in buf[] and is updated for
4223 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 */
4225 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004226check_termcode(
4227 int max_offset,
4228 char_u *buf,
4229 int bufsize,
4230 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 char_u *tp;
4233 char_u *p;
4234 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004237 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 int offset;
4239 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004240 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004241 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004242 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 int new_slen;
4244 int extra;
4245 char_u string[MAX_KEY_CODE_LEN + 1];
4246 int i, j;
4247 int idx = 0;
4248#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004249# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4250 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 char_u bytes[6];
4252 int num_bytes;
4253# endif
4254 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int is_click, is_drag;
4256 int wheel_code = 0;
4257 int current_button;
4258 static int held_button = MOUSE_RELEASE;
4259 static int orig_num_clicks = 1;
4260 static int orig_mouse_code = 0x0;
4261# ifdef CHECK_DOUBLE_CLICK
4262 static int orig_mouse_col = 0;
4263 static int orig_mouse_row = 0;
4264 static struct timeval orig_mouse_time = {0, 0};
4265 /* time of previous mouse click */
4266 struct timeval mouse_time; /* time of current mouse click */
4267 long timediff; /* elapsed time in msec */
4268# endif
4269#endif
4270 int cpo_koffset;
4271#ifdef FEAT_MOUSE_GPM
4272 extern int gpm_flag; /* gpm library variable */
4273#endif
4274
4275 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4276
4277 /*
4278 * Speed up the checks for terminal codes by gathering all first bytes
4279 * used in termleader[]. Often this is just a single <Esc>.
4280 */
4281 if (need_gather)
4282 gather_termleader();
4283
4284 /*
4285 * Check at several positions in typebuf.tb_buf[], to catch something like
4286 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4287 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004288 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 * This is used often, KEEP IT FAST!
4290 */
4291 for (offset = 0; offset < max_offset; ++offset)
4292 {
4293 if (buf == NULL)
4294 {
4295 if (offset >= typebuf.tb_len)
4296 break;
4297 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4298 len = typebuf.tb_len - offset; /* length of the input */
4299 }
4300 else
4301 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004302 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 break;
4304 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004305 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307
4308 /*
4309 * Don't check characters after K_SPECIAL, those are already
4310 * translated terminal chars (avoid translating ~@^Hx).
4311 */
4312 if (*tp == K_SPECIAL)
4313 {
4314 offset += 2; /* there are always 2 extra characters */
4315 continue;
4316 }
4317
4318 /*
4319 * Skip this position if the character does not appear as the first
4320 * character in term_strings. This speeds up a lot, since most
4321 * termcodes start with the same character (ESC or CSI).
4322 */
4323 i = *tp;
4324 for (p = termleader; *p && *p != i; ++p)
4325 ;
4326 if (*p == NUL)
4327 continue;
4328
4329 /*
4330 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4331 * are in Insert mode.
4332 */
4333 if (*tp == ESC && !p_ek && (State & INSERT))
4334 continue;
4335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004337 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004338 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340#ifdef FEAT_GUI
4341 if (gui.in_use)
4342 {
4343 /*
4344 * GUI special key codes are all of the form [CSI xx].
4345 */
4346 if (*tp == CSI) /* Special key from GUI */
4347 {
4348 if (len < 3)
4349 return -1; /* Shouldn't happen */
4350 slen = 3;
4351 key_name[0] = tp[1];
4352 key_name[1] = tp[2];
4353 }
4354 }
4355 else
4356#endif /* FEAT_GUI */
4357 {
4358 for (idx = 0; idx < tc_len; ++idx)
4359 {
4360 /*
4361 * Ignore the entry if we are not at the start of
4362 * typebuf.tb_buf[]
4363 * and there are not enough characters to make a match.
4364 * But only when the 'K' flag is in 'cpoptions'.
4365 */
4366 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004367 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (cpo_koffset && offset && len < slen)
4369 continue;
4370 if (STRNCMP(termcodes[idx].code, tp,
4371 (size_t)(slen > len ? len : slen)) == 0)
4372 {
4373 if (len < slen) /* got a partial sequence */
4374 return -1; /* need to get more chars */
4375
4376 /*
4377 * When found a keypad key, check if there is another key
4378 * that matches and use that one. This makes <Home> to be
4379 * found instead of <kHome> when they produce the same
4380 * key code.
4381 */
4382 if (termcodes[idx].name[0] == 'K'
4383 && VIM_ISDIGIT(termcodes[idx].name[1]))
4384 {
4385 for (j = idx + 1; j < tc_len; ++j)
4386 if (termcodes[j].len == slen &&
4387 STRNCMP(termcodes[idx].code,
4388 termcodes[j].code, slen) == 0)
4389 {
4390 idx = j;
4391 break;
4392 }
4393 }
4394
4395 key_name[0] = termcodes[idx].name[0];
4396 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004399
4400 /*
4401 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004402 * <Esc>[123;*X (modslen == slen - 3)
4403 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4404 * When there is a modifier the * matches a number.
4405 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004406 */
4407 if (termcodes[idx].modlen > 0)
4408 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004409 modslen = termcodes[idx].modlen;
4410 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004411 continue;
4412 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004413 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004414 {
4415 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004416
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004417 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004418 return -1; /* need to get more chars */
4419
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004420 if (tp[modslen] == termcodes[idx].code[slen - 1])
4421 slen = modslen + 1; /* no modifiers */
4422 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004423 continue; /* no match */
4424 else
4425 {
4426 /* Skip over the digits, the final char must
4427 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004428 for (j = slen - 2; j < len && (isdigit(tp[j])
4429 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004430 ;
4431 ++j;
4432 if (len < j) /* got a partial sequence */
4433 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004434 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4435 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004436
Bram Moolenaara529ce02017-06-22 22:37:57 +02004437 modifiers_start = tp + slen - 2;
4438
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004439 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004440 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004441 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004442 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004443 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004445 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004446 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004447 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004448 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004449
4450 slen = j;
4451 }
4452 key_name[0] = termcodes[idx].name[0];
4453 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004454 break;
4455 }
4456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 }
4459
4460#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004461 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004462 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004463 * detecting the start of these mouse codes they might as well be
4464 * another key code or terminal response. */
4465# ifdef FEAT_MOUSE_DEC
4466 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004467# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004468# ifdef FEAT_MOUSE_PTERM
4469 || key_name[0] == KS_PTERM_MOUSE
4470# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004471 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004473 /* Check for some responses from the terminal starting with
4474 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004475 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004476 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004477 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004478 * Also eat other possible responses to t_RV, rxvt returns
4479 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4480 * mrxvt has been reported to have "+" in the version. Assume
4481 * the escape sequence ends with a letter or one of "{|}~".
4482 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004483 * - Cursor position report: <Esc>[{row};{col}R
4484 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004485 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004486 *
4487 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004488 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004489 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004490
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004491 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004492 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004493 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004494 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004496 int col = 0;
4497 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004498#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004499 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004500#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004503 for (i = 2 + (tp[0] != CSI); i < len
4504 && !(tp[i] >= '{' && tp[i] <= '~')
4505 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004506 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004507 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004508 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004509#ifdef FEAT_MBYTE
4510 row_char = tp[i - 1];
4511#endif
4512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004514 {
4515 LOG_TR("Not enough characters for CRV");
4516 return -1;
4517 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004518 if (extra > 0)
4519 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004520
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004521#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004522 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4523 * u7_status is not "sent", it may be from a previous Vim that
4524 * just exited. But not for <S-F3>, it sends something
4525 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004526 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004527 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004528 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004529 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004530 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004531
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004532 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004533 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004534 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004535 if (col == 2)
4536 aw = "single";
4537 else if (col == 3)
4538 aw = "double";
4539 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4540 {
4541 /* Setting the option causes a screen redraw. Do
4542 * that right away if possible, keeping any
4543 * messages. */
4544 set_option_value((char_u *)"ambw", 0L,
4545 (char_u *)aw, 0);
4546# ifdef DEBUG_TERMRESPONSE
4547 {
4548 char buf[100];
4549 int r = redraw_asap(CLEAR);
4550
4551 sprintf(buf,
4552 "set 'ambiwidth', redraw_asap(): %d",
4553 r);
4554 log_tr(buf);
4555 }
4556# else
4557 redraw_asap(CLEAR);
4558# endif
4559 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004560 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004561 key_name[0] = (int)KS_EXTRA;
4562 key_name[1] = (int)KE_IGNORE;
4563 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004564# ifdef FEAT_EVAL
4565 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4566# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004567 }
4568 else
4569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004571 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004573 int version = col;
4574
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004575 LOG_TR("Received CRV response");
4576 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004577 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 /* If this code starts with CSI, you can bet that the
4580 * terminal uses 8-bit codes. */
4581 if (tp[0] == CSI)
4582 switch_to_8bit();
4583
4584 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004585 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 * Ignore it for when the user has set 'term' to xterm,
4587 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004588 if (version > 20000)
4589 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004591 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004593 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004594# ifdef FEAT_MOUSE_SGR
4595 int is_iterm2 = FALSE;
4596# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004599 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004601 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 check_for_codes = TRUE;
4603 need_gather = TRUE;
4604 req_codes_from_term();
4605 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004606
4607 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004608 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004609 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004610 {
4611 /* If run from Vim $COLORS is set to the number of
4612 * colors the terminal supports. Otherwise assume
4613 * 256, libvterm supports even more. */
4614 if (mch_getenv((char_u *)"COLORS") == NULL)
4615 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004616# ifdef FEAT_MOUSE_SGR
4617 /* Libvterm can handle SGR mouse reporting. */
4618 if (!option_was_set((char_u *)"ttym"))
4619 set_option_value((char_u *)"ttym", 0L,
4620 (char_u *)"sgr", 0);
4621# endif
4622 }
4623
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004624 if (version == 95)
4625 {
4626 /* Mac Terminal.app sends 1;95;0 */
4627 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4628 {
4629 is_not_xterm = TRUE;
4630 is_mac_terminal = TRUE;
4631 }
4632# ifdef FEAT_MOUSE_SGR
Bram Moolenaar05684312017-12-09 15:11:24 +01004633 /* iTerm2 sends 0;95;0 */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004634 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4635 is_iterm2 = TRUE;
4636# endif
4637 }
4638
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004639 /* Only set 'ttymouse' automatically if it was not set
4640 * by the user already. */
4641 if (!option_was_set((char_u *)"ttym"))
4642 {
4643# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004644 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004645 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004646 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004647 set_option_value((char_u *)"ttym", 0L,
4648 (char_u *)"sgr", 0);
4649 else
4650# endif
4651 /* if xterm version >= 95 use mouse dragging */
4652 if (version >= 95)
4653 set_option_value((char_u *)"ttym", 0L,
4654 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004655 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004656
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004657 /* Detect terminals that set $TERM to something like
4658 * "xterm-256colors" but are not fully xterm
4659 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004660
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004661 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004662 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004663 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004664 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004665 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004666 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004667 is_not_xterm = TRUE;
4668
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004669 /* PuTTY sends 0;136;0
4670 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004671 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004672 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004673 is_not_xterm = TRUE;
4674
4675 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004676 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004677 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004678 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004679
4680 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004681 * set. Only supported properly by xterm since version
4682 * 279 (otherwise it returns 0x18).
4683 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004684 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004685 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004686 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004687 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004688 && *T_CSH != NUL
4689 && *T_CRS != NUL)
4690 {
4691 LOG_TR("Sending cursor style request");
4692 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004693 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004694 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004695 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004696
4697 /* Only request the cursor blink mode if t_RC set. Not
4698 * for Gnome terminal, it can't handle t_RC, it
4699 * echoes the characters to the screen. */
4700 if (rbm_status == STATUS_GET
4701 && !is_not_xterm
4702 && *T_CRC != NUL)
4703 {
4704 LOG_TR("Sending cursor blink mode request");
4705 out_str(T_CRC);
4706 rbm_status = STATUS_SENT;
4707 need_flush = TRUE;
4708 }
4709
4710 if (need_flush)
4711 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004713 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004715 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 apply_autocmds(EVENT_TERMRESPONSE,
4718 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 key_name[0] = (int)KS_EXTRA;
4720 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004722
Bram Moolenaar4db25542017-08-28 22:43:05 +02004723 /* Check blinking cursor from xterm:
4724 * {lead}?12;1$y set
4725 * {lead}?12;2$y not set
4726 *
4727 * {lead} can be <Esc>[ or CSI
4728 */
4729 else if (rbm_status == STATUS_SENT
4730 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4731 && i == j + 6
4732 && tp[j + 1] == '1'
4733 && tp[j + 2] == '2'
4734 && tp[j + 3] == ';'
4735 && tp[i - 1] == '$'
4736 && tp[i] == 'y')
4737 {
4738 initial_cursor_blink = (tp[j + 4] == '1');
4739 rbm_status = STATUS_GOT;
4740 LOG_TR("Received cursor blinking mode response");
4741 key_name[0] = (int)KS_EXTRA;
4742 key_name[1] = (int)KE_IGNORE;
4743 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004744# ifdef FEAT_EVAL
4745 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4746# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004747 }
4748
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004749 /*
4750 * Check for a window position response from the terminal:
4751 * {lead}3;{x}:{y}t
4752 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004753 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004754 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4755 || (len >= 3 && tp[0] == CSI))
4756 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4757 && tp[j + 1] == ';')
4758 {
4759 j += 2;
4760 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4761 ;
4762 if (i < len && tp[i] == ';')
4763 {
4764 winpos_x = atoi((char *)tp + j);
4765 j = i + 1;
4766 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4767 ;
4768 if (i < len && tp[i] == 't')
4769 {
4770 winpos_y = atoi((char *)tp + j);
4771 /* got finished code: consume it */
4772 key_name[0] = (int)KS_EXTRA;
4773 key_name[1] = (int)KE_IGNORE;
4774 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004775
4776 if (--did_request_winpos <= 0)
4777 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004778 }
4779 }
4780 if (i == len)
4781 {
4782 LOG_TR("not enough characters for winpos");
4783 return -1;
4784 }
4785 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004786 }
4787
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004788 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004789 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004790 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004791 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004792 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004793 * {lead} can be <Esc>] or OSC
4794 * {tail} can be '\007', <Esc>\ or STERM.
4795 *
4796 * Consume any code that starts with "{lead}11;", it's also
4797 * possible that "rgba" is following.
4798 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004799 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004800 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4801 || tp[0] == OSC))
4802 {
4803 j = 1 + (tp[0] == ESC);
4804 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004805 || (argp[1] != '1' && argp[1] != '0')
4806 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004807 i = 0; /* no match */
4808 else
4809 for (i = j; i < len; ++i)
4810 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4811 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004812 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004813 int is_bg = argp[1] == '1';
4814
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004815 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004816 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004817 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004818#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004819 int rval = hexhex2nr(tp + j + 7);
4820 int gval = hexhex2nr(tp + j + 12);
4821 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004822#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004823 if (is_bg)
4824 {
4825 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004826 + tp[j+17]) ? "light" : "dark";
4827
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004828 LOG_TR("Received RBG response");
4829 rbg_status = STATUS_GOT;
4830#ifdef FEAT_TERMINAL
4831 bg_r = rval;
4832 bg_g = gval;
4833 bg_b = bval;
4834#endif
4835 if (!option_was_set((char_u *)"bg")
4836 && STRCMP(p_bg, newval) != 0)
4837 {
4838 /* value differs, apply it */
4839 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004840 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004841 reset_option_was_set((char_u *)"bg");
4842 redraw_asap(CLEAR);
4843 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004844 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004845#ifdef FEAT_TERMINAL
4846 else
4847 {
4848 LOG_TR("Received RFG response");
4849 rfg_status = STATUS_GOT;
4850 fg_r = rval;
4851 fg_g = gval;
4852 fg_b = bval;
4853 }
4854#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004855 }
4856
4857 /* got finished code: consume it */
4858 key_name[0] = (int)KS_EXTRA;
4859 key_name[1] = (int)KE_IGNORE;
4860 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004861# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004862 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4863 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004864# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004865 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004866 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004867 if (i == len)
4868 {
4869 LOG_TR("not enough characters for RB");
4870 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004874 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004875 * {lead}{flag}+r<hex bytes><{tail}
4876 *
4877 * {lead} can be <Esc>P or DCS
4878 * {flag} can be '0' or '1'
4879 * {tail} can be Esc>\ or STERM
4880 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004881 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004882 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004883 *
4884 * {lead} can be <Esc>P or DCS
4885 * {tail} can be Esc>\ or STERM
4886 *
4887 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004888 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004889 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004890 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 || tp[0] == DCS))
4892 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004893 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004894 if (len < j + 3)
4895 i = len; /* need more chars */
4896 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004897 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004898 else if (argp[1] == '+')
4899 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004900 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004901 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004902 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 || tp[i] == STERM)
4904 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004905 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 got_code_from_term(tp + j, i);
4907 key_name[0] = (int)KS_EXTRA;
4908 key_name[1] = (int)KE_IGNORE;
4909 slen = i + 1 + (tp[i] == ESC);
4910 break;
4911 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004912 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004913 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004914 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004915 /* Probably the cursor shape response. Make sure that "i"
4916 * is equal to "len" when there are not sufficient
4917 * characters. */
4918 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004919 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004920 if (i - j == 3 && !isdigit(tp[i]))
4921 break;
4922 if (i - j == 4 && tp[i] != ' ')
4923 break;
4924 if (i - j == 5 && tp[i] != 'q')
4925 break;
4926 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4927 break;
4928 if ((i - j == 6 && tp[i] == STERM)
4929 || (i - j == 7 && tp[i] == '\\'))
4930 {
4931 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004932
Bram Moolenaar590ec872018-03-02 20:58:42 +01004933 /* 0, 1 = block blink, 2 = block
4934 * 3 = underline blink, 4 = underline
4935 * 5 = vertical bar blink, 6 = vertical bar */
4936 number = number == 0 ? 1 : number;
4937 initial_cursor_shape = (number + 1) / 2;
4938 /* The blink flag is actually inverted, compared to
4939 * the value set with T_SH. */
4940 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004941 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004942 rcs_status = STATUS_GOT;
4943 LOG_TR("Received cursor shape response");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004944
Bram Moolenaar590ec872018-03-02 20:58:42 +01004945 key_name[0] = (int)KS_EXTRA;
4946 key_name[1] = (int)KE_IGNORE;
4947 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004948# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004949 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004950# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004951 break;
4952 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004953 }
4954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004957 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004958 /* These codes arrive many together, each code can be
4959 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004960 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004961 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 }
4965#endif
4966
4967 if (key_name[0] == NUL)
4968 continue; /* No match at this position, try next one */
4969
4970 /* We only get here when we have a complete termcode match */
4971
4972#ifdef FEAT_MOUSE
4973# ifdef FEAT_GUI
4974 /*
4975 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4976 * so that we know which window to scroll later.
4977 */
4978 if (gui.in_use
4979 && key_name[0] == (int)KS_EXTRA
4980 && (key_name[1] == (int)KE_X1MOUSE
4981 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004982 || key_name[1] == (int)KE_MOUSELEFT
4983 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 || key_name[1] == (int)KE_MOUSEDOWN
4985 || key_name[1] == (int)KE_MOUSEUP))
4986 {
4987 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4988 if (num_bytes == -1) /* not enough coordinates */
4989 return -1;
4990 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4991 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4992 slen += num_bytes;
4993 }
4994 else
4995# endif
4996 /*
4997 * If it is a mouse click, get the coordinates.
4998 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004999 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005001 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002# endif
5003# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005004 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005# endif
5006# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005007 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008# endif
5009# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005010 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005012# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005013 || key_name[0] == KS_URXVT_MOUSE
5014# endif
5015# ifdef FEAT_MOUSE_SGR
5016 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005017 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 )
5020 {
5021 is_click = is_drag = FALSE;
5022
Bram Moolenaar864207d2008-06-24 22:14:38 +00005023# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5024 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (key_name[0] == (int)KS_MOUSE)
5026 {
5027 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005028 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * s == encoded button state:
5030 * 0x20 = left button down
5031 * 0x21 = middle button down
5032 * 0x22 = right button down
5033 * 0x23 = any button release
5034 * 0x60 = button 4 down (scroll wheel down)
5035 * 0x61 = button 5 down (scroll wheel up)
5036 * add 0x04 for SHIFT
5037 * add 0x08 for ALT
5038 * add 0x10 for CTRL
5039 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005040 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5041 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * c == column + ' ' + 1 == column + 33
5043 * r == row + ' ' + 1 == row + 33
5044 *
5045 * The coordinates are passed on through global variables.
5046 * Ugly, but this avoids trouble with mouse clicks at an
5047 * unexpected moment and allows for mapping them.
5048 */
5049 for (;;)
5050 {
5051#ifdef FEAT_GUI
5052 if (gui.in_use)
5053 {
5054 /* GUI uses more bits for columns > 223 */
5055 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5056 if (num_bytes == -1) /* not enough coordinates */
5057 return -1;
5058 mouse_code = bytes[0];
5059 mouse_col = 128 * (bytes[1] - ' ' - 1)
5060 + bytes[2] - ' ' - 1;
5061 mouse_row = 128 * (bytes[3] - ' ' - 1)
5062 + bytes[4] - ' ' - 1;
5063 }
5064 else
5065#endif
5066 {
5067 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5068 if (num_bytes == -1) /* not enough coordinates */
5069 return -1;
5070 mouse_code = bytes[0];
5071 mouse_col = bytes[1] - ' ' - 1;
5072 mouse_row = bytes[2] - ' ' - 1;
5073 }
5074 slen += num_bytes;
5075
5076 /* If the following bytes is also a mouse code and it has
5077 * the same code, dump this one and get the next. This
5078 * makes dragging a whole lot faster. */
5079#ifdef FEAT_GUI
5080 if (gui.in_use)
5081 j = 3;
5082 else
5083#endif
5084 j = termcodes[idx].len;
5085 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5086 && tp[slen + j] == mouse_code
5087 && tp[slen + j + 1] != NUL
5088 && tp[slen + j + 2] != NUL
5089#ifdef FEAT_GUI
5090 && (!gui.in_use
5091 || (tp[slen + j + 3] != NUL
5092 && tp[slen + j + 4] != NUL))
5093#endif
5094 )
5095 slen += j;
5096 else
5097 break;
5098 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005101# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5102 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005103 || key_name[0] == KS_SGR_MOUSE
5104 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005105 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005106 /* URXVT 1015 mouse reporting mode:
5107 * Almost identical to xterm mouse mode, except the values
5108 * are decimal instead of bytes.
5109 *
5110 * \033[%d;%d;%dM
5111 * ^-- row
5112 * ^----- column
5113 * ^-------- code
5114 *
5115 * SGR 1006 mouse reporting mode:
5116 * Almost identical to xterm mouse mode, except the values
5117 * are decimal instead of bytes.
5118 *
5119 * \033[<%d;%d;%dM
5120 * ^-- row
5121 * ^----- column
5122 * ^-------- code
5123 *
5124 * \033[<%d;%d;%dm : mouse release event
5125 * ^-- row
5126 * ^----- column
5127 * ^-------- code
5128 */
5129 p = modifiers_start;
5130 if (p == NULL)
5131 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005132
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005133 mouse_code = getdigits(&p);
5134 if (*p++ != ';')
5135 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005136
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005137 /* when mouse reporting is SGR, add 32 to mouse code */
5138 if (key_name[0] == KS_SGR_MOUSE
5139 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5140 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005141
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005142 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5143 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005144
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005145 mouse_col = getdigits(&p) - 1;
5146 if (*p++ != ';')
5147 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005148
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005149 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005150
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005151 /* The modifiers were the mouse coordinates, not the
5152 * modifier keys (alt/shift/ctrl/meta) state. */
5153 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005154 }
5155# endif
5156
5157 if (key_name[0] == (int)KS_MOUSE
5158#ifdef FEAT_MOUSE_URXVT
5159 || key_name[0] == (int)KS_URXVT_MOUSE
5160#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005161#ifdef FEAT_MOUSE_SGR
5162 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005163 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005164#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005165 )
5166 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005167# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /*
5169 * Handle mouse events.
5170 * Recognize the xterm mouse wheel, but not in the GUI, the
5171 * Linux console with GPM and the MS-DOS or Win32 console
5172 * (multi-clicks use >= 0x60).
5173 */
5174 if (mouse_code >= MOUSEWHEEL_LOW
5175# ifdef FEAT_GUI
5176 && !gui.in_use
5177# endif
5178# ifdef FEAT_MOUSE_GPM
5179 && gpm_flag == 0
5180# endif
5181 )
5182 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005183# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5184 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5185 /* mouse-move event, using MOUSE_DRAG works */
5186 mouse_code = MOUSE_DRAG;
5187 else
5188# endif
5189 /* Keep the mouse_code before it's changed, so that we
5190 * remember that it was a mouse wheel click. */
5191 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193# ifdef FEAT_MOUSE_XTERM
5194 else if (held_button == MOUSE_RELEASE
5195# ifdef FEAT_GUI
5196 && !gui.in_use
5197# endif
5198 && (mouse_code == 0x23 || mouse_code == 0x24))
5199 {
5200 /* Apparently used by rxvt scroll wheel. */
5201 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5202 }
5203# endif
5204
5205# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5206 else if (use_xterm_mouse() > 1)
5207 {
5208 if (mouse_code & MOUSE_DRAG_XTERM)
5209 mouse_code |= MOUSE_DRAG;
5210 }
5211# endif
5212# ifdef FEAT_XCLIPBOARD
5213 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5214 {
5215 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5216 stop_xterm_trace();
5217 else
5218 start_xterm_trace(mouse_code);
5219 }
5220# endif
5221# endif
5222 }
5223# endif /* !UNIX || FEAT_MOUSE_XTERM */
5224# ifdef FEAT_MOUSE_NET
5225 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5226 {
5227 int mc, mr;
5228
5229 /* expect a rather limited sequence like: balancing {
5230 * \033}6,45\r
5231 * '6' is the row, 45 is the column
5232 */
5233 p = tp + slen;
5234 mr = getdigits(&p);
5235 if (*p++ != ',')
5236 return -1;
5237 mc = getdigits(&p);
5238 if (*p++ != '\r')
5239 return -1;
5240
5241 mouse_col = mc - 1;
5242 mouse_row = mr - 1;
5243 mouse_code = MOUSE_LEFT;
5244 slen += (int)(p - (tp + slen));
5245 }
5246# endif /* FEAT_MOUSE_NET */
5247# ifdef FEAT_MOUSE_JSB
5248 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5249 {
5250 int mult, val, iter, button, status;
5251
5252 /* JSBTERM Input Model
5253 * \033[0~zw uniq escape sequence
5254 * (L-x) Left button pressed - not pressed x not reporting
5255 * (M-x) Middle button pressed - not pressed x not reporting
5256 * (R-x) Right button pressed - not pressed x not reporting
5257 * (SDmdu) Single , Double click, m mouse move d button down
5258 * u button up
5259 * ### X cursor position padded to 3 digits
5260 * ### Y cursor position padded to 3 digits
5261 * (s-x) SHIFT key pressed - not pressed x not reporting
5262 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005263 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 */
5265
5266 p = tp + slen;
5267 button = mouse_code = 0;
5268 switch (*p++)
5269 {
5270 case 'L': button = 1; break;
5271 case '-': break;
5272 case 'x': break; /* ignore sequence */
5273 default: return -1; /* Unknown Result */
5274 }
5275 switch (*p++)
5276 {
5277 case 'M': button |= 2; break;
5278 case '-': break;
5279 case 'x': break; /* ignore sequence */
5280 default: return -1; /* Unknown Result */
5281 }
5282 switch (*p++)
5283 {
5284 case 'R': button |= 4; break;
5285 case '-': break;
5286 case 'x': break; /* ignore sequence */
5287 default: return -1; /* Unknown Result */
5288 }
5289 status = *p++;
5290 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5291 mult /= 10, p++)
5292 if (*p >= '0' && *p <= '9')
5293 val += (*p - '0') * mult;
5294 else
5295 return -1;
5296 mouse_col = val;
5297 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5298 mult /= 10, p++)
5299 if (*p >= '0' && *p <= '9')
5300 val += (*p - '0') * mult;
5301 else
5302 return -1;
5303 mouse_row = val;
5304 switch (*p++)
5305 {
5306 case 's': button |= 8; break; /* SHIFT key Pressed */
5307 case '-': break; /* Not Pressed */
5308 case 'x': break; /* Not Reporting */
5309 default: return -1; /* Unknown Result */
5310 }
5311 switch (*p++)
5312 {
5313 case 'c': button |= 16; break; /* CTRL key Pressed */
5314 case '-': break; /* Not Pressed */
5315 case 'x': break; /* Not Reporting */
5316 default: return -1; /* Unknown Result */
5317 }
5318 if (*p++ != '\033')
5319 return -1;
5320 if (*p++ != '\\')
5321 return -1;
5322 switch (status)
5323 {
5324 case 'D': /* Double Click */
5325 case 'S': /* Single Click */
5326 if (button & 1) mouse_code |= MOUSE_LEFT;
5327 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5328 if (button & 4) mouse_code |= MOUSE_RIGHT;
5329 if (button & 8) mouse_code |= MOUSE_SHIFT;
5330 if (button & 16) mouse_code |= MOUSE_CTRL;
5331 break;
5332 case 'm': /* Mouse move */
5333 if (button & 1) mouse_code |= MOUSE_LEFT;
5334 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5335 if (button & 4) mouse_code |= MOUSE_RIGHT;
5336 if (button & 8) mouse_code |= MOUSE_SHIFT;
5337 if (button & 16) mouse_code |= MOUSE_CTRL;
5338 if ((button & 7) != 0)
5339 {
5340 held_button = mouse_code;
5341 mouse_code |= MOUSE_DRAG;
5342 }
5343 is_drag = TRUE;
5344 showmode();
5345 break;
5346 case 'd': /* Button Down */
5347 if (button & 1) mouse_code |= MOUSE_LEFT;
5348 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5349 if (button & 4) mouse_code |= MOUSE_RIGHT;
5350 if (button & 8) mouse_code |= MOUSE_SHIFT;
5351 if (button & 16) mouse_code |= MOUSE_CTRL;
5352 break;
5353 case 'u': /* Button Up */
5354 if (button & 1)
5355 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5356 if (button & 2)
5357 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5358 if (button & 4)
5359 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5360 if (button & 8)
5361 mouse_code |= MOUSE_SHIFT;
5362 if (button & 16)
5363 mouse_code |= MOUSE_CTRL;
5364 break;
5365 default: return -1; /* Unknown Result */
5366 }
5367
5368 slen += (p - (tp + slen));
5369 }
5370# endif /* FEAT_MOUSE_JSB */
5371# ifdef FEAT_MOUSE_DEC
5372 if (key_name[0] == (int)KS_DEC_MOUSE)
5373 {
5374 /* The DEC Locator Input Model
5375 * Netterm delivers the code sequence:
5376 * \033[2;4;24;80&w (left button down)
5377 * \033[3;0;24;80&w (left button up)
5378 * \033[6;1;24;80&w (right button down)
5379 * \033[7;0;24;80&w (right button up)
5380 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5381 * Pe is the event code
5382 * Pb is the button code
5383 * Pr is the row coordinate
5384 * Pc is the column coordinate
5385 * Pp is the third coordinate (page number)
5386 * Pe, the event code indicates what event caused this report
5387 * The following event codes are defined:
5388 * 0 - request, the terminal received an explicit request
5389 * for a locator report, but the locator is unavailable
5390 * 1 - request, the terminal received an explicit request
5391 * for a locator report
5392 * 2 - left button down
5393 * 3 - left button up
5394 * 4 - middle button down
5395 * 5 - middle button up
5396 * 6 - right button down
5397 * 7 - right button up
5398 * 8 - fourth button down
5399 * 9 - fourth button up
5400 * 10 - locator outside filter rectangle
5401 * Pb, the button code, ASCII decimal 0-15 indicating which
5402 * buttons are down if any. The state of the four buttons
5403 * on the locator correspond to the low four bits of the
5404 * decimal value,
5405 * "1" means button depressed
5406 * 0 - no buttons down,
5407 * 1 - right,
5408 * 2 - middle,
5409 * 4 - left,
5410 * 8 - fourth
5411 * Pr is the row coordinate of the locator position in the page,
5412 * encoded as an ASCII decimal value.
5413 * If Pr is omitted, the locator position is undefined
5414 * (outside the terminal window for example).
5415 * Pc is the column coordinate of the locator position in the
5416 * page, encoded as an ASCII decimal value.
5417 * If Pc is omitted, the locator position is undefined
5418 * (outside the terminal window for example).
5419 * Pp is the page coordinate of the locator position
5420 * encoded as an ASCII decimal value.
5421 * The page coordinate may be omitted if the locator is on
5422 * page one (the default). We ignore it anyway.
5423 */
5424 int Pe, Pb, Pr, Pc;
5425
5426 p = tp + slen;
5427
5428 /* get event status */
5429 Pe = getdigits(&p);
5430 if (*p++ != ';')
5431 return -1;
5432
5433 /* get button status */
5434 Pb = getdigits(&p);
5435 if (*p++ != ';')
5436 return -1;
5437
5438 /* get row status */
5439 Pr = getdigits(&p);
5440 if (*p++ != ';')
5441 return -1;
5442
5443 /* get column status */
5444 Pc = getdigits(&p);
5445
5446 /* the page parameter is optional */
5447 if (*p == ';')
5448 {
5449 p++;
5450 (void)getdigits(&p);
5451 }
5452 if (*p++ != '&')
5453 return -1;
5454 if (*p++ != 'w')
5455 return -1;
5456
5457 mouse_code = 0;
5458 switch (Pe)
5459 {
5460 case 0: return -1; /* position request while unavailable */
5461 case 1: /* a response to a locator position request includes
5462 the status of all buttons */
5463 Pb &= 7; /* mask off and ignore fourth button */
5464 if (Pb & 4)
5465 mouse_code = MOUSE_LEFT;
5466 if (Pb & 2)
5467 mouse_code = MOUSE_MIDDLE;
5468 if (Pb & 1)
5469 mouse_code = MOUSE_RIGHT;
5470 if (Pb)
5471 {
5472 held_button = mouse_code;
5473 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005474 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 is_drag = TRUE;
5477 showmode();
5478 break;
5479 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005480 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 break;
5482 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5483 break;
5484 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005485 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 break;
5487 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5488 break;
5489 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005490 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 break;
5492 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5493 break;
5494 case 8: return -1; /* fourth button down */
5495 case 9: return -1; /* fourth button up */
5496 case 10: return -1; /* mouse outside of filter rectangle */
5497 default: return -1; /* should never occur */
5498 }
5499
5500 mouse_col = Pc - 1;
5501 mouse_row = Pr - 1;
5502
5503 slen += (int)(p - (tp + slen));
5504 }
5505# endif /* FEAT_MOUSE_DEC */
5506# ifdef FEAT_MOUSE_PTERM
5507 if (key_name[0] == (int)KS_PTERM_MOUSE)
5508 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005509 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
5511 p = tp + slen;
5512
5513 action = getdigits(&p);
5514 if (*p++ != ';')
5515 return -1;
5516
5517 mouse_row = getdigits(&p);
5518 if (*p++ != ';')
5519 return -1;
5520 mouse_col = getdigits(&p);
5521 if (*p++ != ';')
5522 return -1;
5523
5524 button = getdigits(&p);
5525 mouse_code = 0;
5526
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005527 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 case 4: mouse_code = MOUSE_LEFT; break;
5530 case 1: mouse_code = MOUSE_RIGHT; break;
5531 case 2: mouse_code = MOUSE_MIDDLE; break;
5532 default: return -1;
5533 }
5534
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005535 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 case 31: /* Initial press */
5538 if (*p++ != ';')
5539 return -1;
5540
5541 num_clicks = getdigits(&p); /* Not used */
5542 break;
5543
5544 case 32: /* Release */
5545 mouse_code |= MOUSE_RELEASE;
5546 break;
5547
5548 case 33: /* Drag */
5549 held_button = mouse_code;
5550 mouse_code |= MOUSE_DRAG;
5551 break;
5552
5553 default:
5554 return -1;
5555 }
5556
5557 if (*p++ != 't')
5558 return -1;
5559
5560 slen += (p - (tp + slen));
5561 }
5562# endif /* FEAT_MOUSE_PTERM */
5563
5564 /* Interpret the mouse code */
5565 current_button = (mouse_code & MOUSE_CLICK_MASK);
5566 if (current_button == MOUSE_RELEASE
5567# ifdef FEAT_MOUSE_XTERM
5568 && wheel_code == 0
5569# endif
5570 )
5571 {
5572 /*
5573 * If we get a mouse drag or release event when
5574 * there is no mouse button held down (held_button ==
5575 * MOUSE_RELEASE), produce a K_IGNORE below.
5576 * (can happen when you hold down two buttons
5577 * and then let them go, or click in the menu bar, but not
5578 * on a menu, and drag into the text).
5579 */
5580 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5581 is_drag = TRUE;
5582 current_button = held_button;
5583 }
5584 else if (wheel_code == 0)
5585 {
5586# ifdef CHECK_DOUBLE_CLICK
5587# ifdef FEAT_MOUSE_GPM
5588# ifdef FEAT_GUI
5589 /*
5590 * Only for Unix, when GUI or gpm is not active, we handle
5591 * multi-clicks here.
5592 */
5593 if (gpm_flag == 0 && !gui.in_use)
5594# else
5595 if (gpm_flag == 0)
5596# endif
5597# else
5598# ifdef FEAT_GUI
5599 if (!gui.in_use)
5600# endif
5601# endif
5602 {
5603 /*
5604 * Compute the time elapsed since the previous mouse click.
5605 */
5606 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005607 if (orig_mouse_time.tv_sec == 0)
5608 {
5609 /*
5610 * Avoid computing the difference between mouse_time
5611 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005612 * difference would be huge and would cause
5613 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005614 */
5615 timediff = p_mouset;
5616 }
5617 else
5618 {
5619 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005620 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005621 if (timediff < 0)
5622 --orig_mouse_time.tv_sec;
5623 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005624 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 orig_mouse_time = mouse_time;
5627 if (mouse_code == orig_mouse_code
5628 && timediff < p_mouset
5629 && orig_num_clicks != 4
5630 && orig_mouse_col == mouse_col
5631 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005632 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005634 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005636 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005637 /* Double click in tab pages line also works
5638 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005639 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005640 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 ++orig_num_clicks;
5642 else
5643 orig_num_clicks = 1;
5644 orig_mouse_col = mouse_col;
5645 orig_mouse_row = mouse_row;
5646 orig_topline = curwin->w_topline;
5647#ifdef FEAT_DIFF
5648 orig_topfill = curwin->w_topfill;
5649#endif
5650 }
5651# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5652 else
5653 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5654# endif
5655# else
5656 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5657# endif
5658 is_click = TRUE;
5659 orig_mouse_code = mouse_code;
5660 }
5661 if (!is_drag)
5662 held_button = mouse_code & MOUSE_CLICK_MASK;
5663
5664 /*
5665 * Translate the actual mouse event into a pseudo mouse event.
5666 * First work out what modifiers are to be used.
5667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (orig_mouse_code & MOUSE_SHIFT)
5669 modifiers |= MOD_MASK_SHIFT;
5670 if (orig_mouse_code & MOUSE_CTRL)
5671 modifiers |= MOD_MASK_CTRL;
5672 if (orig_mouse_code & MOUSE_ALT)
5673 modifiers |= MOD_MASK_ALT;
5674 if (orig_num_clicks == 2)
5675 modifiers |= MOD_MASK_2CLICK;
5676 else if (orig_num_clicks == 3)
5677 modifiers |= MOD_MASK_3CLICK;
5678 else if (orig_num_clicks == 4)
5679 modifiers |= MOD_MASK_4CLICK;
5680
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005681 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5682 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005684 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005685 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005686 {
5687 if (wheel_code & MOUSE_CTRL)
5688 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005689 if (wheel_code & MOUSE_ALT)
5690 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 key_name[1] = (wheel_code & 1)
5692 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005693 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 else
5696 key_name[1] = get_pseudo_mouse_code(current_button,
5697 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005698
5699 /* Make sure the mouse position is valid. Some terminals may
5700 * return weird values. */
5701 if (mouse_col >= Columns)
5702 mouse_col = Columns - 1;
5703 if (mouse_row >= Rows)
5704 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706#endif /* FEAT_MOUSE */
5707
5708#ifdef FEAT_GUI
5709 /*
5710 * If using the GUI, then we get menu and scrollbar events.
5711 *
5712 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5713 * four bytes which are to be taken as a pointer to the vimmenu_T
5714 * structure.
5715 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005716 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005717 * is one byte with the tab index.
5718 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5720 * by one byte representing the scrollbar number, and then four bytes
5721 * representing a long_u which is the new value of the scrollbar.
5722 *
5723 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5724 * KE_FILLER followed by four bytes representing a long_u which is the
5725 * new value of the scrollbar.
5726 */
5727# ifdef FEAT_MENU
5728 else if (key_name[0] == (int)KS_MENU)
5729 {
5730 long_u val;
5731
5732 num_bytes = get_long_from_buf(tp + slen, &val);
5733 if (num_bytes == -1)
5734 return -1;
5735 current_menu = (vimmenu_T *)val;
5736 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005737
5738 /* The menu may have been deleted right after it was used, check
5739 * for that. */
5740 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5741 {
5742 key_name[0] = KS_EXTRA;
5743 key_name[1] = (int)KE_IGNORE;
5744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005747# ifdef FEAT_GUI_TABLINE
5748 else if (key_name[0] == (int)KS_TABLINE)
5749 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005750 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005751 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5752 if (num_bytes == -1)
5753 return -1;
5754 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005755 if (current_tab == 255) /* -1 in a byte gives 255 */
5756 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005757 slen += num_bytes;
5758 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005759 else if (key_name[0] == (int)KS_TABMENU)
5760 {
5761 /* Selecting tabline tab or using its menu. */
5762 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5763 if (num_bytes == -1)
5764 return -1;
5765 current_tab = (int)bytes[0];
5766 current_tabmenu = (int)bytes[1];
5767 slen += num_bytes;
5768 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770# ifndef USE_ON_FLY_SCROLL
5771 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5772 {
5773 long_u val;
5774
5775 /* Get the last scrollbar event in the queue of the same type */
5776 j = 0;
5777 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5778 && tp[j + 2] != NUL; ++i)
5779 {
5780 j += 3;
5781 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5782 if (num_bytes == -1)
5783 break;
5784 if (i == 0)
5785 current_scrollbar = (int)bytes[0];
5786 else if (current_scrollbar != (int)bytes[0])
5787 break;
5788 j += num_bytes;
5789 num_bytes = get_long_from_buf(tp + j, &val);
5790 if (num_bytes == -1)
5791 break;
5792 scrollbar_value = val;
5793 j += num_bytes;
5794 slen = j;
5795 }
5796 if (i == 0) /* not enough characters to make one */
5797 return -1;
5798 }
5799 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5800 {
5801 long_u val;
5802
5803 /* Get the last horiz. scrollbar event in the queue */
5804 j = 0;
5805 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5806 && tp[j + 2] != NUL; ++i)
5807 {
5808 j += 3;
5809 num_bytes = get_long_from_buf(tp + j, &val);
5810 if (num_bytes == -1)
5811 break;
5812 scrollbar_value = val;
5813 j += num_bytes;
5814 slen = j;
5815 }
5816 if (i == 0) /* not enough characters to make one */
5817 return -1;
5818 }
5819# endif /* !USE_ON_FLY_SCROLL */
5820#endif /* FEAT_GUI */
5821
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005822 /*
5823 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5824 */
5825 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5826
5827 /*
5828 * Add any modifier codes to our string.
5829 */
5830 new_slen = 0; /* Length of what will replace the termcode */
5831 if (modifiers != 0)
5832 {
5833 /* Some keys have the modifier included. Need to handle that here
5834 * to make mappings work. */
5835 key = simplify_key(key, &modifiers);
5836 if (modifiers != 0)
5837 {
5838 string[new_slen++] = K_SPECIAL;
5839 string[new_slen++] = (int)KS_MODIFIER;
5840 string[new_slen++] = modifiers;
5841 }
5842 }
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005845 key_name[0] = KEY2TERMCAP0(key);
5846 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005848 {
5849 /* from ":set <M-b>=xx" */
5850#ifdef FEAT_MBYTE
5851 if (has_mbyte)
5852 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5853 else
5854#endif
5855 string[new_slen++] = key_name[1];
5856 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005857 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5858 && key_name[1] == KE_IGNORE)
5859 {
5860 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5861 * to indicate what happened. */
5862 retval = KEYLEN_REMOVED;
5863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 else
5865 {
5866 string[new_slen++] = K_SPECIAL;
5867 string[new_slen++] = key_name[0];
5868 string[new_slen++] = key_name[1];
5869 }
5870 string[new_slen] = NUL;
5871 extra = new_slen - slen;
5872 if (buf == NULL)
5873 {
5874 if (extra < 0)
5875 /* remove matched chars, taking care of noremap */
5876 del_typebuf(-extra, offset);
5877 else if (extra > 0)
5878 /* insert the extra space we need */
5879 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5880
5881 /*
5882 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5883 * typebuf.tb_buf[]!
5884 */
5885 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5886 (size_t)new_slen);
5887 }
5888 else
5889 {
5890 if (extra < 0)
5891 /* remove matched characters */
5892 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005893 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005895 {
5896 /* Insert the extra space we need. If there is insufficient
5897 * space return -1. */
5898 if (*buflen + extra + new_slen >= bufsize)
5899 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005901 (size_t)(*buflen - offset));
5902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005904 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005906 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908
Bram Moolenaar2951b772013-07-03 12:45:31 +02005909#ifdef FEAT_TERMRESPONSE
5910 LOG_TR("normal character");
5911#endif
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 return 0; /* no match found */
5914}
5915
Bram Moolenaar9377df32017-10-15 13:22:01 +02005916#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005917/*
5918 * Get the text foreground color, if known.
5919 */
5920 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005921term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005922{
5923 if (rfg_status == STATUS_GOT)
5924 {
5925 *r = fg_r;
5926 *g = fg_g;
5927 *b = fg_b;
5928 }
5929}
5930
5931/*
5932 * Get the text background color, if known.
5933 */
5934 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005935term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005936{
5937 if (rbg_status == STATUS_GOT)
5938 {
5939 *r = bg_r;
5940 *g = bg_g;
5941 *b = bg_b;
5942 }
5943}
5944#endif
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946/*
5947 * Replace any terminal code strings in from[] with the equivalent internal
5948 * vim representation. This is used for the "from" and "to" part of a
5949 * mapping, and the "to" part of a menu command.
5950 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5951 * '<'.
5952 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5953 *
5954 * The replacement is done in result[] and finally copied into allocated
5955 * memory. If this all works well *bufp is set to the allocated memory and a
5956 * pointer to it is returned. If something fails *bufp is set to NULL and from
5957 * is returned.
5958 *
5959 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5960 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5961 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5962 * instead of a CTRL-V.
5963 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005964 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005965replace_termcodes(
5966 char_u *from,
5967 char_u **bufp,
5968 int from_part,
5969 int do_lt, /* also translate <lt> */
5970 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 int i;
5973 int slen;
5974 int key;
5975 int dlen = 0;
5976 char_u *src;
5977 int do_backslash; /* backslash is a special character */
5978 int do_special; /* recognize <> key codes */
5979 int do_key_code; /* recognize raw key codes */
5980 char_u *result; /* buffer for resulting string */
5981
5982 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005983 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5985
5986 /*
5987 * Allocate space for the translation. Worst case a single character is
5988 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5989 */
5990 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5991 if (result == NULL) /* out of memory */
5992 {
5993 *bufp = NULL;
5994 return from;
5995 }
5996
5997 src = from;
5998
5999 /*
6000 * Check for #n at start only: function key n
6001 */
6002 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6003 {
6004 result[dlen++] = K_SPECIAL;
6005 result[dlen++] = 'k';
6006 if (src[1] == '0')
6007 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6008 else
6009 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6010 src += 2;
6011 }
6012
6013 /*
6014 * Copy each byte from *from to result[dlen]
6015 */
6016 while (*src != NUL)
6017 {
6018 /*
6019 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006020 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 */
6022 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6023 {
6024#ifdef FEAT_EVAL
6025 /*
6026 * Replace <SID> by K_SNR <script-nr> _.
6027 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6028 */
6029 if (STRNICMP(src, "<SID>", 5) == 0)
6030 {
6031 if (current_SID <= 0)
6032 EMSG(_(e_usingsid));
6033 else
6034 {
6035 src += 5;
6036 result[dlen++] = K_SPECIAL;
6037 result[dlen++] = (int)KS_EXTRA;
6038 result[dlen++] = (int)KE_SNR;
6039 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6040 dlen += (int)STRLEN(result + dlen);
6041 result[dlen++] = '_';
6042 continue;
6043 }
6044 }
6045#endif
6046
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006047 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 if (slen)
6049 {
6050 dlen += slen;
6051 continue;
6052 }
6053 }
6054
6055 /*
6056 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6057 * Note that this is also checked after replacing the <> form.
6058 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6059 * it could be a character in the file.
6060 */
6061 if (do_key_code)
6062 {
6063 i = find_term_bykeys(src);
6064 if (i >= 0)
6065 {
6066 result[dlen++] = K_SPECIAL;
6067 result[dlen++] = termcodes[i].name[0];
6068 result[dlen++] = termcodes[i].name[1];
6069 src += termcodes[i].len;
6070 /* If terminal code matched, continue after it. */
6071 continue;
6072 }
6073 }
6074
6075#ifdef FEAT_EVAL
6076 if (do_special)
6077 {
6078 char_u *p, *s, len;
6079
6080 /*
6081 * Replace <Leader> by the value of "mapleader".
6082 * Replace <LocalLeader> by the value of "maplocalleader".
6083 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6084 */
6085 if (STRNICMP(src, "<Leader>", 8) == 0)
6086 {
6087 len = 8;
6088 p = get_var_value((char_u *)"g:mapleader");
6089 }
6090 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6091 {
6092 len = 13;
6093 p = get_var_value((char_u *)"g:maplocalleader");
6094 }
6095 else
6096 {
6097 len = 0;
6098 p = NULL;
6099 }
6100 if (len != 0)
6101 {
6102 /* Allow up to 8 * 6 characters for "mapleader". */
6103 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6104 s = (char_u *)"\\";
6105 else
6106 s = p;
6107 while (*s != NUL)
6108 result[dlen++] = *s++;
6109 src += len;
6110 continue;
6111 }
6112 }
6113#endif
6114
6115 /*
6116 * Remove CTRL-V and ignore the next character.
6117 * For "from" side the CTRL-V at the end is included, for the "to"
6118 * part it is removed.
6119 * If 'cpoptions' does not contain 'B', also accept a backslash.
6120 */
6121 key = *src;
6122 if (key == Ctrl_V || (do_backslash && key == '\\'))
6123 {
6124 ++src; /* skip CTRL-V or backslash */
6125 if (*src == NUL)
6126 {
6127 if (from_part)
6128 result[dlen++] = key;
6129 break;
6130 }
6131 }
6132
6133#ifdef FEAT_MBYTE
6134 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006135 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
6137 {
6138 /*
6139 * If the character is K_SPECIAL, replace it with K_SPECIAL
6140 * KS_SPECIAL KE_FILLER.
6141 * If compiled with the GUI replace CSI with K_CSI.
6142 */
6143 if (*src == K_SPECIAL)
6144 {
6145 result[dlen++] = K_SPECIAL;
6146 result[dlen++] = KS_SPECIAL;
6147 result[dlen++] = KE_FILLER;
6148 }
6149# ifdef FEAT_GUI
6150 else if (*src == CSI)
6151 {
6152 result[dlen++] = K_SPECIAL;
6153 result[dlen++] = KS_EXTRA;
6154 result[dlen++] = (int)KE_CSI;
6155 }
6156# endif
6157 else
6158 result[dlen++] = *src;
6159 ++src;
6160 }
6161 }
6162 result[dlen] = NUL;
6163
6164 /*
6165 * Copy the new string to allocated memory.
6166 * If this fails, just return from.
6167 */
6168 if ((*bufp = vim_strsave(result)) != NULL)
6169 from = *bufp;
6170 vim_free(result);
6171 return from;
6172}
6173
6174/*
6175 * Find a termcode with keys 'src' (must be NUL terminated).
6176 * Return the index in termcodes[], or -1 if not found.
6177 */
6178 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006179find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006182 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 for (i = 0; i < tc_len; ++i)
6185 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006186 if (slen == termcodes[i].len
6187 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return i;
6189 }
6190 return -1;
6191}
6192
6193/*
6194 * Gather the first characters in the terminal key codes into a string.
6195 * Used to speed up check_termcode().
6196 */
6197 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006198gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 int i;
6201 int len = 0;
6202
6203#ifdef FEAT_GUI
6204 if (gui.in_use)
6205 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6206#endif
6207#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006208 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 termleader[len++] = DCS; /* the termcode response starts with DCS
6210 in 8-bit mode */
6211#endif
6212 termleader[len] = NUL;
6213
6214 for (i = 0; i < tc_len; ++i)
6215 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6216 {
6217 termleader[len++] = termcodes[i].code[0];
6218 termleader[len] = NUL;
6219 }
6220
6221 need_gather = FALSE;
6222}
6223
6224/*
6225 * Show all termcodes (for ":set termcap")
6226 * This code looks a lot like showoptions(), but is different.
6227 */
6228 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006229show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 int col;
6232 int *items;
6233 int item_count;
6234 int run;
6235 int row, rows;
6236 int cols;
6237 int i;
6238 int len;
6239
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006240#define INC3 27 /* try to make three columns */
6241#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#define GAP 2 /* spaces between columns */
6243
6244 if (tc_len == 0) /* no terminal codes (must be GUI) */
6245 return;
6246 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6247 if (items == NULL)
6248 return;
6249
6250 /* Highlight title */
6251 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6252
6253 /*
6254 * do the loop two times:
6255 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006256 * 2. display the medium items (medium length strings)
6257 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006259 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 /*
6262 * collect the items in items[]
6263 */
6264 item_count = 0;
6265 for (i = 0; i < tc_len; i++)
6266 {
6267 len = show_one_termcode(termcodes[i].name,
6268 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006269 if (len <= INC3 - GAP ? run == 1
6270 : len <= INC2 - GAP ? run == 2
6271 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 items[item_count++] = i;
6273 }
6274
6275 /*
6276 * display the items
6277 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006278 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006280 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 if (cols == 0)
6282 cols = 1;
6283 rows = (item_count + cols - 1) / cols;
6284 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006285 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 rows = item_count;
6287 for (row = 0; row < rows && !got_int; ++row)
6288 {
6289 msg_putchar('\n'); /* go to next line */
6290 if (got_int) /* 'q' typed in more */
6291 break;
6292 col = 0;
6293 for (i = row; i < item_count; i += rows)
6294 {
6295 msg_col = col; /* make columns */
6296 show_one_termcode(termcodes[items[i]].name,
6297 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006298 if (run == 2)
6299 col += INC2;
6300 else
6301 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303 out_flush();
6304 ui_breakcheck();
6305 }
6306 }
6307 vim_free(items);
6308}
6309
6310/*
6311 * Show one termcode entry.
6312 * Output goes into IObuff[]
6313 */
6314 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006315show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 char_u *p;
6318 int len;
6319
6320 if (name[0] > '~')
6321 {
6322 IObuff[0] = ' ';
6323 IObuff[1] = ' ';
6324 IObuff[2] = ' ';
6325 IObuff[3] = ' ';
6326 }
6327 else
6328 {
6329 IObuff[0] = 't';
6330 IObuff[1] = '_';
6331 IObuff[2] = name[0];
6332 IObuff[3] = name[1];
6333 }
6334 IObuff[4] = ' ';
6335
6336 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6337 if (p[1] != 't')
6338 STRCPY(IObuff + 5, p);
6339 else
6340 IObuff[5] = NUL;
6341 len = (int)STRLEN(IObuff);
6342 do
6343 IObuff[len++] = ' ';
6344 while (len < 17);
6345 IObuff[len] = NUL;
6346 if (code == NULL)
6347 len += 4;
6348 else
6349 len += vim_strsize(code);
6350
6351 if (printit)
6352 {
6353 msg_puts(IObuff);
6354 if (code == NULL)
6355 msg_puts((char_u *)"NULL");
6356 else
6357 msg_outtrans(code);
6358 }
6359 return len;
6360}
6361
6362#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6363/*
6364 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6365 * termcap codes from the terminal itself.
6366 * We get them one by one to avoid a very long response string.
6367 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006368static int xt_index_in = 0;
6369static int xt_index_out = 0;
6370
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006372req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 xt_index_out = 0;
6375 xt_index_in = 0;
6376 req_more_codes_from_term();
6377}
6378
6379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006380req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 char buf[11];
6383 int old_idx = xt_index_out;
6384
6385 /* Don't do anything when going to exit. */
6386 if (exiting)
6387 return;
6388
6389 /* Send up to 10 more requests out than we received. Avoid sending too
6390 * many, there can be a buffer overflow somewhere. */
6391 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6392 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006393# ifdef DEBUG_TERMRESPONSE
6394 char dbuf[100];
6395
6396 sprintf(dbuf, "Requesting XT %d: %s",
6397 xt_index_out, key_names[xt_index_out]);
6398 log_tr(dbuf);
6399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 sprintf(buf, "\033P+q%02x%02x\033\\",
6401 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6402 out_str_nf((char_u *)buf);
6403 ++xt_index_out;
6404 }
6405
6406 /* Send the codes out right away. */
6407 if (xt_index_out != old_idx)
6408 out_flush();
6409}
6410
6411/*
6412 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6413 * A "0" instead of the "1" indicates a code that isn't supported.
6414 * Both <name> and <string> are encoded in hex.
6415 * "code" points to the "0" or "1".
6416 */
6417 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006418got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420#define XT_LEN 100
6421 char_u name[3];
6422 char_u str[XT_LEN];
6423 int i;
6424 int j = 0;
6425 int c;
6426
6427 /* A '1' means the code is supported, a '0' means it isn't.
6428 * When half the length is > XT_LEN we can't use it.
6429 * Our names are currently all 2 characters. */
6430 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6431 {
6432 /* Get the name from the response and find it in the table. */
6433 name[0] = hexhex2nr(code + 3);
6434 name[1] = hexhex2nr(code + 5);
6435 name[2] = NUL;
6436 for (i = 0; key_names[i] != NULL; ++i)
6437 {
6438 if (STRCMP(key_names[i], name) == 0)
6439 {
6440 xt_index_in = i;
6441 break;
6442 }
6443 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006444# ifdef DEBUG_TERMRESPONSE
6445 {
6446 char buf[100];
6447
6448 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6449 log_tr(buf);
6450 }
6451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (key_names[i] != NULL)
6453 {
6454 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6455 str[j++] = c;
6456 str[j] = NUL;
6457 if (name[0] == 'C' && name[1] == 'o')
6458 {
6459 /* Color count is not a key code. */
6460 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006461 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 }
6463 else
6464 {
6465 /* First delete any existing entry with the same code. */
6466 i = find_term_bykeys(str);
6467 if (i >= 0)
6468 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006469 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
6471 }
6472 }
6473
6474 /* May request more codes now that we received one. */
6475 ++xt_index_in;
6476 req_more_codes_from_term();
6477}
6478
6479/*
6480 * Check if there are any unanswered requests and deal with them.
6481 * This is called before starting an external program or getting direct
6482 * keyboard input. We don't want responses to be send to that program or
6483 * handled as typed text.
6484 */
6485 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006486check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 int c;
6489
6490 /* If no codes requested or all are answered, no need to wait. */
6491 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6492 return;
6493
6494 /* Vgetc() will check for and handle any response.
6495 * Keep calling vpeekc() until we don't get any responses. */
6496 ++no_mapping;
6497 ++allow_keys;
6498 for (;;)
6499 {
6500 c = vpeekc();
6501 if (c == NUL) /* nothing available */
6502 break;
6503
6504 /* If a response is recognized it's replaced with K_IGNORE, must read
6505 * it from the input stream. If there is no K_IGNORE we can't do
6506 * anything, break here (there might be some responses further on, but
6507 * we don't want to throw away any typed chars). */
6508 if (c != K_SPECIAL && c != K_IGNORE)
6509 break;
6510 c = vgetc();
6511 if (c != K_IGNORE)
6512 {
6513 vungetc(c);
6514 break;
6515 }
6516 }
6517 --no_mapping;
6518 --allow_keys;
6519}
6520#endif
6521
6522#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6523/*
6524 * Translate an internal mapping/abbreviation representation into the
6525 * corresponding external one recognized by :map/:abbrev commands;
6526 * respects the current B/k/< settings of 'cpoption'.
6527 *
6528 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006529 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 *
6531 * It uses a growarray to build the translation string since the
6532 * latter can be wider than the original description. The caller has to
6533 * free the string afterwards.
6534 *
6535 * Returns NULL when there is a problem.
6536 */
6537 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006538translate_mapping(
6539 char_u *str,
6540 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 garray_T ga;
6543 int c;
6544 int modifiers;
6545 int cpo_bslash;
6546 int cpo_special;
6547 int cpo_keycode;
6548
6549 ga_init(&ga);
6550 ga.ga_itemsize = 1;
6551 ga.ga_growsize = 40;
6552
6553 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6554 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6555 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6556
6557 for (; *str; ++str)
6558 {
6559 c = *str;
6560 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6561 {
6562 modifiers = 0;
6563 if (str[1] == KS_MODIFIER)
6564 {
6565 str++;
6566 modifiers = *++str;
6567 c = *++str;
6568 }
6569 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6570 {
6571 int i;
6572
6573 /* try to find special key in termcodes */
6574 for (i = 0; i < tc_len; ++i)
6575 if (termcodes[i].name[0] == str[1]
6576 && termcodes[i].name[1] == str[2])
6577 break;
6578 if (i < tc_len)
6579 {
6580 ga_concat(&ga, termcodes[i].code);
6581 str += 2;
6582 continue; /* for (str) */
6583 }
6584 }
6585 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6586 {
6587 if (expmap && cpo_special)
6588 {
6589 ga_clear(&ga);
6590 return NULL;
6591 }
6592 c = TO_SPECIAL(str[1], str[2]);
6593 if (c == K_ZERO) /* display <Nul> as ^@ */
6594 c = NUL;
6595 str += 2;
6596 }
6597 if (IS_SPECIAL(c) || modifiers) /* special key */
6598 {
6599 if (expmap && cpo_special)
6600 {
6601 ga_clear(&ga);
6602 return NULL;
6603 }
6604 ga_concat(&ga, get_special_key_name(c, modifiers));
6605 continue; /* for (str) */
6606 }
6607 }
6608 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6609 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6610 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6611 if (c)
6612 ga_append(&ga, c);
6613 }
6614 ga_append(&ga, NUL);
6615 return (char_u *)(ga.ga_data);
6616}
6617#endif
6618
6619#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6620static char ksme_str[20];
6621static char ksmr_str[20];
6622static char ksmd_str[20];
6623
6624/*
6625 * For Win32 console: update termcap codes for existing console attributes.
6626 */
6627 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006628update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 struct builtin_term *p;
6631
6632 p = find_builtin_term(DEFAULT_TERM);
6633 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6634 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6635 attr | 0x08); /* FOREGROUND_INTENSITY */
6636 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6637 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6638
6639 while (p->bt_string != NULL)
6640 {
6641 if (p->bt_entry == (int)KS_ME)
6642 p->bt_string = &ksme_str[0];
6643 else if (p->bt_entry == (int)KS_MR)
6644 p->bt_string = &ksmr_str[0];
6645 else if (p->bt_entry == (int)KS_MD)
6646 p->bt_string = &ksmd_str[0];
6647 ++p;
6648 }
6649}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006650
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006651# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006652struct ks_tbl_s
6653{
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006654 int code; /* value of KS_ */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006655 char *vtp; /* code in vtp mode */
6656 char *buf; /* buffer in non-vtp mode */
6657 char *vbuf; /* buffer in vtp mode */
6658};
6659
6660static struct ks_tbl_s ks_tbl[] =
6661{
6662 {(int)KS_ME, "\033|0m" }, /* normal */
6663 {(int)KS_MR, "\033|7m" }, /* reverse */
6664 {(int)KS_MD, "\033|1m" }, /* bold */
6665 {(int)KS_SO, "\033|91m"}, /* standout: bright red text */
6666 {(int)KS_SE, "\033|39m"}, /* standout end: default color */
6667 {(int)KS_CZH, "\033|95m"}, /* italic: bright magenta text */
6668 {(int)KS_CZR, "\033|0m",}, /* italic end */
6669 {(int)KS_US, "\033|4m",}, /* underscore */
6670 {(int)KS_UE, "\033|24m"}, /* underscore end */
6671 {(int)KS_NAME, NULL}
6672};
6673
6674 static struct builtin_term *
6675find_first_tcap(
6676 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006677 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006678{
6679 struct builtin_term *p;
6680
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006681 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006682 if (p->bt_entry == code)
6683 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006684 return NULL;
6685}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006686# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006687
6688/*
6689 * For Win32 console: replace the sequence immediately after termguicolors.
6690 */
6691 void
6692swap_tcap(void)
6693{
6694# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006695 static int init_done = FALSE;
6696 static int last_tgc;
6697 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006698 struct builtin_term *bt;
6699
6700 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006701 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006702 {
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006703 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006704 {
6705 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006706 if (bt != NULL)
6707 {
6708 ks->buf = bt->bt_string;
6709 ks->vbuf = ks->vtp;
6710 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006711 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006712 init_done = TRUE;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006713 last_tgc = p_tgc;
6714 return;
6715 }
6716
6717 if (last_tgc != p_tgc)
6718 {
6719 if (p_tgc)
6720 {
6721 /* switch to special character sequence */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006722 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006723 {
6724 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006725 if (bt != NULL)
6726 {
6727 ks->buf = bt->bt_string;
6728 bt->bt_string = ks->vbuf;
6729 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006730 }
6731 }
6732 else
6733 {
6734 /* switch to index color */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006735 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006736 {
6737 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006738 if (bt != NULL)
6739 {
6740 ks->vbuf = bt->bt_string;
6741 bt->bt_string = ks->buf;
6742 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006743 }
6744 }
6745
6746 last_tgc = p_tgc;
6747 }
6748# endif
6749}
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006752
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006753#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006754 static int
6755hex_digit(int c)
6756{
6757 if (isdigit(c))
6758 return c - '0';
6759 c = TOLOWER_ASC(c);
6760 if (c >= 'a' && c <= 'f')
6761 return c - 'a' + 10;
6762 return 0x1ffffff;
6763}
6764
6765 guicolor_T
6766gui_get_color_cmn(char_u *name)
6767{
Bram Moolenaar28726652017-01-06 18:16:19 +01006768 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6769 * values as used by the MS-Windows GDI api. It should be used only for
6770 * MS-Windows GDI builds. */
6771# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6772# undef RGB
6773# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006774# ifndef RGB
6775# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6776# endif
6777# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006778 FILE *fd;
6779 char line[LINE_LEN];
6780 char_u *fname;
6781 int r, g, b, i;
6782 guicolor_T color;
6783
6784 struct rgbcolor_table_S {
6785 char_u *color_name;
6786 guicolor_T color;
6787 };
6788
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006789 /* Only non X11 colors (not present in rgb.txt) and colors in
6790 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006791 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006792 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6793 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6794 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6795 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6796 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6797 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6798 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6799 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6800 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6801 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6802 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6803 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6804 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006805 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6806 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006807 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006808 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006809 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006810 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6811 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6812 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6813 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6814 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6815 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6816 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6817 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6818 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006819 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006820 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006821 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6822 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006823 };
6824
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006825 static struct rgbcolor_table_S *colornames_table;
6826 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006827
6828 if (name[0] == '#' && STRLEN(name) == 7)
6829 {
6830 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006831 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006832 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6833 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6834 if (color > 0xffffff)
6835 return INVALCOLOR;
6836 return color;
6837 }
6838
6839 /* Check if the name is one of the colors we know */
6840 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6841 if (STRICMP(name, rgb_table[i].color_name) == 0)
6842 return rgb_table[i].color;
6843
6844 /*
6845 * Last attempt. Look in the file "$VIM/rgb.txt".
6846 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006847 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006848 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006849 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006850
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006851 /* colornames_table not yet initialized */
6852 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6853 if (fname == NULL)
6854 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006855
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006856 fd = fopen((char *)fname, "rt");
6857 vim_free(fname);
6858 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006859 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006860 if (p_verbose > 1)
6861 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6862 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006863 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006864
6865 for (counting = 1; counting >= 0; --counting)
6866 {
6867 if (!counting)
6868 {
6869 colornames_table = (struct rgbcolor_table_S *)alloc(
6870 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6871 if (colornames_table == NULL)
6872 {
6873 fclose(fd);
6874 return INVALCOLOR;
6875 }
6876 rewind(fd);
6877 }
6878 size = 0;
6879
6880 while (!feof(fd))
6881 {
6882 size_t len;
6883 int pos;
6884
6885 ignoredp = fgets(line, LINE_LEN, fd);
6886 len = strlen(line);
6887
6888 if (len <= 1 || line[len - 1] != '\n')
6889 continue;
6890
6891 line[len - 1] = '\0';
6892
6893 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6894 if (i != 3)
6895 continue;
6896
6897 if (!counting)
6898 {
6899 char_u *s = vim_strsave((char_u *)line + pos);
6900
6901 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006902 {
6903 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006904 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006905 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006906 colornames_table[size].color_name = s;
6907 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6908 }
6909 size++;
6910 }
6911 }
6912 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006913 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006914
6915 for (i = 0; i < size; i++)
6916 if (STRICMP(name, colornames_table[i].color_name) == 0)
6917 return colornames_table[i].color;
6918
Bram Moolenaarab302212016-04-26 20:59:29 +02006919 return INVALCOLOR;
6920}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006921
6922 guicolor_T
6923gui_get_rgb_color_cmn(int r, int g, int b)
6924{
6925 guicolor_T color = RGB(r, g, b);
6926
6927 if (color > 0xffffff)
6928 return INVALCOLOR;
6929 return color;
6930}
Bram Moolenaarab302212016-04-26 20:59:29 +02006931#endif