blob: b063d9189ca820ee31e54f2a0883c6d8e0e46231 [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 Moolenaar071d4272004-06-13 20:20:40 +0000146# endif
147
148/*
149 * Don't declare these variables if termcap.h contains them.
150 * Autoconf checks if these variables should be declared extern (not all
151 * systems have them).
152 * Some versions define ospeed to be speed_t, but that is incompatible with
153 * BSD, where ospeed is short and speed_t is long.
154 */
155# ifndef HAVE_OSPEED
156# ifdef OSPEED_EXTERN
157extern short ospeed;
158# else
159short ospeed;
160# endif
161# endif
162# ifndef HAVE_UP_BC_PC
163# ifdef UP_BC_PC_EXTERN
164extern char *UP, *BC, PC;
165# else
166char *UP, *BC, PC;
167# endif
168# endif
169
170# define TGETSTR(s, p) vim_tgetstr((s), (p))
171# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif /* HAVE_TGETENT */
174
175static int detected_8bit = FALSE; /* detected 8-bit terminal */
176
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200177#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200178/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200179 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200180static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200181
182/* The blink flag from the style response may be inverted from the actual
183 * blinking state, xterm XORs the flags. */
184static int initial_cursor_shape_blink = FALSE;
185
186/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200187static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200188#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200189
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000190static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192
193#if defined(FEAT_GUI)
194/*
195 * GUI pseudo term-cap.
196 */
197 {(int)KS_NAME, "gui"},
198 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
199 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
200# ifdef TERMINFO
201 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
202# else
203 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
204# endif
205 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
206# ifdef TERMINFO
207 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
208 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# else
211 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
212 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# endif
215 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
216 /* attributes switched on with 'h', off with * 'H' */
217 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
218 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
219 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
220 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
221 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
222 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
223 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000224 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
225 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
227 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
229 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
230 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
231 {(int)KS_MS, "y"},
232 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100233 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 {(int)KS_LE, "\b"}, /* cursor-left = BS */
235 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
236# ifdef TERMINFO
237 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
238# else
239 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
240# endif
241 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000242 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
244
245#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
248/*
249 * Amiga console window, default for Amiga
250 */
251 {(int)KS_NAME, "amiga"},
252 {(int)KS_CE, "\033[K"},
253 {(int)KS_CD, "\033[J"},
254 {(int)KS_AL, "\033[L"},
255# ifdef TERMINFO
256 {(int)KS_CAL, "\033[%p1%dL"},
257# else
258 {(int)KS_CAL, "\033[%dL"},
259# endif
260 {(int)KS_DL, "\033[M"},
261# ifdef TERMINFO
262 {(int)KS_CDL, "\033[%p1%dM"},
263# else
264 {(int)KS_CDL, "\033[%dM"},
265# endif
266 {(int)KS_CL, "\014"},
267 {(int)KS_VI, "\033[0 p"},
268 {(int)KS_VE, "\033[1 p"},
269 {(int)KS_ME, "\033[0m"},
270 {(int)KS_MR, "\033[7m"},
271 {(int)KS_MD, "\033[1m"},
272 {(int)KS_SE, "\033[0m"},
273 {(int)KS_SO, "\033[33m"},
274 {(int)KS_US, "\033[4m"},
275 {(int)KS_UE, "\033[0m"},
276 {(int)KS_CZH, "\033[3m"},
277 {(int)KS_CZR, "\033[0m"},
278#if defined(__MORPHOS__) || defined(__AROS__)
279 {(int)KS_CCO, "8"}, /* allow 8 colors */
280# ifdef TERMINFO
281 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
282 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
283# else
284 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
285 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
286# endif
287 {(int)KS_OP, "\033[m"}, /* reset colors */
288#endif
289 {(int)KS_MS, "y"},
290 {(int)KS_UT, "y"}, /* guessed */
291 {(int)KS_LE, "\b"},
292# ifdef TERMINFO
293 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
294# else
295 {(int)KS_CM, "\033[%i%d;%dH"},
296# endif
297#if defined(__MORPHOS__)
298 {(int)KS_SR, "\033M"},
299#endif
300# ifdef TERMINFO
301 {(int)KS_CRI, "\033[%p1%dC"},
302# else
303 {(int)KS_CRI, "\033[%dC"},
304# endif
305 {K_UP, "\233A"},
306 {K_DOWN, "\233B"},
307 {K_LEFT, "\233D"},
308 {K_RIGHT, "\233C"},
309 {K_S_UP, "\233T"},
310 {K_S_DOWN, "\233S"},
311 {K_S_LEFT, "\233 A"},
312 {K_S_RIGHT, "\233 @"},
313 {K_S_TAB, "\233Z"},
314 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
315 {K_F2, "\233\061~"},
316 {K_F3, "\233\062~"},
317 {K_F4, "\233\063~"},
318 {K_F5, "\233\064~"},
319 {K_F6, "\233\065~"},
320 {K_F7, "\233\066~"},
321 {K_F8, "\233\067~"},
322 {K_F9, "\233\070~"},
323 {K_F10, "\233\071~"},
324 {K_S_F1, "\233\061\060~"},
325 {K_S_F2, "\233\061\061~"},
326 {K_S_F3, "\233\061\062~"},
327 {K_S_F4, "\233\061\063~"},
328 {K_S_F5, "\233\061\064~"},
329 {K_S_F6, "\233\061\065~"},
330 {K_S_F7, "\233\061\066~"},
331 {K_S_F8, "\233\061\067~"},
332 {K_S_F9, "\233\061\070~"},
333 {K_S_F10, "\233\061\071~"},
334 {K_HELP, "\233?~"},
335 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
336 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
337 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
338 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
339 {K_END, "\233\064\065~"}, /* 101 key keyboard */
340
341 {BT_EXTRA_KEYS, ""},
342 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
343 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
344 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
345# endif
346
347# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
348/*
349 * almost standard ANSI terminal, default for bebox
350 */
351 {(int)KS_NAME, "beos-ansi"},
352 {(int)KS_CE, "\033[K"},
353 {(int)KS_CD, "\033[J"},
354 {(int)KS_AL, "\033[L"},
355# ifdef TERMINFO
356 {(int)KS_CAL, "\033[%p1%dL"},
357# else
358 {(int)KS_CAL, "\033[%dL"},
359# endif
360 {(int)KS_DL, "\033[M"},
361# ifdef TERMINFO
362 {(int)KS_CDL, "\033[%p1%dM"},
363# else
364 {(int)KS_CDL, "\033[%dM"},
365# endif
366#ifdef BEOS_PR_OR_BETTER
367# ifdef TERMINFO
368 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
369# else
370 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
371# endif
372#endif
373 {(int)KS_CL, "\033[H\033[2J"},
374#ifdef notyet
375 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
376 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
377#endif
378 {(int)KS_ME, "\033[m"}, /* normal mode */
379 {(int)KS_MR, "\033[7m"}, /* reverse */
380 {(int)KS_MD, "\033[1m"}, /* bold */
381 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
382 {(int)KS_SE, "\033[m"}, /* standout end */
383 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
384 {(int)KS_CZR, "\033[m"}, /* italic end */
385 {(int)KS_US, "\033[4m"}, /* underscore mode */
386 {(int)KS_UE, "\033[m"}, /* underscore end */
387 {(int)KS_CCO, "8"}, /* allow 8 colors */
388# ifdef TERMINFO
389 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
390 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
391# else
392 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
393 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
394# endif
395 {(int)KS_OP, "\033[m"}, /* reset colors */
396 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
397 {(int)KS_UT, "y"}, /* guessed */
398 {(int)KS_LE, "\b"},
399# ifdef TERMINFO
400 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
401# else
402 {(int)KS_CM, "\033[%i%d;%dH"},
403# endif
404 {(int)KS_SR, "\033M"},
405# ifdef TERMINFO
406 {(int)KS_CRI, "\033[%p1%dC"},
407# else
408 {(int)KS_CRI, "\033[%dC"},
409# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200410# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 {K_UP, "\033[A"},
415 {K_DOWN, "\033[B"},
416 {K_LEFT, "\033[D"},
417 {K_RIGHT, "\033[C"},
418# endif
419
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200420# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421/*
422 * standard ANSI terminal, default for unix
423 */
424 {(int)KS_NAME, "ansi"},
425 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
426 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
427# ifdef TERMINFO
428 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
429# else
430 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
431# endif
432 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
433# ifdef TERMINFO
434 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
435# else
436 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
437# endif
438 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
439 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
440 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
441 {(int)KS_MS, "y"},
442 {(int)KS_UT, "y"}, /* guessed */
443 {(int)KS_LE, "\b"},
444# ifdef TERMINFO
445 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
446# else
447 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
448# endif
449# ifdef TERMINFO
450 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
451# else
452 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
453# endif
454# endif
455
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200456# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457/*
458 * These codes are valid when nansi.sys or equivalent has been installed.
459 * Function keys on a PC are preceded with a NUL. These are converted into
460 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
461 * CTRL-arrow is used instead of SHIFT-arrow.
462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {(int)KS_NAME, "pcansi"},
464 {(int)KS_DL, "\033[M"},
465 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_CE, "\033[K"},
467 {(int)KS_CL, "\033[2J"},
468 {(int)KS_ME, "\033[0m"},
469 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
470 {(int)KS_MD, "\033[1m"}, /* bold: white text */
471 {(int)KS_SE, "\033[0m"}, /* standout end */
472 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
473 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
474 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
475 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
476 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
477 {(int)KS_CCO, "8"}, /* allow 8 colors */
478# ifdef TERMINFO
479 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
480 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
481# else
482 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
483 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
484# endif
485 {(int)KS_OP, "\033[0m"}, /* reset colors */
486 {(int)KS_MS, "y"},
487 {(int)KS_UT, "y"}, /* guessed */
488 {(int)KS_LE, "\b"},
489# ifdef TERMINFO
490 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
491# else
492 {(int)KS_CM, "\033[%i%d;%dH"},
493# endif
494# ifdef TERMINFO
495 {(int)KS_CRI, "\033[%p1%dC"},
496# else
497 {(int)KS_CRI, "\033[%dC"},
498# endif
499 {K_UP, "\316H"},
500 {K_DOWN, "\316P"},
501 {K_LEFT, "\316K"},
502 {K_RIGHT, "\316M"},
503 {K_S_LEFT, "\316s"},
504 {K_S_RIGHT, "\316t"},
505 {K_F1, "\316;"},
506 {K_F2, "\316<"},
507 {K_F3, "\316="},
508 {K_F4, "\316>"},
509 {K_F5, "\316?"},
510 {K_F6, "\316@"},
511 {K_F7, "\316A"},
512 {K_F8, "\316B"},
513 {K_F9, "\316C"},
514 {K_F10, "\316D"},
515 {K_F11, "\316\205"}, /* guessed */
516 {K_F12, "\316\206"}, /* guessed */
517 {K_S_F1, "\316T"},
518 {K_S_F2, "\316U"},
519 {K_S_F3, "\316V"},
520 {K_S_F4, "\316W"},
521 {K_S_F5, "\316X"},
522 {K_S_F6, "\316Y"},
523 {K_S_F7, "\316Z"},
524 {K_S_F8, "\316["},
525 {K_S_F9, "\316\\"},
526 {K_S_F10, "\316]"},
527 {K_S_F11, "\316\207"}, /* guessed */
528 {K_S_F12, "\316\210"}, /* guessed */
529 {K_INS, "\316R"},
530 {K_DEL, "\316S"},
531 {K_HOME, "\316G"},
532 {K_END, "\316O"},
533 {K_PAGEDOWN, "\316Q"},
534 {K_PAGEUP, "\316I"},
535# endif
536
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200537# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538/*
539 * These codes are valid for the Win32 Console . The entries that start with
540 * ESC | are translated into console calls in os_win32.c. The function keys
541 * are also translated in os_win32.c.
542 */
543 {(int)KS_NAME, "win32"},
544 {(int)KS_CE, "\033|K"}, /* clear to end of line */
545 {(int)KS_AL, "\033|L"}, /* add new blank line */
546# ifdef TERMINFO
547 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
548# else
549 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
550# endif
551 {(int)KS_DL, "\033|M"}, /* delete line */
552# ifdef TERMINFO
553 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
554# else
555 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
556# endif
557 {(int)KS_CL, "\033|J"}, /* clear screen */
558 {(int)KS_CD, "\033|j"}, /* clear to end of display */
559 {(int)KS_VI, "\033|v"}, /* cursor invisible */
560 {(int)KS_VE, "\033|V"}, /* cursor visible */
561
562 {(int)KS_ME, "\033|0m"}, /* normal */
563 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
564 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
565#if 1
566 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
567 {(int)KS_SE, "\033|0m"}, /* standout end */
568#else
569 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
570 {(int)KS_SE, "\033|f"}, /* standout end */
571#endif
572 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
573 {(int)KS_CZR, "\033|0m"}, /* italic end */
574 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
575 {(int)KS_UE, "\033|0m"}, /* underscore end */
576 {(int)KS_CCO, "16"}, /* allow 16 colors */
577# ifdef TERMINFO
578 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
579 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
580# else
581 {(int)KS_CAB, "\033|%db"}, /* set background color */
582 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
583# endif
584
585 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
586 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100587 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {(int)KS_LE, "\b"},
589# ifdef TERMINFO
590 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
591# else
592 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
593# endif
594 {(int)KS_VB, "\033|B"}, /* visual bell */
595 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
596 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
597# ifdef TERMINFO
598 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
599# else
600 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
601# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100602# ifdef FEAT_TERMGUICOLORS
603 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
604 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607 {K_UP, "\316H"},
608 {K_DOWN, "\316P"},
609 {K_LEFT, "\316K"},
610 {K_RIGHT, "\316M"},
611 {K_S_UP, "\316\304"},
612 {K_S_DOWN, "\316\317"},
613 {K_S_LEFT, "\316\311"},
614 {K_C_LEFT, "\316s"},
615 {K_S_RIGHT, "\316\313"},
616 {K_C_RIGHT, "\316t"},
617 {K_S_TAB, "\316\017"},
618 {K_F1, "\316;"},
619 {K_F2, "\316<"},
620 {K_F3, "\316="},
621 {K_F4, "\316>"},
622 {K_F5, "\316?"},
623 {K_F6, "\316@"},
624 {K_F7, "\316A"},
625 {K_F8, "\316B"},
626 {K_F9, "\316C"},
627 {K_F10, "\316D"},
628 {K_F11, "\316\205"},
629 {K_F12, "\316\206"},
630 {K_S_F1, "\316T"},
631 {K_S_F2, "\316U"},
632 {K_S_F3, "\316V"},
633 {K_S_F4, "\316W"},
634 {K_S_F5, "\316X"},
635 {K_S_F6, "\316Y"},
636 {K_S_F7, "\316Z"},
637 {K_S_F8, "\316["},
638 {K_S_F9, "\316\\"},
639 {K_S_F10, "\316]"},
640 {K_S_F11, "\316\207"},
641 {K_S_F12, "\316\210"},
642 {K_INS, "\316R"},
643 {K_DEL, "\316S"},
644 {K_HOME, "\316G"},
645 {K_S_HOME, "\316\302"},
646 {K_C_HOME, "\316w"},
647 {K_END, "\316O"},
648 {K_S_END, "\316\315"},
649 {K_C_END, "\316u"},
650 {K_PAGEDOWN, "\316Q"},
651 {K_PAGEUP, "\316I"},
652 {K_KPLUS, "\316N"},
653 {K_KMINUS, "\316J"},
654 {K_KMULTIPLY, "\316\067"},
655 {K_K0, "\316\332"},
656 {K_K1, "\316\336"},
657 {K_K2, "\316\342"},
658 {K_K3, "\316\346"},
659 {K_K4, "\316\352"},
660 {K_K5, "\316\356"},
661 {K_K6, "\316\362"},
662 {K_K7, "\316\366"},
663 {K_K8, "\316\372"},
664 {K_K9, "\316\376"},
665# endif
666
667# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
668/*
669 * VT320 is working as an ANSI terminal compatible DEC terminal.
670 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
671 * Note: K_F1...K_F5 are for internal use, should not be defined.
672 * TODO:- rewrite ESC[ codes to CSI
673 * - keyboard languages (CSI ? 26 n)
674 */
675 {(int)KS_NAME, "vt320"},
676 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
677 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
678# ifdef TERMINFO
679 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
680# else
681 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
682# endif
683 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
684# ifdef TERMINFO
685 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
686# else
687 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
688# endif
689 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000690 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
691 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
693 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
695 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
696 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
697 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
698 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
699 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
700 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
701 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
702 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
703 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {(int)KS_MS, "y"},
705 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100706 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_LE, "\b"},
708# ifdef TERMINFO
709 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
710 ESC_STR "[%i%p1%d;%p2%dH")},
711# else
712 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
713# endif
714# ifdef TERMINFO
715 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
716# else
717 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
718# endif
719 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
720 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
721 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
722 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000723 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
724 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
725 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
726 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
727 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
729 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
730 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
731 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
732 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000733 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
735 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
736 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
737 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
738 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
739 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
740 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
741 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
742 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
743 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
744 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
745 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
746 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
747 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
748 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
749 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
750 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
751 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
752 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
753 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
754 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
755# endif
756
757# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
758/*
759 * Ordinary vt52
760 */
761 {(int)KS_NAME, "vt52"},
762 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
763 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100764# ifdef TERMINFO
765 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
766 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
767# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100771 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
773 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {K_UP, IF_EB("\033A", ESC_STR "A")},
775 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
776 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
777 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100778 {K_F1, IF_EB("\033P", ESC_STR "P")},
779 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
780 {K_F3, IF_EB("\033R", ESC_STR "R")},
781# ifdef __MINT__
782 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
783 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
784 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
785 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
786 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
788 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
789 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
790 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {K_F4, IF_EB("\033S", ESC_STR "S")},
792 {K_F5, IF_EB("\033T", ESC_STR "T")},
793 {K_F6, IF_EB("\033U", ESC_STR "U")},
794 {K_F7, IF_EB("\033V", ESC_STR "V")},
795 {K_F8, IF_EB("\033W", ESC_STR "W")},
796 {K_F9, IF_EB("\033X", ESC_STR "X")},
797 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
798 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
799 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
800 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
801 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
802 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
803 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
804 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
805 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
806 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
807 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
808 {K_INS, IF_EB("\033I", ESC_STR "I")},
809 {K_HOME, IF_EB("\033E", ESC_STR "E")},
810 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
811 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
812# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {(int)KS_MS, "y"},
815# endif
816# endif
817
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200818# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200819 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
821 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
822# ifdef TERMINFO
823 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
824# else
825 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
826# endif
827 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
828# ifdef TERMINFO
829 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
830# else
831 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
832# endif
833# ifdef TERMINFO
834 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
835 ESC_STR "[%i%p1%d;%p2%dr")},
836# else
837 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
838# endif
839 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
840 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
841 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
842 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
843 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
844 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
845 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200846 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
847 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {(int)KS_MS, "y"},
849 {(int)KS_UT, "y"},
850 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200851 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
852 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200853 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200854 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200855# ifdef TERMINFO
856 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
857# else
858 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
859# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200860 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200861 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862# ifdef TERMINFO
863 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
864 ESC_STR "[%i%p1%d;%p2%dH")},
865# else
866 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
867# endif
868 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
869# ifdef TERMINFO
870 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
871# else
872 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
873# endif
874 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
875 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
876# ifdef FEAT_XTERM_SAVE
877 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
878 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
879 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
880# endif
881 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
882 {(int)KS_CIE, "\007"},
883 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
884 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200885 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
886 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887# ifdef TERMINFO
888 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
889 ESC_STR "[8;%p1%d;%p2%dt")},
890 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
891 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200892 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893# else
894 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
895 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200896 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897# endif
898 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200899 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200900 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100901 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200902# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200903 /* These are printf strings, not terminal codes. */
904 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
905 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
906# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100907 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
908 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000909
910 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
911 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
912 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
913 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
914 /* An extra set of cursor keys for vt100 mode */
915 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
916 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
917 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
918 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000920 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
921 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
922 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
923 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000924 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
925 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
926 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
927 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
928 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
929 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
930 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
931 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
932 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
933 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
934 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
935 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000937 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
938 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
939 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
940 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000941 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
942 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000943 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000945 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000946 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000947 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
948 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000949 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000950 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000951 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000952 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
953 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100954 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
955 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
956 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
957 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
958 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
959 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
960 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
961 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
962 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000965 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
966 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
967 /* F14 and F15 are missing, because they send the same codes as the undo
968 * and help key, although they don't work on all keyboards. */
969 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
970 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
971 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
972 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
973 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
974
975 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
976 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
977 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
978 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
979 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
980 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
981 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
982 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
983 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
984 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
985
986 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
987 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
988 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
989 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
990 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
991 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
992 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993# endif
994
995# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
996/*
997 * iris-ansi for Silicon Graphics machines.
998 */
999 {(int)KS_NAME, "iris-ansi"},
1000 {(int)KS_CE, "\033[K"},
1001 {(int)KS_CD, "\033[J"},
1002 {(int)KS_AL, "\033[L"},
1003# ifdef TERMINFO
1004 {(int)KS_CAL, "\033[%p1%dL"},
1005# else
1006 {(int)KS_CAL, "\033[%dL"},
1007# endif
1008 {(int)KS_DL, "\033[M"},
1009# ifdef TERMINFO
1010 {(int)KS_CDL, "\033[%p1%dM"},
1011# else
1012 {(int)KS_CDL, "\033[%dM"},
1013# endif
1014#if 0 /* The scroll region is not working as Vim expects. */
1015# ifdef TERMINFO
1016 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1017# else
1018 {(int)KS_CS, "\033[%i%d;%dr"},
1019# endif
1020#endif
1021 {(int)KS_CL, "\033[H\033[2J"},
1022 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1023 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1024 {(int)KS_TI, "\033[=6h"},
1025 {(int)KS_TE, "\033[=6l"},
1026 {(int)KS_SE, "\033[21;27m"},
1027 {(int)KS_SO, "\033[1;7m"},
1028 {(int)KS_ME, "\033[m"},
1029 {(int)KS_MR, "\033[7m"},
1030 {(int)KS_MD, "\033[1m"},
1031 {(int)KS_CCO, "8"}, /* allow 8 colors */
1032 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1033 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1034 {(int)KS_US, "\033[4m"}, /* underline on */
1035 {(int)KS_UE, "\033[24m"}, /* underline off */
1036# ifdef TERMINFO
1037 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1038 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1039 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1040 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1041# else
1042 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1043 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1044 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1045 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1046# endif
1047 {(int)KS_MS, "y"}, /* guessed */
1048 {(int)KS_UT, "y"}, /* guessed */
1049 {(int)KS_LE, "\b"},
1050# ifdef TERMINFO
1051 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1052# else
1053 {(int)KS_CM, "\033[%i%d;%dH"},
1054# endif
1055 {(int)KS_SR, "\033M"},
1056# ifdef TERMINFO
1057 {(int)KS_CRI, "\033[%p1%dC"},
1058# else
1059 {(int)KS_CRI, "\033[%dC"},
1060# endif
1061 {(int)KS_CIS, "\033P3.y"},
1062 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1063 {(int)KS_TS, "\033P1.y"},
1064 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1065# ifdef TERMINFO
1066 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1067 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1068# else
1069 {(int)KS_CWS, "\033[203;%d;%d/y"},
1070 {(int)KS_CWP, "\033[205;%d;%d/y"},
1071# endif
1072 {K_UP, "\033[A"},
1073 {K_DOWN, "\033[B"},
1074 {K_LEFT, "\033[D"},
1075 {K_RIGHT, "\033[C"},
1076 {K_S_UP, "\033[161q"},
1077 {K_S_DOWN, "\033[164q"},
1078 {K_S_LEFT, "\033[158q"},
1079 {K_S_RIGHT, "\033[167q"},
1080 {K_F1, "\033[001q"},
1081 {K_F2, "\033[002q"},
1082 {K_F3, "\033[003q"},
1083 {K_F4, "\033[004q"},
1084 {K_F5, "\033[005q"},
1085 {K_F6, "\033[006q"},
1086 {K_F7, "\033[007q"},
1087 {K_F8, "\033[008q"},
1088 {K_F9, "\033[009q"},
1089 {K_F10, "\033[010q"},
1090 {K_F11, "\033[011q"},
1091 {K_F12, "\033[012q"},
1092 {K_S_F1, "\033[013q"},
1093 {K_S_F2, "\033[014q"},
1094 {K_S_F3, "\033[015q"},
1095 {K_S_F4, "\033[016q"},
1096 {K_S_F5, "\033[017q"},
1097 {K_S_F6, "\033[018q"},
1098 {K_S_F7, "\033[019q"},
1099 {K_S_F8, "\033[020q"},
1100 {K_S_F9, "\033[021q"},
1101 {K_S_F10, "\033[022q"},
1102 {K_S_F11, "\033[023q"},
1103 {K_S_F12, "\033[024q"},
1104 {K_INS, "\033[139q"},
1105 {K_HOME, "\033[H"},
1106 {K_END, "\033[146q"},
1107 {K_PAGEUP, "\033[150q"},
1108 {K_PAGEDOWN, "\033[154q"},
1109# endif
1110
1111# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1112/*
1113 * for debugging
1114 */
1115 {(int)KS_NAME, "debug"},
1116 {(int)KS_CE, "[CE]"},
1117 {(int)KS_CD, "[CD]"},
1118 {(int)KS_AL, "[AL]"},
1119# ifdef TERMINFO
1120 {(int)KS_CAL, "[CAL%p1%d]"},
1121# else
1122 {(int)KS_CAL, "[CAL%d]"},
1123# endif
1124 {(int)KS_DL, "[DL]"},
1125# ifdef TERMINFO
1126 {(int)KS_CDL, "[CDL%p1%d]"},
1127# else
1128 {(int)KS_CDL, "[CDL%d]"},
1129# endif
1130# ifdef TERMINFO
1131 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1132# else
1133 {(int)KS_CS, "[%dCS%d]"},
1134# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001135# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001137# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139# endif
1140# ifdef TERMINFO
1141 {(int)KS_CAB, "[CAB%p1%d]"},
1142 {(int)KS_CAF, "[CAF%p1%d]"},
1143 {(int)KS_CSB, "[CSB%p1%d]"},
1144 {(int)KS_CSF, "[CSF%p1%d]"},
1145# else
1146 {(int)KS_CAB, "[CAB%d]"},
1147 {(int)KS_CAF, "[CAF%d]"},
1148 {(int)KS_CSB, "[CSB%d]"},
1149 {(int)KS_CSF, "[CSF%d]"},
1150# endif
1151 {(int)KS_OP, "[OP]"},
1152 {(int)KS_LE, "[LE]"},
1153 {(int)KS_CL, "[CL]"},
1154 {(int)KS_VI, "[VI]"},
1155 {(int)KS_VE, "[VE]"},
1156 {(int)KS_VS, "[VS]"},
1157 {(int)KS_ME, "[ME]"},
1158 {(int)KS_MR, "[MR]"},
1159 {(int)KS_MB, "[MB]"},
1160 {(int)KS_MD, "[MD]"},
1161 {(int)KS_SE, "[SE]"},
1162 {(int)KS_SO, "[SO]"},
1163 {(int)KS_UE, "[UE]"},
1164 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001165 {(int)KS_UCE, "[UCE]"},
1166 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001167 {(int)KS_STE, "[STE]"},
1168 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {(int)KS_MS, "[MS]"},
1170 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001171 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# ifdef TERMINFO
1173 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1174# else
1175 {(int)KS_CM, "[%dCM%d]"},
1176# endif
1177 {(int)KS_SR, "[SR]"},
1178# ifdef TERMINFO
1179 {(int)KS_CRI, "[CRI%p1%d]"},
1180# else
1181 {(int)KS_CRI, "[CRI%d]"},
1182# endif
1183 {(int)KS_VB, "[VB]"},
1184 {(int)KS_KS, "[KS]"},
1185 {(int)KS_KE, "[KE]"},
1186 {(int)KS_TI, "[TI]"},
1187 {(int)KS_TE, "[TE]"},
1188 {(int)KS_CIS, "[CIS]"},
1189 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001190 {(int)KS_CSC, "[CSC]"},
1191 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {(int)KS_TS, "[TS]"},
1193 {(int)KS_FS, "[FS]"},
1194# ifdef TERMINFO
1195 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1196 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1197# else
1198 {(int)KS_CWS, "[%dCWS%d]"},
1199 {(int)KS_CWP, "[%dCWP%d]"},
1200# endif
1201 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001202 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001203 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001204 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {K_UP, "[KU]"},
1206 {K_DOWN, "[KD]"},
1207 {K_LEFT, "[KL]"},
1208 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001209 {K_XUP, "[xKU]"},
1210 {K_XDOWN, "[xKD]"},
1211 {K_XLEFT, "[xKL]"},
1212 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {K_S_UP, "[S-KU]"},
1214 {K_S_DOWN, "[S-KD]"},
1215 {K_S_LEFT, "[S-KL]"},
1216 {K_C_LEFT, "[C-KL]"},
1217 {K_S_RIGHT, "[S-KR]"},
1218 {K_C_RIGHT, "[C-KR]"},
1219 {K_F1, "[F1]"},
1220 {K_XF1, "[xF1]"},
1221 {K_F2, "[F2]"},
1222 {K_XF2, "[xF2]"},
1223 {K_F3, "[F3]"},
1224 {K_XF3, "[xF3]"},
1225 {K_F4, "[F4]"},
1226 {K_XF4, "[xF4]"},
1227 {K_F5, "[F5]"},
1228 {K_F6, "[F6]"},
1229 {K_F7, "[F7]"},
1230 {K_F8, "[F8]"},
1231 {K_F9, "[F9]"},
1232 {K_F10, "[F10]"},
1233 {K_F11, "[F11]"},
1234 {K_F12, "[F12]"},
1235 {K_S_F1, "[S-F1]"},
1236 {K_S_XF1, "[S-xF1]"},
1237 {K_S_F2, "[S-F2]"},
1238 {K_S_XF2, "[S-xF2]"},
1239 {K_S_F3, "[S-F3]"},
1240 {K_S_XF3, "[S-xF3]"},
1241 {K_S_F4, "[S-F4]"},
1242 {K_S_XF4, "[S-xF4]"},
1243 {K_S_F5, "[S-F5]"},
1244 {K_S_F6, "[S-F6]"},
1245 {K_S_F7, "[S-F7]"},
1246 {K_S_F8, "[S-F8]"},
1247 {K_S_F9, "[S-F9]"},
1248 {K_S_F10, "[S-F10]"},
1249 {K_S_F11, "[S-F11]"},
1250 {K_S_F12, "[S-F12]"},
1251 {K_HELP, "[HELP]"},
1252 {K_UNDO, "[UNDO]"},
1253 {K_BS, "[BS]"},
1254 {K_INS, "[INS]"},
1255 {K_KINS, "[KINS]"},
1256 {K_DEL, "[DEL]"},
1257 {K_KDEL, "[KDEL]"},
1258 {K_HOME, "[HOME]"},
1259 {K_S_HOME, "[C-HOME]"},
1260 {K_C_HOME, "[C-HOME]"},
1261 {K_KHOME, "[KHOME]"},
1262 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001263 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {K_END, "[END]"},
1265 {K_S_END, "[C-END]"},
1266 {K_C_END, "[C-END]"},
1267 {K_KEND, "[KEND]"},
1268 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001269 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {K_PAGEUP, "[PAGEUP]"},
1271 {K_PAGEDOWN, "[PAGEDOWN]"},
1272 {K_KPAGEUP, "[KPAGEUP]"},
1273 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1274 {K_MOUSE, "[MOUSE]"},
1275 {K_KPLUS, "[KPLUS]"},
1276 {K_KMINUS, "[KMINUS]"},
1277 {K_KDIVIDE, "[KDIVIDE]"},
1278 {K_KMULTIPLY, "[KMULTIPLY]"},
1279 {K_KENTER, "[KENTER]"},
1280 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001281 {K_PS, "[PASTE-START]"},
1282 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {K_K0, "[K0]"},
1284 {K_K1, "[K1]"},
1285 {K_K2, "[K2]"},
1286 {K_K3, "[K3]"},
1287 {K_K4, "[K4]"},
1288 {K_K5, "[K5]"},
1289 {K_K6, "[K6]"},
1290 {K_K7, "[K7]"},
1291 {K_K8, "[K8]"},
1292 {K_K9, "[K9]"},
1293# endif
1294
1295#endif /* NO_BUILTIN_TCAPS */
1296
1297/*
1298 * The most minimal terminal: only clear screen and cursor positioning
1299 * Always included.
1300 */
1301 {(int)KS_NAME, "dumb"},
1302 {(int)KS_CL, "\014"},
1303#ifdef TERMINFO
1304 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1305 ESC_STR "[%i%p1%d;%p2%dH")},
1306#else
1307 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1308#endif
1309
1310/*
1311 * end marker
1312 */
1313 {(int)KS_NAME, NULL}
1314
1315}; /* end of builtin_termcaps */
1316
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001317#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001318 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001319termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001320{
Bram Moolenaarab302212016-04-26 20:59:29 +02001321 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001322}
1323
1324 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001325termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001326{
1327 guicolor_T t;
1328
1329 if (*name == NUL)
1330 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001331 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001332
1333 if (t == INVALCOLOR)
1334 EMSG2(_("E254: Cannot allocate color %s"), name);
1335 return t;
1336}
1337
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001338 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001339termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001340{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001341 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001342}
1343#endif
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345/*
1346 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#ifdef AMIGA
1349# define DEFAULT_TERM (char_u *)"amiga"
1350#endif
1351
1352#ifdef MSWIN
1353# define DEFAULT_TERM (char_u *)"win32"
1354#endif
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#if defined(UNIX) && !defined(__MINT__)
1357# define DEFAULT_TERM (char_u *)"ansi"
1358#endif
1359
1360#ifdef __MINT__
1361# define DEFAULT_TERM (char_u *)"vt52"
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef VMS
1365# define DEFAULT_TERM (char_u *)"vt320"
1366#endif
1367
1368#ifdef __BEOS__
1369# undef DEFAULT_TERM
1370# define DEFAULT_TERM (char_u *)"beos-ansi"
1371#endif
1372
1373#ifndef DEFAULT_TERM
1374# define DEFAULT_TERM (char_u *)"dumb"
1375#endif
1376
1377/*
1378 * Term_strings contains currently used terminal output strings.
1379 * It is initialized with the default values by parse_builtin_tcap().
1380 * The values can be changed by setting the option with the same name.
1381 */
1382char_u *(term_strings[(int)KS_LAST + 1]);
1383
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001384static int need_gather = FALSE; /* need to fill termleader[] */
1385static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001387static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001388static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
1390
1391 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001392find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 struct builtin_term *p;
1395
1396 p = builtin_termcaps;
1397 while (p->bt_string != NULL)
1398 {
1399 if (p->bt_entry == (int)KS_NAME)
1400 {
1401#ifdef UNIX
1402 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1403 return p;
1404 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1405 return p;
1406 else
1407#endif
1408#ifdef VMS
1409 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1410 return p;
1411 else
1412#endif
1413 if (STRCMP(term, p->bt_string) == 0)
1414 return p;
1415 }
1416 ++p;
1417 }
1418 return p;
1419}
1420
1421/*
1422 * Parsing of the builtin termcap entries.
1423 * Caller should check if 'name' is a valid builtin term.
1424 * The terminal's name is not set, as this is already done in termcapinit().
1425 */
1426 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001427parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
1429 struct builtin_term *p;
1430 char_u name[2];
1431 int term_8bit;
1432
1433 p = find_builtin_term(term);
1434 term_8bit = term_is_8bit(term);
1435
1436 /* Do not parse if builtin term not found */
1437 if (p->bt_string == NULL)
1438 return;
1439
1440 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1441 {
1442 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1443 {
1444 /* Only set the value if it wasn't set yet. */
1445 if (term_strings[p->bt_entry] == NULL
1446 || term_strings[p->bt_entry] == empty_option)
1447 {
1448 /* 8bit terminal: use CSI instead of <Esc>[ */
1449 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1450 {
1451 char_u *s, *t;
1452
1453 s = vim_strsave((char_u *)p->bt_string);
1454 if (s != NULL)
1455 {
1456 for (t = s; *t; ++t)
1457 if (term_7to8bit(t))
1458 {
1459 *t = term_7to8bit(t);
1460 STRCPY(t + 1, t + 2);
1461 }
1462 term_strings[p->bt_entry] = s;
1463 set_term_option_alloced(&term_strings[p->bt_entry]);
1464 }
1465 }
1466 else
1467 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1468 }
1469 }
1470 else
1471 {
1472 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1473 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1474 if (find_termcode(name) == NULL)
1475 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1476 }
1477 }
1478}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480/*
1481 * Set number of colors.
1482 * Store it as a number in t_colors.
1483 * Store it as a string in T_CCO (using nr_colors[]).
1484 */
1485 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001486set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 char_u nr_colors[20]; /* string for number of colors */
1489
1490 t_colors = nr;
1491 if (t_colors > 1)
1492 sprintf((char *)nr_colors, "%d", t_colors);
1493 else
1494 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001495 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001497
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001498#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001499/*
1500 * Set the color count to "val" and redraw if it changed.
1501 */
1502 static void
1503may_adjust_color_count(int val)
1504{
1505 if (val != t_colors)
1506 {
1507 /* Nr of colors changed, initialize highlighting and
1508 * redraw everything. This causes a redraw, which usually
1509 * clears the message. Try keeping the message if it
1510 * might work. */
1511 set_keep_msg_from_hist();
1512 set_color_count(val);
1513 init_highlight(TRUE, FALSE);
1514# ifdef DEBUG_TERMRESPONSE
1515 {
1516 char buf[100];
1517 int r = redraw_asap(CLEAR);
1518
1519 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1520 log_tr(buf);
1521 }
1522# else
1523 redraw_asap(CLEAR);
1524# endif
1525 }
1526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#endif
1528
1529#ifdef HAVE_TGETENT
1530static char *(key_names[]) =
1531{
1532#ifdef FEAT_TERMRESPONSE
1533 /* Do this one first, it may cause a screen redraw. */
1534 "Co",
1535#endif
1536 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 "#2", "#4", "%i", "*7",
1538 "k1", "k2", "k3", "k4", "k5", "k6",
1539 "k7", "k8", "k9", "k;", "F1", "F2",
1540 "%1", "&8", "kb", "kI", "kD", "kh",
1541 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1542 NULL
1543};
1544#endif
1545
1546/*
1547 * Set terminal options for terminal "term".
1548 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1549 *
1550 * While doing this, until ttest(), some options may be NULL, be careful.
1551 */
1552 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001553set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 struct builtin_term *termp;
1556#ifdef HAVE_TGETENT
1557 int builtin_first = p_tbi;
1558 int try;
1559 int termcap_cleared = FALSE;
1560#endif
1561 int width = 0, height = 0;
1562 char_u *error_msg = NULL;
1563 char_u *bs_p, *del_p;
1564
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001565 /* In silect mode (ex -s) we don't use the 'term' option. */
1566 if (silent_mode)
1567 return OK;
1568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 detected_8bit = FALSE; /* reset 8-bit detection */
1570
1571 if (term_is_builtin(term))
1572 {
1573 term += 8;
1574#ifdef HAVE_TGETENT
1575 builtin_first = 1;
1576#endif
1577 }
1578
1579/*
1580 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1581 * If builtin_first is TRUE:
1582 * 0. try builtin termcap
1583 * 1. try external termcap
1584 * 2. if both fail default to a builtin terminal
1585 * If builtin_first is FALSE:
1586 * 1. try external termcap
1587 * 2. try builtin termcap, if both fail default to a builtin terminal
1588 */
1589#ifdef HAVE_TGETENT
1590 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1591 {
1592 /*
1593 * Use external termcap
1594 */
1595 if (try == 1)
1596 {
1597 char_u *p;
1598 static char_u tstrbuf[TBUFSZ];
1599 int i;
1600 char_u tbuf[TBUFSZ];
1601 char_u *tp;
1602 static struct {
1603 enum SpecialKey dest; /* index in term_strings[] */
1604 char *name; /* termcap name for string */
1605 } string_names[] =
1606 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1607 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1608 {KS_CL, "cl"}, {KS_CD, "cd"},
1609 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001610 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1612 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001613 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001614 {KS_STE,"Te"}, {KS_STS,"Ts"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001615 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1617 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1618 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1619 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1620 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001621 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001623 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {KS_TS, "ts"}, {KS_FS, "fs"},
1625 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001626 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001627 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001628 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001629 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1630 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {(enum SpecialKey)0, NULL}
1632 };
1633
1634 /*
1635 * If the external termcap does not have a matching entry, try the
1636 * builtin ones.
1637 */
1638 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1639 {
1640 tp = tstrbuf;
1641 if (!termcap_cleared)
1642 {
1643 clear_termoptions(); /* clear old options */
1644 termcap_cleared = TRUE;
1645 }
1646
1647 /* get output strings */
1648 for (i = 0; string_names[i].name != NULL; ++i)
1649 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001650 if (TERM_STR(string_names[i].dest) == NULL
1651 || TERM_STR(string_names[i].dest) == empty_option)
1652 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 TGETSTR(string_names[i].name, &tp);
1654 }
1655
1656 /* tgetflag() returns 1 if the flag is present, 0 if not and
1657 * possibly -1 if the flag doesn't exist. */
1658 if ((T_MS == NULL || T_MS == empty_option)
1659 && tgetflag("ms") > 0)
1660 T_MS = (char_u *)"y";
1661 if ((T_XS == NULL || T_XS == empty_option)
1662 && tgetflag("xs") > 0)
1663 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001664 if ((T_XN == NULL || T_XN == empty_option)
1665 && tgetflag("xn") > 0)
1666 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if ((T_DB == NULL || T_DB == empty_option)
1668 && tgetflag("db") > 0)
1669 T_DB = (char_u *)"y";
1670 if ((T_DA == NULL || T_DA == empty_option)
1671 && tgetflag("da") > 0)
1672 T_DA = (char_u *)"y";
1673 if ((T_UT == NULL || T_UT == empty_option)
1674 && tgetflag("ut") > 0)
1675 T_UT = (char_u *)"y";
1676
1677
1678 /*
1679 * get key codes
1680 */
1681 for (i = 0; key_names[i] != NULL; ++i)
1682 {
1683 if (find_termcode((char_u *)key_names[i]) == NULL)
1684 {
1685 p = TGETSTR(key_names[i], &tp);
1686 /* if cursor-left == backspace, ignore it (televideo
1687 * 925) */
1688 if (p != NULL
1689 && (*p != Ctrl_H
1690 || key_names[i][0] != 'k'
1691 || key_names[i][1] != 'l'))
1692 add_termcode((char_u *)key_names[i], p, FALSE);
1693 }
1694 }
1695
1696 if (height == 0)
1697 height = tgetnum("li");
1698 if (width == 0)
1699 width = tgetnum("co");
1700
1701 /*
1702 * Get number of colors (if not done already).
1703 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001704 if (TERM_STR(KS_CCO) == NULL
1705 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 set_color_count(tgetnum("Co"));
1707
1708# ifndef hpux
1709 BC = (char *)TGETSTR("bc", &tp);
1710 UP = (char *)TGETSTR("up", &tp);
1711 p = TGETSTR("pc", &tp);
1712 if (p)
1713 PC = *p;
1714# endif /* hpux */
1715 }
1716 }
1717 else /* try == 0 || try == 2 */
1718#endif /* HAVE_TGETENT */
1719 /*
1720 * Use builtin termcap
1721 */
1722 {
1723#ifdef HAVE_TGETENT
1724 /*
1725 * If builtin termcap was already used, there is no need to search
1726 * for the builtin termcap again, quit now.
1727 */
1728 if (try == 2 && builtin_first && termcap_cleared)
1729 break;
1730#endif
1731 /*
1732 * search for 'term' in builtin_termcaps[]
1733 */
1734 termp = find_builtin_term(term);
1735 if (termp->bt_string == NULL) /* did not find it */
1736 {
1737#ifdef HAVE_TGETENT
1738 /*
1739 * If try == 0, first try the external termcap. If that is not
1740 * found we'll get back here with try == 2.
1741 * If termcap_cleared is set we used the external termcap,
1742 * don't complain about not finding the term in the builtin
1743 * termcap.
1744 */
1745 if (try == 0) /* try external one */
1746 continue;
1747 if (termcap_cleared) /* found in external termcap */
1748 break;
1749#endif
1750
1751 mch_errmsg("\r\n");
1752 if (error_msg != NULL)
1753 {
1754 mch_errmsg((char *)error_msg);
1755 mch_errmsg("\r\n");
1756 }
1757 mch_errmsg("'");
1758 mch_errmsg((char *)term);
1759 mch_errmsg(_("' not known. Available builtin terminals are:"));
1760 mch_errmsg("\r\n");
1761 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1762 ++termp)
1763 {
1764 if (termp->bt_entry == (int)KS_NAME)
1765 {
1766#ifdef HAVE_TGETENT
1767 mch_errmsg(" builtin_");
1768#else
1769 mch_errmsg(" ");
1770#endif
1771 mch_errmsg(termp->bt_string);
1772 mch_errmsg("\r\n");
1773 }
1774 }
1775 /* when user typed :set term=xxx, quit here */
1776 if (starting != NO_SCREEN)
1777 {
1778 screen_start(); /* don't know where cursor is now */
1779 wait_return(TRUE);
1780 return FAIL;
1781 }
1782 term = DEFAULT_TERM;
1783 mch_errmsg(_("defaulting to '"));
1784 mch_errmsg((char *)term);
1785 mch_errmsg("'\r\n");
1786 if (emsg_silent == 0)
1787 {
1788 screen_start(); /* don't know where cursor is now */
1789 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001790 if (!is_not_a_term())
1791 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001793 set_string_option_direct((char_u *)"term", -1, term,
1794 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 display_errors();
1796 }
1797 out_flush();
1798#ifdef HAVE_TGETENT
1799 if (!termcap_cleared)
1800 {
1801#endif
1802 clear_termoptions(); /* clear old options */
1803#ifdef HAVE_TGETENT
1804 termcap_cleared = TRUE;
1805 }
1806#endif
1807 parse_builtin_tcap(term);
1808#ifdef FEAT_GUI
1809 if (term_is_gui(term))
1810 {
1811 out_flush();
1812 gui_init();
1813 /* If starting the GUI failed, don't do any of the other
1814 * things for this terminal */
1815 if (!gui.in_use)
1816 return FAIL;
1817#ifdef HAVE_TGETENT
1818 break; /* don't try using external termcap */
1819#endif
1820 }
1821#endif /* FEAT_GUI */
1822 }
1823#ifdef HAVE_TGETENT
1824 }
1825#endif
1826
1827/*
1828 * special: There is no info in the termcap about whether the cursor
1829 * positioning is relative to the start of the screen or to the start of the
1830 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1831 * relative.
1832 */
1833 if (STRCMP(term, "pcterm") == 0)
1834 T_CCS = (char_u *)"yes";
1835 else
1836 T_CCS = empty_option;
1837
1838#ifdef UNIX
1839/*
1840 * Any "stty" settings override the default for t_kb from the termcap.
1841 * This is in os_unix.c, because it depends a lot on the version of unix that
1842 * is being used.
1843 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1844 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001845# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 get_stty();
1849#endif
1850
1851/*
1852 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1853 * didn't work, use the default CTRL-H
1854 * The default for t_kD is DEL, unless t_kb is DEL.
1855 * The vim_strsave'd strings are probably lost forever, well it's only two
1856 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1857 * directly.
1858 */
1859#ifdef FEAT_GUI
1860 if (!gui.in_use)
1861#endif
1862 {
1863 bs_p = find_termcode((char_u *)"kb");
1864 del_p = find_termcode((char_u *)"kD");
1865 if (bs_p == NULL || *bs_p == NUL)
1866 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1867 if ((del_p == NULL || *del_p == NUL) &&
1868 (bs_p == NULL || *bs_p != DEL))
1869 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1870 }
1871
1872#if defined(UNIX) || defined(VMS)
1873 term_is_xterm = vim_is_xterm(term);
1874#endif
1875
1876#ifdef FEAT_MOUSE
1877# if defined(UNIX) || defined(VMS)
1878# ifdef FEAT_MOUSE_TTY
1879 /*
1880 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1881 * The termcode for the mouse is added as a side effect in option.c.
1882 */
1883 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001884 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001887 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 if (use_xterm_mouse())
1890 p = NULL; /* keep existing value, might be "xterm2" */
1891 else
1892 p = (char_u *)"xterm";
1893 }
1894# endif
1895 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001898 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1899 * "xterm2" in check_termcode(). */
1900 reset_option_was_set((char_u *)"ttym");
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (p == NULL
1903# ifdef FEAT_GUI
1904 || gui.in_use
1905# endif
1906 )
1907 check_mouse_termcode(); /* set mouse termcode anyway */
1908 }
1909# endif
1910# else
1911 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1912# endif
1913#endif /* FEAT_MOUSE */
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#ifdef USE_TERM_CONSOLE
1916 /* DEFAULT_TERM indicates that it is the machine console. */
1917 if (STRCMP(term, DEFAULT_TERM) != 0)
1918 term_console = FALSE;
1919 else
1920 {
1921 term_console = TRUE;
1922# ifdef AMIGA
1923 win_resize_on(); /* enable window resizing reports */
1924# endif
1925 }
1926#endif
1927
1928#if defined(UNIX) || defined(VMS)
1929 /*
1930 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1931 */
1932 if (vim_is_fastterm(term))
1933 p_tf = TRUE;
1934#endif
1935#ifdef USE_TERM_CONSOLE
1936 /*
1937 * 'ttyfast' is default on consoles
1938 */
1939 if (term_console)
1940 p_tf = TRUE;
1941#endif
1942
1943 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1944
1945 full_screen = TRUE; /* we can use termcap codes from now on */
1946 set_term_defaults(); /* use current values as defaults */
1947#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001948 LOG_TR("setting crv_status to STATUS_GET");
1949 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
1951
1952 /*
1953 * Initialize the terminal with the appropriate termcap codes.
1954 * Set the mouse and window title if possible.
1955 * Don't do this when starting, need to parse the .vimrc first, because it
1956 * may redefine t_TI etc.
1957 */
1958 if (starting != NO_SCREEN)
1959 {
1960 starttermcap(); /* may change terminal mode */
1961#ifdef FEAT_MOUSE
1962 setmouse(); /* may start using the mouse */
1963#endif
1964#ifdef FEAT_TITLE
1965 maketitle(); /* may display window title */
1966#endif
1967 }
1968
1969 /* display initial screen after ttest() checking. jw. */
1970 if (width <= 0 || height <= 0)
1971 {
1972 /* termcap failed to report size */
1973 /* set defaults, in case ui_get_shellsize() also fails */
1974 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001975#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 height = 25; /* console is often 25 lines */
1977#else
1978 height = 24; /* most terminals are 24 lines */
1979#endif
1980 }
1981 set_shellsize(width, height, FALSE); /* may change Rows */
1982 if (starting != NO_SCREEN)
1983 {
1984 if (scroll_region)
1985 scroll_region_reset(); /* In case Rows changed */
1986 check_map_keycodes(); /* check mappings for terminal codes used */
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001989 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 /*
1992 * Execute the TermChanged autocommands for each buffer that is
1993 * loaded.
1994 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001995 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001996 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 if (curbuf->b_ml.ml_mfp != NULL)
1999 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2000 curbuf);
2001 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002002 if (bufref_valid(&old_curbuf))
2003 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006
2007#ifdef FEAT_TERMRESPONSE
2008 may_req_termresponse();
2009#endif
2010
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002011#if defined(WIN3264) && !defined(FEAT_GUI) && defined(FEAT_TERMGUICOLORS)
2012 if (STRCMP(term, "win32") == 0)
2013 set_color_count((p_tgc) ? 256 : 16);
2014#endif
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 return OK;
2017}
2018
2019#if defined(FEAT_MOUSE) || defined(PROTO)
2020
2021# ifdef FEAT_MOUSE_TTY
2022# define HMT_NORMAL 1
2023# define HMT_NETTERM 2
2024# define HMT_DEC 4
2025# define HMT_JSBTERM 8
2026# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002027# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002028# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002029# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030static int has_mouse_termcode = 0;
2031# endif
2032
Bram Moolenaar864207d2008-06-24 22:14:38 +00002033# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002035set_mouse_termcode(
2036 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2037 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 char_u name[2];
2040
2041 name[0] = n;
2042 name[1] = KE_FILLER;
2043 add_termcode(name, s, FALSE);
2044# ifdef FEAT_MOUSE_TTY
2045# ifdef FEAT_MOUSE_JSB
2046 if (n == KS_JSBTERM_MOUSE)
2047 has_mouse_termcode |= HMT_JSBTERM;
2048 else
2049# endif
2050# ifdef FEAT_MOUSE_NET
2051 if (n == KS_NETTERM_MOUSE)
2052 has_mouse_termcode |= HMT_NETTERM;
2053 else
2054# endif
2055# ifdef FEAT_MOUSE_DEC
2056 if (n == KS_DEC_MOUSE)
2057 has_mouse_termcode |= HMT_DEC;
2058 else
2059# endif
2060# ifdef FEAT_MOUSE_PTERM
2061 if (n == KS_PTERM_MOUSE)
2062 has_mouse_termcode |= HMT_PTERM;
2063 else
2064# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002065# ifdef FEAT_MOUSE_URXVT
2066 if (n == KS_URXVT_MOUSE)
2067 has_mouse_termcode |= HMT_URXVT;
2068 else
2069# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002070# ifdef FEAT_MOUSE_SGR
2071 if (n == KS_SGR_MOUSE)
2072 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002073 else if (n == KS_SGR_MOUSE_RELEASE)
2074 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002075 else
2076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 has_mouse_termcode |= HMT_NORMAL;
2078# endif
2079}
2080# endif
2081
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002082# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002083 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002085del_mouse_termcode(
2086 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 char_u name[2];
2089
2090 name[0] = n;
2091 name[1] = KE_FILLER;
2092 del_termcode(name);
2093# ifdef FEAT_MOUSE_TTY
2094# ifdef FEAT_MOUSE_JSB
2095 if (n == KS_JSBTERM_MOUSE)
2096 has_mouse_termcode &= ~HMT_JSBTERM;
2097 else
2098# endif
2099# ifdef FEAT_MOUSE_NET
2100 if (n == KS_NETTERM_MOUSE)
2101 has_mouse_termcode &= ~HMT_NETTERM;
2102 else
2103# endif
2104# ifdef FEAT_MOUSE_DEC
2105 if (n == KS_DEC_MOUSE)
2106 has_mouse_termcode &= ~HMT_DEC;
2107 else
2108# endif
2109# ifdef FEAT_MOUSE_PTERM
2110 if (n == KS_PTERM_MOUSE)
2111 has_mouse_termcode &= ~HMT_PTERM;
2112 else
2113# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002114# ifdef FEAT_MOUSE_URXVT
2115 if (n == KS_URXVT_MOUSE)
2116 has_mouse_termcode &= ~HMT_URXVT;
2117 else
2118# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002119# ifdef FEAT_MOUSE_SGR
2120 if (n == KS_SGR_MOUSE)
2121 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002122 else if (n == KS_SGR_MOUSE_RELEASE)
2123 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002124 else
2125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 has_mouse_termcode &= ~HMT_NORMAL;
2127# endif
2128}
2129# endif
2130#endif
2131
2132#ifdef HAVE_TGETENT
2133/*
2134 * Call tgetent()
2135 * Return error message if it fails, NULL if it's OK.
2136 */
2137 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002138tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 int i;
2141
2142 i = TGETENT(tbuf, term);
2143 if (i < 0 /* -1 is always an error */
2144# ifdef TGETENT_ZERO_ERR
2145 || i == 0 /* sometimes zero is also an error */
2146# endif
2147 )
2148 {
2149 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2150 * tgetent() with the always existing "dumb" entry to avoid a crash or
2151 * hang. */
2152 (void)TGETENT(tbuf, "dumb");
2153
2154 if (i < 0)
2155# ifdef TGETENT_ZERO_ERR
2156 return (char_u *)_("E557: Cannot open termcap file");
2157 if (i == 0)
2158# endif
2159#ifdef TERMINFO
2160 return (char_u *)_("E558: Terminal entry not found in terminfo");
2161#else
2162 return (char_u *)_("E559: Terminal entry not found in termcap");
2163#endif
2164 }
2165 return NULL;
2166}
2167
2168/*
2169 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2170 * Fix that here.
2171 */
2172 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002173vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 char *p;
2176
2177 p = tgetstr(s, (char **)pp);
2178 if (p == (char *)-1)
2179 p = NULL;
2180 return (char_u *)p;
2181}
2182#endif /* HAVE_TGETENT */
2183
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002184#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185/*
2186 * Get Columns and Rows from the termcap. Used after a window signal if the
2187 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2188 * and "li" entries never change. But on some systems this works.
2189 * Errors while getting the entries are ignored.
2190 */
2191 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002192getlinecol(
2193 long *cp, /* pointer to columns */
2194 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195{
2196 char_u tbuf[TBUFSZ];
2197
2198 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2199 {
2200 if (*cp == 0)
2201 *cp = tgetnum("co");
2202 if (*rp == 0)
2203 *rp = tgetnum("li");
2204 }
2205}
2206#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2207
2208/*
2209 * Get a string entry from the termcap and add it to the list of termcodes.
2210 * Used for <t_xx> special keys.
2211 * Give an error message for failure when not sourcing.
2212 * If force given, replace an existing entry.
2213 * Return FAIL if the entry was not found, OK if the entry was added.
2214 */
2215 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002216add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 char_u *term;
2219 int key;
2220 struct builtin_term *termp;
2221#ifdef HAVE_TGETENT
2222 char_u *string;
2223 int i;
2224 int builtin_first;
2225 char_u tbuf[TBUFSZ];
2226 char_u tstrbuf[TBUFSZ];
2227 char_u *tp = tstrbuf;
2228 char_u *error_msg = NULL;
2229#endif
2230
2231/*
2232 * If the GUI is running or will start in a moment, we only support the keys
2233 * that the GUI can produce.
2234 */
2235#ifdef FEAT_GUI
2236 if (gui.in_use || gui.starting)
2237 return gui_mch_haskey(name);
2238#endif
2239
2240 if (!force && find_termcode(name) != NULL) /* it's already there */
2241 return OK;
2242
2243 term = T_NAME;
2244 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2245 return FAIL;
2246
2247 if (term_is_builtin(term)) /* name starts with "builtin_" */
2248 {
2249 term += 8;
2250#ifdef HAVE_TGETENT
2251 builtin_first = TRUE;
2252#endif
2253 }
2254#ifdef HAVE_TGETENT
2255 else
2256 builtin_first = p_tbi;
2257#endif
2258
2259#ifdef HAVE_TGETENT
2260/*
2261 * We can get the entry from the builtin termcap and from the external one.
2262 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2263 * builtin termcap first.
2264 * If 'ttybuiltin' is off, try external termcap first.
2265 */
2266 for (i = 0; i < 2; ++i)
2267 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002268 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#endif
2270 /*
2271 * Search in builtin termcap
2272 */
2273 {
2274 termp = find_builtin_term(term);
2275 if (termp->bt_string != NULL) /* found it */
2276 {
2277 key = TERMCAP2KEY(name[0], name[1]);
2278 while (termp->bt_entry != (int)KS_NAME)
2279 {
2280 if ((int)termp->bt_entry == key)
2281 {
2282 add_termcode(name, (char_u *)termp->bt_string,
2283 term_is_8bit(term));
2284 return OK;
2285 }
2286 ++termp;
2287 }
2288 }
2289 }
2290#ifdef HAVE_TGETENT
2291 else
2292 /*
2293 * Search in external termcap
2294 */
2295 {
2296 error_msg = tgetent_error(tbuf, term);
2297 if (error_msg == NULL)
2298 {
2299 string = TGETSTR((char *)name, &tp);
2300 if (string != NULL && *string != NUL)
2301 {
2302 add_termcode(name, string, FALSE);
2303 return OK;
2304 }
2305 }
2306 }
2307 }
2308#endif
2309
2310 if (sourcing_name == NULL)
2311 {
2312#ifdef HAVE_TGETENT
2313 if (error_msg != NULL)
2314 EMSG(error_msg);
2315 else
2316#endif
2317 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2318 }
2319 return FAIL;
2320}
2321
2322 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002323term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2326}
2327
2328/*
2329 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2330 * Assume that the terminal is using 8-bit controls when the name contains
2331 * "8bit", like in "xterm-8bit".
2332 */
2333 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002334term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2337}
2338
2339/*
2340 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002341 * <Esc>[ -> CSI <M_C_[>
2342 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * <Esc>O -> <M-C-O>
2344 */
2345 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002346term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 if (*p == ESC)
2349 {
2350 if (p[1] == '[')
2351 return CSI;
2352 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002353 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (p[1] == 'O')
2355 return 0x8f;
2356 }
2357 return 0;
2358}
2359
2360#ifdef FEAT_GUI
2361 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002362term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2365}
2366#endif
2367
2368#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2369
2370 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002371tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 static char_u buf[16];
2374 char_u *p;
2375
2376 p = buf + 15;
2377 *p = '\0';
2378 do
2379 {
2380 --p;
2381 *p = (char_u) (i % 10 + '0');
2382 i /= 10;
2383 }
2384 while (i > 0 && p > buf);
2385 return p;
2386}
2387#endif
2388
2389#ifndef HAVE_TGETENT
2390
2391/*
2392 * minimal tgoto() implementation.
2393 * no padding and we only parse for %i %d and %+char
2394 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002395static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002397 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002398tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 static char buf[30];
2401 char *p, *s, *e;
2402
2403 if (!cm)
2404 return "OOPS";
2405 e = buf + 29;
2406 for (s = buf; s < e && *cm; cm++)
2407 {
2408 if (*cm != '%')
2409 {
2410 *s++ = *cm;
2411 continue;
2412 }
2413 switch (*++cm)
2414 {
2415 case 'd':
2416 p = (char *)tltoa((unsigned long)y);
2417 y = x;
2418 while (*p)
2419 *s++ = *p++;
2420 break;
2421 case 'i':
2422 x++;
2423 y++;
2424 break;
2425 case '+':
2426 *s++ = (char)(*++cm + y);
2427 y = x;
2428 break;
2429 case '%':
2430 *s++ = *cm;
2431 break;
2432 default:
2433 return "OOPS";
2434 }
2435 }
2436 *s = '\0';
2437 return buf;
2438}
2439
2440#endif /* HAVE_TGETENT */
2441
2442/*
2443 * Set the terminal name and initialize the terminal options.
2444 * If "name" is NULL or empty, get the terminal name from the environment.
2445 * If that fails, use the default terminal name.
2446 */
2447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002448termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450 char_u *term;
2451
2452 if (name != NULL && *name == NUL)
2453 name = NULL; /* empty name is equal to no name */
2454 term = name;
2455
2456#ifdef __BEOS__
2457 /*
2458 * TERM environment variable is normally set to 'ansi' on the Bebox;
2459 * Since the BeBox doesn't quite support full ANSI yet, we use our
2460 * own custom 'ansi-beos' termcap instead, unless the -T option has
2461 * been given on the command line.
2462 */
2463 if (term == NULL
2464 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2465 term = DEFAULT_TERM;
2466#endif
2467#ifndef MSWIN
2468 if (term == NULL)
2469 term = mch_getenv((char_u *)"TERM");
2470#endif
2471 if (term == NULL || *term == NUL)
2472 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002473 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 /* Set the default terminal name. */
2476 set_string_default("term", term);
2477 set_string_default("ttytype", term);
2478
2479 /*
2480 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2481 */
2482 set_termname(T_NAME != NULL ? T_NAME : term);
2483}
2484
2485/*
2486 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2487 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002488#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2490static char_u out_buf[OUT_SIZE + 1];
2491static int out_pos = 0; /* number of chars in out_buf */
2492
2493/*
2494 * out_flush(): flush the output buffer
2495 */
2496 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002497out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499 int len;
2500
2501 if (out_pos != 0)
2502 {
2503 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2504 len = out_pos;
2505 out_pos = 0;
2506 ui_write(out_buf, len);
2507 }
2508}
2509
Bram Moolenaara338adc2018-01-31 20:51:47 +01002510/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002511 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2512 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002513 */
2514 void
2515out_flush_cursor(
2516 int force UNUSED, /* when TRUE, update cursor even when not moved */
2517 int clear_selection UNUSED) /* clear selection under cursor */
2518{
2519 mch_disable_flush();
2520 out_flush();
2521 mch_enable_flush();
2522#ifdef FEAT_GUI
2523 if (gui.in_use)
2524 {
2525 gui_update_cursor(force, clear_selection);
2526 gui_may_flush();
2527 }
2528#endif
2529}
2530
2531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#if defined(FEAT_MBYTE) || defined(PROTO)
2533/*
2534 * Sometimes a byte out of a multi-byte character is written with out_char().
2535 * To avoid flushing half of the character, call this function first.
2536 */
2537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002538out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2541 out_flush();
2542}
2543#endif
2544
2545#ifdef FEAT_GUI
2546/*
2547 * out_trash(): Throw away the contents of the output buffer
2548 */
2549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 out_pos = 0;
2553}
2554#endif
2555
2556/*
2557 * out_char(c): put a byte into the output buffer.
2558 * Flush it if it becomes full.
2559 * This should not be used for outputting text on the screen (use functions
2560 * like msg_puts() and screen_putchar() for that).
2561 */
2562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002563out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaard0573012017-10-28 21:11:06 +02002565#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2567 out_char('\r');
2568#endif
2569
2570 out_buf[out_pos++] = c;
2571
2572 /* For testing we flush each time. */
2573 if (out_pos >= OUT_SIZE || p_wd)
2574 out_flush();
2575}
2576
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002577static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579/*
2580 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2581 */
2582 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002583out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
Bram Moolenaard0573012017-10-28 21:11:06 +02002585#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2587 out_char_nf('\r');
2588#endif
2589
2590 out_buf[out_pos++] = c;
2591
2592 if (out_pos >= OUT_SIZE)
2593 out_flush();
2594}
2595
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002596#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2597 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598/*
2599 * A never-padding out_str.
2600 * use this whenever you don't want to run the string through tputs.
2601 * tputs above is harmless, but tputs from the termcap library
2602 * is likely to strip off leading digits, that it mistakes for padding
2603 * information, and "%i", "%d", etc.
2604 * This should only be used for writing terminal codes, not for outputting
2605 * normal text (use functions like msg_puts() and screen_putchar() for that).
2606 */
2607 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002608out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2611 out_flush();
2612 while (*s)
2613 out_char_nf(*s++);
2614
2615 /* For testing we write one string at a time. */
2616 if (p_wd)
2617 out_flush();
2618}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002622 * A conditional-flushing out_str, mainly for visualbell.
2623 * Handles a delay internally, because termlib may not respect the delay or do
2624 * it at the wrong time.
2625 * Note: Only for terminal strings.
2626 */
2627 void
2628out_str_cf(char_u *s)
2629{
2630 if (s != NULL && *s)
2631 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002632#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002633 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002634#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002635
2636#ifdef FEAT_GUI
2637 /* Don't use tputs() when GUI is used, ncurses crashes. */
2638 if (gui.in_use)
2639 {
2640 out_str_nf(s);
2641 return;
2642 }
2643#endif
2644 if (out_pos > OUT_SIZE - 20)
2645 out_flush();
2646#ifdef HAVE_TGETENT
2647 for (p = s; *s; ++s)
2648 {
2649 /* flush just before delay command */
2650 if (*s == '$' && *(s + 1) == '<')
2651 {
2652 char_u save_c = *s;
2653 int duration = atoi((char *)s + 2);
2654
2655 *s = NUL;
2656 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2657 *s = save_c;
2658 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002659# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002660 /* Only sleep here if we can limit this happening in
2661 * vim_beep(). */
2662 p = vim_strchr(s, '>');
2663 if (p == NULL || duration <= 0)
2664 {
2665 /* can't parse the time, don't sleep here */
2666 p = s;
2667 }
2668 else
2669 {
2670 ++p;
2671 do_sleep(duration);
2672 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002673# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002674 /* Rely on the terminal library to sleep. */
2675 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002676# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002677 break;
2678 }
2679 }
2680 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2681#else
2682 while (*s)
2683 out_char_nf(*s++);
2684#endif
2685
2686 /* For testing we write one string at a time. */
2687 if (p_wd)
2688 out_flush();
2689 }
2690}
2691
2692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 * out_str(s): Put a character string a byte at a time into the output buffer.
2694 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2695 * This should only be used for writing terminal codes, not for outputting
2696 * normal text (use functions like msg_puts() and screen_putchar() for that).
2697 */
2698 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002699out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 if (s != NULL && *s)
2702 {
2703#ifdef FEAT_GUI
2704 /* Don't use tputs() when GUI is used, ncurses crashes. */
2705 if (gui.in_use)
2706 {
2707 out_str_nf(s);
2708 return;
2709 }
2710#endif
2711 /* avoid terminal strings being split up */
2712 if (out_pos > OUT_SIZE - 20)
2713 out_flush();
2714#ifdef HAVE_TGETENT
2715 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2716#else
2717 while (*s)
2718 out_char_nf(*s++);
2719#endif
2720
2721 /* For testing we write one string at a time. */
2722 if (p_wd)
2723 out_flush();
2724 }
2725}
2726
2727/*
2728 * cursor positioning using termcap parser. (jw)
2729 */
2730 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002731term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 OUT_STR(tgoto((char *)T_CM, col, row));
2734}
2735
2736 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002737term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 OUT_STR(tgoto((char *)T_CRI, 0, i));
2740}
2741
2742 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002743term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2746}
2747
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2752}
2753
2754#if defined(HAVE_TGETENT) || defined(PROTO)
2755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002756term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 /* Can't handle a negative value here */
2759 if (x < 0)
2760 x = 0;
2761 if (y < 0)
2762 y = 0;
2763 OUT_STR(tgoto((char *)T_CWP, y, x));
2764}
2765
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002766# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2767/*
2768 * Return TRUE if we can request the terminal for a response.
2769 */
2770 static int
2771can_get_termresponse()
2772{
2773 return cur_tmode == TMODE_RAW
2774 && termcap_active
2775# ifdef UNIX
2776 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2777# endif
2778 && p_ek;
2779}
2780
2781static int winpos_x;
2782static int winpos_y;
2783static int waiting_for_winpos = FALSE;
2784
2785/*
2786 * Try getting the Vim window position from the terminal.
2787 * Returns OK or FAIL.
2788 */
2789 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002790term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002791{
2792 int count = 0;
2793
2794 if (*T_CGP == NUL || !can_get_termresponse())
2795 return FAIL;
2796 winpos_x = -1;
2797 winpos_y = -1;
2798 waiting_for_winpos = TRUE;
2799 OUT_STR(T_CGP);
2800 out_flush();
2801
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002802 /* Try reading the result for "timeout" msec. */
2803 while (count++ < timeout / 10)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002804 {
2805 (void)vpeekc_nomap();
2806 if (winpos_x >= 0 && winpos_y >= 0)
2807 {
2808 *x = winpos_x;
2809 *y = winpos_y;
2810 waiting_for_winpos = FALSE;
2811 return OK;
2812 }
2813 ui_delay(10, FALSE);
2814 }
2815 waiting_for_winpos = FALSE;
2816 return FALSE;
2817}
2818# endif
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002821term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002823 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824}
2825#endif
2826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002828term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
2830 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002831 int i = *s == CSI ? 1 : 2;
2832 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 /* Special handling of 16 colors, because termcap can't handle it */
2835 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2836 /* Also accept CSI instead of <Esc>[ */
2837 if (n >= 8 && t_colors >= 16
2838 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2839 && s[i] != NUL
2840 && (STRCMP(s + i + 1, "%p1%dm") == 0
2841 || STRCMP(s + i + 1, "%dm") == 0)
2842 && (s[i] == '3' || s[i] == '4'))
2843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002845 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002847 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002849 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2851 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2852 : (n >= 16 ? "48;5;" : "10"));
2853 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2854 }
2855 else
2856 OUT_STR(tgoto((char *)s, 0, n));
2857}
2858
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002859 void
2860term_fg_color(int n)
2861{
2862 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2863 if (*T_CAF)
2864 term_color(T_CAF, n);
2865 else if (*T_CSF)
2866 term_color(T_CSF, n);
2867}
2868
2869 void
2870term_bg_color(int n)
2871{
2872 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2873 if (*T_CAB)
2874 term_color(T_CAB, n);
2875 else if (*T_CSB)
2876 term_color(T_CSB, n);
2877}
2878
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002879#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002880
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002881#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2882#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2883#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002884
2885 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002886term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002887{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002888#define MAX_COLOR_STR_LEN 100
2889 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002890
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002891 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002892 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002893 OUT_STR(buf);
2894}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002895
2896 void
2897term_fg_rgb_color(guicolor_T rgb)
2898{
2899 term_rgb_color(T_8F, rgb);
2900}
2901
2902 void
2903term_bg_rgb_color(guicolor_T rgb)
2904{
2905 term_rgb_color(T_8B, rgb);
2906}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002907#endif
2908
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002909#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2910 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911/*
2912 * Generic function to set window title, using t_ts and t_fs.
2913 */
2914 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002915term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 /* t_ts takes one argument: column in status line */
2918 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2919 out_str_nf(title);
2920 out_str(T_FS); /* set title end */
2921 out_flush();
2922}
2923#endif
2924
2925/*
2926 * Make sure we have a valid set or terminal options.
2927 * Replace all entries that are NULL by empty_option
2928 */
2929 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002930ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002932 char_u *env_colors;
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 check_options(); /* make sure no options are NULL */
2935
2936 /*
2937 * MUST have "cm": cursor motion.
2938 */
2939 if (*T_CM == NUL)
2940 EMSG(_("E437: terminal capability \"cm\" required"));
2941
2942 /*
2943 * if "cs" defined, use a scroll region, it's faster.
2944 */
2945 if (*T_CS != NUL)
2946 scroll_region = TRUE;
2947 else
2948 scroll_region = FALSE;
2949
2950 if (pairs)
2951 {
2952 /*
2953 * optional pairs
2954 */
2955 /* TP goes to normal mode for TI (invert) and TB (bold) */
2956 if (*T_ME == NUL)
2957 T_ME = T_MR = T_MD = T_MB = empty_option;
2958 if (*T_SO == NUL || *T_SE == NUL)
2959 T_SO = T_SE = empty_option;
2960 if (*T_US == NUL || *T_UE == NUL)
2961 T_US = T_UE = empty_option;
2962 if (*T_CZH == NUL || *T_CZR == NUL)
2963 T_CZH = T_CZR = empty_option;
2964
2965 /* T_VE is needed even though T_VI is not defined */
2966 if (*T_VE == NUL)
2967 T_VI = empty_option;
2968
2969 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2970 if (*T_ME == NUL)
2971 {
2972 T_ME = T_SE;
2973 T_MR = T_SO;
2974 T_MD = T_SO;
2975 }
2976
2977 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2978 if (*T_SO == NUL)
2979 {
2980 T_SE = T_ME;
2981 if (*T_MR == NUL)
2982 T_SO = T_MD;
2983 else
2984 T_SO = T_MR;
2985 }
2986
2987 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2988 if (*T_CZH == NUL)
2989 {
2990 T_CZR = T_ME;
2991 if (*T_MR == NUL)
2992 T_CZH = T_MD;
2993 else
2994 T_CZH = T_MR;
2995 }
2996
2997 /* "Sb" and "Sf" come in pairs */
2998 if (*T_CSB == NUL || *T_CSF == NUL)
2999 {
3000 T_CSB = empty_option;
3001 T_CSF = empty_option;
3002 }
3003
3004 /* "AB" and "AF" come in pairs */
3005 if (*T_CAB == NUL || *T_CAF == NUL)
3006 {
3007 T_CAB = empty_option;
3008 T_CAF = empty_option;
3009 }
3010
3011 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3012 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003013 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /* Set 'weirdinvert' according to value of 't_xs' */
3016 p_wiv = (*T_XS != NUL);
3017 }
3018 need_gather = TRUE;
3019
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003020 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003022 env_colors = mch_getenv((char_u *)"COLORS");
3023 if (env_colors != NULL && isdigit(*env_colors))
3024 {
3025 int colors = atoi((char *)env_colors);
3026
3027 if (colors != t_colors)
3028 set_color_count(colors);
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030}
3031
3032#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3033 || defined(PROTO)
3034/*
3035 * Represent the given long_u as individual bytes, with the most significant
3036 * byte first, and store them in dst.
3037 */
3038 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003039add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 int i;
3042 int shift;
3043
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003044 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
3046 shift = 8 * (sizeof(long_u) - i);
3047 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3048 }
3049}
3050
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003051static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053/*
3054 * Interpret the next string of bytes in buf as a long integer, with the most
3055 * significant byte first. Note that it is assumed that buf has been through
3056 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3057 * Puts result in val, and returns the number of bytes read from buf
3058 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3059 * were present.
3060 */
3061 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003062get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 int len;
3065 char_u bytes[sizeof(long_u)];
3066 int i;
3067 int shift;
3068
3069 *val = 0;
3070 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3071 if (len != -1)
3072 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003073 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 shift = 8 * (sizeof(long_u) - 1 - i);
3076 *val += (long_u)bytes[i] << shift;
3077 }
3078 }
3079 return len;
3080}
3081#endif
3082
3083#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003084 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3085 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
3087 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3088 * that buf has been through inchar(). Returns the actual number of bytes used
3089 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3090 * available.
3091 */
3092 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003093get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 int len = 0;
3096 int i;
3097 char_u c;
3098
3099 for (i = 0; i < num_bytes; i++)
3100 {
3101 if ((c = buf[len++]) == NUL)
3102 return -1;
3103 if (c == K_SPECIAL)
3104 {
3105 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3106 return -1;
3107 if (buf[len++] == (int)KS_ZERO)
3108 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003109 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3110 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3111 if (buf[len++] == (int)KE_CSI)
3112 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003114 else if (c == CSI && buf[len] == KS_EXTRA
3115 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003116 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3117 * the start of a special key, see add_to_input_buf_csi(). */
3118 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 bytes[i] = c;
3120 }
3121 return len;
3122}
3123#endif
3124
3125/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003126 * Check if the new shell size is valid, correct it if it's too small or way
3127 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
3129 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003130check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (Rows < min_rows()) /* need room for one window and command line */
3133 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003134 limit_screen_size();
3135}
3136
3137/*
3138 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3139 */
3140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003141limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003142{
3143 if (Columns < MIN_COLUMNS)
3144 Columns = MIN_COLUMNS;
3145 else if (Columns > 10000)
3146 Columns = 10000;
3147 if (Rows > 1000)
3148 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149}
3150
Bram Moolenaar86b68352004-12-27 21:59:20 +00003151/*
3152 * Invoked just before the screen structures are going to be (re)allocated.
3153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003155win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 static int old_Rows = 0;
3158 static int old_Columns = 0;
3159
3160 if (old_Rows != Rows || old_Columns != Columns)
3161 ui_new_shellsize();
3162 if (old_Rows != Rows)
3163 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003164 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003165 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003166 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 old_Rows = Rows;
3168 shell_new_rows(); /* update window sizes */
3169 }
3170 if (old_Columns != Columns)
3171 {
3172 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175}
3176
3177/*
3178 * Call this function when the Vim shell has been resized in any way.
3179 * Will obtain the current size and redraw (also when size didn't change).
3180 */
3181 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003182shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 set_shellsize(0, 0, FALSE);
3185}
3186
3187/*
3188 * Check if the shell size changed. Handle a resize.
3189 * When the size didn't change, nothing happens.
3190 */
3191 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003192shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 int old_Rows = Rows;
3195 int old_Columns = Columns;
3196
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003197 if (!exiting
3198#ifdef FEAT_GUI
3199 /* Do not get the size when executing a shell command during
3200 * startup. */
3201 && !gui.starting
3202#endif
3203 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003204 {
3205 (void)ui_get_shellsize();
3206 check_shellsize();
3207 if (old_Rows != Rows || old_Columns != Columns)
3208 shell_resized();
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210}
3211
3212/*
3213 * Set size of the Vim shell.
3214 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3215 * window size (this is used for the :win command).
3216 * If 'mustset' is FALSE, we may try to get the real window size and if
3217 * it fails use 'width' and 'height'.
3218 */
3219 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003220set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 static int busy = FALSE;
3223
3224 /*
3225 * Avoid recursiveness, can happen when setting the window size causes
3226 * another window-changed signal.
3227 */
3228 if (busy)
3229 return;
3230
3231 if (width < 0 || height < 0) /* just checking... */
3232 return;
3233
Bram Moolenaara971b822011-09-14 14:43:25 +02003234 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003236 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 State = SETWSIZE;
3238 return;
3239 }
3240
Bram Moolenaara971b822011-09-14 14:43:25 +02003241 /* curwin->w_buffer can be NULL when we are closing a window and the
3242 * buffer has already been closed and removing a scrollbar causes a resize
3243 * event. Don't resize then, it will happen after entering another buffer.
3244 */
3245 if (curwin->w_buffer == NULL)
3246 return;
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 ++busy;
3249
3250#ifdef AMIGA
3251 out_flush(); /* must do this before mch_get_shellsize() for
3252 some obscure reason */
3253#endif
3254
3255 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3256 {
3257 Rows = height;
3258 Columns = width;
3259 check_shellsize();
3260 ui_set_shellsize(mustset);
3261 }
3262 else
3263 check_shellsize();
3264
3265 /* The window layout used to be adjusted here, but it now happens in
3266 * screenalloc() (also invoked from screenclear()). That is because the
3267 * "busy" check above may skip this, but not screenalloc(). */
3268
3269 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3270 screenclear();
3271 else
3272 screen_start(); /* don't know where cursor is now */
3273
3274 if (starting != NO_SCREEN)
3275 {
3276#ifdef FEAT_TITLE
3277 maketitle();
3278#endif
3279 changed_line_abv_curs();
3280 invalidate_botline();
3281
3282 /*
3283 * We only redraw when it's needed:
3284 * - While at the more prompt or executing an external command, don't
3285 * redraw, but position the cursor.
3286 * - While editing the command line, only redraw that.
3287 * - in Ex mode, don't redraw anything.
3288 * - Otherwise, redraw right now, and position the cursor.
3289 * Always need to call update_screen() or screenalloc(), to make
3290 * sure Rows/Columns and the size of ScreenLines[] is correct!
3291 */
3292 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3293 || exmode_active)
3294 {
3295 screenalloc(FALSE);
3296 repeat_message();
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 else
3299 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003300 if (curwin->w_p_scb)
3301 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003302 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003303 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003304 update_screen(NOT_VALID);
3305 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003306 }
3307 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003308 {
3309 update_topline();
3310#if defined(FEAT_INS_EXPAND)
3311 if (pum_visible())
3312 {
3313 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003314 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003315 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003316#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003317 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003318 if (redrawing())
3319 setcursor();
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322 cursor_on(); /* redrawing may have switched it off */
3323 }
3324 out_flush();
3325 --busy;
3326}
3327
3328/*
3329 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3330 * commands and Ex mode).
3331 */
3332 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003333settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335#ifdef FEAT_GUI
3336 /* don't set the term where gvim was started to any mode */
3337 if (gui.in_use)
3338 return;
3339#endif
3340
3341 if (full_screen)
3342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /*
3344 * When returning after calling a shell we want to really set the
3345 * terminal to raw mode, even though we think it already is, because
3346 * the shell program may have reset the terminal mode.
3347 * When we think the terminal is normal, don't try to set it to
3348 * normal again, because that causes problems (logout!) on some
3349 * machines.
3350 */
3351 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3352 {
3353#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003354# ifdef FEAT_GUI
3355 if (!gui.in_use && !gui.starting)
3356# endif
3357 {
3358 /* May need to check for T_CRV response and termcodes, it
3359 * doesn't work in Cooked mode, an external program may get
3360 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003361 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3362 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003363#ifdef FEAT_TERMINAL
3364 || rfg_status == STATUS_SENT
3365#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003366 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003367 || rbm_status == STATUS_SENT
3368 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003369 (void)vpeekc_nomap();
3370 check_for_codes_from_term();
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
3373#ifdef FEAT_MOUSE_TTY
3374 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003375 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003377 if (tmode != TMODE_RAW)
3378 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003380 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 cur_tmode = tmode;
3382#ifdef FEAT_MOUSE
3383 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003384 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003386 if (tmode == TMODE_RAW)
3387 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 out_flush();
3389 }
3390#ifdef FEAT_TERMRESPONSE
3391 may_req_termresponse();
3392#endif
3393 }
3394}
3395
3396 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003397starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 if (full_screen && !termcap_active)
3400 {
3401 out_str(T_TI); /* start termcap mode */
3402 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003403 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 out_flush();
3405 termcap_active = TRUE;
3406 screen_start(); /* don't know where cursor is now */
3407#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003408# ifdef FEAT_GUI
3409 if (!gui.in_use && !gui.starting)
3410# endif
3411 {
3412 may_req_termresponse();
3413 /* Immediately check for a response. If t_Co changes, we don't
3414 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003415 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003416 check_for_codes_from_term();
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#endif
3419 }
3420}
3421
3422 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003423stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 screen_stop_highlight();
3426 reset_cterm_colors();
3427 if (termcap_active)
3428 {
3429#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003430# ifdef FEAT_GUI
3431 if (!gui.in_use && !gui.starting)
3432# endif
3433 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003434 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003435 if (crv_status == STATUS_SENT
3436 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003437# ifdef FEAT_TERMINAL
3438 || rfg_status == STATUS_SENT
3439# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003440 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003441 || rbm_status == STATUS_SENT
3442 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003443 {
3444# ifdef UNIX
3445 /* Give the terminal a chance to respond. */
3446 mch_delay(100L, FALSE);
3447# endif
3448# ifdef TCIFLUSH
3449 /* Discard data received but not read. */
3450 if (exiting)
3451 tcflush(fileno(stdin), TCIFLUSH);
3452# endif
3453 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003454 /* Check for termcodes first, otherwise an external program may
3455 * get them. */
3456 check_for_codes_from_term();
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003459 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 out_str(T_KE); /* stop "keypad transmit" mode */
3461 out_flush();
3462 termcap_active = FALSE;
3463 cursor_on(); /* just in case it is still off */
3464 out_str(T_TE); /* stop termcap mode */
3465 screen_start(); /* don't know where cursor is now */
3466 out_flush();
3467 }
3468}
3469
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003470#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471/*
3472 * Request version string (for xterm) when needed.
3473 * Only do this after switching to raw mode, otherwise the result will be
3474 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003475 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003476 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 * Only do this after termcap mode has been started, otherwise the codes for
3478 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003479 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3480 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003481 * On Unix only do it when both output and input are a tty (avoid writing
3482 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 * The result is caught in check_termcode().
3484 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003486may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003488 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003489 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003490 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 && *T_CRV != NUL)
3492 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003493 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003495 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 /* check for the characters now, otherwise they might be eaten by
3497 * get_keystroke() */
3498 out_flush();
3499 (void)vpeekc_nomap();
3500 }
3501}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003502
3503# if defined(FEAT_MBYTE) || defined(PROTO)
3504/*
3505 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003506 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003507 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003508 * If the terminal treats \u25bd as single width, the position is (1, 1),
3509 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003510 * This function has the side effect that changes cursor position, so
3511 * it must be called immediately after entering termcap mode.
3512 */
3513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003514may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003515{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003516 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003517 && can_get_termresponse()
3518 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003519 && *T_U7 != NUL
3520 && !option_was_set((char_u *)"ambiwidth"))
3521 {
3522 char_u buf[16];
3523
Bram Moolenaar2951b772013-07-03 12:45:31 +02003524 LOG_TR("Sending U7 request");
3525 /* Do this in the second row. In the first row the returned sequence
3526 * may be CSI 1;2R, which is the same as <S-F3>. */
3527 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003528 buf[mb_char2bytes(0x25bd, buf)] = 0;
3529 out_str(buf);
3530 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003531 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003532 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003533
3534 /* This overwrites a few characters on the screen, a redraw is needed
3535 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003536 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003537 out_str((char_u *)" ");
3538 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003539
Bram Moolenaar05684312017-12-09 15:11:24 +01003540 /* Need to reset the known cursor position. */
3541 screen_start();
3542
Bram Moolenaar9584b312013-03-13 19:29:28 +01003543 /* check for the characters now, otherwise they might be eaten by
3544 * get_keystroke() */
3545 out_flush();
3546 (void)vpeekc_nomap();
3547 }
3548}
3549# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003550
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003551/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003552 * Similar to requesting the version string: Request the terminal background
3553 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003554 */
3555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003556may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003557{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003558 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003559 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003560 int didit = FALSE;
3561
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003562# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003563 /* Only request foreground if t_RF is set. */
3564 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3565 {
3566 LOG_TR("Sending FG request");
3567 out_str(T_RFG);
3568 rfg_status = STATUS_SENT;
3569 didit = TRUE;
3570 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003571# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003572
3573 /* Only request background if t_RB is set. */
3574 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003575 {
3576 LOG_TR("Sending BG request");
3577 out_str(T_RBG);
3578 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003579 didit = TRUE;
3580 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003581
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003582 if (didit)
3583 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003584 /* check for the characters now, otherwise they might be eaten by
3585 * get_keystroke() */
3586 out_flush();
3587 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003588 }
3589 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003590}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003591
Bram Moolenaar2951b772013-07-03 12:45:31 +02003592# ifdef DEBUG_TERMRESPONSE
3593 static void
3594log_tr(char *msg)
3595{
3596 static FILE *fd_tr = NULL;
3597 static proftime_T start;
3598 proftime_T now;
3599
3600 if (fd_tr == NULL)
3601 {
3602 fd_tr = fopen("termresponse.log", "w");
3603 profile_start(&start);
3604 }
3605 now = start;
3606 profile_end(&now);
3607 fprintf(fd_tr, "%s: %s %s\n",
3608 profile_msg(&now),
3609 must_redraw == NOT_VALID ? "NV"
3610 : must_redraw == CLEAR ? "CL" : " ",
3611 msg);
3612}
3613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614#endif
3615
3616/*
3617 * Return TRUE when saving and restoring the screen.
3618 */
3619 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003620swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
3622 return (full_screen && *T_TI != NUL);
3623}
3624
3625#ifdef FEAT_MOUSE
3626/*
3627 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3628 */
3629 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003630setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632# ifdef FEAT_MOUSE_TTY
3633 int checkfor;
3634# endif
3635
3636# ifdef FEAT_MOUSESHAPE
3637 update_mouseshape(-1);
3638# endif
3639
3640# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3641# ifdef FEAT_GUI
3642 /* In the GUI the mouse is always enabled. */
3643 if (gui.in_use)
3644 return;
3645# endif
3646 /* be quick when mouse is off */
3647 if (*p_mouse == NUL || has_mouse_termcode == 0)
3648 return;
3649
3650 /* don't switch mouse on when not in raw mode (Ex mode) */
3651 if (cur_tmode != TMODE_RAW)
3652 {
3653 mch_setmouse(FALSE);
3654 return;
3655 }
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (VIsual_active)
3658 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003659 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 checkfor = MOUSE_RETURN;
3661 else if (State & INSERT)
3662 checkfor = MOUSE_INSERT;
3663 else if (State & CMDLINE)
3664 checkfor = MOUSE_COMMAND;
3665 else if (State == CONFIRM || State == EXTERNCMD)
3666 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3667 else
3668 checkfor = MOUSE_NORMAL; /* assume normal mode */
3669
3670 if (mouse_has(checkfor))
3671 mch_setmouse(TRUE);
3672 else
3673 mch_setmouse(FALSE);
3674# endif
3675}
3676
3677/*
3678 * Return TRUE if
3679 * - "c" is in 'mouse', or
3680 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3681 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3682 * normal editing mode (not at hit-return message).
3683 */
3684 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003685mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 char_u *p;
3688
3689 for (p = p_mouse; *p; ++p)
3690 switch (*p)
3691 {
3692 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3693 return TRUE;
3694 break;
3695 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3696 return TRUE;
3697 break;
3698 default: if (c == *p) return TRUE; break;
3699 }
3700 return FALSE;
3701}
3702
3703/*
3704 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3705 */
3706 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003707mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 return (p_mousem[0] == 'p');
3710}
3711#endif
3712
3713/*
3714 * By outputting the 'cursor very visible' termcap code, for some windowed
3715 * terminals this makes the screen scrolled to the correct position.
3716 * Used when starting Vim or returning from a shell.
3717 */
3718 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003719scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003721 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
3723 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003724 out_str(T_CVS);
3725 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727}
3728
3729static int cursor_is_off = FALSE;
3730
3731/*
3732 * Enable the cursor.
3733 */
3734 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003735cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 if (cursor_is_off)
3738 {
3739 out_str(T_VE);
3740 cursor_is_off = FALSE;
3741 }
3742}
3743
3744/*
3745 * Disable the cursor.
3746 */
3747 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003748cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003750 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003752 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 cursor_is_off = TRUE;
3754 }
3755}
3756
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003757#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003759 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003760 */
3761 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003762term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003763{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003764 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003765 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003766
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003767 /* Only do something when redrawing the screen and we can restore the
3768 * mode. */
3769 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003770 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003771# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003772 if (forced && initial_cursor_shape > 0)
3773 /* Restore to initial values. */
3774 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003775# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003776 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003777 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003778
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003779 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003780 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003781 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003782 {
3783 if (*T_CSR != NUL)
3784 p = T_CSR; /* Replace mode cursor */
3785 else
3786 p = T_CSI; /* fall back to Insert mode cursor */
3787 if (*p != NUL)
3788 {
3789 out_str(p);
3790 showing_mode = REPLACE;
3791 }
3792 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003793 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003794 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003795 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003796 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003797 {
3798 out_str(T_CSI); /* Insert mode cursor */
3799 showing_mode = INSERT;
3800 }
3801 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003802 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003803 {
3804 out_str(T_CEI); /* non-Insert mode cursor */
3805 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003806 }
3807}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003808
3809# if defined(FEAT_TERMINAL) || defined(PROTO)
3810 void
3811term_cursor_color(char_u *color)
3812{
3813 if (*T_CSC != NUL)
3814 {
3815 out_str(T_CSC); /* set cursor color start */
3816 out_str_nf(color);
3817 out_str(T_CEC); /* set cursor color end */
3818 out_flush();
3819 }
3820}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003821# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003822
Bram Moolenaar4db25542017-08-28 22:43:05 +02003823 int
3824blink_state_is_inverted()
3825{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003826#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003827 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3828 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003829#else
3830 return FALSE;
3831#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003832}
3833
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003834/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003835 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003836 */
3837 void
3838term_cursor_shape(int shape, int blink)
3839{
3840 if (*T_CSH != NUL)
3841 {
3842 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3843 out_flush();
3844 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003845 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003846 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003847 int do_blink = blink;
3848
3849 /* t_SH is empty: try setting just the blink state.
3850 * The blink flags are XORed together, if the initial blinking from
3851 * style and shape differs, we need to invert the flag here. */
3852 if (blink_state_is_inverted())
3853 do_blink = !blink;
3854
3855 if (do_blink && *T_VS != NUL)
3856 {
3857 out_str(T_VS);
3858 out_flush();
3859 }
3860 else if (!do_blink && *T_CVS != NUL)
3861 {
3862 out_str(T_CVS);
3863 out_flush();
3864 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003865 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003866}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003867#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Set scrolling region for window 'wp'.
3871 * The region starts 'off' lines from the start of the window.
3872 * Also set the vertical scroll region for a vertically split window. Always
3873 * the full width of the window, excluding the vertical separator.
3874 */
3875 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003876scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3879 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003881 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3882 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 screen_start(); /* don't know where cursor is now */
3884}
3885
3886/*
3887 * Reset scrolling region to the whole screen.
3888 */
3889 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003890scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (*T_CSV != NUL)
3894 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 screen_start(); /* don't know where cursor is now */
3896}
3897
3898
3899/*
3900 * List of terminal codes that are currently recognized.
3901 */
3902
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003903static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 char_u name[2]; /* termcap name of entry */
3906 char_u *code; /* terminal code (in allocated memory) */
3907 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003908 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909} *termcodes = NULL;
3910
3911static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3912static int tc_len = 0; /* current number of entries in termcodes[] */
3913
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003914static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003917clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 while (tc_len > 0)
3920 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003921 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 tc_max_len = 0;
3923
3924#ifdef HAVE_TGETENT
3925 BC = (char *)empty_option;
3926 UP = (char *)empty_option;
3927 PC = NUL; /* set pad character to NUL */
3928 ospeed = 0;
3929#endif
3930
3931 need_gather = TRUE; /* need to fill termleader[] */
3932}
3933
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003934#define ATC_FROM_TERM 55
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936/*
3937 * Add a new entry to the list of terminal codes.
3938 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003939 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3940 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 */
3942 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003943add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 struct termcode *new_tc;
3946 int i, j;
3947 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003948 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 if (string == NULL || *string == NUL)
3951 {
3952 del_termcode(name);
3953 return;
3954 }
3955
Bram Moolenaar45500912014-07-09 20:51:07 +02003956#if defined(WIN3264) && !defined(FEAT_GUI)
3957 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3958#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (s == NULL)
3962 return;
3963
3964 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003967 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 s[0] = term_7to8bit(string);
3969 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003970
3971#if defined(WIN3264) && !defined(FEAT_GUI)
3972 if (s[0] == K_NUL)
3973 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003974 STRMOVE(s + 1, s);
3975 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003976 }
3977#endif
3978
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003979 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 need_gather = TRUE; /* need to fill termleader[] */
3982
3983 /*
3984 * need to make space for more entries
3985 */
3986 if (tc_len == tc_max_len)
3987 {
3988 tc_max_len += 20;
3989 new_tc = (struct termcode *)alloc(
3990 (unsigned)(tc_max_len * sizeof(struct termcode)));
3991 if (new_tc == NULL)
3992 {
3993 tc_max_len -= 20;
3994 return;
3995 }
3996 for (i = 0; i < tc_len; ++i)
3997 new_tc[i] = termcodes[i];
3998 vim_free(termcodes);
3999 termcodes = new_tc;
4000 }
4001
4002 /*
4003 * Look for existing entry with the same name, it is replaced.
4004 * Look for an existing entry that is alphabetical higher, the new entry
4005 * is inserted in front of it.
4006 */
4007 for (i = 0; i < tc_len; ++i)
4008 {
4009 if (termcodes[i].name[0] < name[0])
4010 continue;
4011 if (termcodes[i].name[0] == name[0])
4012 {
4013 if (termcodes[i].name[1] < name[1])
4014 continue;
4015 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 */
4018 if (termcodes[i].name[1] == name[1])
4019 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004020 if (flags == ATC_FROM_TERM && (j = termcode_star(
4021 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004022 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004023 /* Don't replace ESC[123;*X or ESC O*X with another when
4024 * invoked from got_code_from_term(). */
4025 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004026 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004027 && s[len - 1]
4028 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004029 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004030 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004031 vim_free(s);
4032 return;
4033 }
4034 }
4035 else
4036 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004037 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004038 vim_free(termcodes[i].code);
4039 --tc_len;
4040 break;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043 }
4044 /*
4045 * Found alphabetical larger entry, move rest to insert new entry
4046 */
4047 for (j = tc_len; j > i; --j)
4048 termcodes[j] = termcodes[j - 1];
4049 break;
4050 }
4051
4052 termcodes[i].name[0] = name[0];
4053 termcodes[i].name[1] = name[1];
4054 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004055 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056
4057 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4058 * accept modifiers. */
4059 termcodes[i].modlen = 0;
4060 j = termcode_star(s, len);
4061 if (j > 0)
4062 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 ++tc_len;
4064}
4065
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004066/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004067 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004069 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070 */
4071 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004072termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004073{
4074 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4075 if (len >= 3 && code[len - 2] == '*')
4076 {
4077 if (len >= 5 && code[len - 3] == ';')
4078 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004079 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004080 return 1;
4081 }
4082 return 0;
4083}
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 int i;
4089
4090 for (i = 0; i < tc_len; ++i)
4091 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4092 return termcodes[i].code;
4093 return NULL;
4094}
4095
4096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4097 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004098get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 if (i >= tc_len)
4101 return NULL;
4102 return &termcodes[i].name[0];
4103}
4104#endif
4105
4106 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004107del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 int i;
4110
4111 if (termcodes == NULL) /* nothing there yet */
4112 return;
4113
4114 need_gather = TRUE; /* need to fill termleader[] */
4115
4116 for (i = 0; i < tc_len; ++i)
4117 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4118 {
4119 del_termcode_idx(i);
4120 return;
4121 }
4122 /* not found. Give error message? */
4123}
4124
4125 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004126del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
4128 int i;
4129
4130 vim_free(termcodes[idx].code);
4131 --tc_len;
4132 for (i = idx; i < tc_len; ++i)
4133 termcodes[i] = termcodes[i + 1];
4134}
4135
4136#ifdef FEAT_TERMRESPONSE
4137/*
4138 * Called when detected that the terminal sends 8-bit codes.
4139 * Convert all 7-bit codes to their 8-bit equivalent.
4140 */
4141 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004142switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 int i;
4145 int c;
4146
4147 /* Only need to do something when not already using 8-bit codes. */
4148 if (!term_is_8bit(T_NAME))
4149 {
4150 for (i = 0; i < tc_len; ++i)
4151 {
4152 c = term_7to8bit(termcodes[i].code);
4153 if (c != 0)
4154 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004155 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 termcodes[i].code[0] = c;
4157 }
4158 }
4159 need_gather = TRUE; /* need to fill termleader[] */
4160 }
4161 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004162 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163}
4164#endif
4165
4166#ifdef CHECK_DOUBLE_CLICK
4167static linenr_T orig_topline = 0;
4168# ifdef FEAT_DIFF
4169static int orig_topfill = 0;
4170# endif
4171#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004172#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173/*
4174 * Checking for double clicks ourselves.
4175 * "orig_topline" is used to avoid detecting a double-click when the window
4176 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4177 */
4178/*
4179 * Set orig_topline. Used when jumping to another window, so that a double
4180 * click still works.
4181 */
4182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004183set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
4185 orig_topline = wp->w_topline;
4186# ifdef FEAT_DIFF
4187 orig_topfill = wp->w_topfill;
4188# endif
4189}
4190#endif
4191
4192/*
4193 * Check if typebuf.tb_buf[] contains a terminal key code.
4194 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4195 * + max_offset].
4196 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004197 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * With a match, the match is removed, the replacement code is inserted in
4199 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4200 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004201 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4202 * "buflen" is then the length of the string in buf[] and is updated for
4203 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004206check_termcode(
4207 int max_offset,
4208 char_u *buf,
4209 int bufsize,
4210 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 char_u *tp;
4213 char_u *p;
4214 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004215 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004217 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int offset;
4219 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004221 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int new_slen;
4224 int extra;
4225 char_u string[MAX_KEY_CODE_LEN + 1];
4226 int i, j;
4227 int idx = 0;
4228#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004229# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4230 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u bytes[6];
4232 int num_bytes;
4233# endif
4234 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int is_click, is_drag;
4236 int wheel_code = 0;
4237 int current_button;
4238 static int held_button = MOUSE_RELEASE;
4239 static int orig_num_clicks = 1;
4240 static int orig_mouse_code = 0x0;
4241# ifdef CHECK_DOUBLE_CLICK
4242 static int orig_mouse_col = 0;
4243 static int orig_mouse_row = 0;
4244 static struct timeval orig_mouse_time = {0, 0};
4245 /* time of previous mouse click */
4246 struct timeval mouse_time; /* time of current mouse click */
4247 long timediff; /* elapsed time in msec */
4248# endif
4249#endif
4250 int cpo_koffset;
4251#ifdef FEAT_MOUSE_GPM
4252 extern int gpm_flag; /* gpm library variable */
4253#endif
4254
4255 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4256
4257 /*
4258 * Speed up the checks for terminal codes by gathering all first bytes
4259 * used in termleader[]. Often this is just a single <Esc>.
4260 */
4261 if (need_gather)
4262 gather_termleader();
4263
4264 /*
4265 * Check at several positions in typebuf.tb_buf[], to catch something like
4266 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4267 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004268 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 * This is used often, KEEP IT FAST!
4270 */
4271 for (offset = 0; offset < max_offset; ++offset)
4272 {
4273 if (buf == NULL)
4274 {
4275 if (offset >= typebuf.tb_len)
4276 break;
4277 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4278 len = typebuf.tb_len - offset; /* length of the input */
4279 }
4280 else
4281 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004282 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 break;
4284 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004285 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287
4288 /*
4289 * Don't check characters after K_SPECIAL, those are already
4290 * translated terminal chars (avoid translating ~@^Hx).
4291 */
4292 if (*tp == K_SPECIAL)
4293 {
4294 offset += 2; /* there are always 2 extra characters */
4295 continue;
4296 }
4297
4298 /*
4299 * Skip this position if the character does not appear as the first
4300 * character in term_strings. This speeds up a lot, since most
4301 * termcodes start with the same character (ESC or CSI).
4302 */
4303 i = *tp;
4304 for (p = termleader; *p && *p != i; ++p)
4305 ;
4306 if (*p == NUL)
4307 continue;
4308
4309 /*
4310 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4311 * are in Insert mode.
4312 */
4313 if (*tp == ESC && !p_ek && (State & INSERT))
4314 continue;
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004317 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004318 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320#ifdef FEAT_GUI
4321 if (gui.in_use)
4322 {
4323 /*
4324 * GUI special key codes are all of the form [CSI xx].
4325 */
4326 if (*tp == CSI) /* Special key from GUI */
4327 {
4328 if (len < 3)
4329 return -1; /* Shouldn't happen */
4330 slen = 3;
4331 key_name[0] = tp[1];
4332 key_name[1] = tp[2];
4333 }
4334 }
4335 else
4336#endif /* FEAT_GUI */
4337 {
4338 for (idx = 0; idx < tc_len; ++idx)
4339 {
4340 /*
4341 * Ignore the entry if we are not at the start of
4342 * typebuf.tb_buf[]
4343 * and there are not enough characters to make a match.
4344 * But only when the 'K' flag is in 'cpoptions'.
4345 */
4346 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004347 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 if (cpo_koffset && offset && len < slen)
4349 continue;
4350 if (STRNCMP(termcodes[idx].code, tp,
4351 (size_t)(slen > len ? len : slen)) == 0)
4352 {
4353 if (len < slen) /* got a partial sequence */
4354 return -1; /* need to get more chars */
4355
4356 /*
4357 * When found a keypad key, check if there is another key
4358 * that matches and use that one. This makes <Home> to be
4359 * found instead of <kHome> when they produce the same
4360 * key code.
4361 */
4362 if (termcodes[idx].name[0] == 'K'
4363 && VIM_ISDIGIT(termcodes[idx].name[1]))
4364 {
4365 for (j = idx + 1; j < tc_len; ++j)
4366 if (termcodes[j].len == slen &&
4367 STRNCMP(termcodes[idx].code,
4368 termcodes[j].code, slen) == 0)
4369 {
4370 idx = j;
4371 break;
4372 }
4373 }
4374
4375 key_name[0] = termcodes[idx].name[0];
4376 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 break;
4378 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004379
4380 /*
4381 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004382 * <Esc>[123;*X (modslen == slen - 3)
4383 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4384 * When there is a modifier the * matches a number.
4385 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004386 */
4387 if (termcodes[idx].modlen > 0)
4388 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004389 modslen = termcodes[idx].modlen;
4390 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004391 continue;
4392 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004393 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004394 {
4395 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004396
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004397 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004398 return -1; /* need to get more chars */
4399
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004400 if (tp[modslen] == termcodes[idx].code[slen - 1])
4401 slen = modslen + 1; /* no modifiers */
4402 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004403 continue; /* no match */
4404 else
4405 {
4406 /* Skip over the digits, the final char must
4407 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004408 for (j = slen - 2; j < len && (isdigit(tp[j])
4409 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004410 ;
4411 ++j;
4412 if (len < j) /* got a partial sequence */
4413 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004414 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4415 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004416
Bram Moolenaara529ce02017-06-22 22:37:57 +02004417 modifiers_start = tp + slen - 2;
4418
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004419 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004420 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004421 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004422 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004423 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004424 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004425 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004427 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004428 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004429
4430 slen = j;
4431 }
4432 key_name[0] = termcodes[idx].name[0];
4433 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004434 break;
4435 }
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 }
4439
4440#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004441 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004442 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004443 * detecting the start of these mouse codes they might as well be
4444 * another key code or terminal response. */
4445# ifdef FEAT_MOUSE_DEC
4446 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004447# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004448# ifdef FEAT_MOUSE_PTERM
4449 || key_name[0] == KS_PTERM_MOUSE
4450# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004451 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004453 /* Check for some responses from the terminal starting with
4454 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004455 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004456 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004457 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004458 * Also eat other possible responses to t_RV, rxvt returns
4459 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4460 * mrxvt has been reported to have "+" in the version. Assume
4461 * the escape sequence ends with a letter or one of "{|}~".
4462 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004463 * - Cursor position report: <Esc>[{row};{col}R
4464 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004465 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004466 *
4467 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004468 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004469 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004470
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004471 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004472 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004473 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004474 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004476 int col = 0;
4477 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004478#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004479 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004480#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004483 for (i = 2 + (tp[0] != CSI); i < len
4484 && !(tp[i] >= '{' && tp[i] <= '~')
4485 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004486 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004487 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004488 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004489#ifdef FEAT_MBYTE
4490 row_char = tp[i - 1];
4491#endif
4492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004494 {
4495 LOG_TR("Not enough characters for CRV");
4496 return -1;
4497 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004498 if (extra > 0)
4499 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004500
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004501#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004502 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4503 * u7_status is not "sent", it may be from a previous Vim that
4504 * just exited. But not for <S-F3>, it sends something
4505 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004506 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004507 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004508 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004509 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004510 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004511
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004512 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004513 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004514 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004515 if (col == 2)
4516 aw = "single";
4517 else if (col == 3)
4518 aw = "double";
4519 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4520 {
4521 /* Setting the option causes a screen redraw. Do
4522 * that right away if possible, keeping any
4523 * messages. */
4524 set_option_value((char_u *)"ambw", 0L,
4525 (char_u *)aw, 0);
4526# ifdef DEBUG_TERMRESPONSE
4527 {
4528 char buf[100];
4529 int r = redraw_asap(CLEAR);
4530
4531 sprintf(buf,
4532 "set 'ambiwidth', redraw_asap(): %d",
4533 r);
4534 log_tr(buf);
4535 }
4536# else
4537 redraw_asap(CLEAR);
4538# endif
4539 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004540 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004541 key_name[0] = (int)KS_EXTRA;
4542 key_name[1] = (int)KE_IGNORE;
4543 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004544# ifdef FEAT_EVAL
4545 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4546# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004547 }
4548 else
4549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004551 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004553 int version = col;
4554
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004555 LOG_TR("Received CRV response");
4556 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004557 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 /* If this code starts with CSI, you can bet that the
4560 * terminal uses 8-bit codes. */
4561 if (tp[0] == CSI)
4562 switch_to_8bit();
4563
4564 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004565 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 * Ignore it for when the user has set 'term' to xterm,
4567 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004568 if (version > 20000)
4569 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004571 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004573 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004574# ifdef FEAT_MOUSE_SGR
4575 int is_iterm2 = FALSE;
4576# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004579 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004581 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 check_for_codes = TRUE;
4583 need_gather = TRUE;
4584 req_codes_from_term();
4585 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004586
4587 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004588 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004589 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004590 {
4591 /* If run from Vim $COLORS is set to the number of
4592 * colors the terminal supports. Otherwise assume
4593 * 256, libvterm supports even more. */
4594 if (mch_getenv((char_u *)"COLORS") == NULL)
4595 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004596# ifdef FEAT_MOUSE_SGR
4597 /* Libvterm can handle SGR mouse reporting. */
4598 if (!option_was_set((char_u *)"ttym"))
4599 set_option_value((char_u *)"ttym", 0L,
4600 (char_u *)"sgr", 0);
4601# endif
4602 }
4603
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004604 if (version == 95)
4605 {
4606 /* Mac Terminal.app sends 1;95;0 */
4607 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4608 {
4609 is_not_xterm = TRUE;
4610 is_mac_terminal = TRUE;
4611 }
4612# ifdef FEAT_MOUSE_SGR
Bram Moolenaar05684312017-12-09 15:11:24 +01004613 /* iTerm2 sends 0;95;0 */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004614 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4615 is_iterm2 = TRUE;
4616# endif
4617 }
4618
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004619 /* Only set 'ttymouse' automatically if it was not set
4620 * by the user already. */
4621 if (!option_was_set((char_u *)"ttym"))
4622 {
4623# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004624 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004625 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004626 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004627 set_option_value((char_u *)"ttym", 0L,
4628 (char_u *)"sgr", 0);
4629 else
4630# endif
4631 /* if xterm version >= 95 use mouse dragging */
4632 if (version >= 95)
4633 set_option_value((char_u *)"ttym", 0L,
4634 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004635 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004636
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004637 /* Detect terminals that set $TERM to something like
4638 * "xterm-256colors" but are not fully xterm
4639 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004640
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004641 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004642 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004643 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004644 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004645 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004646 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004647 is_not_xterm = TRUE;
4648
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004649 /* PuTTY sends 0;136;0
4650 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004651 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004652 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004653 is_not_xterm = TRUE;
4654
4655 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004656 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004657 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004658 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004659
4660 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004661 * set. Only supported properly by xterm since version
4662 * 279 (otherwise it returns 0x18).
4663 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004664 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004665 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004666 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004667 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004668 && *T_CSH != NUL
4669 && *T_CRS != NUL)
4670 {
4671 LOG_TR("Sending cursor style request");
4672 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004673 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004674 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004675 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004676
4677 /* Only request the cursor blink mode if t_RC set. Not
4678 * for Gnome terminal, it can't handle t_RC, it
4679 * echoes the characters to the screen. */
4680 if (rbm_status == STATUS_GET
4681 && !is_not_xterm
4682 && *T_CRC != NUL)
4683 {
4684 LOG_TR("Sending cursor blink mode request");
4685 out_str(T_CRC);
4686 rbm_status = STATUS_SENT;
4687 need_flush = TRUE;
4688 }
4689
4690 if (need_flush)
4691 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004693 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004695 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 apply_autocmds(EVENT_TERMRESPONSE,
4698 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 key_name[0] = (int)KS_EXTRA;
4700 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004702
Bram Moolenaar4db25542017-08-28 22:43:05 +02004703 /* Check blinking cursor from xterm:
4704 * {lead}?12;1$y set
4705 * {lead}?12;2$y not set
4706 *
4707 * {lead} can be <Esc>[ or CSI
4708 */
4709 else if (rbm_status == STATUS_SENT
4710 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4711 && i == j + 6
4712 && tp[j + 1] == '1'
4713 && tp[j + 2] == '2'
4714 && tp[j + 3] == ';'
4715 && tp[i - 1] == '$'
4716 && tp[i] == 'y')
4717 {
4718 initial_cursor_blink = (tp[j + 4] == '1');
4719 rbm_status = STATUS_GOT;
4720 LOG_TR("Received cursor blinking mode response");
4721 key_name[0] = (int)KS_EXTRA;
4722 key_name[1] = (int)KE_IGNORE;
4723 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004724# ifdef FEAT_EVAL
4725 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4726# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004727 }
4728
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004729 /*
4730 * Check for a window position response from the terminal:
4731 * {lead}3;{x}:{y}t
4732 */
4733 else if (waiting_for_winpos
4734 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4735 || (len >= 3 && tp[0] == CSI))
4736 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4737 && tp[j + 1] == ';')
4738 {
4739 j += 2;
4740 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4741 ;
4742 if (i < len && tp[i] == ';')
4743 {
4744 winpos_x = atoi((char *)tp + j);
4745 j = i + 1;
4746 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4747 ;
4748 if (i < len && tp[i] == 't')
4749 {
4750 winpos_y = atoi((char *)tp + j);
4751 /* got finished code: consume it */
4752 key_name[0] = (int)KS_EXTRA;
4753 key_name[1] = (int)KE_IGNORE;
4754 slen = i + 1;
4755 }
4756 }
4757 if (i == len)
4758 {
4759 LOG_TR("not enough characters for winpos");
4760 return -1;
4761 }
4762 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004763 }
4764
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004765 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004766 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004767 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004768 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004769 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004770 * {lead} can be <Esc>] or OSC
4771 * {tail} can be '\007', <Esc>\ or STERM.
4772 *
4773 * Consume any code that starts with "{lead}11;", it's also
4774 * possible that "rgba" is following.
4775 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004776 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004777 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4778 || tp[0] == OSC))
4779 {
4780 j = 1 + (tp[0] == ESC);
4781 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004782 || (argp[1] != '1' && argp[1] != '0')
4783 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004784 i = 0; /* no match */
4785 else
4786 for (i = j; i < len; ++i)
4787 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4788 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004789 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004790 int is_bg = argp[1] == '1';
4791
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004792 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004793 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004794 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004795#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004796 int rval = hexhex2nr(tp + j + 7);
4797 int gval = hexhex2nr(tp + j + 12);
4798 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004799#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004800 if (is_bg)
4801 {
4802 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004803 + tp[j+17]) ? "light" : "dark";
4804
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004805 LOG_TR("Received RBG response");
4806 rbg_status = STATUS_GOT;
4807#ifdef FEAT_TERMINAL
4808 bg_r = rval;
4809 bg_g = gval;
4810 bg_b = bval;
4811#endif
4812 if (!option_was_set((char_u *)"bg")
4813 && STRCMP(p_bg, newval) != 0)
4814 {
4815 /* value differs, apply it */
4816 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004817 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004818 reset_option_was_set((char_u *)"bg");
4819 redraw_asap(CLEAR);
4820 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004821 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004822#ifdef FEAT_TERMINAL
4823 else
4824 {
4825 LOG_TR("Received RFG response");
4826 rfg_status = STATUS_GOT;
4827 fg_r = rval;
4828 fg_g = gval;
4829 fg_b = bval;
4830 }
4831#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004832 }
4833
4834 /* got finished code: consume it */
4835 key_name[0] = (int)KS_EXTRA;
4836 key_name[1] = (int)KE_IGNORE;
4837 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004838# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004839 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4840 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004841# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004842 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004843 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004844 if (i == len)
4845 {
4846 LOG_TR("not enough characters for RB");
4847 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004851 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004852 * {lead}{flag}+r<hex bytes><{tail}
4853 *
4854 * {lead} can be <Esc>P or DCS
4855 * {flag} can be '0' or '1'
4856 * {tail} can be Esc>\ or STERM
4857 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004858 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004859 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004860 *
4861 * {lead} can be <Esc>P or DCS
4862 * {tail} can be Esc>\ or STERM
4863 *
4864 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004865 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004866 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004867 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 || tp[0] == DCS))
4869 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004870 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004871 if (len < j + 3)
4872 i = len; /* need more chars */
4873 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004874 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004875 else if (argp[1] == '+')
4876 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004877 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004878 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004879 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 || tp[i] == STERM)
4881 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004882 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 got_code_from_term(tp + j, i);
4884 key_name[0] = (int)KS_EXTRA;
4885 key_name[1] = (int)KE_IGNORE;
4886 slen = i + 1 + (tp[i] == ESC);
4887 break;
4888 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004889 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004890 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004891 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004892 /* Probably the cursor shape response. Make sure that "i"
4893 * is equal to "len" when there are not sufficient
4894 * characters. */
4895 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004896 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004897 if (i - j == 3 && !isdigit(tp[i]))
4898 break;
4899 if (i - j == 4 && tp[i] != ' ')
4900 break;
4901 if (i - j == 5 && tp[i] != 'q')
4902 break;
4903 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4904 break;
4905 if ((i - j == 6 && tp[i] == STERM)
4906 || (i - j == 7 && tp[i] == '\\'))
4907 {
4908 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004909
Bram Moolenaar590ec872018-03-02 20:58:42 +01004910 /* 0, 1 = block blink, 2 = block
4911 * 3 = underline blink, 4 = underline
4912 * 5 = vertical bar blink, 6 = vertical bar */
4913 number = number == 0 ? 1 : number;
4914 initial_cursor_shape = (number + 1) / 2;
4915 /* The blink flag is actually inverted, compared to
4916 * the value set with T_SH. */
4917 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004918 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004919 rcs_status = STATUS_GOT;
4920 LOG_TR("Received cursor shape response");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004921
Bram Moolenaar590ec872018-03-02 20:58:42 +01004922 key_name[0] = (int)KS_EXTRA;
4923 key_name[1] = (int)KE_IGNORE;
4924 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004925# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004926 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004927# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004928 break;
4929 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004930 }
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004934 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004935 /* These codes arrive many together, each code can be
4936 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004937 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004938 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 }
4942#endif
4943
4944 if (key_name[0] == NUL)
4945 continue; /* No match at this position, try next one */
4946
4947 /* We only get here when we have a complete termcode match */
4948
4949#ifdef FEAT_MOUSE
4950# ifdef FEAT_GUI
4951 /*
4952 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4953 * so that we know which window to scroll later.
4954 */
4955 if (gui.in_use
4956 && key_name[0] == (int)KS_EXTRA
4957 && (key_name[1] == (int)KE_X1MOUSE
4958 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004959 || key_name[1] == (int)KE_MOUSELEFT
4960 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 || key_name[1] == (int)KE_MOUSEDOWN
4962 || key_name[1] == (int)KE_MOUSEUP))
4963 {
4964 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4965 if (num_bytes == -1) /* not enough coordinates */
4966 return -1;
4967 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4968 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4969 slen += num_bytes;
4970 }
4971 else
4972# endif
4973 /*
4974 * If it is a mouse click, get the coordinates.
4975 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004976 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004978 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979# endif
4980# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004981 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982# endif
4983# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004984 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985# endif
4986# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004987 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004989# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004990 || key_name[0] == KS_URXVT_MOUSE
4991# endif
4992# ifdef FEAT_MOUSE_SGR
4993 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004994 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 )
4997 {
4998 is_click = is_drag = FALSE;
4999
Bram Moolenaar864207d2008-06-24 22:14:38 +00005000# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5001 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (key_name[0] == (int)KS_MOUSE)
5003 {
5004 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005005 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 * s == encoded button state:
5007 * 0x20 = left button down
5008 * 0x21 = middle button down
5009 * 0x22 = right button down
5010 * 0x23 = any button release
5011 * 0x60 = button 4 down (scroll wheel down)
5012 * 0x61 = button 5 down (scroll wheel up)
5013 * add 0x04 for SHIFT
5014 * add 0x08 for ALT
5015 * add 0x10 for CTRL
5016 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005017 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5018 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 * c == column + ' ' + 1 == column + 33
5020 * r == row + ' ' + 1 == row + 33
5021 *
5022 * The coordinates are passed on through global variables.
5023 * Ugly, but this avoids trouble with mouse clicks at an
5024 * unexpected moment and allows for mapping them.
5025 */
5026 for (;;)
5027 {
5028#ifdef FEAT_GUI
5029 if (gui.in_use)
5030 {
5031 /* GUI uses more bits for columns > 223 */
5032 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5033 if (num_bytes == -1) /* not enough coordinates */
5034 return -1;
5035 mouse_code = bytes[0];
5036 mouse_col = 128 * (bytes[1] - ' ' - 1)
5037 + bytes[2] - ' ' - 1;
5038 mouse_row = 128 * (bytes[3] - ' ' - 1)
5039 + bytes[4] - ' ' - 1;
5040 }
5041 else
5042#endif
5043 {
5044 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5045 if (num_bytes == -1) /* not enough coordinates */
5046 return -1;
5047 mouse_code = bytes[0];
5048 mouse_col = bytes[1] - ' ' - 1;
5049 mouse_row = bytes[2] - ' ' - 1;
5050 }
5051 slen += num_bytes;
5052
5053 /* If the following bytes is also a mouse code and it has
5054 * the same code, dump this one and get the next. This
5055 * makes dragging a whole lot faster. */
5056#ifdef FEAT_GUI
5057 if (gui.in_use)
5058 j = 3;
5059 else
5060#endif
5061 j = termcodes[idx].len;
5062 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5063 && tp[slen + j] == mouse_code
5064 && tp[slen + j + 1] != NUL
5065 && tp[slen + j + 2] != NUL
5066#ifdef FEAT_GUI
5067 && (!gui.in_use
5068 || (tp[slen + j + 3] != NUL
5069 && tp[slen + j + 4] != NUL))
5070#endif
5071 )
5072 slen += j;
5073 else
5074 break;
5075 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005078# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5079 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005080 || key_name[0] == KS_SGR_MOUSE
5081 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005082 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005083 /* URXVT 1015 mouse reporting mode:
5084 * Almost identical to xterm mouse mode, except the values
5085 * are decimal instead of bytes.
5086 *
5087 * \033[%d;%d;%dM
5088 * ^-- row
5089 * ^----- column
5090 * ^-------- code
5091 *
5092 * SGR 1006 mouse reporting mode:
5093 * Almost identical to xterm mouse mode, except the values
5094 * are decimal instead of bytes.
5095 *
5096 * \033[<%d;%d;%dM
5097 * ^-- row
5098 * ^----- column
5099 * ^-------- code
5100 *
5101 * \033[<%d;%d;%dm : mouse release event
5102 * ^-- row
5103 * ^----- column
5104 * ^-------- code
5105 */
5106 p = modifiers_start;
5107 if (p == NULL)
5108 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005109
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005110 mouse_code = getdigits(&p);
5111 if (*p++ != ';')
5112 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005113
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005114 /* when mouse reporting is SGR, add 32 to mouse code */
5115 if (key_name[0] == KS_SGR_MOUSE
5116 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5117 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005118
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005119 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5120 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005121
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005122 mouse_col = getdigits(&p) - 1;
5123 if (*p++ != ';')
5124 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005125
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005126 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005127
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005128 /* The modifiers were the mouse coordinates, not the
5129 * modifier keys (alt/shift/ctrl/meta) state. */
5130 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005131 }
5132# endif
5133
5134 if (key_name[0] == (int)KS_MOUSE
5135#ifdef FEAT_MOUSE_URXVT
5136 || key_name[0] == (int)KS_URXVT_MOUSE
5137#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005138#ifdef FEAT_MOUSE_SGR
5139 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005140 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005141#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005142 )
5143 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005144# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /*
5146 * Handle mouse events.
5147 * Recognize the xterm mouse wheel, but not in the GUI, the
5148 * Linux console with GPM and the MS-DOS or Win32 console
5149 * (multi-clicks use >= 0x60).
5150 */
5151 if (mouse_code >= MOUSEWHEEL_LOW
5152# ifdef FEAT_GUI
5153 && !gui.in_use
5154# endif
5155# ifdef FEAT_MOUSE_GPM
5156 && gpm_flag == 0
5157# endif
5158 )
5159 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005160# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5161 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5162 /* mouse-move event, using MOUSE_DRAG works */
5163 mouse_code = MOUSE_DRAG;
5164 else
5165# endif
5166 /* Keep the mouse_code before it's changed, so that we
5167 * remember that it was a mouse wheel click. */
5168 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170# ifdef FEAT_MOUSE_XTERM
5171 else if (held_button == MOUSE_RELEASE
5172# ifdef FEAT_GUI
5173 && !gui.in_use
5174# endif
5175 && (mouse_code == 0x23 || mouse_code == 0x24))
5176 {
5177 /* Apparently used by rxvt scroll wheel. */
5178 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5179 }
5180# endif
5181
5182# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5183 else if (use_xterm_mouse() > 1)
5184 {
5185 if (mouse_code & MOUSE_DRAG_XTERM)
5186 mouse_code |= MOUSE_DRAG;
5187 }
5188# endif
5189# ifdef FEAT_XCLIPBOARD
5190 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5191 {
5192 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5193 stop_xterm_trace();
5194 else
5195 start_xterm_trace(mouse_code);
5196 }
5197# endif
5198# endif
5199 }
5200# endif /* !UNIX || FEAT_MOUSE_XTERM */
5201# ifdef FEAT_MOUSE_NET
5202 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5203 {
5204 int mc, mr;
5205
5206 /* expect a rather limited sequence like: balancing {
5207 * \033}6,45\r
5208 * '6' is the row, 45 is the column
5209 */
5210 p = tp + slen;
5211 mr = getdigits(&p);
5212 if (*p++ != ',')
5213 return -1;
5214 mc = getdigits(&p);
5215 if (*p++ != '\r')
5216 return -1;
5217
5218 mouse_col = mc - 1;
5219 mouse_row = mr - 1;
5220 mouse_code = MOUSE_LEFT;
5221 slen += (int)(p - (tp + slen));
5222 }
5223# endif /* FEAT_MOUSE_NET */
5224# ifdef FEAT_MOUSE_JSB
5225 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5226 {
5227 int mult, val, iter, button, status;
5228
5229 /* JSBTERM Input Model
5230 * \033[0~zw uniq escape sequence
5231 * (L-x) Left button pressed - not pressed x not reporting
5232 * (M-x) Middle button pressed - not pressed x not reporting
5233 * (R-x) Right button pressed - not pressed x not reporting
5234 * (SDmdu) Single , Double click, m mouse move d button down
5235 * u button up
5236 * ### X cursor position padded to 3 digits
5237 * ### Y cursor position padded to 3 digits
5238 * (s-x) SHIFT key pressed - not pressed x not reporting
5239 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005240 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 */
5242
5243 p = tp + slen;
5244 button = mouse_code = 0;
5245 switch (*p++)
5246 {
5247 case 'L': button = 1; break;
5248 case '-': break;
5249 case 'x': break; /* ignore sequence */
5250 default: return -1; /* Unknown Result */
5251 }
5252 switch (*p++)
5253 {
5254 case 'M': button |= 2; break;
5255 case '-': break;
5256 case 'x': break; /* ignore sequence */
5257 default: return -1; /* Unknown Result */
5258 }
5259 switch (*p++)
5260 {
5261 case 'R': button |= 4; break;
5262 case '-': break;
5263 case 'x': break; /* ignore sequence */
5264 default: return -1; /* Unknown Result */
5265 }
5266 status = *p++;
5267 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5268 mult /= 10, p++)
5269 if (*p >= '0' && *p <= '9')
5270 val += (*p - '0') * mult;
5271 else
5272 return -1;
5273 mouse_col = val;
5274 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5275 mult /= 10, p++)
5276 if (*p >= '0' && *p <= '9')
5277 val += (*p - '0') * mult;
5278 else
5279 return -1;
5280 mouse_row = val;
5281 switch (*p++)
5282 {
5283 case 's': button |= 8; break; /* SHIFT key Pressed */
5284 case '-': break; /* Not Pressed */
5285 case 'x': break; /* Not Reporting */
5286 default: return -1; /* Unknown Result */
5287 }
5288 switch (*p++)
5289 {
5290 case 'c': button |= 16; break; /* CTRL key Pressed */
5291 case '-': break; /* Not Pressed */
5292 case 'x': break; /* Not Reporting */
5293 default: return -1; /* Unknown Result */
5294 }
5295 if (*p++ != '\033')
5296 return -1;
5297 if (*p++ != '\\')
5298 return -1;
5299 switch (status)
5300 {
5301 case 'D': /* Double Click */
5302 case 'S': /* Single Click */
5303 if (button & 1) mouse_code |= MOUSE_LEFT;
5304 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5305 if (button & 4) mouse_code |= MOUSE_RIGHT;
5306 if (button & 8) mouse_code |= MOUSE_SHIFT;
5307 if (button & 16) mouse_code |= MOUSE_CTRL;
5308 break;
5309 case 'm': /* Mouse move */
5310 if (button & 1) mouse_code |= MOUSE_LEFT;
5311 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5312 if (button & 4) mouse_code |= MOUSE_RIGHT;
5313 if (button & 8) mouse_code |= MOUSE_SHIFT;
5314 if (button & 16) mouse_code |= MOUSE_CTRL;
5315 if ((button & 7) != 0)
5316 {
5317 held_button = mouse_code;
5318 mouse_code |= MOUSE_DRAG;
5319 }
5320 is_drag = TRUE;
5321 showmode();
5322 break;
5323 case 'd': /* Button Down */
5324 if (button & 1) mouse_code |= MOUSE_LEFT;
5325 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5326 if (button & 4) mouse_code |= MOUSE_RIGHT;
5327 if (button & 8) mouse_code |= MOUSE_SHIFT;
5328 if (button & 16) mouse_code |= MOUSE_CTRL;
5329 break;
5330 case 'u': /* Button Up */
5331 if (button & 1)
5332 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5333 if (button & 2)
5334 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5335 if (button & 4)
5336 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5337 if (button & 8)
5338 mouse_code |= MOUSE_SHIFT;
5339 if (button & 16)
5340 mouse_code |= MOUSE_CTRL;
5341 break;
5342 default: return -1; /* Unknown Result */
5343 }
5344
5345 slen += (p - (tp + slen));
5346 }
5347# endif /* FEAT_MOUSE_JSB */
5348# ifdef FEAT_MOUSE_DEC
5349 if (key_name[0] == (int)KS_DEC_MOUSE)
5350 {
5351 /* The DEC Locator Input Model
5352 * Netterm delivers the code sequence:
5353 * \033[2;4;24;80&w (left button down)
5354 * \033[3;0;24;80&w (left button up)
5355 * \033[6;1;24;80&w (right button down)
5356 * \033[7;0;24;80&w (right button up)
5357 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5358 * Pe is the event code
5359 * Pb is the button code
5360 * Pr is the row coordinate
5361 * Pc is the column coordinate
5362 * Pp is the third coordinate (page number)
5363 * Pe, the event code indicates what event caused this report
5364 * The following event codes are defined:
5365 * 0 - request, the terminal received an explicit request
5366 * for a locator report, but the locator is unavailable
5367 * 1 - request, the terminal received an explicit request
5368 * for a locator report
5369 * 2 - left button down
5370 * 3 - left button up
5371 * 4 - middle button down
5372 * 5 - middle button up
5373 * 6 - right button down
5374 * 7 - right button up
5375 * 8 - fourth button down
5376 * 9 - fourth button up
5377 * 10 - locator outside filter rectangle
5378 * Pb, the button code, ASCII decimal 0-15 indicating which
5379 * buttons are down if any. The state of the four buttons
5380 * on the locator correspond to the low four bits of the
5381 * decimal value,
5382 * "1" means button depressed
5383 * 0 - no buttons down,
5384 * 1 - right,
5385 * 2 - middle,
5386 * 4 - left,
5387 * 8 - fourth
5388 * Pr is the row coordinate of the locator position in the page,
5389 * encoded as an ASCII decimal value.
5390 * If Pr is omitted, the locator position is undefined
5391 * (outside the terminal window for example).
5392 * Pc is the column coordinate of the locator position in the
5393 * page, encoded as an ASCII decimal value.
5394 * If Pc is omitted, the locator position is undefined
5395 * (outside the terminal window for example).
5396 * Pp is the page coordinate of the locator position
5397 * encoded as an ASCII decimal value.
5398 * The page coordinate may be omitted if the locator is on
5399 * page one (the default). We ignore it anyway.
5400 */
5401 int Pe, Pb, Pr, Pc;
5402
5403 p = tp + slen;
5404
5405 /* get event status */
5406 Pe = getdigits(&p);
5407 if (*p++ != ';')
5408 return -1;
5409
5410 /* get button status */
5411 Pb = getdigits(&p);
5412 if (*p++ != ';')
5413 return -1;
5414
5415 /* get row status */
5416 Pr = getdigits(&p);
5417 if (*p++ != ';')
5418 return -1;
5419
5420 /* get column status */
5421 Pc = getdigits(&p);
5422
5423 /* the page parameter is optional */
5424 if (*p == ';')
5425 {
5426 p++;
5427 (void)getdigits(&p);
5428 }
5429 if (*p++ != '&')
5430 return -1;
5431 if (*p++ != 'w')
5432 return -1;
5433
5434 mouse_code = 0;
5435 switch (Pe)
5436 {
5437 case 0: return -1; /* position request while unavailable */
5438 case 1: /* a response to a locator position request includes
5439 the status of all buttons */
5440 Pb &= 7; /* mask off and ignore fourth button */
5441 if (Pb & 4)
5442 mouse_code = MOUSE_LEFT;
5443 if (Pb & 2)
5444 mouse_code = MOUSE_MIDDLE;
5445 if (Pb & 1)
5446 mouse_code = MOUSE_RIGHT;
5447 if (Pb)
5448 {
5449 held_button = mouse_code;
5450 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005451 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 is_drag = TRUE;
5454 showmode();
5455 break;
5456 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005457 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 break;
5459 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5460 break;
5461 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005462 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 break;
5464 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5465 break;
5466 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005467 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 break;
5469 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5470 break;
5471 case 8: return -1; /* fourth button down */
5472 case 9: return -1; /* fourth button up */
5473 case 10: return -1; /* mouse outside of filter rectangle */
5474 default: return -1; /* should never occur */
5475 }
5476
5477 mouse_col = Pc - 1;
5478 mouse_row = Pr - 1;
5479
5480 slen += (int)(p - (tp + slen));
5481 }
5482# endif /* FEAT_MOUSE_DEC */
5483# ifdef FEAT_MOUSE_PTERM
5484 if (key_name[0] == (int)KS_PTERM_MOUSE)
5485 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005486 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488 p = tp + slen;
5489
5490 action = getdigits(&p);
5491 if (*p++ != ';')
5492 return -1;
5493
5494 mouse_row = getdigits(&p);
5495 if (*p++ != ';')
5496 return -1;
5497 mouse_col = getdigits(&p);
5498 if (*p++ != ';')
5499 return -1;
5500
5501 button = getdigits(&p);
5502 mouse_code = 0;
5503
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005504 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 case 4: mouse_code = MOUSE_LEFT; break;
5507 case 1: mouse_code = MOUSE_RIGHT; break;
5508 case 2: mouse_code = MOUSE_MIDDLE; break;
5509 default: return -1;
5510 }
5511
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005512 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 case 31: /* Initial press */
5515 if (*p++ != ';')
5516 return -1;
5517
5518 num_clicks = getdigits(&p); /* Not used */
5519 break;
5520
5521 case 32: /* Release */
5522 mouse_code |= MOUSE_RELEASE;
5523 break;
5524
5525 case 33: /* Drag */
5526 held_button = mouse_code;
5527 mouse_code |= MOUSE_DRAG;
5528 break;
5529
5530 default:
5531 return -1;
5532 }
5533
5534 if (*p++ != 't')
5535 return -1;
5536
5537 slen += (p - (tp + slen));
5538 }
5539# endif /* FEAT_MOUSE_PTERM */
5540
5541 /* Interpret the mouse code */
5542 current_button = (mouse_code & MOUSE_CLICK_MASK);
5543 if (current_button == MOUSE_RELEASE
5544# ifdef FEAT_MOUSE_XTERM
5545 && wheel_code == 0
5546# endif
5547 )
5548 {
5549 /*
5550 * If we get a mouse drag or release event when
5551 * there is no mouse button held down (held_button ==
5552 * MOUSE_RELEASE), produce a K_IGNORE below.
5553 * (can happen when you hold down two buttons
5554 * and then let them go, or click in the menu bar, but not
5555 * on a menu, and drag into the text).
5556 */
5557 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5558 is_drag = TRUE;
5559 current_button = held_button;
5560 }
5561 else if (wheel_code == 0)
5562 {
5563# ifdef CHECK_DOUBLE_CLICK
5564# ifdef FEAT_MOUSE_GPM
5565# ifdef FEAT_GUI
5566 /*
5567 * Only for Unix, when GUI or gpm is not active, we handle
5568 * multi-clicks here.
5569 */
5570 if (gpm_flag == 0 && !gui.in_use)
5571# else
5572 if (gpm_flag == 0)
5573# endif
5574# else
5575# ifdef FEAT_GUI
5576 if (!gui.in_use)
5577# endif
5578# endif
5579 {
5580 /*
5581 * Compute the time elapsed since the previous mouse click.
5582 */
5583 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005584 if (orig_mouse_time.tv_sec == 0)
5585 {
5586 /*
5587 * Avoid computing the difference between mouse_time
5588 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005589 * difference would be huge and would cause
5590 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005591 */
5592 timediff = p_mouset;
5593 }
5594 else
5595 {
5596 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005597 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005598 if (timediff < 0)
5599 --orig_mouse_time.tv_sec;
5600 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005601 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 orig_mouse_time = mouse_time;
5604 if (mouse_code == orig_mouse_code
5605 && timediff < p_mouset
5606 && orig_num_clicks != 4
5607 && orig_mouse_col == mouse_col
5608 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005609 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005611 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005613 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005614 /* Double click in tab pages line also works
5615 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005616 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005617 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ++orig_num_clicks;
5619 else
5620 orig_num_clicks = 1;
5621 orig_mouse_col = mouse_col;
5622 orig_mouse_row = mouse_row;
5623 orig_topline = curwin->w_topline;
5624#ifdef FEAT_DIFF
5625 orig_topfill = curwin->w_topfill;
5626#endif
5627 }
5628# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5629 else
5630 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5631# endif
5632# else
5633 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5634# endif
5635 is_click = TRUE;
5636 orig_mouse_code = mouse_code;
5637 }
5638 if (!is_drag)
5639 held_button = mouse_code & MOUSE_CLICK_MASK;
5640
5641 /*
5642 * Translate the actual mouse event into a pseudo mouse event.
5643 * First work out what modifiers are to be used.
5644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (orig_mouse_code & MOUSE_SHIFT)
5646 modifiers |= MOD_MASK_SHIFT;
5647 if (orig_mouse_code & MOUSE_CTRL)
5648 modifiers |= MOD_MASK_CTRL;
5649 if (orig_mouse_code & MOUSE_ALT)
5650 modifiers |= MOD_MASK_ALT;
5651 if (orig_num_clicks == 2)
5652 modifiers |= MOD_MASK_2CLICK;
5653 else if (orig_num_clicks == 3)
5654 modifiers |= MOD_MASK_3CLICK;
5655 else if (orig_num_clicks == 4)
5656 modifiers |= MOD_MASK_4CLICK;
5657
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005658 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5659 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005661 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005662 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005663 {
5664 if (wheel_code & MOUSE_CTRL)
5665 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005666 if (wheel_code & MOUSE_ALT)
5667 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 key_name[1] = (wheel_code & 1)
5669 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005670 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
5673 key_name[1] = get_pseudo_mouse_code(current_button,
5674 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005675
5676 /* Make sure the mouse position is valid. Some terminals may
5677 * return weird values. */
5678 if (mouse_col >= Columns)
5679 mouse_col = Columns - 1;
5680 if (mouse_row >= Rows)
5681 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683#endif /* FEAT_MOUSE */
5684
5685#ifdef FEAT_GUI
5686 /*
5687 * If using the GUI, then we get menu and scrollbar events.
5688 *
5689 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5690 * four bytes which are to be taken as a pointer to the vimmenu_T
5691 * structure.
5692 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005693 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005694 * is one byte with the tab index.
5695 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5697 * by one byte representing the scrollbar number, and then four bytes
5698 * representing a long_u which is the new value of the scrollbar.
5699 *
5700 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5701 * KE_FILLER followed by four bytes representing a long_u which is the
5702 * new value of the scrollbar.
5703 */
5704# ifdef FEAT_MENU
5705 else if (key_name[0] == (int)KS_MENU)
5706 {
5707 long_u val;
5708
5709 num_bytes = get_long_from_buf(tp + slen, &val);
5710 if (num_bytes == -1)
5711 return -1;
5712 current_menu = (vimmenu_T *)val;
5713 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005714
5715 /* The menu may have been deleted right after it was used, check
5716 * for that. */
5717 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5718 {
5719 key_name[0] = KS_EXTRA;
5720 key_name[1] = (int)KE_IGNORE;
5721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005724# ifdef FEAT_GUI_TABLINE
5725 else if (key_name[0] == (int)KS_TABLINE)
5726 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005727 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005728 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5729 if (num_bytes == -1)
5730 return -1;
5731 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005732 if (current_tab == 255) /* -1 in a byte gives 255 */
5733 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005734 slen += num_bytes;
5735 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005736 else if (key_name[0] == (int)KS_TABMENU)
5737 {
5738 /* Selecting tabline tab or using its menu. */
5739 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5740 if (num_bytes == -1)
5741 return -1;
5742 current_tab = (int)bytes[0];
5743 current_tabmenu = (int)bytes[1];
5744 slen += num_bytes;
5745 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747# ifndef USE_ON_FLY_SCROLL
5748 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5749 {
5750 long_u val;
5751
5752 /* Get the last scrollbar event in the queue of the same type */
5753 j = 0;
5754 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5755 && tp[j + 2] != NUL; ++i)
5756 {
5757 j += 3;
5758 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5759 if (num_bytes == -1)
5760 break;
5761 if (i == 0)
5762 current_scrollbar = (int)bytes[0];
5763 else if (current_scrollbar != (int)bytes[0])
5764 break;
5765 j += num_bytes;
5766 num_bytes = get_long_from_buf(tp + j, &val);
5767 if (num_bytes == -1)
5768 break;
5769 scrollbar_value = val;
5770 j += num_bytes;
5771 slen = j;
5772 }
5773 if (i == 0) /* not enough characters to make one */
5774 return -1;
5775 }
5776 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5777 {
5778 long_u val;
5779
5780 /* Get the last horiz. scrollbar event in the queue */
5781 j = 0;
5782 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5783 && tp[j + 2] != NUL; ++i)
5784 {
5785 j += 3;
5786 num_bytes = get_long_from_buf(tp + j, &val);
5787 if (num_bytes == -1)
5788 break;
5789 scrollbar_value = val;
5790 j += num_bytes;
5791 slen = j;
5792 }
5793 if (i == 0) /* not enough characters to make one */
5794 return -1;
5795 }
5796# endif /* !USE_ON_FLY_SCROLL */
5797#endif /* FEAT_GUI */
5798
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005799 /*
5800 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5801 */
5802 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5803
5804 /*
5805 * Add any modifier codes to our string.
5806 */
5807 new_slen = 0; /* Length of what will replace the termcode */
5808 if (modifiers != 0)
5809 {
5810 /* Some keys have the modifier included. Need to handle that here
5811 * to make mappings work. */
5812 key = simplify_key(key, &modifiers);
5813 if (modifiers != 0)
5814 {
5815 string[new_slen++] = K_SPECIAL;
5816 string[new_slen++] = (int)KS_MODIFIER;
5817 string[new_slen++] = modifiers;
5818 }
5819 }
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005822 key_name[0] = KEY2TERMCAP0(key);
5823 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005825 {
5826 /* from ":set <M-b>=xx" */
5827#ifdef FEAT_MBYTE
5828 if (has_mbyte)
5829 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5830 else
5831#endif
5832 string[new_slen++] = key_name[1];
5833 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005834 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5835 && key_name[1] == KE_IGNORE)
5836 {
5837 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5838 * to indicate what happened. */
5839 retval = KEYLEN_REMOVED;
5840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 else
5842 {
5843 string[new_slen++] = K_SPECIAL;
5844 string[new_slen++] = key_name[0];
5845 string[new_slen++] = key_name[1];
5846 }
5847 string[new_slen] = NUL;
5848 extra = new_slen - slen;
5849 if (buf == NULL)
5850 {
5851 if (extra < 0)
5852 /* remove matched chars, taking care of noremap */
5853 del_typebuf(-extra, offset);
5854 else if (extra > 0)
5855 /* insert the extra space we need */
5856 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5857
5858 /*
5859 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5860 * typebuf.tb_buf[]!
5861 */
5862 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5863 (size_t)new_slen);
5864 }
5865 else
5866 {
5867 if (extra < 0)
5868 /* remove matched characters */
5869 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005870 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005872 {
5873 /* Insert the extra space we need. If there is insufficient
5874 * space return -1. */
5875 if (*buflen + extra + new_slen >= bufsize)
5876 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005878 (size_t)(*buflen - offset));
5879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005881 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005883 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885
Bram Moolenaar2951b772013-07-03 12:45:31 +02005886#ifdef FEAT_TERMRESPONSE
5887 LOG_TR("normal character");
5888#endif
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return 0; /* no match found */
5891}
5892
Bram Moolenaar9377df32017-10-15 13:22:01 +02005893#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005894/*
5895 * Get the text foreground color, if known.
5896 */
5897 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005898term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005899{
5900 if (rfg_status == STATUS_GOT)
5901 {
5902 *r = fg_r;
5903 *g = fg_g;
5904 *b = fg_b;
5905 }
5906}
5907
5908/*
5909 * Get the text background color, if known.
5910 */
5911 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005912term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005913{
5914 if (rbg_status == STATUS_GOT)
5915 {
5916 *r = bg_r;
5917 *g = bg_g;
5918 *b = bg_b;
5919 }
5920}
5921#endif
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923/*
5924 * Replace any terminal code strings in from[] with the equivalent internal
5925 * vim representation. This is used for the "from" and "to" part of a
5926 * mapping, and the "to" part of a menu command.
5927 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5928 * '<'.
5929 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5930 *
5931 * The replacement is done in result[] and finally copied into allocated
5932 * memory. If this all works well *bufp is set to the allocated memory and a
5933 * pointer to it is returned. If something fails *bufp is set to NULL and from
5934 * is returned.
5935 *
5936 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5937 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5938 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5939 * instead of a CTRL-V.
5940 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005941 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005942replace_termcodes(
5943 char_u *from,
5944 char_u **bufp,
5945 int from_part,
5946 int do_lt, /* also translate <lt> */
5947 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 int i;
5950 int slen;
5951 int key;
5952 int dlen = 0;
5953 char_u *src;
5954 int do_backslash; /* backslash is a special character */
5955 int do_special; /* recognize <> key codes */
5956 int do_key_code; /* recognize raw key codes */
5957 char_u *result; /* buffer for resulting string */
5958
5959 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005960 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5962
5963 /*
5964 * Allocate space for the translation. Worst case a single character is
5965 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5966 */
5967 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5968 if (result == NULL) /* out of memory */
5969 {
5970 *bufp = NULL;
5971 return from;
5972 }
5973
5974 src = from;
5975
5976 /*
5977 * Check for #n at start only: function key n
5978 */
5979 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5980 {
5981 result[dlen++] = K_SPECIAL;
5982 result[dlen++] = 'k';
5983 if (src[1] == '0')
5984 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5985 else
5986 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5987 src += 2;
5988 }
5989
5990 /*
5991 * Copy each byte from *from to result[dlen]
5992 */
5993 while (*src != NUL)
5994 {
5995 /*
5996 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005997 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6000 {
6001#ifdef FEAT_EVAL
6002 /*
6003 * Replace <SID> by K_SNR <script-nr> _.
6004 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6005 */
6006 if (STRNICMP(src, "<SID>", 5) == 0)
6007 {
6008 if (current_SID <= 0)
6009 EMSG(_(e_usingsid));
6010 else
6011 {
6012 src += 5;
6013 result[dlen++] = K_SPECIAL;
6014 result[dlen++] = (int)KS_EXTRA;
6015 result[dlen++] = (int)KE_SNR;
6016 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6017 dlen += (int)STRLEN(result + dlen);
6018 result[dlen++] = '_';
6019 continue;
6020 }
6021 }
6022#endif
6023
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006024 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (slen)
6026 {
6027 dlen += slen;
6028 continue;
6029 }
6030 }
6031
6032 /*
6033 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6034 * Note that this is also checked after replacing the <> form.
6035 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6036 * it could be a character in the file.
6037 */
6038 if (do_key_code)
6039 {
6040 i = find_term_bykeys(src);
6041 if (i >= 0)
6042 {
6043 result[dlen++] = K_SPECIAL;
6044 result[dlen++] = termcodes[i].name[0];
6045 result[dlen++] = termcodes[i].name[1];
6046 src += termcodes[i].len;
6047 /* If terminal code matched, continue after it. */
6048 continue;
6049 }
6050 }
6051
6052#ifdef FEAT_EVAL
6053 if (do_special)
6054 {
6055 char_u *p, *s, len;
6056
6057 /*
6058 * Replace <Leader> by the value of "mapleader".
6059 * Replace <LocalLeader> by the value of "maplocalleader".
6060 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6061 */
6062 if (STRNICMP(src, "<Leader>", 8) == 0)
6063 {
6064 len = 8;
6065 p = get_var_value((char_u *)"g:mapleader");
6066 }
6067 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6068 {
6069 len = 13;
6070 p = get_var_value((char_u *)"g:maplocalleader");
6071 }
6072 else
6073 {
6074 len = 0;
6075 p = NULL;
6076 }
6077 if (len != 0)
6078 {
6079 /* Allow up to 8 * 6 characters for "mapleader". */
6080 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6081 s = (char_u *)"\\";
6082 else
6083 s = p;
6084 while (*s != NUL)
6085 result[dlen++] = *s++;
6086 src += len;
6087 continue;
6088 }
6089 }
6090#endif
6091
6092 /*
6093 * Remove CTRL-V and ignore the next character.
6094 * For "from" side the CTRL-V at the end is included, for the "to"
6095 * part it is removed.
6096 * If 'cpoptions' does not contain 'B', also accept a backslash.
6097 */
6098 key = *src;
6099 if (key == Ctrl_V || (do_backslash && key == '\\'))
6100 {
6101 ++src; /* skip CTRL-V or backslash */
6102 if (*src == NUL)
6103 {
6104 if (from_part)
6105 result[dlen++] = key;
6106 break;
6107 }
6108 }
6109
6110#ifdef FEAT_MBYTE
6111 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006112 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113#endif
6114 {
6115 /*
6116 * If the character is K_SPECIAL, replace it with K_SPECIAL
6117 * KS_SPECIAL KE_FILLER.
6118 * If compiled with the GUI replace CSI with K_CSI.
6119 */
6120 if (*src == K_SPECIAL)
6121 {
6122 result[dlen++] = K_SPECIAL;
6123 result[dlen++] = KS_SPECIAL;
6124 result[dlen++] = KE_FILLER;
6125 }
6126# ifdef FEAT_GUI
6127 else if (*src == CSI)
6128 {
6129 result[dlen++] = K_SPECIAL;
6130 result[dlen++] = KS_EXTRA;
6131 result[dlen++] = (int)KE_CSI;
6132 }
6133# endif
6134 else
6135 result[dlen++] = *src;
6136 ++src;
6137 }
6138 }
6139 result[dlen] = NUL;
6140
6141 /*
6142 * Copy the new string to allocated memory.
6143 * If this fails, just return from.
6144 */
6145 if ((*bufp = vim_strsave(result)) != NULL)
6146 from = *bufp;
6147 vim_free(result);
6148 return from;
6149}
6150
6151/*
6152 * Find a termcode with keys 'src' (must be NUL terminated).
6153 * Return the index in termcodes[], or -1 if not found.
6154 */
6155 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006156find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006159 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 for (i = 0; i < tc_len; ++i)
6162 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006163 if (slen == termcodes[i].len
6164 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return i;
6166 }
6167 return -1;
6168}
6169
6170/*
6171 * Gather the first characters in the terminal key codes into a string.
6172 * Used to speed up check_termcode().
6173 */
6174 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006175gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 int i;
6178 int len = 0;
6179
6180#ifdef FEAT_GUI
6181 if (gui.in_use)
6182 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6183#endif
6184#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006185 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 termleader[len++] = DCS; /* the termcode response starts with DCS
6187 in 8-bit mode */
6188#endif
6189 termleader[len] = NUL;
6190
6191 for (i = 0; i < tc_len; ++i)
6192 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6193 {
6194 termleader[len++] = termcodes[i].code[0];
6195 termleader[len] = NUL;
6196 }
6197
6198 need_gather = FALSE;
6199}
6200
6201/*
6202 * Show all termcodes (for ":set termcap")
6203 * This code looks a lot like showoptions(), but is different.
6204 */
6205 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006206show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 int col;
6209 int *items;
6210 int item_count;
6211 int run;
6212 int row, rows;
6213 int cols;
6214 int i;
6215 int len;
6216
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006217#define INC3 27 /* try to make three columns */
6218#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219#define GAP 2 /* spaces between columns */
6220
6221 if (tc_len == 0) /* no terminal codes (must be GUI) */
6222 return;
6223 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6224 if (items == NULL)
6225 return;
6226
6227 /* Highlight title */
6228 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6229
6230 /*
6231 * do the loop two times:
6232 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006233 * 2. display the medium items (medium length strings)
6234 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006236 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 /*
6239 * collect the items in items[]
6240 */
6241 item_count = 0;
6242 for (i = 0; i < tc_len; i++)
6243 {
6244 len = show_one_termcode(termcodes[i].name,
6245 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006246 if (len <= INC3 - GAP ? run == 1
6247 : len <= INC2 - GAP ? run == 2
6248 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 items[item_count++] = i;
6250 }
6251
6252 /*
6253 * display the items
6254 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006255 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006257 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (cols == 0)
6259 cols = 1;
6260 rows = (item_count + cols - 1) / cols;
6261 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006262 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 rows = item_count;
6264 for (row = 0; row < rows && !got_int; ++row)
6265 {
6266 msg_putchar('\n'); /* go to next line */
6267 if (got_int) /* 'q' typed in more */
6268 break;
6269 col = 0;
6270 for (i = row; i < item_count; i += rows)
6271 {
6272 msg_col = col; /* make columns */
6273 show_one_termcode(termcodes[items[i]].name,
6274 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006275 if (run == 2)
6276 col += INC2;
6277 else
6278 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 out_flush();
6281 ui_breakcheck();
6282 }
6283 }
6284 vim_free(items);
6285}
6286
6287/*
6288 * Show one termcode entry.
6289 * Output goes into IObuff[]
6290 */
6291 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006292show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 char_u *p;
6295 int len;
6296
6297 if (name[0] > '~')
6298 {
6299 IObuff[0] = ' ';
6300 IObuff[1] = ' ';
6301 IObuff[2] = ' ';
6302 IObuff[3] = ' ';
6303 }
6304 else
6305 {
6306 IObuff[0] = 't';
6307 IObuff[1] = '_';
6308 IObuff[2] = name[0];
6309 IObuff[3] = name[1];
6310 }
6311 IObuff[4] = ' ';
6312
6313 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6314 if (p[1] != 't')
6315 STRCPY(IObuff + 5, p);
6316 else
6317 IObuff[5] = NUL;
6318 len = (int)STRLEN(IObuff);
6319 do
6320 IObuff[len++] = ' ';
6321 while (len < 17);
6322 IObuff[len] = NUL;
6323 if (code == NULL)
6324 len += 4;
6325 else
6326 len += vim_strsize(code);
6327
6328 if (printit)
6329 {
6330 msg_puts(IObuff);
6331 if (code == NULL)
6332 msg_puts((char_u *)"NULL");
6333 else
6334 msg_outtrans(code);
6335 }
6336 return len;
6337}
6338
6339#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6340/*
6341 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6342 * termcap codes from the terminal itself.
6343 * We get them one by one to avoid a very long response string.
6344 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006345static int xt_index_in = 0;
6346static int xt_index_out = 0;
6347
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006349req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 xt_index_out = 0;
6352 xt_index_in = 0;
6353 req_more_codes_from_term();
6354}
6355
6356 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006357req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 char buf[11];
6360 int old_idx = xt_index_out;
6361
6362 /* Don't do anything when going to exit. */
6363 if (exiting)
6364 return;
6365
6366 /* Send up to 10 more requests out than we received. Avoid sending too
6367 * many, there can be a buffer overflow somewhere. */
6368 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6369 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006370# ifdef DEBUG_TERMRESPONSE
6371 char dbuf[100];
6372
6373 sprintf(dbuf, "Requesting XT %d: %s",
6374 xt_index_out, key_names[xt_index_out]);
6375 log_tr(dbuf);
6376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 sprintf(buf, "\033P+q%02x%02x\033\\",
6378 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6379 out_str_nf((char_u *)buf);
6380 ++xt_index_out;
6381 }
6382
6383 /* Send the codes out right away. */
6384 if (xt_index_out != old_idx)
6385 out_flush();
6386}
6387
6388/*
6389 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6390 * A "0" instead of the "1" indicates a code that isn't supported.
6391 * Both <name> and <string> are encoded in hex.
6392 * "code" points to the "0" or "1".
6393 */
6394 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006395got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397#define XT_LEN 100
6398 char_u name[3];
6399 char_u str[XT_LEN];
6400 int i;
6401 int j = 0;
6402 int c;
6403
6404 /* A '1' means the code is supported, a '0' means it isn't.
6405 * When half the length is > XT_LEN we can't use it.
6406 * Our names are currently all 2 characters. */
6407 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6408 {
6409 /* Get the name from the response and find it in the table. */
6410 name[0] = hexhex2nr(code + 3);
6411 name[1] = hexhex2nr(code + 5);
6412 name[2] = NUL;
6413 for (i = 0; key_names[i] != NULL; ++i)
6414 {
6415 if (STRCMP(key_names[i], name) == 0)
6416 {
6417 xt_index_in = i;
6418 break;
6419 }
6420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006421# ifdef DEBUG_TERMRESPONSE
6422 {
6423 char buf[100];
6424
6425 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6426 log_tr(buf);
6427 }
6428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (key_names[i] != NULL)
6430 {
6431 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6432 str[j++] = c;
6433 str[j] = NUL;
6434 if (name[0] == 'C' && name[1] == 'o')
6435 {
6436 /* Color count is not a key code. */
6437 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006438 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
6440 else
6441 {
6442 /* First delete any existing entry with the same code. */
6443 i = find_term_bykeys(str);
6444 if (i >= 0)
6445 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006446 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 }
6449 }
6450
6451 /* May request more codes now that we received one. */
6452 ++xt_index_in;
6453 req_more_codes_from_term();
6454}
6455
6456/*
6457 * Check if there are any unanswered requests and deal with them.
6458 * This is called before starting an external program or getting direct
6459 * keyboard input. We don't want responses to be send to that program or
6460 * handled as typed text.
6461 */
6462 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006463check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 int c;
6466
6467 /* If no codes requested or all are answered, no need to wait. */
6468 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6469 return;
6470
6471 /* Vgetc() will check for and handle any response.
6472 * Keep calling vpeekc() until we don't get any responses. */
6473 ++no_mapping;
6474 ++allow_keys;
6475 for (;;)
6476 {
6477 c = vpeekc();
6478 if (c == NUL) /* nothing available */
6479 break;
6480
6481 /* If a response is recognized it's replaced with K_IGNORE, must read
6482 * it from the input stream. If there is no K_IGNORE we can't do
6483 * anything, break here (there might be some responses further on, but
6484 * we don't want to throw away any typed chars). */
6485 if (c != K_SPECIAL && c != K_IGNORE)
6486 break;
6487 c = vgetc();
6488 if (c != K_IGNORE)
6489 {
6490 vungetc(c);
6491 break;
6492 }
6493 }
6494 --no_mapping;
6495 --allow_keys;
6496}
6497#endif
6498
6499#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6500/*
6501 * Translate an internal mapping/abbreviation representation into the
6502 * corresponding external one recognized by :map/:abbrev commands;
6503 * respects the current B/k/< settings of 'cpoption'.
6504 *
6505 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006506 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 *
6508 * It uses a growarray to build the translation string since the
6509 * latter can be wider than the original description. The caller has to
6510 * free the string afterwards.
6511 *
6512 * Returns NULL when there is a problem.
6513 */
6514 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006515translate_mapping(
6516 char_u *str,
6517 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 garray_T ga;
6520 int c;
6521 int modifiers;
6522 int cpo_bslash;
6523 int cpo_special;
6524 int cpo_keycode;
6525
6526 ga_init(&ga);
6527 ga.ga_itemsize = 1;
6528 ga.ga_growsize = 40;
6529
6530 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6531 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6532 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6533
6534 for (; *str; ++str)
6535 {
6536 c = *str;
6537 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6538 {
6539 modifiers = 0;
6540 if (str[1] == KS_MODIFIER)
6541 {
6542 str++;
6543 modifiers = *++str;
6544 c = *++str;
6545 }
6546 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6547 {
6548 int i;
6549
6550 /* try to find special key in termcodes */
6551 for (i = 0; i < tc_len; ++i)
6552 if (termcodes[i].name[0] == str[1]
6553 && termcodes[i].name[1] == str[2])
6554 break;
6555 if (i < tc_len)
6556 {
6557 ga_concat(&ga, termcodes[i].code);
6558 str += 2;
6559 continue; /* for (str) */
6560 }
6561 }
6562 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6563 {
6564 if (expmap && cpo_special)
6565 {
6566 ga_clear(&ga);
6567 return NULL;
6568 }
6569 c = TO_SPECIAL(str[1], str[2]);
6570 if (c == K_ZERO) /* display <Nul> as ^@ */
6571 c = NUL;
6572 str += 2;
6573 }
6574 if (IS_SPECIAL(c) || modifiers) /* special key */
6575 {
6576 if (expmap && cpo_special)
6577 {
6578 ga_clear(&ga);
6579 return NULL;
6580 }
6581 ga_concat(&ga, get_special_key_name(c, modifiers));
6582 continue; /* for (str) */
6583 }
6584 }
6585 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6586 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6587 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6588 if (c)
6589 ga_append(&ga, c);
6590 }
6591 ga_append(&ga, NUL);
6592 return (char_u *)(ga.ga_data);
6593}
6594#endif
6595
6596#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6597static char ksme_str[20];
6598static char ksmr_str[20];
6599static char ksmd_str[20];
6600
6601/*
6602 * For Win32 console: update termcap codes for existing console attributes.
6603 */
6604 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006605update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 struct builtin_term *p;
6608
6609 p = find_builtin_term(DEFAULT_TERM);
6610 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6611 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6612 attr | 0x08); /* FOREGROUND_INTENSITY */
6613 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6614 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6615
6616 while (p->bt_string != NULL)
6617 {
6618 if (p->bt_entry == (int)KS_ME)
6619 p->bt_string = &ksme_str[0];
6620 else if (p->bt_entry == (int)KS_MR)
6621 p->bt_string = &ksmr_str[0];
6622 else if (p->bt_entry == (int)KS_MD)
6623 p->bt_string = &ksmd_str[0];
6624 ++p;
6625 }
6626}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006627
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006628# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006629struct ks_tbl_s
6630{
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006631 int code; /* value of KS_ */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006632 char *vtp; /* code in vtp mode */
6633 char *buf; /* buffer in non-vtp mode */
6634 char *vbuf; /* buffer in vtp mode */
6635};
6636
6637static struct ks_tbl_s ks_tbl[] =
6638{
6639 {(int)KS_ME, "\033|0m" }, /* normal */
6640 {(int)KS_MR, "\033|7m" }, /* reverse */
6641 {(int)KS_MD, "\033|1m" }, /* bold */
6642 {(int)KS_SO, "\033|91m"}, /* standout: bright red text */
6643 {(int)KS_SE, "\033|39m"}, /* standout end: default color */
6644 {(int)KS_CZH, "\033|95m"}, /* italic: bright magenta text */
6645 {(int)KS_CZR, "\033|0m",}, /* italic end */
6646 {(int)KS_US, "\033|4m",}, /* underscore */
6647 {(int)KS_UE, "\033|24m"}, /* underscore end */
6648 {(int)KS_NAME, NULL}
6649};
6650
6651 static struct builtin_term *
6652find_first_tcap(
6653 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006654 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006655{
6656 struct builtin_term *p;
6657
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006658 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659 if (p->bt_entry == code)
6660 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006661 return NULL;
6662}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006663# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006664
6665/*
6666 * For Win32 console: replace the sequence immediately after termguicolors.
6667 */
6668 void
6669swap_tcap(void)
6670{
6671# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006672 static int init_done = FALSE;
6673 static int last_tgc;
6674 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006675 struct builtin_term *bt;
6676
6677 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006678 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006679 {
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006680 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006681 {
6682 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006683 if (bt != NULL)
6684 {
6685 ks->buf = bt->bt_string;
6686 ks->vbuf = ks->vtp;
6687 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006688 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006689 init_done = TRUE;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006690 last_tgc = p_tgc;
6691 return;
6692 }
6693
6694 if (last_tgc != p_tgc)
6695 {
6696 if (p_tgc)
6697 {
6698 /* switch to special character sequence */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006699 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006700 {
6701 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006702 if (bt != NULL)
6703 {
6704 ks->buf = bt->bt_string;
6705 bt->bt_string = ks->vbuf;
6706 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006707 }
6708 }
6709 else
6710 {
6711 /* switch to index color */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006712 for (ks = ks_tbl; ks->vtp != NULL; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006713 {
6714 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006715 if (bt != NULL)
6716 {
6717 ks->vbuf = bt->bt_string;
6718 bt->bt_string = ks->buf;
6719 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006720 }
6721 }
6722
6723 last_tgc = p_tgc;
6724 }
6725# endif
6726}
6727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006729
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006730#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006731 static int
6732hex_digit(int c)
6733{
6734 if (isdigit(c))
6735 return c - '0';
6736 c = TOLOWER_ASC(c);
6737 if (c >= 'a' && c <= 'f')
6738 return c - 'a' + 10;
6739 return 0x1ffffff;
6740}
6741
6742 guicolor_T
6743gui_get_color_cmn(char_u *name)
6744{
Bram Moolenaar28726652017-01-06 18:16:19 +01006745 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6746 * values as used by the MS-Windows GDI api. It should be used only for
6747 * MS-Windows GDI builds. */
6748# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6749# undef RGB
6750# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006751# ifndef RGB
6752# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6753# endif
6754# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006755 FILE *fd;
6756 char line[LINE_LEN];
6757 char_u *fname;
6758 int r, g, b, i;
6759 guicolor_T color;
6760
6761 struct rgbcolor_table_S {
6762 char_u *color_name;
6763 guicolor_T color;
6764 };
6765
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006766 /* Only non X11 colors (not present in rgb.txt) and colors in
6767 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006768 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006769 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6770 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6771 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6772 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6773 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6774 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6775 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6776 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6777 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6778 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6779 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6780 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6781 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006782 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6783 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006784 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006785 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006786 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6787 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6788 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6789 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6790 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6791 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6792 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6793 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6794 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006795 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006796 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006797 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6798 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006799 };
6800
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006801 static struct rgbcolor_table_S *colornames_table;
6802 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006803
6804 if (name[0] == '#' && STRLEN(name) == 7)
6805 {
6806 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006807 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006808 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6809 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6810 if (color > 0xffffff)
6811 return INVALCOLOR;
6812 return color;
6813 }
6814
6815 /* Check if the name is one of the colors we know */
6816 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6817 if (STRICMP(name, rgb_table[i].color_name) == 0)
6818 return rgb_table[i].color;
6819
6820 /*
6821 * Last attempt. Look in the file "$VIM/rgb.txt".
6822 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006823 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006824 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006825 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006826
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006827 /* colornames_table not yet initialized */
6828 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6829 if (fname == NULL)
6830 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006831
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006832 fd = fopen((char *)fname, "rt");
6833 vim_free(fname);
6834 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006835 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006836 if (p_verbose > 1)
6837 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6838 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006839 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006840
6841 for (counting = 1; counting >= 0; --counting)
6842 {
6843 if (!counting)
6844 {
6845 colornames_table = (struct rgbcolor_table_S *)alloc(
6846 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6847 if (colornames_table == NULL)
6848 {
6849 fclose(fd);
6850 return INVALCOLOR;
6851 }
6852 rewind(fd);
6853 }
6854 size = 0;
6855
6856 while (!feof(fd))
6857 {
6858 size_t len;
6859 int pos;
6860
6861 ignoredp = fgets(line, LINE_LEN, fd);
6862 len = strlen(line);
6863
6864 if (len <= 1 || line[len - 1] != '\n')
6865 continue;
6866
6867 line[len - 1] = '\0';
6868
6869 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6870 if (i != 3)
6871 continue;
6872
6873 if (!counting)
6874 {
6875 char_u *s = vim_strsave((char_u *)line + pos);
6876
6877 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006878 {
6879 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006880 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006881 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006882 colornames_table[size].color_name = s;
6883 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6884 }
6885 size++;
6886 }
6887 }
6888 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006889 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006890
6891 for (i = 0; i < size; i++)
6892 if (STRICMP(name, colornames_table[i].color_name) == 0)
6893 return colornames_table[i].color;
6894
Bram Moolenaarab302212016-04-26 20:59:29 +02006895 return INVALCOLOR;
6896}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006897
6898 guicolor_T
6899gui_get_rgb_color_cmn(int r, int g, int b)
6900{
6901 guicolor_T color = RGB(r, g, b);
6902
6903 if (color > 0xffffff)
6904 return INVALCOLOR;
6905 return color;
6906}
Bram Moolenaarab302212016-04-26 20:59:29 +02006907#endif