blob: 8b69cc41a534bb00912f3efc0ad5deb311a16fe3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010079static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static void req_codes_from_term(void);
82static void req_more_codes_from_term(void);
83static void got_code_from_term(char_u *code, int len);
84static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
86#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000087 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
88 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010089static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void del_termcode_idx(int idx);
92static int term_is_builtin(char_u *name);
93static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#endif
97
98#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010099static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
101/*
102 * Here is our own prototype for tgetstr(), any prototypes from the include
103 * files have been disabled by the define at the start of this file.
104 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100105char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200108 /* Change this to "if 1" to debug what happens with termresponse. */
109# if 0
110# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200111static void log_tr(const char *fmt, ...);
112# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200113# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200114# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200115# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200116
117# define STATUS_GET 1 /* send request when switching to RAW mode */
118# define STATUS_SENT 2 /* did send request, waiting for response */
119# define STATUS_GOT 3 /* received response */
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200122static int crv_status = STATUS_GET;
123
Bram Moolenaar9584b312013-03-13 19:29:28 +0100124/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200125static int u7_status = STATUS_GET;
126
Bram Moolenaar9377df32017-10-15 13:22:01 +0200127# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200128/* Request foreground color report: */
129static int rfg_status = STATUS_GET;
130static int fg_r = 0;
131static int fg_g = 0;
132static int fg_b = 0;
133static int bg_r = 255;
134static int bg_g = 255;
135static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200136# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200137
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200138/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200139static int rbg_status = STATUS_GET;
140
Bram Moolenaar4db25542017-08-28 22:43:05 +0200141/* Request cursor blinking mode report: */
142static int rbm_status = STATUS_GET;
143
144/* Request cursor style report: */
145static int rcs_status = STATUS_GET;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100146
147/* Request windos position report: */
148static int winpos_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# endif
150
151/*
152 * Don't declare these variables if termcap.h contains them.
153 * Autoconf checks if these variables should be declared extern (not all
154 * systems have them).
155 * Some versions define ospeed to be speed_t, but that is incompatible with
156 * BSD, where ospeed is short and speed_t is long.
157 */
158# ifndef HAVE_OSPEED
159# ifdef OSPEED_EXTERN
160extern short ospeed;
161# else
162short ospeed;
163# endif
164# endif
165# ifndef HAVE_UP_BC_PC
166# ifdef UP_BC_PC_EXTERN
167extern char *UP, *BC, PC;
168# else
169char *UP, *BC, PC;
170# endif
171# endif
172
173# define TGETSTR(s, p) vim_tgetstr((s), (p))
174# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif /* HAVE_TGETENT */
177
178static int detected_8bit = FALSE; /* detected 8-bit terminal */
179
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200180#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200181/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200182 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200183static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200184
185/* The blink flag from the style response may be inverted from the actual
186 * blinking state, xterm XORs the flags. */
187static int initial_cursor_shape_blink = FALSE;
188
189/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200190static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200191#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200192
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000193static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194{
195
196#if defined(FEAT_GUI)
197/*
198 * GUI pseudo term-cap.
199 */
200 {(int)KS_NAME, "gui"},
201 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
202 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
203# ifdef TERMINFO
204 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
205# else
206 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
207# endif
208 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
209# ifdef TERMINFO
210 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
211 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# else
214 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
215 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# endif
218 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
219 /* attributes switched on with 'h', off with * 'H' */
220 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
221 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
222 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
223 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
224 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
225 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
226 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000227 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
228 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200229 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
230 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
232 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
233 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
234 {(int)KS_MS, "y"},
235 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100236 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {(int)KS_LE, "\b"}, /* cursor-left = BS */
238 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
239# ifdef TERMINFO
240 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
241# else
242 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
243# endif
244 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000245 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#endif
247
248#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
251/*
252 * Amiga console window, default for Amiga
253 */
254 {(int)KS_NAME, "amiga"},
255 {(int)KS_CE, "\033[K"},
256 {(int)KS_CD, "\033[J"},
257 {(int)KS_AL, "\033[L"},
258# ifdef TERMINFO
259 {(int)KS_CAL, "\033[%p1%dL"},
260# else
261 {(int)KS_CAL, "\033[%dL"},
262# endif
263 {(int)KS_DL, "\033[M"},
264# ifdef TERMINFO
265 {(int)KS_CDL, "\033[%p1%dM"},
266# else
267 {(int)KS_CDL, "\033[%dM"},
268# endif
269 {(int)KS_CL, "\014"},
270 {(int)KS_VI, "\033[0 p"},
271 {(int)KS_VE, "\033[1 p"},
272 {(int)KS_ME, "\033[0m"},
273 {(int)KS_MR, "\033[7m"},
274 {(int)KS_MD, "\033[1m"},
275 {(int)KS_SE, "\033[0m"},
276 {(int)KS_SO, "\033[33m"},
277 {(int)KS_US, "\033[4m"},
278 {(int)KS_UE, "\033[0m"},
279 {(int)KS_CZH, "\033[3m"},
280 {(int)KS_CZR, "\033[0m"},
281#if defined(__MORPHOS__) || defined(__AROS__)
282 {(int)KS_CCO, "8"}, /* allow 8 colors */
283# ifdef TERMINFO
284 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
285 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
286# else
287 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
288 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
289# endif
290 {(int)KS_OP, "\033[m"}, /* reset colors */
291#endif
292 {(int)KS_MS, "y"},
293 {(int)KS_UT, "y"}, /* guessed */
294 {(int)KS_LE, "\b"},
295# ifdef TERMINFO
296 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
297# else
298 {(int)KS_CM, "\033[%i%d;%dH"},
299# endif
300#if defined(__MORPHOS__)
301 {(int)KS_SR, "\033M"},
302#endif
303# ifdef TERMINFO
304 {(int)KS_CRI, "\033[%p1%dC"},
305# else
306 {(int)KS_CRI, "\033[%dC"},
307# endif
308 {K_UP, "\233A"},
309 {K_DOWN, "\233B"},
310 {K_LEFT, "\233D"},
311 {K_RIGHT, "\233C"},
312 {K_S_UP, "\233T"},
313 {K_S_DOWN, "\233S"},
314 {K_S_LEFT, "\233 A"},
315 {K_S_RIGHT, "\233 @"},
316 {K_S_TAB, "\233Z"},
317 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
318 {K_F2, "\233\061~"},
319 {K_F3, "\233\062~"},
320 {K_F4, "\233\063~"},
321 {K_F5, "\233\064~"},
322 {K_F6, "\233\065~"},
323 {K_F7, "\233\066~"},
324 {K_F8, "\233\067~"},
325 {K_F9, "\233\070~"},
326 {K_F10, "\233\071~"},
327 {K_S_F1, "\233\061\060~"},
328 {K_S_F2, "\233\061\061~"},
329 {K_S_F3, "\233\061\062~"},
330 {K_S_F4, "\233\061\063~"},
331 {K_S_F5, "\233\061\064~"},
332 {K_S_F6, "\233\061\065~"},
333 {K_S_F7, "\233\061\066~"},
334 {K_S_F8, "\233\061\067~"},
335 {K_S_F9, "\233\061\070~"},
336 {K_S_F10, "\233\061\071~"},
337 {K_HELP, "\233?~"},
338 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
339 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
340 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
341 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
342 {K_END, "\233\064\065~"}, /* 101 key keyboard */
343
344 {BT_EXTRA_KEYS, ""},
345 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
346 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
347 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
348# endif
349
350# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
351/*
352 * almost standard ANSI terminal, default for bebox
353 */
354 {(int)KS_NAME, "beos-ansi"},
355 {(int)KS_CE, "\033[K"},
356 {(int)KS_CD, "\033[J"},
357 {(int)KS_AL, "\033[L"},
358# ifdef TERMINFO
359 {(int)KS_CAL, "\033[%p1%dL"},
360# else
361 {(int)KS_CAL, "\033[%dL"},
362# endif
363 {(int)KS_DL, "\033[M"},
364# ifdef TERMINFO
365 {(int)KS_CDL, "\033[%p1%dM"},
366# else
367 {(int)KS_CDL, "\033[%dM"},
368# endif
369#ifdef BEOS_PR_OR_BETTER
370# ifdef TERMINFO
371 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
372# else
373 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
374# endif
375#endif
376 {(int)KS_CL, "\033[H\033[2J"},
377#ifdef notyet
378 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
379 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
380#endif
381 {(int)KS_ME, "\033[m"}, /* normal mode */
382 {(int)KS_MR, "\033[7m"}, /* reverse */
383 {(int)KS_MD, "\033[1m"}, /* bold */
384 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
385 {(int)KS_SE, "\033[m"}, /* standout end */
386 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
387 {(int)KS_CZR, "\033[m"}, /* italic end */
388 {(int)KS_US, "\033[4m"}, /* underscore mode */
389 {(int)KS_UE, "\033[m"}, /* underscore end */
390 {(int)KS_CCO, "8"}, /* allow 8 colors */
391# ifdef TERMINFO
392 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
393 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
394# else
395 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
396 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
397# endif
398 {(int)KS_OP, "\033[m"}, /* reset colors */
399 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
400 {(int)KS_UT, "y"}, /* guessed */
401 {(int)KS_LE, "\b"},
402# ifdef TERMINFO
403 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
404# else
405 {(int)KS_CM, "\033[%i%d;%dH"},
406# endif
407 {(int)KS_SR, "\033M"},
408# ifdef TERMINFO
409 {(int)KS_CRI, "\033[%p1%dC"},
410# else
411 {(int)KS_CRI, "\033[%dC"},
412# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200413# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
417 {K_UP, "\033[A"},
418 {K_DOWN, "\033[B"},
419 {K_LEFT, "\033[D"},
420 {K_RIGHT, "\033[C"},
421# endif
422
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200423# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424/*
425 * standard ANSI terminal, default for unix
426 */
427 {(int)KS_NAME, "ansi"},
428 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
429 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
430# ifdef TERMINFO
431 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
432# else
433 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
434# endif
435 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
436# ifdef TERMINFO
437 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
438# else
439 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
440# endif
441 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
442 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
443 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
444 {(int)KS_MS, "y"},
445 {(int)KS_UT, "y"}, /* guessed */
446 {(int)KS_LE, "\b"},
447# ifdef TERMINFO
448 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
449# else
450 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
451# endif
452# ifdef TERMINFO
453 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
454# else
455 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
456# endif
457# endif
458
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200459# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
461 * These codes are valid when nansi.sys or equivalent has been installed.
462 * Function keys on a PC are preceded with a NUL. These are converted into
463 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
464 * CTRL-arrow is used instead of SHIFT-arrow.
465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_NAME, "pcansi"},
467 {(int)KS_DL, "\033[M"},
468 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {(int)KS_CE, "\033[K"},
470 {(int)KS_CL, "\033[2J"},
471 {(int)KS_ME, "\033[0m"},
472 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
473 {(int)KS_MD, "\033[1m"}, /* bold: white text */
474 {(int)KS_SE, "\033[0m"}, /* standout end */
475 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
476 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
477 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
478 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
479 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
480 {(int)KS_CCO, "8"}, /* allow 8 colors */
481# ifdef TERMINFO
482 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
483 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
484# else
485 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
486 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
487# endif
488 {(int)KS_OP, "\033[0m"}, /* reset colors */
489 {(int)KS_MS, "y"},
490 {(int)KS_UT, "y"}, /* guessed */
491 {(int)KS_LE, "\b"},
492# ifdef TERMINFO
493 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
494# else
495 {(int)KS_CM, "\033[%i%d;%dH"},
496# endif
497# ifdef TERMINFO
498 {(int)KS_CRI, "\033[%p1%dC"},
499# else
500 {(int)KS_CRI, "\033[%dC"},
501# endif
502 {K_UP, "\316H"},
503 {K_DOWN, "\316P"},
504 {K_LEFT, "\316K"},
505 {K_RIGHT, "\316M"},
506 {K_S_LEFT, "\316s"},
507 {K_S_RIGHT, "\316t"},
508 {K_F1, "\316;"},
509 {K_F2, "\316<"},
510 {K_F3, "\316="},
511 {K_F4, "\316>"},
512 {K_F5, "\316?"},
513 {K_F6, "\316@"},
514 {K_F7, "\316A"},
515 {K_F8, "\316B"},
516 {K_F9, "\316C"},
517 {K_F10, "\316D"},
518 {K_F11, "\316\205"}, /* guessed */
519 {K_F12, "\316\206"}, /* guessed */
520 {K_S_F1, "\316T"},
521 {K_S_F2, "\316U"},
522 {K_S_F3, "\316V"},
523 {K_S_F4, "\316W"},
524 {K_S_F5, "\316X"},
525 {K_S_F6, "\316Y"},
526 {K_S_F7, "\316Z"},
527 {K_S_F8, "\316["},
528 {K_S_F9, "\316\\"},
529 {K_S_F10, "\316]"},
530 {K_S_F11, "\316\207"}, /* guessed */
531 {K_S_F12, "\316\210"}, /* guessed */
532 {K_INS, "\316R"},
533 {K_DEL, "\316S"},
534 {K_HOME, "\316G"},
535 {K_END, "\316O"},
536 {K_PAGEDOWN, "\316Q"},
537 {K_PAGEUP, "\316I"},
538# endif
539
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200540# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/*
542 * These codes are valid for the Win32 Console . The entries that start with
543 * ESC | are translated into console calls in os_win32.c. The function keys
544 * are also translated in os_win32.c.
545 */
546 {(int)KS_NAME, "win32"},
547 {(int)KS_CE, "\033|K"}, /* clear to end of line */
548 {(int)KS_AL, "\033|L"}, /* add new blank line */
549# ifdef TERMINFO
550 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
551# else
552 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
553# endif
554 {(int)KS_DL, "\033|M"}, /* delete line */
555# ifdef TERMINFO
556 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
557# else
558 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
559# endif
560 {(int)KS_CL, "\033|J"}, /* clear screen */
561 {(int)KS_CD, "\033|j"}, /* clear to end of display */
562 {(int)KS_VI, "\033|v"}, /* cursor invisible */
563 {(int)KS_VE, "\033|V"}, /* cursor visible */
564
565 {(int)KS_ME, "\033|0m"}, /* normal */
566 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
567 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
568#if 1
569 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
570 {(int)KS_SE, "\033|0m"}, /* standout end */
571#else
572 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
573 {(int)KS_SE, "\033|f"}, /* standout end */
574#endif
575 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
576 {(int)KS_CZR, "\033|0m"}, /* italic end */
577 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
578 {(int)KS_UE, "\033|0m"}, /* underscore end */
579 {(int)KS_CCO, "16"}, /* allow 16 colors */
580# ifdef TERMINFO
581 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
582 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
583# else
584 {(int)KS_CAB, "\033|%db"}, /* set background color */
585 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
586# endif
587
588 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
589 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100590 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {(int)KS_LE, "\b"},
592# ifdef TERMINFO
593 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
594# else
595 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
596# endif
597 {(int)KS_VB, "\033|B"}, /* visual bell */
598 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
599 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
600# ifdef TERMINFO
601 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
602# else
603 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
604# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100605# ifdef FEAT_TERMGUICOLORS
606 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
607 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 {K_UP, "\316H"},
611 {K_DOWN, "\316P"},
612 {K_LEFT, "\316K"},
613 {K_RIGHT, "\316M"},
614 {K_S_UP, "\316\304"},
615 {K_S_DOWN, "\316\317"},
616 {K_S_LEFT, "\316\311"},
617 {K_C_LEFT, "\316s"},
618 {K_S_RIGHT, "\316\313"},
619 {K_C_RIGHT, "\316t"},
620 {K_S_TAB, "\316\017"},
621 {K_F1, "\316;"},
622 {K_F2, "\316<"},
623 {K_F3, "\316="},
624 {K_F4, "\316>"},
625 {K_F5, "\316?"},
626 {K_F6, "\316@"},
627 {K_F7, "\316A"},
628 {K_F8, "\316B"},
629 {K_F9, "\316C"},
630 {K_F10, "\316D"},
631 {K_F11, "\316\205"},
632 {K_F12, "\316\206"},
633 {K_S_F1, "\316T"},
634 {K_S_F2, "\316U"},
635 {K_S_F3, "\316V"},
636 {K_S_F4, "\316W"},
637 {K_S_F5, "\316X"},
638 {K_S_F6, "\316Y"},
639 {K_S_F7, "\316Z"},
640 {K_S_F8, "\316["},
641 {K_S_F9, "\316\\"},
642 {K_S_F10, "\316]"},
643 {K_S_F11, "\316\207"},
644 {K_S_F12, "\316\210"},
645 {K_INS, "\316R"},
646 {K_DEL, "\316S"},
647 {K_HOME, "\316G"},
648 {K_S_HOME, "\316\302"},
649 {K_C_HOME, "\316w"},
650 {K_END, "\316O"},
651 {K_S_END, "\316\315"},
652 {K_C_END, "\316u"},
653 {K_PAGEDOWN, "\316Q"},
654 {K_PAGEUP, "\316I"},
655 {K_KPLUS, "\316N"},
656 {K_KMINUS, "\316J"},
657 {K_KMULTIPLY, "\316\067"},
658 {K_K0, "\316\332"},
659 {K_K1, "\316\336"},
660 {K_K2, "\316\342"},
661 {K_K3, "\316\346"},
662 {K_K4, "\316\352"},
663 {K_K5, "\316\356"},
664 {K_K6, "\316\362"},
665 {K_K7, "\316\366"},
666 {K_K8, "\316\372"},
667 {K_K9, "\316\376"},
668# endif
669
670# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
671/*
672 * VT320 is working as an ANSI terminal compatible DEC terminal.
673 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
674 * Note: K_F1...K_F5 are for internal use, should not be defined.
675 * TODO:- rewrite ESC[ codes to CSI
676 * - keyboard languages (CSI ? 26 n)
677 */
678 {(int)KS_NAME, "vt320"},
679 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
680 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
681# ifdef TERMINFO
682 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
683# else
684 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
685# endif
686 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
687# ifdef TERMINFO
688 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
689# else
690 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
691# endif
692 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000693 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
694 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
696 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
698 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
699 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
700 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
701 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
702 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
703 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
704 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
705 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
706 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_MS, "y"},
708 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100709 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {(int)KS_LE, "\b"},
711# ifdef TERMINFO
712 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
713 ESC_STR "[%i%p1%d;%p2%dH")},
714# else
715 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
716# endif
717# ifdef TERMINFO
718 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
719# else
720 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
721# endif
722 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
723 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
724 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
725 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
727 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
728 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
729 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
730 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
732 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
733 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
734 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
735 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
738 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
739 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
740 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
741 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
742 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
743 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
744 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
745 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
746 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
747 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
748 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
749 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
750 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
751 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
752 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
753 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
754 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
755 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
756 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
757 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
758# endif
759
760# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
761/*
762 * Ordinary vt52
763 */
764 {(int)KS_NAME, "vt52"},
765 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
766 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100767# ifdef TERMINFO
768 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
769 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100774 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
776 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_UP, IF_EB("\033A", ESC_STR "A")},
778 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
779 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
780 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100781 {K_F1, IF_EB("\033P", ESC_STR "P")},
782 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
783 {K_F3, IF_EB("\033R", ESC_STR "R")},
784# ifdef __MINT__
785 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
786 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
787 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
788 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
789 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
791 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
792 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
793 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {K_F4, IF_EB("\033S", ESC_STR "S")},
795 {K_F5, IF_EB("\033T", ESC_STR "T")},
796 {K_F6, IF_EB("\033U", ESC_STR "U")},
797 {K_F7, IF_EB("\033V", ESC_STR "V")},
798 {K_F8, IF_EB("\033W", ESC_STR "W")},
799 {K_F9, IF_EB("\033X", ESC_STR "X")},
800 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
801 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
802 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
803 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
804 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
805 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
806 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
807 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
808 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
809 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
810 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
811 {K_INS, IF_EB("\033I", ESC_STR "I")},
812 {K_HOME, IF_EB("\033E", ESC_STR "E")},
813 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
814 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
815# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {(int)KS_MS, "y"},
818# endif
819# endif
820
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200821# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200822 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
824 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
825# ifdef TERMINFO
826 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
827# else
828 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
829# endif
830 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
831# ifdef TERMINFO
832 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
833# else
834 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
835# endif
836# ifdef TERMINFO
837 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
838 ESC_STR "[%i%p1%d;%p2%dr")},
839# else
840 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
841# endif
842 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
843 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
844 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
845 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
846 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
847 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
848 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200849 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
850 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {(int)KS_MS, "y"},
852 {(int)KS_UT, "y"},
853 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200854 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
855 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200856 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200857 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200858# ifdef TERMINFO
859 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
860# else
861 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
862# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200863 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200864 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# ifdef TERMINFO
866 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
867 ESC_STR "[%i%p1%d;%p2%dH")},
868# else
869 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
870# endif
871 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
872# ifdef TERMINFO
873 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
874# else
875 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
876# endif
877 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
878 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
879# ifdef FEAT_XTERM_SAVE
880 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
881 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
882 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
883# endif
884 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
885 {(int)KS_CIE, "\007"},
886 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
887 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200888 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
889 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890# ifdef TERMINFO
891 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
892 ESC_STR "[8;%p1%d;%p2%dt")},
893 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
894 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200895 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896# else
897 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
898 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200899 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900# endif
901 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200902 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200903 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100904 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200905# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200906 /* These are printf strings, not terminal codes. */
907 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
908 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
909# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100910 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
911 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000912
913 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
914 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
915 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
916 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
917 /* An extra set of cursor keys for vt100 mode */
918 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
919 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
920 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
921 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000923 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
924 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
925 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
926 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000927 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
928 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
929 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
930 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
931 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
932 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
933 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
934 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
935 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
936 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
937 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
938 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000940 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
941 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
942 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
943 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
945 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000946 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000947 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000948 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000949 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000950 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
951 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000952 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000954 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000955 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
956 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100957 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
958 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
959 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
960 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
961 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
962 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
963 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
964 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
965 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
969 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
970 /* F14 and F15 are missing, because they send the same codes as the undo
971 * and help key, although they don't work on all keyboards. */
972 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
973 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
974 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
975 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
976 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
977
978 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
979 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
980 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
981 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
982 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
983 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
984 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
985 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
986 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
987 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
988
989 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
990 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
991 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
992 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
993 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
994 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
995 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# endif
997
998# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
999/*
1000 * iris-ansi for Silicon Graphics machines.
1001 */
1002 {(int)KS_NAME, "iris-ansi"},
1003 {(int)KS_CE, "\033[K"},
1004 {(int)KS_CD, "\033[J"},
1005 {(int)KS_AL, "\033[L"},
1006# ifdef TERMINFO
1007 {(int)KS_CAL, "\033[%p1%dL"},
1008# else
1009 {(int)KS_CAL, "\033[%dL"},
1010# endif
1011 {(int)KS_DL, "\033[M"},
1012# ifdef TERMINFO
1013 {(int)KS_CDL, "\033[%p1%dM"},
1014# else
1015 {(int)KS_CDL, "\033[%dM"},
1016# endif
1017#if 0 /* The scroll region is not working as Vim expects. */
1018# ifdef TERMINFO
1019 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1020# else
1021 {(int)KS_CS, "\033[%i%d;%dr"},
1022# endif
1023#endif
1024 {(int)KS_CL, "\033[H\033[2J"},
1025 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1026 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1027 {(int)KS_TI, "\033[=6h"},
1028 {(int)KS_TE, "\033[=6l"},
1029 {(int)KS_SE, "\033[21;27m"},
1030 {(int)KS_SO, "\033[1;7m"},
1031 {(int)KS_ME, "\033[m"},
1032 {(int)KS_MR, "\033[7m"},
1033 {(int)KS_MD, "\033[1m"},
1034 {(int)KS_CCO, "8"}, /* allow 8 colors */
1035 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1036 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1037 {(int)KS_US, "\033[4m"}, /* underline on */
1038 {(int)KS_UE, "\033[24m"}, /* underline off */
1039# ifdef TERMINFO
1040 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1041 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1042 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1043 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1044# else
1045 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1046 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1047 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1048 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1049# endif
1050 {(int)KS_MS, "y"}, /* guessed */
1051 {(int)KS_UT, "y"}, /* guessed */
1052 {(int)KS_LE, "\b"},
1053# ifdef TERMINFO
1054 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1055# else
1056 {(int)KS_CM, "\033[%i%d;%dH"},
1057# endif
1058 {(int)KS_SR, "\033M"},
1059# ifdef TERMINFO
1060 {(int)KS_CRI, "\033[%p1%dC"},
1061# else
1062 {(int)KS_CRI, "\033[%dC"},
1063# endif
1064 {(int)KS_CIS, "\033P3.y"},
1065 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1066 {(int)KS_TS, "\033P1.y"},
1067 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1068# ifdef TERMINFO
1069 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1070 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1071# else
1072 {(int)KS_CWS, "\033[203;%d;%d/y"},
1073 {(int)KS_CWP, "\033[205;%d;%d/y"},
1074# endif
1075 {K_UP, "\033[A"},
1076 {K_DOWN, "\033[B"},
1077 {K_LEFT, "\033[D"},
1078 {K_RIGHT, "\033[C"},
1079 {K_S_UP, "\033[161q"},
1080 {K_S_DOWN, "\033[164q"},
1081 {K_S_LEFT, "\033[158q"},
1082 {K_S_RIGHT, "\033[167q"},
1083 {K_F1, "\033[001q"},
1084 {K_F2, "\033[002q"},
1085 {K_F3, "\033[003q"},
1086 {K_F4, "\033[004q"},
1087 {K_F5, "\033[005q"},
1088 {K_F6, "\033[006q"},
1089 {K_F7, "\033[007q"},
1090 {K_F8, "\033[008q"},
1091 {K_F9, "\033[009q"},
1092 {K_F10, "\033[010q"},
1093 {K_F11, "\033[011q"},
1094 {K_F12, "\033[012q"},
1095 {K_S_F1, "\033[013q"},
1096 {K_S_F2, "\033[014q"},
1097 {K_S_F3, "\033[015q"},
1098 {K_S_F4, "\033[016q"},
1099 {K_S_F5, "\033[017q"},
1100 {K_S_F6, "\033[018q"},
1101 {K_S_F7, "\033[019q"},
1102 {K_S_F8, "\033[020q"},
1103 {K_S_F9, "\033[021q"},
1104 {K_S_F10, "\033[022q"},
1105 {K_S_F11, "\033[023q"},
1106 {K_S_F12, "\033[024q"},
1107 {K_INS, "\033[139q"},
1108 {K_HOME, "\033[H"},
1109 {K_END, "\033[146q"},
1110 {K_PAGEUP, "\033[150q"},
1111 {K_PAGEDOWN, "\033[154q"},
1112# endif
1113
1114# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1115/*
1116 * for debugging
1117 */
1118 {(int)KS_NAME, "debug"},
1119 {(int)KS_CE, "[CE]"},
1120 {(int)KS_CD, "[CD]"},
1121 {(int)KS_AL, "[AL]"},
1122# ifdef TERMINFO
1123 {(int)KS_CAL, "[CAL%p1%d]"},
1124# else
1125 {(int)KS_CAL, "[CAL%d]"},
1126# endif
1127 {(int)KS_DL, "[DL]"},
1128# ifdef TERMINFO
1129 {(int)KS_CDL, "[CDL%p1%d]"},
1130# else
1131 {(int)KS_CDL, "[CDL%d]"},
1132# endif
1133# ifdef TERMINFO
1134 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1135# else
1136 {(int)KS_CS, "[%dCS%d]"},
1137# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001138# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001140# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142# endif
1143# ifdef TERMINFO
1144 {(int)KS_CAB, "[CAB%p1%d]"},
1145 {(int)KS_CAF, "[CAF%p1%d]"},
1146 {(int)KS_CSB, "[CSB%p1%d]"},
1147 {(int)KS_CSF, "[CSF%p1%d]"},
1148# else
1149 {(int)KS_CAB, "[CAB%d]"},
1150 {(int)KS_CAF, "[CAF%d]"},
1151 {(int)KS_CSB, "[CSB%d]"},
1152 {(int)KS_CSF, "[CSF%d]"},
1153# endif
1154 {(int)KS_OP, "[OP]"},
1155 {(int)KS_LE, "[LE]"},
1156 {(int)KS_CL, "[CL]"},
1157 {(int)KS_VI, "[VI]"},
1158 {(int)KS_VE, "[VE]"},
1159 {(int)KS_VS, "[VS]"},
1160 {(int)KS_ME, "[ME]"},
1161 {(int)KS_MR, "[MR]"},
1162 {(int)KS_MB, "[MB]"},
1163 {(int)KS_MD, "[MD]"},
1164 {(int)KS_SE, "[SE]"},
1165 {(int)KS_SO, "[SO]"},
1166 {(int)KS_UE, "[UE]"},
1167 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001168 {(int)KS_UCE, "[UCE]"},
1169 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001170 {(int)KS_STE, "[STE]"},
1171 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_MS, "[MS]"},
1173 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001174 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# ifdef TERMINFO
1176 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1177# else
1178 {(int)KS_CM, "[%dCM%d]"},
1179# endif
1180 {(int)KS_SR, "[SR]"},
1181# ifdef TERMINFO
1182 {(int)KS_CRI, "[CRI%p1%d]"},
1183# else
1184 {(int)KS_CRI, "[CRI%d]"},
1185# endif
1186 {(int)KS_VB, "[VB]"},
1187 {(int)KS_KS, "[KS]"},
1188 {(int)KS_KE, "[KE]"},
1189 {(int)KS_TI, "[TI]"},
1190 {(int)KS_TE, "[TE]"},
1191 {(int)KS_CIS, "[CIS]"},
1192 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001193 {(int)KS_CSC, "[CSC]"},
1194 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_TS, "[TS]"},
1196 {(int)KS_FS, "[FS]"},
1197# ifdef TERMINFO
1198 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1199 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1200# else
1201 {(int)KS_CWS, "[%dCWS%d]"},
1202 {(int)KS_CWP, "[%dCWP%d]"},
1203# endif
1204 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001205 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001206 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001207 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {K_UP, "[KU]"},
1209 {K_DOWN, "[KD]"},
1210 {K_LEFT, "[KL]"},
1211 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001212 {K_XUP, "[xKU]"},
1213 {K_XDOWN, "[xKD]"},
1214 {K_XLEFT, "[xKL]"},
1215 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {K_S_UP, "[S-KU]"},
1217 {K_S_DOWN, "[S-KD]"},
1218 {K_S_LEFT, "[S-KL]"},
1219 {K_C_LEFT, "[C-KL]"},
1220 {K_S_RIGHT, "[S-KR]"},
1221 {K_C_RIGHT, "[C-KR]"},
1222 {K_F1, "[F1]"},
1223 {K_XF1, "[xF1]"},
1224 {K_F2, "[F2]"},
1225 {K_XF2, "[xF2]"},
1226 {K_F3, "[F3]"},
1227 {K_XF3, "[xF3]"},
1228 {K_F4, "[F4]"},
1229 {K_XF4, "[xF4]"},
1230 {K_F5, "[F5]"},
1231 {K_F6, "[F6]"},
1232 {K_F7, "[F7]"},
1233 {K_F8, "[F8]"},
1234 {K_F9, "[F9]"},
1235 {K_F10, "[F10]"},
1236 {K_F11, "[F11]"},
1237 {K_F12, "[F12]"},
1238 {K_S_F1, "[S-F1]"},
1239 {K_S_XF1, "[S-xF1]"},
1240 {K_S_F2, "[S-F2]"},
1241 {K_S_XF2, "[S-xF2]"},
1242 {K_S_F3, "[S-F3]"},
1243 {K_S_XF3, "[S-xF3]"},
1244 {K_S_F4, "[S-F4]"},
1245 {K_S_XF4, "[S-xF4]"},
1246 {K_S_F5, "[S-F5]"},
1247 {K_S_F6, "[S-F6]"},
1248 {K_S_F7, "[S-F7]"},
1249 {K_S_F8, "[S-F8]"},
1250 {K_S_F9, "[S-F9]"},
1251 {K_S_F10, "[S-F10]"},
1252 {K_S_F11, "[S-F11]"},
1253 {K_S_F12, "[S-F12]"},
1254 {K_HELP, "[HELP]"},
1255 {K_UNDO, "[UNDO]"},
1256 {K_BS, "[BS]"},
1257 {K_INS, "[INS]"},
1258 {K_KINS, "[KINS]"},
1259 {K_DEL, "[DEL]"},
1260 {K_KDEL, "[KDEL]"},
1261 {K_HOME, "[HOME]"},
1262 {K_S_HOME, "[C-HOME]"},
1263 {K_C_HOME, "[C-HOME]"},
1264 {K_KHOME, "[KHOME]"},
1265 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001266 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {K_END, "[END]"},
1268 {K_S_END, "[C-END]"},
1269 {K_C_END, "[C-END]"},
1270 {K_KEND, "[KEND]"},
1271 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001272 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {K_PAGEUP, "[PAGEUP]"},
1274 {K_PAGEDOWN, "[PAGEDOWN]"},
1275 {K_KPAGEUP, "[KPAGEUP]"},
1276 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1277 {K_MOUSE, "[MOUSE]"},
1278 {K_KPLUS, "[KPLUS]"},
1279 {K_KMINUS, "[KMINUS]"},
1280 {K_KDIVIDE, "[KDIVIDE]"},
1281 {K_KMULTIPLY, "[KMULTIPLY]"},
1282 {K_KENTER, "[KENTER]"},
1283 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001284 {K_PS, "[PASTE-START]"},
1285 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {K_K0, "[K0]"},
1287 {K_K1, "[K1]"},
1288 {K_K2, "[K2]"},
1289 {K_K3, "[K3]"},
1290 {K_K4, "[K4]"},
1291 {K_K5, "[K5]"},
1292 {K_K6, "[K6]"},
1293 {K_K7, "[K7]"},
1294 {K_K8, "[K8]"},
1295 {K_K9, "[K9]"},
1296# endif
1297
1298#endif /* NO_BUILTIN_TCAPS */
1299
1300/*
1301 * The most minimal terminal: only clear screen and cursor positioning
1302 * Always included.
1303 */
1304 {(int)KS_NAME, "dumb"},
1305 {(int)KS_CL, "\014"},
1306#ifdef TERMINFO
1307 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1308 ESC_STR "[%i%p1%d;%p2%dH")},
1309#else
1310 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1311#endif
1312
1313/*
1314 * end marker
1315 */
1316 {(int)KS_NAME, NULL}
1317
1318}; /* end of builtin_termcaps */
1319
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001320#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001321 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001322termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001323{
Bram Moolenaarab302212016-04-26 20:59:29 +02001324 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001325}
1326
1327 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001328termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001329{
1330 guicolor_T t;
1331
1332 if (*name == NUL)
1333 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001334 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001335
1336 if (t == INVALCOLOR)
1337 EMSG2(_("E254: Cannot allocate color %s"), name);
1338 return t;
1339}
1340
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001341 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001342termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001343{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001344 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001345}
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
1349 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#ifdef AMIGA
1352# define DEFAULT_TERM (char_u *)"amiga"
1353#endif
1354
1355#ifdef MSWIN
1356# define DEFAULT_TERM (char_u *)"win32"
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#if defined(UNIX) && !defined(__MINT__)
1360# define DEFAULT_TERM (char_u *)"ansi"
1361#endif
1362
1363#ifdef __MINT__
1364# define DEFAULT_TERM (char_u *)"vt52"
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#ifdef VMS
1368# define DEFAULT_TERM (char_u *)"vt320"
1369#endif
1370
1371#ifdef __BEOS__
1372# undef DEFAULT_TERM
1373# define DEFAULT_TERM (char_u *)"beos-ansi"
1374#endif
1375
1376#ifndef DEFAULT_TERM
1377# define DEFAULT_TERM (char_u *)"dumb"
1378#endif
1379
1380/*
1381 * Term_strings contains currently used terminal output strings.
1382 * It is initialized with the default values by parse_builtin_tcap().
1383 * The values can be changed by setting the option with the same name.
1384 */
1385char_u *(term_strings[(int)KS_LAST + 1]);
1386
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001387static int need_gather = FALSE; /* need to fill termleader[] */
1388static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001390static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001391static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
1394 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001395find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
1397 struct builtin_term *p;
1398
1399 p = builtin_termcaps;
1400 while (p->bt_string != NULL)
1401 {
1402 if (p->bt_entry == (int)KS_NAME)
1403 {
1404#ifdef UNIX
1405 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1406 return p;
1407 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1408 return p;
1409 else
1410#endif
1411#ifdef VMS
1412 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1413 return p;
1414 else
1415#endif
1416 if (STRCMP(term, p->bt_string) == 0)
1417 return p;
1418 }
1419 ++p;
1420 }
1421 return p;
1422}
1423
1424/*
1425 * Parsing of the builtin termcap entries.
1426 * Caller should check if 'name' is a valid builtin term.
1427 * The terminal's name is not set, as this is already done in termcapinit().
1428 */
1429 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001430parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 struct builtin_term *p;
1433 char_u name[2];
1434 int term_8bit;
1435
1436 p = find_builtin_term(term);
1437 term_8bit = term_is_8bit(term);
1438
1439 /* Do not parse if builtin term not found */
1440 if (p->bt_string == NULL)
1441 return;
1442
1443 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1444 {
1445 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1446 {
1447 /* Only set the value if it wasn't set yet. */
1448 if (term_strings[p->bt_entry] == NULL
1449 || term_strings[p->bt_entry] == empty_option)
1450 {
1451 /* 8bit terminal: use CSI instead of <Esc>[ */
1452 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1453 {
1454 char_u *s, *t;
1455
1456 s = vim_strsave((char_u *)p->bt_string);
1457 if (s != NULL)
1458 {
1459 for (t = s; *t; ++t)
1460 if (term_7to8bit(t))
1461 {
1462 *t = term_7to8bit(t);
1463 STRCPY(t + 1, t + 2);
1464 }
1465 term_strings[p->bt_entry] = s;
1466 set_term_option_alloced(&term_strings[p->bt_entry]);
1467 }
1468 }
1469 else
1470 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1471 }
1472 }
1473 else
1474 {
1475 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1476 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1477 if (find_termcode(name) == NULL)
1478 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1479 }
1480 }
1481}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Set number of colors.
1485 * Store it as a number in t_colors.
1486 * Store it as a string in T_CCO (using nr_colors[]).
1487 */
1488 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 char_u nr_colors[20]; /* string for number of colors */
1492
1493 t_colors = nr;
1494 if (t_colors > 1)
1495 sprintf((char *)nr_colors, "%d", t_colors);
1496 else
1497 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001498 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001500
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001501#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001502/*
1503 * Set the color count to "val" and redraw if it changed.
1504 */
1505 static void
1506may_adjust_color_count(int val)
1507{
1508 if (val != t_colors)
1509 {
1510 /* Nr of colors changed, initialize highlighting and
1511 * redraw everything. This causes a redraw, which usually
1512 * clears the message. Try keeping the message if it
1513 * might work. */
1514 set_keep_msg_from_hist();
1515 set_color_count(val);
1516 init_highlight(TRUE, FALSE);
1517# ifdef DEBUG_TERMRESPONSE
1518 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001519 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001520
Bram Moolenaarb255b902018-04-24 21:40:10 +02001521 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001522 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001523#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001524 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001525#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001526 }
1527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#endif
1529
1530#ifdef HAVE_TGETENT
1531static char *(key_names[]) =
1532{
1533#ifdef FEAT_TERMRESPONSE
1534 /* Do this one first, it may cause a screen redraw. */
1535 "Co",
1536#endif
1537 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 "#2", "#4", "%i", "*7",
1539 "k1", "k2", "k3", "k4", "k5", "k6",
1540 "k7", "k8", "k9", "k;", "F1", "F2",
1541 "%1", "&8", "kb", "kI", "kD", "kh",
1542 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1543 NULL
1544};
1545#endif
1546
Bram Moolenaar9289df52018-05-10 14:40:57 +02001547#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001548 static void
1549get_term_entries(int *height, int *width)
1550{
1551 static struct {
1552 enum SpecialKey dest; /* index in term_strings[] */
1553 char *name; /* termcap name for string */
1554 } string_names[] =
1555 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1556 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1557 {KS_CL, "cl"}, {KS_CD, "cd"},
1558 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1559 {KS_ME, "me"}, {KS_MR, "mr"},
1560 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1561 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1562 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1563 {KS_STE,"Te"}, {KS_STS,"Ts"},
1564 {KS_CM, "cm"}, {KS_SR, "sr"},
1565 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1566 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1567 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1568 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1569 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1570 {KS_VS, "vs"}, {KS_CVS, "VS"},
1571 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1572 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1573 {KS_TS, "ts"}, {KS_FS, "fs"},
1574 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1575 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1576 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1577 {KS_8F, "8f"}, {KS_8B, "8b"},
1578 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1579 {KS_CPS, "PS"}, {KS_CPE, "PE"},
1580 {(enum SpecialKey)0, NULL}
1581 };
1582 int i;
1583 char_u *p;
1584 static char_u tstrbuf[TBUFSZ];
1585 char_u *tp = tstrbuf;
1586
1587 /*
1588 * get output strings
1589 */
1590 for (i = 0; string_names[i].name != NULL; ++i)
1591 {
1592 if (TERM_STR(string_names[i].dest) == NULL
1593 || TERM_STR(string_names[i].dest) == empty_option)
1594 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
1595 }
1596
1597 /* tgetflag() returns 1 if the flag is present, 0 if not and
1598 * possibly -1 if the flag doesn't exist. */
1599 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1600 T_MS = (char_u *)"y";
1601 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1602 T_XS = (char_u *)"y";
1603 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1604 T_XN = (char_u *)"y";
1605 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1606 T_DB = (char_u *)"y";
1607 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1608 T_DA = (char_u *)"y";
1609 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1610 T_UT = (char_u *)"y";
1611
1612 /*
1613 * get key codes
1614 */
1615 for (i = 0; key_names[i] != NULL; ++i)
1616 if (find_termcode((char_u *)key_names[i]) == NULL)
1617 {
1618 p = TGETSTR(key_names[i], &tp);
1619 /* if cursor-left == backspace, ignore it (televideo 925) */
1620 if (p != NULL
1621 && (*p != Ctrl_H
1622 || key_names[i][0] != 'k'
1623 || key_names[i][1] != 'l'))
1624 add_termcode((char_u *)key_names[i], p, FALSE);
1625 }
1626
1627 if (*height == 0)
1628 *height = tgetnum("li");
1629 if (*width == 0)
1630 *width = tgetnum("co");
1631
1632 /*
1633 * Get number of colors (if not done already).
1634 */
1635 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
1636 set_color_count(tgetnum("Co"));
1637
1638# ifndef hpux
1639 BC = (char *)TGETSTR("bc", &tp);
1640 UP = (char *)TGETSTR("up", &tp);
1641 p = TGETSTR("pc", &tp);
1642 if (p)
1643 PC = *p;
1644# endif
1645}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001646#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001647
1648 static void
1649report_term_error(char_u *error_msg, char_u *term)
1650{
1651 struct builtin_term *termp;
1652
1653 mch_errmsg("\r\n");
1654 if (error_msg != NULL)
1655 {
1656 mch_errmsg((char *)error_msg);
1657 mch_errmsg("\r\n");
1658 }
1659 mch_errmsg("'");
1660 mch_errmsg((char *)term);
1661 mch_errmsg(_("' not known. Available builtin terminals are:"));
1662 mch_errmsg("\r\n");
1663 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1664 {
1665 if (termp->bt_entry == (int)KS_NAME)
1666 {
1667#ifdef HAVE_TGETENT
1668 mch_errmsg(" builtin_");
1669#else
1670 mch_errmsg(" ");
1671#endif
1672 mch_errmsg(termp->bt_string);
1673 mch_errmsg("\r\n");
1674 }
1675 }
1676}
1677
1678 static void
1679report_default_term(char_u *term)
1680{
1681 mch_errmsg(_("defaulting to '"));
1682 mch_errmsg((char *)term);
1683 mch_errmsg("'\r\n");
1684 if (emsg_silent == 0)
1685 {
1686 screen_start(); /* don't know where cursor is now */
1687 out_flush();
1688 if (!is_not_a_term())
1689 ui_delay(2000L, TRUE);
1690 }
1691}
1692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
1694 * Set terminal options for terminal "term".
1695 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1696 *
1697 * While doing this, until ttest(), some options may be NULL, be careful.
1698 */
1699 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001700set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 struct builtin_term *termp;
1703#ifdef HAVE_TGETENT
1704 int builtin_first = p_tbi;
1705 int try;
1706 int termcap_cleared = FALSE;
1707#endif
1708 int width = 0, height = 0;
1709 char_u *error_msg = NULL;
1710 char_u *bs_p, *del_p;
1711
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001712 /* In silect mode (ex -s) we don't use the 'term' option. */
1713 if (silent_mode)
1714 return OK;
1715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 detected_8bit = FALSE; /* reset 8-bit detection */
1717
1718 if (term_is_builtin(term))
1719 {
1720 term += 8;
1721#ifdef HAVE_TGETENT
1722 builtin_first = 1;
1723#endif
1724 }
1725
1726/*
1727 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1728 * If builtin_first is TRUE:
1729 * 0. try builtin termcap
1730 * 1. try external termcap
1731 * 2. if both fail default to a builtin terminal
1732 * If builtin_first is FALSE:
1733 * 1. try external termcap
1734 * 2. try builtin termcap, if both fail default to a builtin terminal
1735 */
1736#ifdef HAVE_TGETENT
1737 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1738 {
1739 /*
1740 * Use external termcap
1741 */
1742 if (try == 1)
1743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 /*
1747 * If the external termcap does not have a matching entry, try the
1748 * builtin ones.
1749 */
1750 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (!termcap_cleared)
1753 {
1754 clear_termoptions(); /* clear old options */
1755 termcap_cleared = TRUE;
1756 }
1757
Bram Moolenaar69e05692018-05-10 14:11:52 +02001758 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761 else /* try == 0 || try == 2 */
1762#endif /* HAVE_TGETENT */
1763 /*
1764 * Use builtin termcap
1765 */
1766 {
1767#ifdef HAVE_TGETENT
1768 /*
1769 * If builtin termcap was already used, there is no need to search
1770 * for the builtin termcap again, quit now.
1771 */
1772 if (try == 2 && builtin_first && termcap_cleared)
1773 break;
1774#endif
1775 /*
1776 * search for 'term' in builtin_termcaps[]
1777 */
1778 termp = find_builtin_term(term);
1779 if (termp->bt_string == NULL) /* did not find it */
1780 {
1781#ifdef HAVE_TGETENT
1782 /*
1783 * If try == 0, first try the external termcap. If that is not
1784 * found we'll get back here with try == 2.
1785 * If termcap_cleared is set we used the external termcap,
1786 * don't complain about not finding the term in the builtin
1787 * termcap.
1788 */
1789 if (try == 0) /* try external one */
1790 continue;
1791 if (termcap_cleared) /* found in external termcap */
1792 break;
1793#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001794 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 /* when user typed :set term=xxx, quit here */
1797 if (starting != NO_SCREEN)
1798 {
1799 screen_start(); /* don't know where cursor is now */
1800 wait_return(TRUE);
1801 return FAIL;
1802 }
1803 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001804 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001805 set_string_option_direct((char_u *)"term", -1, term,
1806 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 display_errors();
1808 }
1809 out_flush();
1810#ifdef HAVE_TGETENT
1811 if (!termcap_cleared)
1812 {
1813#endif
1814 clear_termoptions(); /* clear old options */
1815#ifdef HAVE_TGETENT
1816 termcap_cleared = TRUE;
1817 }
1818#endif
1819 parse_builtin_tcap(term);
1820#ifdef FEAT_GUI
1821 if (term_is_gui(term))
1822 {
1823 out_flush();
1824 gui_init();
1825 /* If starting the GUI failed, don't do any of the other
1826 * things for this terminal */
1827 if (!gui.in_use)
1828 return FAIL;
1829#ifdef HAVE_TGETENT
1830 break; /* don't try using external termcap */
1831#endif
1832 }
1833#endif /* FEAT_GUI */
1834 }
1835#ifdef HAVE_TGETENT
1836 }
1837#endif
1838
1839/*
1840 * special: There is no info in the termcap about whether the cursor
1841 * positioning is relative to the start of the screen or to the start of the
1842 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1843 * relative.
1844 */
1845 if (STRCMP(term, "pcterm") == 0)
1846 T_CCS = (char_u *)"yes";
1847 else
1848 T_CCS = empty_option;
1849
1850#ifdef UNIX
1851/*
1852 * Any "stty" settings override the default for t_kb from the termcap.
1853 * This is in os_unix.c, because it depends a lot on the version of unix that
1854 * is being used.
1855 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1856 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001857# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 get_stty();
1861#endif
1862
1863/*
1864 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1865 * didn't work, use the default CTRL-H
1866 * The default for t_kD is DEL, unless t_kb is DEL.
1867 * The vim_strsave'd strings are probably lost forever, well it's only two
1868 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1869 * directly.
1870 */
1871#ifdef FEAT_GUI
1872 if (!gui.in_use)
1873#endif
1874 {
1875 bs_p = find_termcode((char_u *)"kb");
1876 del_p = find_termcode((char_u *)"kD");
1877 if (bs_p == NULL || *bs_p == NUL)
1878 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1879 if ((del_p == NULL || *del_p == NUL) &&
1880 (bs_p == NULL || *bs_p != DEL))
1881 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1882 }
1883
1884#if defined(UNIX) || defined(VMS)
1885 term_is_xterm = vim_is_xterm(term);
1886#endif
1887
1888#ifdef FEAT_MOUSE
1889# if defined(UNIX) || defined(VMS)
1890# ifdef FEAT_MOUSE_TTY
1891 /*
1892 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1893 * The termcode for the mouse is added as a side effect in option.c.
1894 */
1895 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001896 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001899 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 if (use_xterm_mouse())
1902 p = NULL; /* keep existing value, might be "xterm2" */
1903 else
1904 p = (char_u *)"xterm";
1905 }
1906# endif
1907 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001910 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1911 * "xterm2" in check_termcode(). */
1912 reset_option_was_set((char_u *)"ttym");
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (p == NULL
1915# ifdef FEAT_GUI
1916 || gui.in_use
1917# endif
1918 )
1919 check_mouse_termcode(); /* set mouse termcode anyway */
1920 }
1921# endif
1922# else
1923 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1924# endif
1925#endif /* FEAT_MOUSE */
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef USE_TERM_CONSOLE
1928 /* DEFAULT_TERM indicates that it is the machine console. */
1929 if (STRCMP(term, DEFAULT_TERM) != 0)
1930 term_console = FALSE;
1931 else
1932 {
1933 term_console = TRUE;
1934# ifdef AMIGA
1935 win_resize_on(); /* enable window resizing reports */
1936# endif
1937 }
1938#endif
1939
1940#if defined(UNIX) || defined(VMS)
1941 /*
1942 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1943 */
1944 if (vim_is_fastterm(term))
1945 p_tf = TRUE;
1946#endif
1947#ifdef USE_TERM_CONSOLE
1948 /*
1949 * 'ttyfast' is default on consoles
1950 */
1951 if (term_console)
1952 p_tf = TRUE;
1953#endif
1954
1955 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1956
1957 full_screen = TRUE; /* we can use termcap codes from now on */
1958 set_term_defaults(); /* use current values as defaults */
1959#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02001960 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001961 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962#endif
1963
1964 /*
1965 * Initialize the terminal with the appropriate termcap codes.
1966 * Set the mouse and window title if possible.
1967 * Don't do this when starting, need to parse the .vimrc first, because it
1968 * may redefine t_TI etc.
1969 */
1970 if (starting != NO_SCREEN)
1971 {
1972 starttermcap(); /* may change terminal mode */
1973#ifdef FEAT_MOUSE
1974 setmouse(); /* may start using the mouse */
1975#endif
1976#ifdef FEAT_TITLE
1977 maketitle(); /* may display window title */
1978#endif
1979 }
1980
1981 /* display initial screen after ttest() checking. jw. */
1982 if (width <= 0 || height <= 0)
1983 {
1984 /* termcap failed to report size */
1985 /* set defaults, in case ui_get_shellsize() also fails */
1986 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001987#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 height = 25; /* console is often 25 lines */
1989#else
1990 height = 24; /* most terminals are 24 lines */
1991#endif
1992 }
1993 set_shellsize(width, height, FALSE); /* may change Rows */
1994 if (starting != NO_SCREEN)
1995 {
1996 if (scroll_region)
1997 scroll_region_reset(); /* In case Rows changed */
1998 check_map_keycodes(); /* check mappings for terminal codes used */
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002001 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 /*
2004 * Execute the TermChanged autocommands for each buffer that is
2005 * loaded.
2006 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002007 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002008 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 if (curbuf->b_ml.ml_mfp != NULL)
2011 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2012 curbuf);
2013 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002014 if (bufref_valid(&old_curbuf))
2015 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
2019#ifdef FEAT_TERMRESPONSE
2020 may_req_termresponse();
2021#endif
2022
2023 return OK;
2024}
2025
2026#if defined(FEAT_MOUSE) || defined(PROTO)
2027
2028# ifdef FEAT_MOUSE_TTY
2029# define HMT_NORMAL 1
2030# define HMT_NETTERM 2
2031# define HMT_DEC 4
2032# define HMT_JSBTERM 8
2033# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002034# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002035# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002036# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037static int has_mouse_termcode = 0;
2038# endif
2039
Bram Moolenaar864207d2008-06-24 22:14:38 +00002040# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002042set_mouse_termcode(
2043 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2044 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 char_u name[2];
2047
2048 name[0] = n;
2049 name[1] = KE_FILLER;
2050 add_termcode(name, s, FALSE);
2051# ifdef FEAT_MOUSE_TTY
2052# ifdef FEAT_MOUSE_JSB
2053 if (n == KS_JSBTERM_MOUSE)
2054 has_mouse_termcode |= HMT_JSBTERM;
2055 else
2056# endif
2057# ifdef FEAT_MOUSE_NET
2058 if (n == KS_NETTERM_MOUSE)
2059 has_mouse_termcode |= HMT_NETTERM;
2060 else
2061# endif
2062# ifdef FEAT_MOUSE_DEC
2063 if (n == KS_DEC_MOUSE)
2064 has_mouse_termcode |= HMT_DEC;
2065 else
2066# endif
2067# ifdef FEAT_MOUSE_PTERM
2068 if (n == KS_PTERM_MOUSE)
2069 has_mouse_termcode |= HMT_PTERM;
2070 else
2071# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002072# ifdef FEAT_MOUSE_URXVT
2073 if (n == KS_URXVT_MOUSE)
2074 has_mouse_termcode |= HMT_URXVT;
2075 else
2076# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002077# ifdef FEAT_MOUSE_SGR
2078 if (n == KS_SGR_MOUSE)
2079 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002080 else if (n == KS_SGR_MOUSE_RELEASE)
2081 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002082 else
2083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 has_mouse_termcode |= HMT_NORMAL;
2085# endif
2086}
2087# endif
2088
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002089# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002090 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002092del_mouse_termcode(
2093 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095 char_u name[2];
2096
2097 name[0] = n;
2098 name[1] = KE_FILLER;
2099 del_termcode(name);
2100# ifdef FEAT_MOUSE_TTY
2101# ifdef FEAT_MOUSE_JSB
2102 if (n == KS_JSBTERM_MOUSE)
2103 has_mouse_termcode &= ~HMT_JSBTERM;
2104 else
2105# endif
2106# ifdef FEAT_MOUSE_NET
2107 if (n == KS_NETTERM_MOUSE)
2108 has_mouse_termcode &= ~HMT_NETTERM;
2109 else
2110# endif
2111# ifdef FEAT_MOUSE_DEC
2112 if (n == KS_DEC_MOUSE)
2113 has_mouse_termcode &= ~HMT_DEC;
2114 else
2115# endif
2116# ifdef FEAT_MOUSE_PTERM
2117 if (n == KS_PTERM_MOUSE)
2118 has_mouse_termcode &= ~HMT_PTERM;
2119 else
2120# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002121# ifdef FEAT_MOUSE_URXVT
2122 if (n == KS_URXVT_MOUSE)
2123 has_mouse_termcode &= ~HMT_URXVT;
2124 else
2125# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002126# ifdef FEAT_MOUSE_SGR
2127 if (n == KS_SGR_MOUSE)
2128 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002129 else if (n == KS_SGR_MOUSE_RELEASE)
2130 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002131 else
2132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 has_mouse_termcode &= ~HMT_NORMAL;
2134# endif
2135}
2136# endif
2137#endif
2138
2139#ifdef HAVE_TGETENT
2140/*
2141 * Call tgetent()
2142 * Return error message if it fails, NULL if it's OK.
2143 */
2144 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002145tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147 int i;
2148
2149 i = TGETENT(tbuf, term);
2150 if (i < 0 /* -1 is always an error */
2151# ifdef TGETENT_ZERO_ERR
2152 || i == 0 /* sometimes zero is also an error */
2153# endif
2154 )
2155 {
2156 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2157 * tgetent() with the always existing "dumb" entry to avoid a crash or
2158 * hang. */
2159 (void)TGETENT(tbuf, "dumb");
2160
2161 if (i < 0)
2162# ifdef TGETENT_ZERO_ERR
2163 return (char_u *)_("E557: Cannot open termcap file");
2164 if (i == 0)
2165# endif
2166#ifdef TERMINFO
2167 return (char_u *)_("E558: Terminal entry not found in terminfo");
2168#else
2169 return (char_u *)_("E559: Terminal entry not found in termcap");
2170#endif
2171 }
2172 return NULL;
2173}
2174
2175/*
2176 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2177 * Fix that here.
2178 */
2179 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002180vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 char *p;
2183
2184 p = tgetstr(s, (char **)pp);
2185 if (p == (char *)-1)
2186 p = NULL;
2187 return (char_u *)p;
2188}
2189#endif /* HAVE_TGETENT */
2190
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002191#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192/*
2193 * Get Columns and Rows from the termcap. Used after a window signal if the
2194 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2195 * and "li" entries never change. But on some systems this works.
2196 * Errors while getting the entries are ignored.
2197 */
2198 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002199getlinecol(
2200 long *cp, /* pointer to columns */
2201 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 char_u tbuf[TBUFSZ];
2204
2205 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2206 {
2207 if (*cp == 0)
2208 *cp = tgetnum("co");
2209 if (*rp == 0)
2210 *rp = tgetnum("li");
2211 }
2212}
2213#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2214
2215/*
2216 * Get a string entry from the termcap and add it to the list of termcodes.
2217 * Used for <t_xx> special keys.
2218 * Give an error message for failure when not sourcing.
2219 * If force given, replace an existing entry.
2220 * Return FAIL if the entry was not found, OK if the entry was added.
2221 */
2222 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002223add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 char_u *term;
2226 int key;
2227 struct builtin_term *termp;
2228#ifdef HAVE_TGETENT
2229 char_u *string;
2230 int i;
2231 int builtin_first;
2232 char_u tbuf[TBUFSZ];
2233 char_u tstrbuf[TBUFSZ];
2234 char_u *tp = tstrbuf;
2235 char_u *error_msg = NULL;
2236#endif
2237
2238/*
2239 * If the GUI is running or will start in a moment, we only support the keys
2240 * that the GUI can produce.
2241 */
2242#ifdef FEAT_GUI
2243 if (gui.in_use || gui.starting)
2244 return gui_mch_haskey(name);
2245#endif
2246
2247 if (!force && find_termcode(name) != NULL) /* it's already there */
2248 return OK;
2249
2250 term = T_NAME;
2251 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2252 return FAIL;
2253
2254 if (term_is_builtin(term)) /* name starts with "builtin_" */
2255 {
2256 term += 8;
2257#ifdef HAVE_TGETENT
2258 builtin_first = TRUE;
2259#endif
2260 }
2261#ifdef HAVE_TGETENT
2262 else
2263 builtin_first = p_tbi;
2264#endif
2265
2266#ifdef HAVE_TGETENT
2267/*
2268 * We can get the entry from the builtin termcap and from the external one.
2269 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2270 * builtin termcap first.
2271 * If 'ttybuiltin' is off, try external termcap first.
2272 */
2273 for (i = 0; i < 2; ++i)
2274 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002275 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#endif
2277 /*
2278 * Search in builtin termcap
2279 */
2280 {
2281 termp = find_builtin_term(term);
2282 if (termp->bt_string != NULL) /* found it */
2283 {
2284 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002285 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 while (termp->bt_entry != (int)KS_NAME)
2287 {
2288 if ((int)termp->bt_entry == key)
2289 {
2290 add_termcode(name, (char_u *)termp->bt_string,
2291 term_is_8bit(term));
2292 return OK;
2293 }
2294 ++termp;
2295 }
2296 }
2297 }
2298#ifdef HAVE_TGETENT
2299 else
2300 /*
2301 * Search in external termcap
2302 */
2303 {
2304 error_msg = tgetent_error(tbuf, term);
2305 if (error_msg == NULL)
2306 {
2307 string = TGETSTR((char *)name, &tp);
2308 if (string != NULL && *string != NUL)
2309 {
2310 add_termcode(name, string, FALSE);
2311 return OK;
2312 }
2313 }
2314 }
2315 }
2316#endif
2317
2318 if (sourcing_name == NULL)
2319 {
2320#ifdef HAVE_TGETENT
2321 if (error_msg != NULL)
2322 EMSG(error_msg);
2323 else
2324#endif
2325 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2326 }
2327 return FAIL;
2328}
2329
2330 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002331term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2334}
2335
2336/*
2337 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2338 * Assume that the terminal is using 8-bit controls when the name contains
2339 * "8bit", like in "xterm-8bit".
2340 */
2341 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2345}
2346
2347/*
2348 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002349 * <Esc>[ -> CSI <M_C_[>
2350 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * <Esc>O -> <M-C-O>
2352 */
2353 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002354term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 if (*p == ESC)
2357 {
2358 if (p[1] == '[')
2359 return CSI;
2360 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002361 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (p[1] == 'O')
2363 return 0x8f;
2364 }
2365 return 0;
2366}
2367
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002368#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002370term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2373}
2374#endif
2375
2376#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2377
2378 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002379tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 static char_u buf[16];
2382 char_u *p;
2383
2384 p = buf + 15;
2385 *p = '\0';
2386 do
2387 {
2388 --p;
2389 *p = (char_u) (i % 10 + '0');
2390 i /= 10;
2391 }
2392 while (i > 0 && p > buf);
2393 return p;
2394}
2395#endif
2396
2397#ifndef HAVE_TGETENT
2398
2399/*
2400 * minimal tgoto() implementation.
2401 * no padding and we only parse for %i %d and %+char
2402 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002403static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002405 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002406tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 static char buf[30];
2409 char *p, *s, *e;
2410
2411 if (!cm)
2412 return "OOPS";
2413 e = buf + 29;
2414 for (s = buf; s < e && *cm; cm++)
2415 {
2416 if (*cm != '%')
2417 {
2418 *s++ = *cm;
2419 continue;
2420 }
2421 switch (*++cm)
2422 {
2423 case 'd':
2424 p = (char *)tltoa((unsigned long)y);
2425 y = x;
2426 while (*p)
2427 *s++ = *p++;
2428 break;
2429 case 'i':
2430 x++;
2431 y++;
2432 break;
2433 case '+':
2434 *s++ = (char)(*++cm + y);
2435 y = x;
2436 break;
2437 case '%':
2438 *s++ = *cm;
2439 break;
2440 default:
2441 return "OOPS";
2442 }
2443 }
2444 *s = '\0';
2445 return buf;
2446}
2447
2448#endif /* HAVE_TGETENT */
2449
2450/*
2451 * Set the terminal name and initialize the terminal options.
2452 * If "name" is NULL or empty, get the terminal name from the environment.
2453 * If that fails, use the default terminal name.
2454 */
2455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002456termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 char_u *term;
2459
2460 if (name != NULL && *name == NUL)
2461 name = NULL; /* empty name is equal to no name */
2462 term = name;
2463
2464#ifdef __BEOS__
2465 /*
2466 * TERM environment variable is normally set to 'ansi' on the Bebox;
2467 * Since the BeBox doesn't quite support full ANSI yet, we use our
2468 * own custom 'ansi-beos' termcap instead, unless the -T option has
2469 * been given on the command line.
2470 */
2471 if (term == NULL
2472 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2473 term = DEFAULT_TERM;
2474#endif
2475#ifndef MSWIN
2476 if (term == NULL)
2477 term = mch_getenv((char_u *)"TERM");
2478#endif
2479 if (term == NULL || *term == NUL)
2480 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002481 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 /* Set the default terminal name. */
2484 set_string_default("term", term);
2485 set_string_default("ttytype", term);
2486
2487 /*
2488 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2489 */
2490 set_termname(T_NAME != NULL ? T_NAME : term);
2491}
2492
2493/*
2494 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2495 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002496#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2498static char_u out_buf[OUT_SIZE + 1];
2499static int out_pos = 0; /* number of chars in out_buf */
2500
2501/*
2502 * out_flush(): flush the output buffer
2503 */
2504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002505out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507 int len;
2508
2509 if (out_pos != 0)
2510 {
2511 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2512 len = out_pos;
2513 out_pos = 0;
2514 ui_write(out_buf, len);
2515 }
2516}
2517
Bram Moolenaara338adc2018-01-31 20:51:47 +01002518/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002519 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2520 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002521 */
2522 void
2523out_flush_cursor(
2524 int force UNUSED, /* when TRUE, update cursor even when not moved */
2525 int clear_selection UNUSED) /* clear selection under cursor */
2526{
2527 mch_disable_flush();
2528 out_flush();
2529 mch_enable_flush();
2530#ifdef FEAT_GUI
2531 if (gui.in_use)
2532 {
2533 gui_update_cursor(force, clear_selection);
2534 gui_may_flush();
2535 }
2536#endif
2537}
2538
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540#if defined(FEAT_MBYTE) || defined(PROTO)
2541/*
2542 * Sometimes a byte out of a multi-byte character is written with out_char().
2543 * To avoid flushing half of the character, call this function first.
2544 */
2545 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002546out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2549 out_flush();
2550}
2551#endif
2552
2553#ifdef FEAT_GUI
2554/*
2555 * out_trash(): Throw away the contents of the output buffer
2556 */
2557 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002558out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 out_pos = 0;
2561}
2562#endif
2563
2564/*
2565 * out_char(c): put a byte into the output buffer.
2566 * Flush it if it becomes full.
2567 * This should not be used for outputting text on the screen (use functions
2568 * like msg_puts() and screen_putchar() for that).
2569 */
2570 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002571out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaard0573012017-10-28 21:11:06 +02002573#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2575 out_char('\r');
2576#endif
2577
2578 out_buf[out_pos++] = c;
2579
2580 /* For testing we flush each time. */
2581 if (out_pos >= OUT_SIZE || p_wd)
2582 out_flush();
2583}
2584
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002585static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587/*
2588 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2589 */
2590 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002591out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaard0573012017-10-28 21:11:06 +02002593#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2595 out_char_nf('\r');
2596#endif
2597
2598 out_buf[out_pos++] = c;
2599
2600 if (out_pos >= OUT_SIZE)
2601 out_flush();
2602}
2603
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002604#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2605 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
2607 * A never-padding out_str.
2608 * use this whenever you don't want to run the string through tputs.
2609 * tputs above is harmless, but tputs from the termcap library
2610 * is likely to strip off leading digits, that it mistakes for padding
2611 * information, and "%i", "%d", etc.
2612 * This should only be used for writing terminal codes, not for outputting
2613 * normal text (use functions like msg_puts() and screen_putchar() for that).
2614 */
2615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002616out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2619 out_flush();
2620 while (*s)
2621 out_char_nf(*s++);
2622
2623 /* For testing we write one string at a time. */
2624 if (p_wd)
2625 out_flush();
2626}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002630 * A conditional-flushing out_str, mainly for visualbell.
2631 * Handles a delay internally, because termlib may not respect the delay or do
2632 * it at the wrong time.
2633 * Note: Only for terminal strings.
2634 */
2635 void
2636out_str_cf(char_u *s)
2637{
2638 if (s != NULL && *s)
2639 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002640#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002641 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002642#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002643
2644#ifdef FEAT_GUI
2645 /* Don't use tputs() when GUI is used, ncurses crashes. */
2646 if (gui.in_use)
2647 {
2648 out_str_nf(s);
2649 return;
2650 }
2651#endif
2652 if (out_pos > OUT_SIZE - 20)
2653 out_flush();
2654#ifdef HAVE_TGETENT
2655 for (p = s; *s; ++s)
2656 {
2657 /* flush just before delay command */
2658 if (*s == '$' && *(s + 1) == '<')
2659 {
2660 char_u save_c = *s;
2661 int duration = atoi((char *)s + 2);
2662
2663 *s = NUL;
2664 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2665 *s = save_c;
2666 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002667# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002668 /* Only sleep here if we can limit this happening in
2669 * vim_beep(). */
2670 p = vim_strchr(s, '>');
2671 if (p == NULL || duration <= 0)
2672 {
2673 /* can't parse the time, don't sleep here */
2674 p = s;
2675 }
2676 else
2677 {
2678 ++p;
2679 do_sleep(duration);
2680 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002681# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002682 /* Rely on the terminal library to sleep. */
2683 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002684# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002685 break;
2686 }
2687 }
2688 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2689#else
2690 while (*s)
2691 out_char_nf(*s++);
2692#endif
2693
2694 /* For testing we write one string at a time. */
2695 if (p_wd)
2696 out_flush();
2697 }
2698}
2699
2700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 * out_str(s): Put a character string a byte at a time into the output buffer.
2702 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2703 * This should only be used for writing terminal codes, not for outputting
2704 * normal text (use functions like msg_puts() and screen_putchar() for that).
2705 */
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 if (s != NULL && *s)
2710 {
2711#ifdef FEAT_GUI
2712 /* Don't use tputs() when GUI is used, ncurses crashes. */
2713 if (gui.in_use)
2714 {
2715 out_str_nf(s);
2716 return;
2717 }
2718#endif
2719 /* avoid terminal strings being split up */
2720 if (out_pos > OUT_SIZE - 20)
2721 out_flush();
2722#ifdef HAVE_TGETENT
2723 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2724#else
2725 while (*s)
2726 out_char_nf(*s++);
2727#endif
2728
2729 /* For testing we write one string at a time. */
2730 if (p_wd)
2731 out_flush();
2732 }
2733}
2734
2735/*
2736 * cursor positioning using termcap parser. (jw)
2737 */
2738 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002739term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 OUT_STR(tgoto((char *)T_CM, col, row));
2742}
2743
2744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002745term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 OUT_STR(tgoto((char *)T_CRI, 0, i));
2748}
2749
2750 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002751term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2754}
2755
2756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002757term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2760}
2761
2762#if defined(HAVE_TGETENT) || defined(PROTO)
2763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002764term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 /* Can't handle a negative value here */
2767 if (x < 0)
2768 x = 0;
2769 if (y < 0)
2770 y = 0;
2771 OUT_STR(tgoto((char *)T_CWP, y, x));
2772}
2773
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002774# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2775/*
2776 * Return TRUE if we can request the terminal for a response.
2777 */
2778 static int
2779can_get_termresponse()
2780{
2781 return cur_tmode == TMODE_RAW
2782 && termcap_active
2783# ifdef UNIX
2784 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2785# endif
2786 && p_ek;
2787}
2788
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002789static int winpos_x = -1;
2790static int winpos_y = -1;
2791static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002792
2793/*
2794 * Try getting the Vim window position from the terminal.
2795 * Returns OK or FAIL.
2796 */
2797 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002798term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002799{
2800 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002801 int prev_winpos_x = winpos_x;
2802 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002803
2804 if (*T_CGP == NUL || !can_get_termresponse())
2805 return FAIL;
2806 winpos_x = -1;
2807 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002808 ++did_request_winpos;
2809 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002810 OUT_STR(T_CGP);
2811 out_flush();
2812
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002813 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002814 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002815 {
2816 (void)vpeekc_nomap();
2817 if (winpos_x >= 0 && winpos_y >= 0)
2818 {
2819 *x = winpos_x;
2820 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002821 return OK;
2822 }
2823 ui_delay(10, FALSE);
2824 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002825 /* Do not reset "did_request_winpos", if we timed out the response might
2826 * still come later and we must consume it. */
2827
2828 winpos_x = prev_winpos_x;
2829 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002830 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002831 {
2832 /* Polling: return previous values if we have them. */
2833 *x = winpos_x;
2834 *y = winpos_y;
2835 return OK;
2836 }
2837
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838 return FALSE;
2839}
2840# endif
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002843term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002845 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846}
2847#endif
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002850term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002853 int i = *s == CSI ? 1 : 2;
2854 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 /* Special handling of 16 colors, because termcap can't handle it */
2857 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2858 /* Also accept CSI instead of <Esc>[ */
2859 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002860 && ((s[0] == ESC && s[1] == '[')
2861#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2862 || (s[0] == ESC && s[1] == '|')
2863#endif
2864 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 && s[i] != NUL
2866 && (STRCMP(s + i + 1, "%p1%dm") == 0
2867 || STRCMP(s + i + 1, "%dm") == 0)
2868 && (s[i] == '3' || s[i] == '4'))
2869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002871 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002873 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002875 sprintf(buf, format,
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002876 i == 2 ?
2877#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2878 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
2879#endif
2880 IF_EB("\033[", ESC_STR "[") : "\233",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2882 : (n >= 16 ? "48;5;" : "10"));
2883 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2884 }
2885 else
2886 OUT_STR(tgoto((char *)s, 0, n));
2887}
2888
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002889 void
2890term_fg_color(int n)
2891{
2892 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2893 if (*T_CAF)
2894 term_color(T_CAF, n);
2895 else if (*T_CSF)
2896 term_color(T_CSF, n);
2897}
2898
2899 void
2900term_bg_color(int n)
2901{
2902 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2903 if (*T_CAB)
2904 term_color(T_CAB, n);
2905 else if (*T_CSB)
2906 term_color(T_CSB, n);
2907}
2908
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002909#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002910
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002911#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2912#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2913#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002914
2915 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002916term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002917{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002918#define MAX_COLOR_STR_LEN 100
2919 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002920
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002921 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002922 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002923 OUT_STR(buf);
2924}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002925
2926 void
2927term_fg_rgb_color(guicolor_T rgb)
2928{
2929 term_rgb_color(T_8F, rgb);
2930}
2931
2932 void
2933term_bg_rgb_color(guicolor_T rgb)
2934{
2935 term_rgb_color(T_8B, rgb);
2936}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002937#endif
2938
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002939#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2940 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941/*
2942 * Generic function to set window title, using t_ts and t_fs.
2943 */
2944 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002945term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 /* t_ts takes one argument: column in status line */
2948 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2949 out_str_nf(title);
2950 out_str(T_FS); /* set title end */
2951 out_flush();
2952}
2953#endif
2954
2955/*
2956 * Make sure we have a valid set or terminal options.
2957 * Replace all entries that are NULL by empty_option
2958 */
2959 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002960ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002962 char_u *env_colors;
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 check_options(); /* make sure no options are NULL */
2965
2966 /*
2967 * MUST have "cm": cursor motion.
2968 */
2969 if (*T_CM == NUL)
2970 EMSG(_("E437: terminal capability \"cm\" required"));
2971
2972 /*
2973 * if "cs" defined, use a scroll region, it's faster.
2974 */
2975 if (*T_CS != NUL)
2976 scroll_region = TRUE;
2977 else
2978 scroll_region = FALSE;
2979
2980 if (pairs)
2981 {
2982 /*
2983 * optional pairs
2984 */
2985 /* TP goes to normal mode for TI (invert) and TB (bold) */
2986 if (*T_ME == NUL)
2987 T_ME = T_MR = T_MD = T_MB = empty_option;
2988 if (*T_SO == NUL || *T_SE == NUL)
2989 T_SO = T_SE = empty_option;
2990 if (*T_US == NUL || *T_UE == NUL)
2991 T_US = T_UE = empty_option;
2992 if (*T_CZH == NUL || *T_CZR == NUL)
2993 T_CZH = T_CZR = empty_option;
2994
2995 /* T_VE is needed even though T_VI is not defined */
2996 if (*T_VE == NUL)
2997 T_VI = empty_option;
2998
2999 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3000 if (*T_ME == NUL)
3001 {
3002 T_ME = T_SE;
3003 T_MR = T_SO;
3004 T_MD = T_SO;
3005 }
3006
3007 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3008 if (*T_SO == NUL)
3009 {
3010 T_SE = T_ME;
3011 if (*T_MR == NUL)
3012 T_SO = T_MD;
3013 else
3014 T_SO = T_MR;
3015 }
3016
3017 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3018 if (*T_CZH == NUL)
3019 {
3020 T_CZR = T_ME;
3021 if (*T_MR == NUL)
3022 T_CZH = T_MD;
3023 else
3024 T_CZH = T_MR;
3025 }
3026
3027 /* "Sb" and "Sf" come in pairs */
3028 if (*T_CSB == NUL || *T_CSF == NUL)
3029 {
3030 T_CSB = empty_option;
3031 T_CSF = empty_option;
3032 }
3033
3034 /* "AB" and "AF" come in pairs */
3035 if (*T_CAB == NUL || *T_CAF == NUL)
3036 {
3037 T_CAB = empty_option;
3038 T_CAF = empty_option;
3039 }
3040
3041 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3042 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003043 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 /* Set 'weirdinvert' according to value of 't_xs' */
3046 p_wiv = (*T_XS != NUL);
3047 }
3048 need_gather = TRUE;
3049
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003050 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003052 env_colors = mch_getenv((char_u *)"COLORS");
3053 if (env_colors != NULL && isdigit(*env_colors))
3054 {
3055 int colors = atoi((char *)env_colors);
3056
3057 if (colors != t_colors)
3058 set_color_count(colors);
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060}
3061
3062#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3063 || defined(PROTO)
3064/*
3065 * Represent the given long_u as individual bytes, with the most significant
3066 * byte first, and store them in dst.
3067 */
3068 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003069add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 int i;
3072 int shift;
3073
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003074 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 shift = 8 * (sizeof(long_u) - i);
3077 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3078 }
3079}
3080
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003081static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083/*
3084 * Interpret the next string of bytes in buf as a long integer, with the most
3085 * significant byte first. Note that it is assumed that buf has been through
3086 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3087 * Puts result in val, and returns the number of bytes read from buf
3088 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3089 * were present.
3090 */
3091 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003092get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 int len;
3095 char_u bytes[sizeof(long_u)];
3096 int i;
3097 int shift;
3098
3099 *val = 0;
3100 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3101 if (len != -1)
3102 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003103 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 shift = 8 * (sizeof(long_u) - 1 - i);
3106 *val += (long_u)bytes[i] << shift;
3107 }
3108 }
3109 return len;
3110}
3111#endif
3112
3113#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003114 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3115 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116/*
3117 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3118 * that buf has been through inchar(). Returns the actual number of bytes used
3119 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3120 * available.
3121 */
3122 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003123get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 int len = 0;
3126 int i;
3127 char_u c;
3128
3129 for (i = 0; i < num_bytes; i++)
3130 {
3131 if ((c = buf[len++]) == NUL)
3132 return -1;
3133 if (c == K_SPECIAL)
3134 {
3135 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3136 return -1;
3137 if (buf[len++] == (int)KS_ZERO)
3138 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003139 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3140 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3141 if (buf[len++] == (int)KE_CSI)
3142 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003144 else if (c == CSI && buf[len] == KS_EXTRA
3145 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003146 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3147 * the start of a special key, see add_to_input_buf_csi(). */
3148 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 bytes[i] = c;
3150 }
3151 return len;
3152}
3153#endif
3154
3155/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003156 * Check if the new shell size is valid, correct it if it's too small or way
3157 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 */
3159 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003160check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (Rows < min_rows()) /* need room for one window and command line */
3163 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003164 limit_screen_size();
3165}
3166
3167/*
3168 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3169 */
3170 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003171limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003172{
3173 if (Columns < MIN_COLUMNS)
3174 Columns = MIN_COLUMNS;
3175 else if (Columns > 10000)
3176 Columns = 10000;
3177 if (Rows > 1000)
3178 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
Bram Moolenaar86b68352004-12-27 21:59:20 +00003181/*
3182 * Invoked just before the screen structures are going to be (re)allocated.
3183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003185win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 static int old_Rows = 0;
3188 static int old_Columns = 0;
3189
3190 if (old_Rows != Rows || old_Columns != Columns)
3191 ui_new_shellsize();
3192 if (old_Rows != Rows)
3193 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003194 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003195 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003196 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 old_Rows = Rows;
3198 shell_new_rows(); /* update window sizes */
3199 }
3200 if (old_Columns != Columns)
3201 {
3202 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205}
3206
3207/*
3208 * Call this function when the Vim shell has been resized in any way.
3209 * Will obtain the current size and redraw (also when size didn't change).
3210 */
3211 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003212shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 set_shellsize(0, 0, FALSE);
3215}
3216
3217/*
3218 * Check if the shell size changed. Handle a resize.
3219 * When the size didn't change, nothing happens.
3220 */
3221 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003222shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 int old_Rows = Rows;
3225 int old_Columns = Columns;
3226
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003227 if (!exiting
3228#ifdef FEAT_GUI
3229 /* Do not get the size when executing a shell command during
3230 * startup. */
3231 && !gui.starting
3232#endif
3233 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003234 {
3235 (void)ui_get_shellsize();
3236 check_shellsize();
3237 if (old_Rows != Rows || old_Columns != Columns)
3238 shell_resized();
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240}
3241
3242/*
3243 * Set size of the Vim shell.
3244 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3245 * window size (this is used for the :win command).
3246 * If 'mustset' is FALSE, we may try to get the real window size and if
3247 * it fails use 'width' and 'height'.
3248 */
3249 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003250set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 static int busy = FALSE;
3253
3254 /*
3255 * Avoid recursiveness, can happen when setting the window size causes
3256 * another window-changed signal.
3257 */
3258 if (busy)
3259 return;
3260
3261 if (width < 0 || height < 0) /* just checking... */
3262 return;
3263
Bram Moolenaara971b822011-09-14 14:43:25 +02003264 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003266 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 State = SETWSIZE;
3268 return;
3269 }
3270
Bram Moolenaara971b822011-09-14 14:43:25 +02003271 /* curwin->w_buffer can be NULL when we are closing a window and the
3272 * buffer has already been closed and removing a scrollbar causes a resize
3273 * event. Don't resize then, it will happen after entering another buffer.
3274 */
3275 if (curwin->w_buffer == NULL)
3276 return;
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 ++busy;
3279
3280#ifdef AMIGA
3281 out_flush(); /* must do this before mch_get_shellsize() for
3282 some obscure reason */
3283#endif
3284
3285 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3286 {
3287 Rows = height;
3288 Columns = width;
3289 check_shellsize();
3290 ui_set_shellsize(mustset);
3291 }
3292 else
3293 check_shellsize();
3294
3295 /* The window layout used to be adjusted here, but it now happens in
3296 * screenalloc() (also invoked from screenclear()). That is because the
3297 * "busy" check above may skip this, but not screenalloc(). */
3298
3299 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3300 screenclear();
3301 else
3302 screen_start(); /* don't know where cursor is now */
3303
3304 if (starting != NO_SCREEN)
3305 {
3306#ifdef FEAT_TITLE
3307 maketitle();
3308#endif
3309 changed_line_abv_curs();
3310 invalidate_botline();
3311
3312 /*
3313 * We only redraw when it's needed:
3314 * - While at the more prompt or executing an external command, don't
3315 * redraw, but position the cursor.
3316 * - While editing the command line, only redraw that.
3317 * - in Ex mode, don't redraw anything.
3318 * - Otherwise, redraw right now, and position the cursor.
3319 * Always need to call update_screen() or screenalloc(), to make
3320 * sure Rows/Columns and the size of ScreenLines[] is correct!
3321 */
3322 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3323 || exmode_active)
3324 {
3325 screenalloc(FALSE);
3326 repeat_message();
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
3329 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003330 if (curwin->w_p_scb)
3331 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003332 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003333 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003334 update_screen(NOT_VALID);
3335 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003336 }
3337 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003338 {
3339 update_topline();
3340#if defined(FEAT_INS_EXPAND)
3341 if (pum_visible())
3342 {
3343 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003344 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003345 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003346#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003347 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003348 if (redrawing())
3349 setcursor();
3350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352 cursor_on(); /* redrawing may have switched it off */
3353 }
3354 out_flush();
3355 --busy;
3356}
3357
3358/*
3359 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3360 * commands and Ex mode).
3361 */
3362 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003363settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365#ifdef FEAT_GUI
3366 /* don't set the term where gvim was started to any mode */
3367 if (gui.in_use)
3368 return;
3369#endif
3370
3371 if (full_screen)
3372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 /*
3374 * When returning after calling a shell we want to really set the
3375 * terminal to raw mode, even though we think it already is, because
3376 * the shell program may have reset the terminal mode.
3377 * When we think the terminal is normal, don't try to set it to
3378 * normal again, because that causes problems (logout!) on some
3379 * machines.
3380 */
3381 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3382 {
3383#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003384# ifdef FEAT_GUI
3385 if (!gui.in_use && !gui.starting)
3386# endif
3387 {
3388 /* May need to check for T_CRV response and termcodes, it
3389 * doesn't work in Cooked mode, an external program may get
3390 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003391 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3392 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003393#ifdef FEAT_TERMINAL
3394 || rfg_status == STATUS_SENT
3395#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003396 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003397 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003398 || rcs_status == STATUS_SENT
3399 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003400 (void)vpeekc_nomap();
3401 check_for_codes_from_term();
3402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#endif
3404#ifdef FEAT_MOUSE_TTY
3405 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003406 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003408 if (tmode != TMODE_RAW)
3409 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003411 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 cur_tmode = tmode;
3413#ifdef FEAT_MOUSE
3414 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003415 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003417 if (tmode == TMODE_RAW)
3418 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 out_flush();
3420 }
3421#ifdef FEAT_TERMRESPONSE
3422 may_req_termresponse();
3423#endif
3424 }
3425}
3426
3427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003428starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 if (full_screen && !termcap_active)
3431 {
3432 out_str(T_TI); /* start termcap mode */
3433 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003434 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 out_flush();
3436 termcap_active = TRUE;
3437 screen_start(); /* don't know where cursor is now */
3438#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003439# ifdef FEAT_GUI
3440 if (!gui.in_use && !gui.starting)
3441# endif
3442 {
3443 may_req_termresponse();
3444 /* Immediately check for a response. If t_Co changes, we don't
3445 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003446 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003447 check_for_codes_from_term();
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#endif
3450 }
3451}
3452
3453 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003454stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 screen_stop_highlight();
3457 reset_cterm_colors();
3458 if (termcap_active)
3459 {
3460#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003461# ifdef FEAT_GUI
3462 if (!gui.in_use && !gui.starting)
3463# endif
3464 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003465 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003466 if (crv_status == STATUS_SENT
3467 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003468# ifdef FEAT_TERMINAL
3469 || rfg_status == STATUS_SENT
3470# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003471 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003472 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003473 || rcs_status == STATUS_SENT
3474 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003475 {
3476# ifdef UNIX
3477 /* Give the terminal a chance to respond. */
3478 mch_delay(100L, FALSE);
3479# endif
3480# ifdef TCIFLUSH
3481 /* Discard data received but not read. */
3482 if (exiting)
3483 tcflush(fileno(stdin), TCIFLUSH);
3484# endif
3485 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003486 /* Check for termcodes first, otherwise an external program may
3487 * get them. */
3488 check_for_codes_from_term();
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003491 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 out_str(T_KE); /* stop "keypad transmit" mode */
3493 out_flush();
3494 termcap_active = FALSE;
3495 cursor_on(); /* just in case it is still off */
3496 out_str(T_TE); /* stop termcap mode */
3497 screen_start(); /* don't know where cursor is now */
3498 out_flush();
3499 }
3500}
3501
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003502#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503/*
3504 * Request version string (for xterm) when needed.
3505 * Only do this after switching to raw mode, otherwise the result will be
3506 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003507 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003508 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 * Only do this after termcap mode has been started, otherwise the codes for
3510 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003511 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3512 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003513 * On Unix only do it when both output and input are a tty (avoid writing
3514 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 * The result is caught in check_termcode().
3516 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003518may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003520 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003521 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003522 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 && *T_CRV != NUL)
3524 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003525 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003527 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /* check for the characters now, otherwise they might be eaten by
3529 * get_keystroke() */
3530 out_flush();
3531 (void)vpeekc_nomap();
3532 }
3533}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003534
3535# if defined(FEAT_MBYTE) || defined(PROTO)
3536/*
3537 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003538 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003539 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003540 * If the terminal treats \u25bd as single width, the position is (1, 1),
3541 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003542 * This function has the side effect that changes cursor position, so
3543 * it must be called immediately after entering termcap mode.
3544 */
3545 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003546may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003547{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003548 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003549 && can_get_termresponse()
3550 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003551 && *T_U7 != NUL
3552 && !option_was_set((char_u *)"ambiwidth"))
3553 {
3554 char_u buf[16];
3555
Bram Moolenaarb255b902018-04-24 21:40:10 +02003556 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003557 /* Do this in the second row. In the first row the returned sequence
3558 * may be CSI 1;2R, which is the same as <S-F3>. */
3559 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003560 buf[mb_char2bytes(0x25bd, buf)] = 0;
3561 out_str(buf);
3562 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003563 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003564 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003565
3566 /* This overwrites a few characters on the screen, a redraw is needed
3567 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003568 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003569 out_str((char_u *)" ");
3570 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003571
Bram Moolenaar05684312017-12-09 15:11:24 +01003572 /* Need to reset the known cursor position. */
3573 screen_start();
3574
Bram Moolenaar9584b312013-03-13 19:29:28 +01003575 /* check for the characters now, otherwise they might be eaten by
3576 * get_keystroke() */
3577 out_flush();
3578 (void)vpeekc_nomap();
3579 }
3580}
3581# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003582
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003583/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003584 * Similar to requesting the version string: Request the terminal background
3585 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003586 */
3587 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003588may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003589{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003590 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003591 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003592 int didit = FALSE;
3593
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003594# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003595 /* Only request foreground if t_RF is set. */
3596 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3597 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003598 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003599 out_str(T_RFG);
3600 rfg_status = STATUS_SENT;
3601 didit = TRUE;
3602 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003603# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003604
3605 /* Only request background if t_RB is set. */
3606 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003607 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003608 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003609 out_str(T_RBG);
3610 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003611 didit = TRUE;
3612 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003613
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003614 if (didit)
3615 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003616 /* check for the characters now, otherwise they might be eaten by
3617 * get_keystroke() */
3618 out_flush();
3619 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003620 }
3621 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003622}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003623
Bram Moolenaar2951b772013-07-03 12:45:31 +02003624# ifdef DEBUG_TERMRESPONSE
3625 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003626log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003627{
3628 static FILE *fd_tr = NULL;
3629 static proftime_T start;
3630 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003631 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003632
3633 if (fd_tr == NULL)
3634 {
3635 fd_tr = fopen("termresponse.log", "w");
3636 profile_start(&start);
3637 }
3638 now = start;
3639 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003640 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3641 must_redraw == NOT_VALID ? "NV"
3642 : must_redraw == CLEAR ? "CL" : " ");
3643 va_start(ap, fmt);
3644 vfprintf(fd_tr, fmt, ap);
3645 va_end(ap);
3646 fputc('\n', fd_tr);
3647 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003648}
3649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#endif
3651
3652/*
3653 * Return TRUE when saving and restoring the screen.
3654 */
3655 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003656swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 return (full_screen && *T_TI != NUL);
3659}
3660
Bram Moolenaarecadf432018-03-20 11:17:04 +01003661#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3664 */
3665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003666setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668# ifdef FEAT_MOUSE_TTY
3669 int checkfor;
3670# endif
3671
3672# ifdef FEAT_MOUSESHAPE
3673 update_mouseshape(-1);
3674# endif
3675
3676# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3677# ifdef FEAT_GUI
3678 /* In the GUI the mouse is always enabled. */
3679 if (gui.in_use)
3680 return;
3681# endif
3682 /* be quick when mouse is off */
3683 if (*p_mouse == NUL || has_mouse_termcode == 0)
3684 return;
3685
3686 /* don't switch mouse on when not in raw mode (Ex mode) */
3687 if (cur_tmode != TMODE_RAW)
3688 {
3689 mch_setmouse(FALSE);
3690 return;
3691 }
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (VIsual_active)
3694 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003695 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 checkfor = MOUSE_RETURN;
3697 else if (State & INSERT)
3698 checkfor = MOUSE_INSERT;
3699 else if (State & CMDLINE)
3700 checkfor = MOUSE_COMMAND;
3701 else if (State == CONFIRM || State == EXTERNCMD)
3702 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3703 else
3704 checkfor = MOUSE_NORMAL; /* assume normal mode */
3705
3706 if (mouse_has(checkfor))
3707 mch_setmouse(TRUE);
3708 else
3709 mch_setmouse(FALSE);
3710# endif
3711}
3712
3713/*
3714 * Return TRUE if
3715 * - "c" is in 'mouse', or
3716 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3717 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3718 * normal editing mode (not at hit-return message).
3719 */
3720 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003721mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 char_u *p;
3724
3725 for (p = p_mouse; *p; ++p)
3726 switch (*p)
3727 {
3728 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3729 return TRUE;
3730 break;
3731 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3732 return TRUE;
3733 break;
3734 default: if (c == *p) return TRUE; break;
3735 }
3736 return FALSE;
3737}
3738
3739/*
3740 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3741 */
3742 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003743mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 return (p_mousem[0] == 'p');
3746}
3747#endif
3748
3749/*
3750 * By outputting the 'cursor very visible' termcap code, for some windowed
3751 * terminals this makes the screen scrolled to the correct position.
3752 * Used when starting Vim or returning from a shell.
3753 */
3754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003755scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003757 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003760 out_str(T_CVS);
3761 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763}
3764
3765static int cursor_is_off = FALSE;
3766
3767/*
3768 * Enable the cursor.
3769 */
3770 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003771cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 if (cursor_is_off)
3774 {
3775 out_str(T_VE);
3776 cursor_is_off = FALSE;
3777 }
3778}
3779
3780/*
3781 * Disable the cursor.
3782 */
3783 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003784cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003786 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003788 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 cursor_is_off = TRUE;
3790 }
3791}
3792
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003793#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003795 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003796 */
3797 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003798term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003799{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003800 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003801 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003802
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003803 /* Only do something when redrawing the screen and we can restore the
3804 * mode. */
3805 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003806 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003807# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003808 if (forced && initial_cursor_shape > 0)
3809 /* Restore to initial values. */
3810 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003811# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003812 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003813 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003814
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003815 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003816 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003817 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003818 {
3819 if (*T_CSR != NUL)
3820 p = T_CSR; /* Replace mode cursor */
3821 else
3822 p = T_CSI; /* fall back to Insert mode cursor */
3823 if (*p != NUL)
3824 {
3825 out_str(p);
3826 showing_mode = REPLACE;
3827 }
3828 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003829 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003830 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003831 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003832 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003833 {
3834 out_str(T_CSI); /* Insert mode cursor */
3835 showing_mode = INSERT;
3836 }
3837 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003838 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003839 {
3840 out_str(T_CEI); /* non-Insert mode cursor */
3841 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003842 }
3843}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003844
3845# if defined(FEAT_TERMINAL) || defined(PROTO)
3846 void
3847term_cursor_color(char_u *color)
3848{
3849 if (*T_CSC != NUL)
3850 {
3851 out_str(T_CSC); /* set cursor color start */
3852 out_str_nf(color);
3853 out_str(T_CEC); /* set cursor color end */
3854 out_flush();
3855 }
3856}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003857# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003858
Bram Moolenaar4db25542017-08-28 22:43:05 +02003859 int
3860blink_state_is_inverted()
3861{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003862#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003863 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3864 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003865#else
3866 return FALSE;
3867#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003868}
3869
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003870/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003871 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003872 */
3873 void
3874term_cursor_shape(int shape, int blink)
3875{
3876 if (*T_CSH != NUL)
3877 {
3878 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3879 out_flush();
3880 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003881 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003882 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003883 int do_blink = blink;
3884
3885 /* t_SH is empty: try setting just the blink state.
3886 * The blink flags are XORed together, if the initial blinking from
3887 * style and shape differs, we need to invert the flag here. */
3888 if (blink_state_is_inverted())
3889 do_blink = !blink;
3890
3891 if (do_blink && *T_VS != NUL)
3892 {
3893 out_str(T_VS);
3894 out_flush();
3895 }
3896 else if (!do_blink && *T_CVS != NUL)
3897 {
3898 out_str(T_CVS);
3899 out_flush();
3900 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003901 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003902}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003903#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003904
3905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 * Set scrolling region for window 'wp'.
3907 * The region starts 'off' lines from the start of the window.
3908 * Also set the vertical scroll region for a vertically split window. Always
3909 * the full width of the window, excluding the vertical separator.
3910 */
3911 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003912scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3915 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003917 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3918 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 screen_start(); /* don't know where cursor is now */
3920}
3921
3922/*
3923 * Reset scrolling region to the whole screen.
3924 */
3925 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003926scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
3928 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (*T_CSV != NUL)
3930 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 screen_start(); /* don't know where cursor is now */
3932}
3933
3934
3935/*
3936 * List of terminal codes that are currently recognized.
3937 */
3938
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003939static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 char_u name[2]; /* termcap name of entry */
3942 char_u *code; /* terminal code (in allocated memory) */
3943 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003944 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945} *termcodes = NULL;
3946
3947static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3948static int tc_len = 0; /* current number of entries in termcodes[] */
3949
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003950static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003953clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 while (tc_len > 0)
3956 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003957 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 tc_max_len = 0;
3959
3960#ifdef HAVE_TGETENT
3961 BC = (char *)empty_option;
3962 UP = (char *)empty_option;
3963 PC = NUL; /* set pad character to NUL */
3964 ospeed = 0;
3965#endif
3966
3967 need_gather = TRUE; /* need to fill termleader[] */
3968}
3969
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003970#define ATC_FROM_TERM 55
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972/*
3973 * Add a new entry to the list of terminal codes.
3974 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3976 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
3978 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003979add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 struct termcode *new_tc;
3982 int i, j;
3983 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003984 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 if (string == NULL || *string == NUL)
3987 {
3988 del_termcode(name);
3989 return;
3990 }
3991
Bram Moolenaar45500912014-07-09 20:51:07 +02003992#if defined(WIN3264) && !defined(FEAT_GUI)
3993 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3994#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (s == NULL)
3998 return;
3999
4000 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004003 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 s[0] = term_7to8bit(string);
4005 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004006
4007#if defined(WIN3264) && !defined(FEAT_GUI)
4008 if (s[0] == K_NUL)
4009 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004010 STRMOVE(s + 1, s);
4011 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004012 }
4013#endif
4014
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004015 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 need_gather = TRUE; /* need to fill termleader[] */
4018
4019 /*
4020 * need to make space for more entries
4021 */
4022 if (tc_len == tc_max_len)
4023 {
4024 tc_max_len += 20;
4025 new_tc = (struct termcode *)alloc(
4026 (unsigned)(tc_max_len * sizeof(struct termcode)));
4027 if (new_tc == NULL)
4028 {
4029 tc_max_len -= 20;
4030 return;
4031 }
4032 for (i = 0; i < tc_len; ++i)
4033 new_tc[i] = termcodes[i];
4034 vim_free(termcodes);
4035 termcodes = new_tc;
4036 }
4037
4038 /*
4039 * Look for existing entry with the same name, it is replaced.
4040 * Look for an existing entry that is alphabetical higher, the new entry
4041 * is inserted in front of it.
4042 */
4043 for (i = 0; i < tc_len; ++i)
4044 {
4045 if (termcodes[i].name[0] < name[0])
4046 continue;
4047 if (termcodes[i].name[0] == name[0])
4048 {
4049 if (termcodes[i].name[1] < name[1])
4050 continue;
4051 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004052 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054 if (termcodes[i].name[1] == name[1])
4055 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056 if (flags == ATC_FROM_TERM && (j = termcode_star(
4057 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004058 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004059 /* Don't replace ESC[123;*X or ESC O*X with another when
4060 * invoked from got_code_from_term(). */
4061 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004062 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 && s[len - 1]
4064 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004066 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004067 vim_free(s);
4068 return;
4069 }
4070 }
4071 else
4072 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004073 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004074 vim_free(termcodes[i].code);
4075 --tc_len;
4076 break;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 }
4080 /*
4081 * Found alphabetical larger entry, move rest to insert new entry
4082 */
4083 for (j = tc_len; j > i; --j)
4084 termcodes[j] = termcodes[j - 1];
4085 break;
4086 }
4087
4088 termcodes[i].name[0] = name[0];
4089 termcodes[i].name[1] = name[1];
4090 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092
4093 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4094 * accept modifiers. */
4095 termcodes[i].modlen = 0;
4096 j = termcode_star(s, len);
4097 if (j > 0)
4098 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 ++tc_len;
4100}
4101
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004102/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004103 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004104 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004105 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 */
4107 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004108termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004109{
4110 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4111 if (len >= 3 && code[len - 2] == '*')
4112 {
4113 if (len >= 5 && code[len - 3] == ';')
4114 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004115 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004116 return 1;
4117 }
4118 return 0;
4119}
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004122find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 int i;
4125
4126 for (i = 0; i < tc_len; ++i)
4127 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4128 return termcodes[i].code;
4129 return NULL;
4130}
4131
4132#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4133 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004134get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 if (i >= tc_len)
4137 return NULL;
4138 return &termcodes[i].name[0];
4139}
4140#endif
4141
4142 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004143del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 int i;
4146
4147 if (termcodes == NULL) /* nothing there yet */
4148 return;
4149
4150 need_gather = TRUE; /* need to fill termleader[] */
4151
4152 for (i = 0; i < tc_len; ++i)
4153 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4154 {
4155 del_termcode_idx(i);
4156 return;
4157 }
4158 /* not found. Give error message? */
4159}
4160
4161 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004162del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 int i;
4165
4166 vim_free(termcodes[idx].code);
4167 --tc_len;
4168 for (i = idx; i < tc_len; ++i)
4169 termcodes[i] = termcodes[i + 1];
4170}
4171
4172#ifdef FEAT_TERMRESPONSE
4173/*
4174 * Called when detected that the terminal sends 8-bit codes.
4175 * Convert all 7-bit codes to their 8-bit equivalent.
4176 */
4177 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004178switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 int i;
4181 int c;
4182
4183 /* Only need to do something when not already using 8-bit codes. */
4184 if (!term_is_8bit(T_NAME))
4185 {
4186 for (i = 0; i < tc_len; ++i)
4187 {
4188 c = term_7to8bit(termcodes[i].code);
4189 if (c != 0)
4190 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004191 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 termcodes[i].code[0] = c;
4193 }
4194 }
4195 need_gather = TRUE; /* need to fill termleader[] */
4196 }
4197 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004198 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199}
4200#endif
4201
4202#ifdef CHECK_DOUBLE_CLICK
4203static linenr_T orig_topline = 0;
4204# ifdef FEAT_DIFF
4205static int orig_topfill = 0;
4206# endif
4207#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004208#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209/*
4210 * Checking for double clicks ourselves.
4211 * "orig_topline" is used to avoid detecting a double-click when the window
4212 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4213 */
4214/*
4215 * Set orig_topline. Used when jumping to another window, so that a double
4216 * click still works.
4217 */
4218 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004219set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 orig_topline = wp->w_topline;
4222# ifdef FEAT_DIFF
4223 orig_topfill = wp->w_topfill;
4224# endif
4225}
4226#endif
4227
4228/*
4229 * Check if typebuf.tb_buf[] contains a terminal key code.
4230 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4231 * + max_offset].
4232 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004233 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 * With a match, the match is removed, the replacement code is inserted in
4235 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4236 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004237 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4238 * "buflen" is then the length of the string in buf[] and is updated for
4239 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 */
4241 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004242check_termcode(
4243 int max_offset,
4244 char_u *buf,
4245 int bufsize,
4246 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 char_u *tp;
4249 char_u *p;
4250 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004251 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004253 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 int offset;
4255 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004257 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004258 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int new_slen;
4260 int extra;
4261 char_u string[MAX_KEY_CODE_LEN + 1];
4262 int i, j;
4263 int idx = 0;
4264#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004265# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4266 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 char_u bytes[6];
4268 int num_bytes;
4269# endif
4270 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int is_click, is_drag;
4272 int wheel_code = 0;
4273 int current_button;
4274 static int held_button = MOUSE_RELEASE;
4275 static int orig_num_clicks = 1;
4276 static int orig_mouse_code = 0x0;
4277# ifdef CHECK_DOUBLE_CLICK
4278 static int orig_mouse_col = 0;
4279 static int orig_mouse_row = 0;
4280 static struct timeval orig_mouse_time = {0, 0};
4281 /* time of previous mouse click */
4282 struct timeval mouse_time; /* time of current mouse click */
4283 long timediff; /* elapsed time in msec */
4284# endif
4285#endif
4286 int cpo_koffset;
4287#ifdef FEAT_MOUSE_GPM
4288 extern int gpm_flag; /* gpm library variable */
4289#endif
4290
4291 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4292
4293 /*
4294 * Speed up the checks for terminal codes by gathering all first bytes
4295 * used in termleader[]. Often this is just a single <Esc>.
4296 */
4297 if (need_gather)
4298 gather_termleader();
4299
4300 /*
4301 * Check at several positions in typebuf.tb_buf[], to catch something like
4302 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4303 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004304 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * This is used often, KEEP IT FAST!
4306 */
4307 for (offset = 0; offset < max_offset; ++offset)
4308 {
4309 if (buf == NULL)
4310 {
4311 if (offset >= typebuf.tb_len)
4312 break;
4313 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4314 len = typebuf.tb_len - offset; /* length of the input */
4315 }
4316 else
4317 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004318 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 break;
4320 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004321 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323
4324 /*
4325 * Don't check characters after K_SPECIAL, those are already
4326 * translated terminal chars (avoid translating ~@^Hx).
4327 */
4328 if (*tp == K_SPECIAL)
4329 {
4330 offset += 2; /* there are always 2 extra characters */
4331 continue;
4332 }
4333
4334 /*
4335 * Skip this position if the character does not appear as the first
4336 * character in term_strings. This speeds up a lot, since most
4337 * termcodes start with the same character (ESC or CSI).
4338 */
4339 i = *tp;
4340 for (p = termleader; *p && *p != i; ++p)
4341 ;
4342 if (*p == NUL)
4343 continue;
4344
4345 /*
4346 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4347 * are in Insert mode.
4348 */
4349 if (*tp == ESC && !p_ek && (State & INSERT))
4350 continue;
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004353 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356#ifdef FEAT_GUI
4357 if (gui.in_use)
4358 {
4359 /*
4360 * GUI special key codes are all of the form [CSI xx].
4361 */
4362 if (*tp == CSI) /* Special key from GUI */
4363 {
4364 if (len < 3)
4365 return -1; /* Shouldn't happen */
4366 slen = 3;
4367 key_name[0] = tp[1];
4368 key_name[1] = tp[2];
4369 }
4370 }
4371 else
4372#endif /* FEAT_GUI */
4373 {
4374 for (idx = 0; idx < tc_len; ++idx)
4375 {
4376 /*
4377 * Ignore the entry if we are not at the start of
4378 * typebuf.tb_buf[]
4379 * and there are not enough characters to make a match.
4380 * But only when the 'K' flag is in 'cpoptions'.
4381 */
4382 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004383 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (cpo_koffset && offset && len < slen)
4385 continue;
4386 if (STRNCMP(termcodes[idx].code, tp,
4387 (size_t)(slen > len ? len : slen)) == 0)
4388 {
4389 if (len < slen) /* got a partial sequence */
4390 return -1; /* need to get more chars */
4391
4392 /*
4393 * When found a keypad key, check if there is another key
4394 * that matches and use that one. This makes <Home> to be
4395 * found instead of <kHome> when they produce the same
4396 * key code.
4397 */
4398 if (termcodes[idx].name[0] == 'K'
4399 && VIM_ISDIGIT(termcodes[idx].name[1]))
4400 {
4401 for (j = idx + 1; j < tc_len; ++j)
4402 if (termcodes[j].len == slen &&
4403 STRNCMP(termcodes[idx].code,
4404 termcodes[j].code, slen) == 0)
4405 {
4406 idx = j;
4407 break;
4408 }
4409 }
4410
4411 key_name[0] = termcodes[idx].name[0];
4412 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 break;
4414 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004415
4416 /*
4417 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004418 * <Esc>[123;*X (modslen == slen - 3)
4419 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4420 * When there is a modifier the * matches a number.
4421 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004422 */
4423 if (termcodes[idx].modlen > 0)
4424 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004425 modslen = termcodes[idx].modlen;
4426 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004427 continue;
4428 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004429 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004430 {
4431 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004432
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004433 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004434 return -1; /* need to get more chars */
4435
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004436 if (tp[modslen] == termcodes[idx].code[slen - 1])
4437 slen = modslen + 1; /* no modifiers */
4438 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004439 continue; /* no match */
4440 else
4441 {
4442 /* Skip over the digits, the final char must
4443 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004444 for (j = slen - 2; j < len && (isdigit(tp[j])
4445 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004446 ;
4447 ++j;
4448 if (len < j) /* got a partial sequence */
4449 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004450 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4451 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004452
Bram Moolenaara529ce02017-06-22 22:37:57 +02004453 modifiers_start = tp + slen - 2;
4454
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004455 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004456 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004457 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004458 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004459 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004461 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004463 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004464 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004465
4466 slen = j;
4467 }
4468 key_name[0] = termcodes[idx].name[0];
4469 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004470 break;
4471 }
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
4475
4476#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004477 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004478 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004479 * detecting the start of these mouse codes they might as well be
4480 * another key code or terminal response. */
4481# ifdef FEAT_MOUSE_DEC
4482 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004483# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004484# ifdef FEAT_MOUSE_PTERM
4485 || key_name[0] == KS_PTERM_MOUSE
4486# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004487 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004489 /* Check for some responses from the terminal starting with
4490 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004491 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004492 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004493 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004494 * Also eat other possible responses to t_RV, rxvt returns
4495 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4496 * mrxvt has been reported to have "+" in the version. Assume
4497 * the escape sequence ends with a letter or one of "{|}~".
4498 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004499 * - Cursor position report: <Esc>[{row};{col}R
4500 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004501 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004502 *
4503 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004504 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004505 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004506
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004507 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004508 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004509 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004510 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004512 int col = 0;
4513 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004514#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004515 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004516#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004519 for (i = 2 + (tp[0] != CSI); i < len
4520 && !(tp[i] >= '{' && tp[i] <= '~')
4521 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004522 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004523 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004524 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004525#ifdef FEAT_MBYTE
4526 row_char = tp[i - 1];
4527#endif
4528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004530 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004531 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004532 return -1;
4533 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004534 if (extra > 0)
4535 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004536
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004537#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004538 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4539 * u7_status is not "sent", it may be from a previous Vim that
4540 * just exited. But not for <S-F3>, it sends something
4541 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004542 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004543 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004544 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004545 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004546 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004547
Bram Moolenaarb255b902018-04-24 21:40:10 +02004548 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004549 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004550 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004551 if (col == 2)
4552 aw = "single";
4553 else if (col == 3)
4554 aw = "double";
4555 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4556 {
4557 /* Setting the option causes a screen redraw. Do
4558 * that right away if possible, keeping any
4559 * messages. */
4560 set_option_value((char_u *)"ambw", 0L,
4561 (char_u *)aw, 0);
4562# ifdef DEBUG_TERMRESPONSE
4563 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004564 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004565
Bram Moolenaarb255b902018-04-24 21:40:10 +02004566 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004567 }
4568# else
4569 redraw_asap(CLEAR);
4570# endif
4571 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004572 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004573 key_name[0] = (int)KS_EXTRA;
4574 key_name[1] = (int)KE_IGNORE;
4575 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004576# ifdef FEAT_EVAL
4577 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4578# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004579 }
4580 else
4581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004583 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004585 int version = col;
4586
Bram Moolenaarb255b902018-04-24 21:40:10 +02004587 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004588 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004589 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 /* If this code starts with CSI, you can bet that the
4592 * terminal uses 8-bit codes. */
4593 if (tp[0] == CSI)
4594 switch_to_8bit();
4595
4596 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004597 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 * Ignore it for when the user has set 'term' to xterm,
4599 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004600 if (version > 20000)
4601 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004603 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004605 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004606# ifdef FEAT_MOUSE_SGR
4607 int is_iterm2 = FALSE;
4608# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004611 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004613 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 check_for_codes = TRUE;
4615 need_gather = TRUE;
4616 req_codes_from_term();
4617 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004618
4619 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004620 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004621 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004622 {
4623 /* If run from Vim $COLORS is set to the number of
4624 * colors the terminal supports. Otherwise assume
4625 * 256, libvterm supports even more. */
4626 if (mch_getenv((char_u *)"COLORS") == NULL)
4627 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004628# ifdef FEAT_MOUSE_SGR
4629 /* Libvterm can handle SGR mouse reporting. */
4630 if (!option_was_set((char_u *)"ttym"))
4631 set_option_value((char_u *)"ttym", 0L,
4632 (char_u *)"sgr", 0);
4633# endif
4634 }
4635
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004636 if (version == 95)
4637 {
4638 /* Mac Terminal.app sends 1;95;0 */
4639 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4640 {
4641 is_not_xterm = TRUE;
4642 is_mac_terminal = TRUE;
4643 }
4644# ifdef FEAT_MOUSE_SGR
Bram Moolenaar05684312017-12-09 15:11:24 +01004645 /* iTerm2 sends 0;95;0 */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004646 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4647 is_iterm2 = TRUE;
4648# endif
4649 }
4650
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004651 /* Only set 'ttymouse' automatically if it was not set
4652 * by the user already. */
4653 if (!option_was_set((char_u *)"ttym"))
4654 {
4655# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004656 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004657 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004658 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004659 set_option_value((char_u *)"ttym", 0L,
4660 (char_u *)"sgr", 0);
4661 else
4662# endif
4663 /* if xterm version >= 95 use mouse dragging */
4664 if (version >= 95)
4665 set_option_value((char_u *)"ttym", 0L,
4666 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004667 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004668
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004669 /* Detect terminals that set $TERM to something like
4670 * "xterm-256colors" but are not fully xterm
4671 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004672
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004673 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004674 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004675 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004676 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004677 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004678 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004679 is_not_xterm = TRUE;
4680
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004681 /* PuTTY sends 0;136;0
4682 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004683 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004684 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004685 is_not_xterm = TRUE;
4686
4687 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004688 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004689 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004690 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004691
4692 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004693 * set. Only supported properly by xterm since version
4694 * 279 (otherwise it returns 0x18).
4695 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004696 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004697 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004698 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004699 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004700 && *T_CSH != NUL
4701 && *T_CRS != NUL)
4702 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004703 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004704 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004705 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004706 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004707 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004708
4709 /* Only request the cursor blink mode if t_RC set. Not
4710 * for Gnome terminal, it can't handle t_RC, it
4711 * echoes the characters to the screen. */
4712 if (rbm_status == STATUS_GET
4713 && !is_not_xterm
4714 && *T_CRC != NUL)
4715 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004716 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004717 out_str(T_CRC);
4718 rbm_status = STATUS_SENT;
4719 need_flush = TRUE;
4720 }
4721
4722 if (need_flush)
4723 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004725 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004727 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 apply_autocmds(EVENT_TERMRESPONSE,
4730 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 key_name[0] = (int)KS_EXTRA;
4732 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004734
Bram Moolenaar4db25542017-08-28 22:43:05 +02004735 /* Check blinking cursor from xterm:
4736 * {lead}?12;1$y set
4737 * {lead}?12;2$y not set
4738 *
4739 * {lead} can be <Esc>[ or CSI
4740 */
4741 else if (rbm_status == STATUS_SENT
4742 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4743 && i == j + 6
4744 && tp[j + 1] == '1'
4745 && tp[j + 2] == '2'
4746 && tp[j + 3] == ';'
4747 && tp[i - 1] == '$'
4748 && tp[i] == 'y')
4749 {
4750 initial_cursor_blink = (tp[j + 4] == '1');
4751 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004752 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004753 key_name[0] = (int)KS_EXTRA;
4754 key_name[1] = (int)KE_IGNORE;
4755 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004756# ifdef FEAT_EVAL
4757 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4758# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004759 }
4760
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004761 /*
4762 * Check for a window position response from the terminal:
4763 * {lead}3;{x}:{y}t
4764 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004765 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004766 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4767 || (len >= 3 && tp[0] == CSI))
4768 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4769 && tp[j + 1] == ';')
4770 {
4771 j += 2;
4772 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4773 ;
4774 if (i < len && tp[i] == ';')
4775 {
4776 winpos_x = atoi((char *)tp + j);
4777 j = i + 1;
4778 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4779 ;
4780 if (i < len && tp[i] == 't')
4781 {
4782 winpos_y = atoi((char *)tp + j);
4783 /* got finished code: consume it */
4784 key_name[0] = (int)KS_EXTRA;
4785 key_name[1] = (int)KE_IGNORE;
4786 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004787
4788 if (--did_request_winpos <= 0)
4789 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004790 }
4791 }
4792 if (i == len)
4793 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004794 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004795 return -1;
4796 }
4797 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004798 }
4799
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004800 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004801 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004802 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004803 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004804 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004805 * {lead} can be <Esc>] or OSC
4806 * {tail} can be '\007', <Esc>\ or STERM.
4807 *
4808 * Consume any code that starts with "{lead}11;", it's also
4809 * possible that "rgba" is following.
4810 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004811 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004812 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4813 || tp[0] == OSC))
4814 {
4815 j = 1 + (tp[0] == ESC);
4816 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004817 || (argp[1] != '1' && argp[1] != '0')
4818 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004819 i = 0; /* no match */
4820 else
4821 for (i = j; i < len; ++i)
4822 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4823 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004824 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004825 int is_bg = argp[1] == '1';
4826
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004827 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004828 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004829 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004830#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004831 int rval = hexhex2nr(tp + j + 7);
4832 int gval = hexhex2nr(tp + j + 12);
4833 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004834#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004835 if (is_bg)
4836 {
4837 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004838 + tp[j+17]) ? "light" : "dark";
4839
Bram Moolenaarb255b902018-04-24 21:40:10 +02004840 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004841 rbg_status = STATUS_GOT;
4842#ifdef FEAT_TERMINAL
4843 bg_r = rval;
4844 bg_g = gval;
4845 bg_b = bval;
4846#endif
4847 if (!option_was_set((char_u *)"bg")
4848 && STRCMP(p_bg, newval) != 0)
4849 {
4850 /* value differs, apply it */
4851 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004852 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004853 reset_option_was_set((char_u *)"bg");
4854 redraw_asap(CLEAR);
4855 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004856 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004857#ifdef FEAT_TERMINAL
4858 else
4859 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004860 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004861 rfg_status = STATUS_GOT;
4862 fg_r = rval;
4863 fg_g = gval;
4864 fg_b = bval;
4865 }
4866#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004867 }
4868
4869 /* got finished code: consume it */
4870 key_name[0] = (int)KS_EXTRA;
4871 key_name[1] = (int)KE_IGNORE;
4872 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004873# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004874 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4875 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004876# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004877 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004878 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004879 if (i == len)
4880 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004881 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004882 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004886 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004887 * {lead}{flag}+r<hex bytes><{tail}
4888 *
4889 * {lead} can be <Esc>P or DCS
4890 * {flag} can be '0' or '1'
4891 * {tail} can be Esc>\ or STERM
4892 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004893 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004894 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004895 *
4896 * {lead} can be <Esc>P or DCS
4897 * {tail} can be Esc>\ or STERM
4898 *
4899 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004900 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004901 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004902 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 || tp[0] == DCS))
4904 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004905 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004906 if (len < j + 3)
4907 i = len; /* need more chars */
4908 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004909 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004910 else if (argp[1] == '+')
4911 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004912 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004913 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004914 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 || tp[i] == STERM)
4916 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004917 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 got_code_from_term(tp + j, i);
4919 key_name[0] = (int)KS_EXTRA;
4920 key_name[1] = (int)KE_IGNORE;
4921 slen = i + 1 + (tp[i] == ESC);
4922 break;
4923 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004924 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004925 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004926 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004927 /* Probably the cursor shape response. Make sure that "i"
4928 * is equal to "len" when there are not sufficient
4929 * characters. */
4930 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004931 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004932 if (i - j == 3 && !isdigit(tp[i]))
4933 break;
4934 if (i - j == 4 && tp[i] != ' ')
4935 break;
4936 if (i - j == 5 && tp[i] != 'q')
4937 break;
4938 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4939 break;
4940 if ((i - j == 6 && tp[i] == STERM)
4941 || (i - j == 7 && tp[i] == '\\'))
4942 {
4943 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004944
Bram Moolenaar590ec872018-03-02 20:58:42 +01004945 /* 0, 1 = block blink, 2 = block
4946 * 3 = underline blink, 4 = underline
4947 * 5 = vertical bar blink, 6 = vertical bar */
4948 number = number == 0 ? 1 : number;
4949 initial_cursor_shape = (number + 1) / 2;
4950 /* The blink flag is actually inverted, compared to
4951 * the value set with T_SH. */
4952 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004953 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004954 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004955 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004956
Bram Moolenaar590ec872018-03-02 20:58:42 +01004957 key_name[0] = (int)KS_EXTRA;
4958 key_name[1] = (int)KE_IGNORE;
4959 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004960# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004961 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004962# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004963 break;
4964 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004965 }
4966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004969 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004970 /* These codes arrive many together, each code can be
4971 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02004972 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004973 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 }
4977#endif
4978
4979 if (key_name[0] == NUL)
4980 continue; /* No match at this position, try next one */
4981
4982 /* We only get here when we have a complete termcode match */
4983
4984#ifdef FEAT_MOUSE
4985# ifdef FEAT_GUI
4986 /*
4987 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4988 * so that we know which window to scroll later.
4989 */
4990 if (gui.in_use
4991 && key_name[0] == (int)KS_EXTRA
4992 && (key_name[1] == (int)KE_X1MOUSE
4993 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004994 || key_name[1] == (int)KE_MOUSELEFT
4995 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 || key_name[1] == (int)KE_MOUSEDOWN
4997 || key_name[1] == (int)KE_MOUSEUP))
4998 {
4999 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5000 if (num_bytes == -1) /* not enough coordinates */
5001 return -1;
5002 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5003 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5004 slen += num_bytes;
5005 }
5006 else
5007# endif
5008 /*
5009 * If it is a mouse click, get the coordinates.
5010 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005011 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005013 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014# endif
5015# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005016 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017# endif
5018# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005019 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020# endif
5021# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005022 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005024# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005025 || key_name[0] == KS_URXVT_MOUSE
5026# endif
5027# ifdef FEAT_MOUSE_SGR
5028 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005029 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 )
5032 {
5033 is_click = is_drag = FALSE;
5034
Bram Moolenaar864207d2008-06-24 22:14:38 +00005035# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5036 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (key_name[0] == (int)KS_MOUSE)
5038 {
5039 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005040 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 * s == encoded button state:
5042 * 0x20 = left button down
5043 * 0x21 = middle button down
5044 * 0x22 = right button down
5045 * 0x23 = any button release
5046 * 0x60 = button 4 down (scroll wheel down)
5047 * 0x61 = button 5 down (scroll wheel up)
5048 * add 0x04 for SHIFT
5049 * add 0x08 for ALT
5050 * add 0x10 for CTRL
5051 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005052 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5053 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 * c == column + ' ' + 1 == column + 33
5055 * r == row + ' ' + 1 == row + 33
5056 *
5057 * The coordinates are passed on through global variables.
5058 * Ugly, but this avoids trouble with mouse clicks at an
5059 * unexpected moment and allows for mapping them.
5060 */
5061 for (;;)
5062 {
5063#ifdef FEAT_GUI
5064 if (gui.in_use)
5065 {
5066 /* GUI uses more bits for columns > 223 */
5067 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5068 if (num_bytes == -1) /* not enough coordinates */
5069 return -1;
5070 mouse_code = bytes[0];
5071 mouse_col = 128 * (bytes[1] - ' ' - 1)
5072 + bytes[2] - ' ' - 1;
5073 mouse_row = 128 * (bytes[3] - ' ' - 1)
5074 + bytes[4] - ' ' - 1;
5075 }
5076 else
5077#endif
5078 {
5079 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5080 if (num_bytes == -1) /* not enough coordinates */
5081 return -1;
5082 mouse_code = bytes[0];
5083 mouse_col = bytes[1] - ' ' - 1;
5084 mouse_row = bytes[2] - ' ' - 1;
5085 }
5086 slen += num_bytes;
5087
5088 /* If the following bytes is also a mouse code and it has
5089 * the same code, dump this one and get the next. This
5090 * makes dragging a whole lot faster. */
5091#ifdef FEAT_GUI
5092 if (gui.in_use)
5093 j = 3;
5094 else
5095#endif
5096 j = termcodes[idx].len;
5097 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5098 && tp[slen + j] == mouse_code
5099 && tp[slen + j + 1] != NUL
5100 && tp[slen + j + 2] != NUL
5101#ifdef FEAT_GUI
5102 && (!gui.in_use
5103 || (tp[slen + j + 3] != NUL
5104 && tp[slen + j + 4] != NUL))
5105#endif
5106 )
5107 slen += j;
5108 else
5109 break;
5110 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005113# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5114 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005115 || key_name[0] == KS_SGR_MOUSE
5116 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005117 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005118 /* URXVT 1015 mouse reporting mode:
5119 * Almost identical to xterm mouse mode, except the values
5120 * are decimal instead of bytes.
5121 *
5122 * \033[%d;%d;%dM
5123 * ^-- row
5124 * ^----- column
5125 * ^-------- code
5126 *
5127 * SGR 1006 mouse reporting mode:
5128 * Almost identical to xterm mouse mode, except the values
5129 * are decimal instead of bytes.
5130 *
5131 * \033[<%d;%d;%dM
5132 * ^-- row
5133 * ^----- column
5134 * ^-------- code
5135 *
5136 * \033[<%d;%d;%dm : mouse release event
5137 * ^-- row
5138 * ^----- column
5139 * ^-------- code
5140 */
5141 p = modifiers_start;
5142 if (p == NULL)
5143 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005144
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005145 mouse_code = getdigits(&p);
5146 if (*p++ != ';')
5147 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005148
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005149 /* when mouse reporting is SGR, add 32 to mouse code */
5150 if (key_name[0] == KS_SGR_MOUSE
5151 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5152 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005153
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005154 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5155 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005156
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005157 mouse_col = getdigits(&p) - 1;
5158 if (*p++ != ';')
5159 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005160
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005161 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005162
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005163 /* The modifiers were the mouse coordinates, not the
5164 * modifier keys (alt/shift/ctrl/meta) state. */
5165 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005166 }
5167# endif
5168
5169 if (key_name[0] == (int)KS_MOUSE
5170#ifdef FEAT_MOUSE_URXVT
5171 || key_name[0] == (int)KS_URXVT_MOUSE
5172#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005173#ifdef FEAT_MOUSE_SGR
5174 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005175 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005176#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005177 )
5178 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005179# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /*
5181 * Handle mouse events.
5182 * Recognize the xterm mouse wheel, but not in the GUI, the
5183 * Linux console with GPM and the MS-DOS or Win32 console
5184 * (multi-clicks use >= 0x60).
5185 */
5186 if (mouse_code >= MOUSEWHEEL_LOW
5187# ifdef FEAT_GUI
5188 && !gui.in_use
5189# endif
5190# ifdef FEAT_MOUSE_GPM
5191 && gpm_flag == 0
5192# endif
5193 )
5194 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005195# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5196 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5197 /* mouse-move event, using MOUSE_DRAG works */
5198 mouse_code = MOUSE_DRAG;
5199 else
5200# endif
5201 /* Keep the mouse_code before it's changed, so that we
5202 * remember that it was a mouse wheel click. */
5203 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205# ifdef FEAT_MOUSE_XTERM
5206 else if (held_button == MOUSE_RELEASE
5207# ifdef FEAT_GUI
5208 && !gui.in_use
5209# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005210 && (mouse_code == 0x23 || mouse_code == 0x24
5211 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005213 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5214 * And 0x40 and 0x41 are used by some xterm emulator. */
5215 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5216 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218# endif
5219
5220# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5221 else if (use_xterm_mouse() > 1)
5222 {
5223 if (mouse_code & MOUSE_DRAG_XTERM)
5224 mouse_code |= MOUSE_DRAG;
5225 }
5226# endif
5227# ifdef FEAT_XCLIPBOARD
5228 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5229 {
5230 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5231 stop_xterm_trace();
5232 else
5233 start_xterm_trace(mouse_code);
5234 }
5235# endif
5236# endif
5237 }
5238# endif /* !UNIX || FEAT_MOUSE_XTERM */
5239# ifdef FEAT_MOUSE_NET
5240 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5241 {
5242 int mc, mr;
5243
5244 /* expect a rather limited sequence like: balancing {
5245 * \033}6,45\r
5246 * '6' is the row, 45 is the column
5247 */
5248 p = tp + slen;
5249 mr = getdigits(&p);
5250 if (*p++ != ',')
5251 return -1;
5252 mc = getdigits(&p);
5253 if (*p++ != '\r')
5254 return -1;
5255
5256 mouse_col = mc - 1;
5257 mouse_row = mr - 1;
5258 mouse_code = MOUSE_LEFT;
5259 slen += (int)(p - (tp + slen));
5260 }
5261# endif /* FEAT_MOUSE_NET */
5262# ifdef FEAT_MOUSE_JSB
5263 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5264 {
5265 int mult, val, iter, button, status;
5266
5267 /* JSBTERM Input Model
5268 * \033[0~zw uniq escape sequence
5269 * (L-x) Left button pressed - not pressed x not reporting
5270 * (M-x) Middle button pressed - not pressed x not reporting
5271 * (R-x) Right button pressed - not pressed x not reporting
5272 * (SDmdu) Single , Double click, m mouse move d button down
5273 * u button up
5274 * ### X cursor position padded to 3 digits
5275 * ### Y cursor position padded to 3 digits
5276 * (s-x) SHIFT key pressed - not pressed x not reporting
5277 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005278 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 */
5280
5281 p = tp + slen;
5282 button = mouse_code = 0;
5283 switch (*p++)
5284 {
5285 case 'L': button = 1; break;
5286 case '-': break;
5287 case 'x': break; /* ignore sequence */
5288 default: return -1; /* Unknown Result */
5289 }
5290 switch (*p++)
5291 {
5292 case 'M': button |= 2; break;
5293 case '-': break;
5294 case 'x': break; /* ignore sequence */
5295 default: return -1; /* Unknown Result */
5296 }
5297 switch (*p++)
5298 {
5299 case 'R': button |= 4; break;
5300 case '-': break;
5301 case 'x': break; /* ignore sequence */
5302 default: return -1; /* Unknown Result */
5303 }
5304 status = *p++;
5305 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5306 mult /= 10, p++)
5307 if (*p >= '0' && *p <= '9')
5308 val += (*p - '0') * mult;
5309 else
5310 return -1;
5311 mouse_col = val;
5312 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5313 mult /= 10, p++)
5314 if (*p >= '0' && *p <= '9')
5315 val += (*p - '0') * mult;
5316 else
5317 return -1;
5318 mouse_row = val;
5319 switch (*p++)
5320 {
5321 case 's': button |= 8; break; /* SHIFT key Pressed */
5322 case '-': break; /* Not Pressed */
5323 case 'x': break; /* Not Reporting */
5324 default: return -1; /* Unknown Result */
5325 }
5326 switch (*p++)
5327 {
5328 case 'c': button |= 16; break; /* CTRL key Pressed */
5329 case '-': break; /* Not Pressed */
5330 case 'x': break; /* Not Reporting */
5331 default: return -1; /* Unknown Result */
5332 }
5333 if (*p++ != '\033')
5334 return -1;
5335 if (*p++ != '\\')
5336 return -1;
5337 switch (status)
5338 {
5339 case 'D': /* Double Click */
5340 case 'S': /* Single Click */
5341 if (button & 1) mouse_code |= MOUSE_LEFT;
5342 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5343 if (button & 4) mouse_code |= MOUSE_RIGHT;
5344 if (button & 8) mouse_code |= MOUSE_SHIFT;
5345 if (button & 16) mouse_code |= MOUSE_CTRL;
5346 break;
5347 case 'm': /* Mouse move */
5348 if (button & 1) mouse_code |= MOUSE_LEFT;
5349 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5350 if (button & 4) mouse_code |= MOUSE_RIGHT;
5351 if (button & 8) mouse_code |= MOUSE_SHIFT;
5352 if (button & 16) mouse_code |= MOUSE_CTRL;
5353 if ((button & 7) != 0)
5354 {
5355 held_button = mouse_code;
5356 mouse_code |= MOUSE_DRAG;
5357 }
5358 is_drag = TRUE;
5359 showmode();
5360 break;
5361 case 'd': /* Button Down */
5362 if (button & 1) mouse_code |= MOUSE_LEFT;
5363 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5364 if (button & 4) mouse_code |= MOUSE_RIGHT;
5365 if (button & 8) mouse_code |= MOUSE_SHIFT;
5366 if (button & 16) mouse_code |= MOUSE_CTRL;
5367 break;
5368 case 'u': /* Button Up */
5369 if (button & 1)
5370 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5371 if (button & 2)
5372 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5373 if (button & 4)
5374 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5375 if (button & 8)
5376 mouse_code |= MOUSE_SHIFT;
5377 if (button & 16)
5378 mouse_code |= MOUSE_CTRL;
5379 break;
5380 default: return -1; /* Unknown Result */
5381 }
5382
5383 slen += (p - (tp + slen));
5384 }
5385# endif /* FEAT_MOUSE_JSB */
5386# ifdef FEAT_MOUSE_DEC
5387 if (key_name[0] == (int)KS_DEC_MOUSE)
5388 {
5389 /* The DEC Locator Input Model
5390 * Netterm delivers the code sequence:
5391 * \033[2;4;24;80&w (left button down)
5392 * \033[3;0;24;80&w (left button up)
5393 * \033[6;1;24;80&w (right button down)
5394 * \033[7;0;24;80&w (right button up)
5395 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5396 * Pe is the event code
5397 * Pb is the button code
5398 * Pr is the row coordinate
5399 * Pc is the column coordinate
5400 * Pp is the third coordinate (page number)
5401 * Pe, the event code indicates what event caused this report
5402 * The following event codes are defined:
5403 * 0 - request, the terminal received an explicit request
5404 * for a locator report, but the locator is unavailable
5405 * 1 - request, the terminal received an explicit request
5406 * for a locator report
5407 * 2 - left button down
5408 * 3 - left button up
5409 * 4 - middle button down
5410 * 5 - middle button up
5411 * 6 - right button down
5412 * 7 - right button up
5413 * 8 - fourth button down
5414 * 9 - fourth button up
5415 * 10 - locator outside filter rectangle
5416 * Pb, the button code, ASCII decimal 0-15 indicating which
5417 * buttons are down if any. The state of the four buttons
5418 * on the locator correspond to the low four bits of the
5419 * decimal value,
5420 * "1" means button depressed
5421 * 0 - no buttons down,
5422 * 1 - right,
5423 * 2 - middle,
5424 * 4 - left,
5425 * 8 - fourth
5426 * Pr is the row coordinate of the locator position in the page,
5427 * encoded as an ASCII decimal value.
5428 * If Pr is omitted, the locator position is undefined
5429 * (outside the terminal window for example).
5430 * Pc is the column coordinate of the locator position in the
5431 * page, encoded as an ASCII decimal value.
5432 * If Pc is omitted, the locator position is undefined
5433 * (outside the terminal window for example).
5434 * Pp is the page coordinate of the locator position
5435 * encoded as an ASCII decimal value.
5436 * The page coordinate may be omitted if the locator is on
5437 * page one (the default). We ignore it anyway.
5438 */
5439 int Pe, Pb, Pr, Pc;
5440
5441 p = tp + slen;
5442
5443 /* get event status */
5444 Pe = getdigits(&p);
5445 if (*p++ != ';')
5446 return -1;
5447
5448 /* get button status */
5449 Pb = getdigits(&p);
5450 if (*p++ != ';')
5451 return -1;
5452
5453 /* get row status */
5454 Pr = getdigits(&p);
5455 if (*p++ != ';')
5456 return -1;
5457
5458 /* get column status */
5459 Pc = getdigits(&p);
5460
5461 /* the page parameter is optional */
5462 if (*p == ';')
5463 {
5464 p++;
5465 (void)getdigits(&p);
5466 }
5467 if (*p++ != '&')
5468 return -1;
5469 if (*p++ != 'w')
5470 return -1;
5471
5472 mouse_code = 0;
5473 switch (Pe)
5474 {
5475 case 0: return -1; /* position request while unavailable */
5476 case 1: /* a response to a locator position request includes
5477 the status of all buttons */
5478 Pb &= 7; /* mask off and ignore fourth button */
5479 if (Pb & 4)
5480 mouse_code = MOUSE_LEFT;
5481 if (Pb & 2)
5482 mouse_code = MOUSE_MIDDLE;
5483 if (Pb & 1)
5484 mouse_code = MOUSE_RIGHT;
5485 if (Pb)
5486 {
5487 held_button = mouse_code;
5488 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005489 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 is_drag = TRUE;
5492 showmode();
5493 break;
5494 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005495 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 break;
5497 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5498 break;
5499 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005500 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 break;
5502 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5503 break;
5504 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005505 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 break;
5507 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5508 break;
5509 case 8: return -1; /* fourth button down */
5510 case 9: return -1; /* fourth button up */
5511 case 10: return -1; /* mouse outside of filter rectangle */
5512 default: return -1; /* should never occur */
5513 }
5514
5515 mouse_col = Pc - 1;
5516 mouse_row = Pr - 1;
5517
5518 slen += (int)(p - (tp + slen));
5519 }
5520# endif /* FEAT_MOUSE_DEC */
5521# ifdef FEAT_MOUSE_PTERM
5522 if (key_name[0] == (int)KS_PTERM_MOUSE)
5523 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005524 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 p = tp + slen;
5527
5528 action = getdigits(&p);
5529 if (*p++ != ';')
5530 return -1;
5531
5532 mouse_row = getdigits(&p);
5533 if (*p++ != ';')
5534 return -1;
5535 mouse_col = getdigits(&p);
5536 if (*p++ != ';')
5537 return -1;
5538
5539 button = getdigits(&p);
5540 mouse_code = 0;
5541
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005542 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 case 4: mouse_code = MOUSE_LEFT; break;
5545 case 1: mouse_code = MOUSE_RIGHT; break;
5546 case 2: mouse_code = MOUSE_MIDDLE; break;
5547 default: return -1;
5548 }
5549
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005550 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
5552 case 31: /* Initial press */
5553 if (*p++ != ';')
5554 return -1;
5555
5556 num_clicks = getdigits(&p); /* Not used */
5557 break;
5558
5559 case 32: /* Release */
5560 mouse_code |= MOUSE_RELEASE;
5561 break;
5562
5563 case 33: /* Drag */
5564 held_button = mouse_code;
5565 mouse_code |= MOUSE_DRAG;
5566 break;
5567
5568 default:
5569 return -1;
5570 }
5571
5572 if (*p++ != 't')
5573 return -1;
5574
5575 slen += (p - (tp + slen));
5576 }
5577# endif /* FEAT_MOUSE_PTERM */
5578
5579 /* Interpret the mouse code */
5580 current_button = (mouse_code & MOUSE_CLICK_MASK);
5581 if (current_button == MOUSE_RELEASE
5582# ifdef FEAT_MOUSE_XTERM
5583 && wheel_code == 0
5584# endif
5585 )
5586 {
5587 /*
5588 * If we get a mouse drag or release event when
5589 * there is no mouse button held down (held_button ==
5590 * MOUSE_RELEASE), produce a K_IGNORE below.
5591 * (can happen when you hold down two buttons
5592 * and then let them go, or click in the menu bar, but not
5593 * on a menu, and drag into the text).
5594 */
5595 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5596 is_drag = TRUE;
5597 current_button = held_button;
5598 }
5599 else if (wheel_code == 0)
5600 {
5601# ifdef CHECK_DOUBLE_CLICK
5602# ifdef FEAT_MOUSE_GPM
5603# ifdef FEAT_GUI
5604 /*
5605 * Only for Unix, when GUI or gpm is not active, we handle
5606 * multi-clicks here.
5607 */
5608 if (gpm_flag == 0 && !gui.in_use)
5609# else
5610 if (gpm_flag == 0)
5611# endif
5612# else
5613# ifdef FEAT_GUI
5614 if (!gui.in_use)
5615# endif
5616# endif
5617 {
5618 /*
5619 * Compute the time elapsed since the previous mouse click.
5620 */
5621 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005622 if (orig_mouse_time.tv_sec == 0)
5623 {
5624 /*
5625 * Avoid computing the difference between mouse_time
5626 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005627 * difference would be huge and would cause
5628 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005629 */
5630 timediff = p_mouset;
5631 }
5632 else
5633 {
5634 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005635 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005636 if (timediff < 0)
5637 --orig_mouse_time.tv_sec;
5638 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005639 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 orig_mouse_time = mouse_time;
5642 if (mouse_code == orig_mouse_code
5643 && timediff < p_mouset
5644 && orig_num_clicks != 4
5645 && orig_mouse_col == mouse_col
5646 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005647 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005649 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005651 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005652 /* Double click in tab pages line also works
5653 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005654 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 ++orig_num_clicks;
5657 else
5658 orig_num_clicks = 1;
5659 orig_mouse_col = mouse_col;
5660 orig_mouse_row = mouse_row;
5661 orig_topline = curwin->w_topline;
5662#ifdef FEAT_DIFF
5663 orig_topfill = curwin->w_topfill;
5664#endif
5665 }
5666# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5667 else
5668 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5669# endif
5670# else
5671 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5672# endif
5673 is_click = TRUE;
5674 orig_mouse_code = mouse_code;
5675 }
5676 if (!is_drag)
5677 held_button = mouse_code & MOUSE_CLICK_MASK;
5678
5679 /*
5680 * Translate the actual mouse event into a pseudo mouse event.
5681 * First work out what modifiers are to be used.
5682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (orig_mouse_code & MOUSE_SHIFT)
5684 modifiers |= MOD_MASK_SHIFT;
5685 if (orig_mouse_code & MOUSE_CTRL)
5686 modifiers |= MOD_MASK_CTRL;
5687 if (orig_mouse_code & MOUSE_ALT)
5688 modifiers |= MOD_MASK_ALT;
5689 if (orig_num_clicks == 2)
5690 modifiers |= MOD_MASK_2CLICK;
5691 else if (orig_num_clicks == 3)
5692 modifiers |= MOD_MASK_3CLICK;
5693 else if (orig_num_clicks == 4)
5694 modifiers |= MOD_MASK_4CLICK;
5695
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005696 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5697 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005699 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005700 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005701 {
5702 if (wheel_code & MOUSE_CTRL)
5703 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005704 if (wheel_code & MOUSE_ALT)
5705 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 key_name[1] = (wheel_code & 1)
5707 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005708 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else
5711 key_name[1] = get_pseudo_mouse_code(current_button,
5712 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005713
5714 /* Make sure the mouse position is valid. Some terminals may
5715 * return weird values. */
5716 if (mouse_col >= Columns)
5717 mouse_col = Columns - 1;
5718 if (mouse_row >= Rows)
5719 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721#endif /* FEAT_MOUSE */
5722
5723#ifdef FEAT_GUI
5724 /*
5725 * If using the GUI, then we get menu and scrollbar events.
5726 *
5727 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5728 * four bytes which are to be taken as a pointer to the vimmenu_T
5729 * structure.
5730 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005731 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005732 * is one byte with the tab index.
5733 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5735 * by one byte representing the scrollbar number, and then four bytes
5736 * representing a long_u which is the new value of the scrollbar.
5737 *
5738 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5739 * KE_FILLER followed by four bytes representing a long_u which is the
5740 * new value of the scrollbar.
5741 */
5742# ifdef FEAT_MENU
5743 else if (key_name[0] == (int)KS_MENU)
5744 {
5745 long_u val;
5746
5747 num_bytes = get_long_from_buf(tp + slen, &val);
5748 if (num_bytes == -1)
5749 return -1;
5750 current_menu = (vimmenu_T *)val;
5751 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005752
5753 /* The menu may have been deleted right after it was used, check
5754 * for that. */
5755 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5756 {
5757 key_name[0] = KS_EXTRA;
5758 key_name[1] = (int)KE_IGNORE;
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005762# ifdef FEAT_GUI_TABLINE
5763 else if (key_name[0] == (int)KS_TABLINE)
5764 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005765 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005766 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5767 if (num_bytes == -1)
5768 return -1;
5769 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005770 if (current_tab == 255) /* -1 in a byte gives 255 */
5771 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005772 slen += num_bytes;
5773 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005774 else if (key_name[0] == (int)KS_TABMENU)
5775 {
5776 /* Selecting tabline tab or using its menu. */
5777 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5778 if (num_bytes == -1)
5779 return -1;
5780 current_tab = (int)bytes[0];
5781 current_tabmenu = (int)bytes[1];
5782 slen += num_bytes;
5783 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785# ifndef USE_ON_FLY_SCROLL
5786 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5787 {
5788 long_u val;
5789
5790 /* Get the last scrollbar event in the queue of the same type */
5791 j = 0;
5792 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5793 && tp[j + 2] != NUL; ++i)
5794 {
5795 j += 3;
5796 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5797 if (num_bytes == -1)
5798 break;
5799 if (i == 0)
5800 current_scrollbar = (int)bytes[0];
5801 else if (current_scrollbar != (int)bytes[0])
5802 break;
5803 j += num_bytes;
5804 num_bytes = get_long_from_buf(tp + j, &val);
5805 if (num_bytes == -1)
5806 break;
5807 scrollbar_value = val;
5808 j += num_bytes;
5809 slen = j;
5810 }
5811 if (i == 0) /* not enough characters to make one */
5812 return -1;
5813 }
5814 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5815 {
5816 long_u val;
5817
5818 /* Get the last horiz. scrollbar event in the queue */
5819 j = 0;
5820 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5821 && tp[j + 2] != NUL; ++i)
5822 {
5823 j += 3;
5824 num_bytes = get_long_from_buf(tp + j, &val);
5825 if (num_bytes == -1)
5826 break;
5827 scrollbar_value = val;
5828 j += num_bytes;
5829 slen = j;
5830 }
5831 if (i == 0) /* not enough characters to make one */
5832 return -1;
5833 }
5834# endif /* !USE_ON_FLY_SCROLL */
5835#endif /* FEAT_GUI */
5836
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005837 /*
5838 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5839 */
5840 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5841
5842 /*
5843 * Add any modifier codes to our string.
5844 */
5845 new_slen = 0; /* Length of what will replace the termcode */
5846 if (modifiers != 0)
5847 {
5848 /* Some keys have the modifier included. Need to handle that here
5849 * to make mappings work. */
5850 key = simplify_key(key, &modifiers);
5851 if (modifiers != 0)
5852 {
5853 string[new_slen++] = K_SPECIAL;
5854 string[new_slen++] = (int)KS_MODIFIER;
5855 string[new_slen++] = modifiers;
5856 }
5857 }
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005860 key_name[0] = KEY2TERMCAP0(key);
5861 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005863 {
5864 /* from ":set <M-b>=xx" */
5865#ifdef FEAT_MBYTE
5866 if (has_mbyte)
5867 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5868 else
5869#endif
5870 string[new_slen++] = key_name[1];
5871 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005872 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5873 && key_name[1] == KE_IGNORE)
5874 {
5875 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5876 * to indicate what happened. */
5877 retval = KEYLEN_REMOVED;
5878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 else
5880 {
5881 string[new_slen++] = K_SPECIAL;
5882 string[new_slen++] = key_name[0];
5883 string[new_slen++] = key_name[1];
5884 }
5885 string[new_slen] = NUL;
5886 extra = new_slen - slen;
5887 if (buf == NULL)
5888 {
5889 if (extra < 0)
5890 /* remove matched chars, taking care of noremap */
5891 del_typebuf(-extra, offset);
5892 else if (extra > 0)
5893 /* insert the extra space we need */
5894 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5895
5896 /*
5897 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5898 * typebuf.tb_buf[]!
5899 */
5900 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5901 (size_t)new_slen);
5902 }
5903 else
5904 {
5905 if (extra < 0)
5906 /* remove matched characters */
5907 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005908 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005910 {
5911 /* Insert the extra space we need. If there is insufficient
5912 * space return -1. */
5913 if (*buflen + extra + new_slen >= bufsize)
5914 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005916 (size_t)(*buflen - offset));
5917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005919 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005921 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
5923
Bram Moolenaar2951b772013-07-03 12:45:31 +02005924#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005925 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 return 0; /* no match found */
5929}
5930
Bram Moolenaar9377df32017-10-15 13:22:01 +02005931#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005932/*
5933 * Get the text foreground color, if known.
5934 */
5935 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005936term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005937{
5938 if (rfg_status == STATUS_GOT)
5939 {
5940 *r = fg_r;
5941 *g = fg_g;
5942 *b = fg_b;
5943 }
5944}
5945
5946/*
5947 * Get the text background color, if known.
5948 */
5949 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005950term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005951{
5952 if (rbg_status == STATUS_GOT)
5953 {
5954 *r = bg_r;
5955 *g = bg_g;
5956 *b = bg_b;
5957 }
5958}
5959#endif
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961/*
5962 * Replace any terminal code strings in from[] with the equivalent internal
5963 * vim representation. This is used for the "from" and "to" part of a
5964 * mapping, and the "to" part of a menu command.
5965 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5966 * '<'.
5967 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5968 *
5969 * The replacement is done in result[] and finally copied into allocated
5970 * memory. If this all works well *bufp is set to the allocated memory and a
5971 * pointer to it is returned. If something fails *bufp is set to NULL and from
5972 * is returned.
5973 *
5974 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5975 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5976 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5977 * instead of a CTRL-V.
5978 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005979 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005980replace_termcodes(
5981 char_u *from,
5982 char_u **bufp,
5983 int from_part,
5984 int do_lt, /* also translate <lt> */
5985 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 int i;
5988 int slen;
5989 int key;
5990 int dlen = 0;
5991 char_u *src;
5992 int do_backslash; /* backslash is a special character */
5993 int do_special; /* recognize <> key codes */
5994 int do_key_code; /* recognize raw key codes */
5995 char_u *result; /* buffer for resulting string */
5996
5997 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005998 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6000
6001 /*
6002 * Allocate space for the translation. Worst case a single character is
6003 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6004 */
6005 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6006 if (result == NULL) /* out of memory */
6007 {
6008 *bufp = NULL;
6009 return from;
6010 }
6011
6012 src = from;
6013
6014 /*
6015 * Check for #n at start only: function key n
6016 */
6017 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6018 {
6019 result[dlen++] = K_SPECIAL;
6020 result[dlen++] = 'k';
6021 if (src[1] == '0')
6022 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6023 else
6024 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6025 src += 2;
6026 }
6027
6028 /*
6029 * Copy each byte from *from to result[dlen]
6030 */
6031 while (*src != NUL)
6032 {
6033 /*
6034 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006035 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 */
6037 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6038 {
6039#ifdef FEAT_EVAL
6040 /*
6041 * Replace <SID> by K_SNR <script-nr> _.
6042 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6043 */
6044 if (STRNICMP(src, "<SID>", 5) == 0)
6045 {
6046 if (current_SID <= 0)
6047 EMSG(_(e_usingsid));
6048 else
6049 {
6050 src += 5;
6051 result[dlen++] = K_SPECIAL;
6052 result[dlen++] = (int)KS_EXTRA;
6053 result[dlen++] = (int)KE_SNR;
6054 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6055 dlen += (int)STRLEN(result + dlen);
6056 result[dlen++] = '_';
6057 continue;
6058 }
6059 }
6060#endif
6061
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006062 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (slen)
6064 {
6065 dlen += slen;
6066 continue;
6067 }
6068 }
6069
6070 /*
6071 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6072 * Note that this is also checked after replacing the <> form.
6073 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6074 * it could be a character in the file.
6075 */
6076 if (do_key_code)
6077 {
6078 i = find_term_bykeys(src);
6079 if (i >= 0)
6080 {
6081 result[dlen++] = K_SPECIAL;
6082 result[dlen++] = termcodes[i].name[0];
6083 result[dlen++] = termcodes[i].name[1];
6084 src += termcodes[i].len;
6085 /* If terminal code matched, continue after it. */
6086 continue;
6087 }
6088 }
6089
6090#ifdef FEAT_EVAL
6091 if (do_special)
6092 {
6093 char_u *p, *s, len;
6094
6095 /*
6096 * Replace <Leader> by the value of "mapleader".
6097 * Replace <LocalLeader> by the value of "maplocalleader".
6098 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6099 */
6100 if (STRNICMP(src, "<Leader>", 8) == 0)
6101 {
6102 len = 8;
6103 p = get_var_value((char_u *)"g:mapleader");
6104 }
6105 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6106 {
6107 len = 13;
6108 p = get_var_value((char_u *)"g:maplocalleader");
6109 }
6110 else
6111 {
6112 len = 0;
6113 p = NULL;
6114 }
6115 if (len != 0)
6116 {
6117 /* Allow up to 8 * 6 characters for "mapleader". */
6118 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6119 s = (char_u *)"\\";
6120 else
6121 s = p;
6122 while (*s != NUL)
6123 result[dlen++] = *s++;
6124 src += len;
6125 continue;
6126 }
6127 }
6128#endif
6129
6130 /*
6131 * Remove CTRL-V and ignore the next character.
6132 * For "from" side the CTRL-V at the end is included, for the "to"
6133 * part it is removed.
6134 * If 'cpoptions' does not contain 'B', also accept a backslash.
6135 */
6136 key = *src;
6137 if (key == Ctrl_V || (do_backslash && key == '\\'))
6138 {
6139 ++src; /* skip CTRL-V or backslash */
6140 if (*src == NUL)
6141 {
6142 if (from_part)
6143 result[dlen++] = key;
6144 break;
6145 }
6146 }
6147
6148#ifdef FEAT_MBYTE
6149 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006150 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151#endif
6152 {
6153 /*
6154 * If the character is K_SPECIAL, replace it with K_SPECIAL
6155 * KS_SPECIAL KE_FILLER.
6156 * If compiled with the GUI replace CSI with K_CSI.
6157 */
6158 if (*src == K_SPECIAL)
6159 {
6160 result[dlen++] = K_SPECIAL;
6161 result[dlen++] = KS_SPECIAL;
6162 result[dlen++] = KE_FILLER;
6163 }
6164# ifdef FEAT_GUI
6165 else if (*src == CSI)
6166 {
6167 result[dlen++] = K_SPECIAL;
6168 result[dlen++] = KS_EXTRA;
6169 result[dlen++] = (int)KE_CSI;
6170 }
6171# endif
6172 else
6173 result[dlen++] = *src;
6174 ++src;
6175 }
6176 }
6177 result[dlen] = NUL;
6178
6179 /*
6180 * Copy the new string to allocated memory.
6181 * If this fails, just return from.
6182 */
6183 if ((*bufp = vim_strsave(result)) != NULL)
6184 from = *bufp;
6185 vim_free(result);
6186 return from;
6187}
6188
6189/*
6190 * Find a termcode with keys 'src' (must be NUL terminated).
6191 * Return the index in termcodes[], or -1 if not found.
6192 */
6193 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006194find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006197 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 for (i = 0; i < tc_len; ++i)
6200 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006201 if (slen == termcodes[i].len
6202 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 return i;
6204 }
6205 return -1;
6206}
6207
6208/*
6209 * Gather the first characters in the terminal key codes into a string.
6210 * Used to speed up check_termcode().
6211 */
6212 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006213gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 int i;
6216 int len = 0;
6217
6218#ifdef FEAT_GUI
6219 if (gui.in_use)
6220 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6221#endif
6222#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006223 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 termleader[len++] = DCS; /* the termcode response starts with DCS
6225 in 8-bit mode */
6226#endif
6227 termleader[len] = NUL;
6228
6229 for (i = 0; i < tc_len; ++i)
6230 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6231 {
6232 termleader[len++] = termcodes[i].code[0];
6233 termleader[len] = NUL;
6234 }
6235
6236 need_gather = FALSE;
6237}
6238
6239/*
6240 * Show all termcodes (for ":set termcap")
6241 * This code looks a lot like showoptions(), but is different.
6242 */
6243 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006244show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 int col;
6247 int *items;
6248 int item_count;
6249 int run;
6250 int row, rows;
6251 int cols;
6252 int i;
6253 int len;
6254
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006255#define INC3 27 /* try to make three columns */
6256#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#define GAP 2 /* spaces between columns */
6258
6259 if (tc_len == 0) /* no terminal codes (must be GUI) */
6260 return;
6261 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6262 if (items == NULL)
6263 return;
6264
6265 /* Highlight title */
6266 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6267
6268 /*
6269 * do the loop two times:
6270 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006271 * 2. display the medium items (medium length strings)
6272 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006274 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
6276 /*
6277 * collect the items in items[]
6278 */
6279 item_count = 0;
6280 for (i = 0; i < tc_len; i++)
6281 {
6282 len = show_one_termcode(termcodes[i].name,
6283 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006284 if (len <= INC3 - GAP ? run == 1
6285 : len <= INC2 - GAP ? run == 2
6286 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 items[item_count++] = i;
6288 }
6289
6290 /*
6291 * display the items
6292 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006293 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006295 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (cols == 0)
6297 cols = 1;
6298 rows = (item_count + cols - 1) / cols;
6299 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006300 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 rows = item_count;
6302 for (row = 0; row < rows && !got_int; ++row)
6303 {
6304 msg_putchar('\n'); /* go to next line */
6305 if (got_int) /* 'q' typed in more */
6306 break;
6307 col = 0;
6308 for (i = row; i < item_count; i += rows)
6309 {
6310 msg_col = col; /* make columns */
6311 show_one_termcode(termcodes[items[i]].name,
6312 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006313 if (run == 2)
6314 col += INC2;
6315 else
6316 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
6318 out_flush();
6319 ui_breakcheck();
6320 }
6321 }
6322 vim_free(items);
6323}
6324
6325/*
6326 * Show one termcode entry.
6327 * Output goes into IObuff[]
6328 */
6329 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006330show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *p;
6333 int len;
6334
6335 if (name[0] > '~')
6336 {
6337 IObuff[0] = ' ';
6338 IObuff[1] = ' ';
6339 IObuff[2] = ' ';
6340 IObuff[3] = ' ';
6341 }
6342 else
6343 {
6344 IObuff[0] = 't';
6345 IObuff[1] = '_';
6346 IObuff[2] = name[0];
6347 IObuff[3] = name[1];
6348 }
6349 IObuff[4] = ' ';
6350
6351 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6352 if (p[1] != 't')
6353 STRCPY(IObuff + 5, p);
6354 else
6355 IObuff[5] = NUL;
6356 len = (int)STRLEN(IObuff);
6357 do
6358 IObuff[len++] = ' ';
6359 while (len < 17);
6360 IObuff[len] = NUL;
6361 if (code == NULL)
6362 len += 4;
6363 else
6364 len += vim_strsize(code);
6365
6366 if (printit)
6367 {
6368 msg_puts(IObuff);
6369 if (code == NULL)
6370 msg_puts((char_u *)"NULL");
6371 else
6372 msg_outtrans(code);
6373 }
6374 return len;
6375}
6376
6377#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6378/*
6379 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6380 * termcap codes from the terminal itself.
6381 * We get them one by one to avoid a very long response string.
6382 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006383static int xt_index_in = 0;
6384static int xt_index_out = 0;
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006387req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 xt_index_out = 0;
6390 xt_index_in = 0;
6391 req_more_codes_from_term();
6392}
6393
6394 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006395req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 char buf[11];
6398 int old_idx = xt_index_out;
6399
6400 /* Don't do anything when going to exit. */
6401 if (exiting)
6402 return;
6403
6404 /* Send up to 10 more requests out than we received. Avoid sending too
6405 * many, there can be a buffer overflow somewhere. */
6406 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6407 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006408 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006409
Bram Moolenaarb255b902018-04-24 21:40:10 +02006410 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6411 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 out_str_nf((char_u *)buf);
6413 ++xt_index_out;
6414 }
6415
6416 /* Send the codes out right away. */
6417 if (xt_index_out != old_idx)
6418 out_flush();
6419}
6420
6421/*
6422 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6423 * A "0" instead of the "1" indicates a code that isn't supported.
6424 * Both <name> and <string> are encoded in hex.
6425 * "code" points to the "0" or "1".
6426 */
6427 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006428got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430#define XT_LEN 100
6431 char_u name[3];
6432 char_u str[XT_LEN];
6433 int i;
6434 int j = 0;
6435 int c;
6436
6437 /* A '1' means the code is supported, a '0' means it isn't.
6438 * When half the length is > XT_LEN we can't use it.
6439 * Our names are currently all 2 characters. */
6440 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6441 {
6442 /* Get the name from the response and find it in the table. */
6443 name[0] = hexhex2nr(code + 3);
6444 name[1] = hexhex2nr(code + 5);
6445 name[2] = NUL;
6446 for (i = 0; key_names[i] != NULL; ++i)
6447 {
6448 if (STRCMP(key_names[i], name) == 0)
6449 {
6450 xt_index_in = i;
6451 break;
6452 }
6453 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006454
Bram Moolenaarb255b902018-04-24 21:40:10 +02006455 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (key_names[i] != NULL)
6458 {
6459 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6460 str[j++] = c;
6461 str[j] = NUL;
6462 if (name[0] == 'C' && name[1] == 'o')
6463 {
6464 /* Color count is not a key code. */
6465 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006466 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 }
6468 else
6469 {
6470 /* First delete any existing entry with the same code. */
6471 i = find_term_bykeys(str);
6472 if (i >= 0)
6473 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006474 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 }
6477 }
6478
6479 /* May request more codes now that we received one. */
6480 ++xt_index_in;
6481 req_more_codes_from_term();
6482}
6483
6484/*
6485 * Check if there are any unanswered requests and deal with them.
6486 * This is called before starting an external program or getting direct
6487 * keyboard input. We don't want responses to be send to that program or
6488 * handled as typed text.
6489 */
6490 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006491check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 int c;
6494
6495 /* If no codes requested or all are answered, no need to wait. */
6496 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6497 return;
6498
6499 /* Vgetc() will check for and handle any response.
6500 * Keep calling vpeekc() until we don't get any responses. */
6501 ++no_mapping;
6502 ++allow_keys;
6503 for (;;)
6504 {
6505 c = vpeekc();
6506 if (c == NUL) /* nothing available */
6507 break;
6508
6509 /* If a response is recognized it's replaced with K_IGNORE, must read
6510 * it from the input stream. If there is no K_IGNORE we can't do
6511 * anything, break here (there might be some responses further on, but
6512 * we don't want to throw away any typed chars). */
6513 if (c != K_SPECIAL && c != K_IGNORE)
6514 break;
6515 c = vgetc();
6516 if (c != K_IGNORE)
6517 {
6518 vungetc(c);
6519 break;
6520 }
6521 }
6522 --no_mapping;
6523 --allow_keys;
6524}
6525#endif
6526
6527#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6528/*
6529 * Translate an internal mapping/abbreviation representation into the
6530 * corresponding external one recognized by :map/:abbrev commands;
6531 * respects the current B/k/< settings of 'cpoption'.
6532 *
6533 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006534 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 *
6536 * It uses a growarray to build the translation string since the
6537 * latter can be wider than the original description. The caller has to
6538 * free the string afterwards.
6539 *
6540 * Returns NULL when there is a problem.
6541 */
6542 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006543translate_mapping(
6544 char_u *str,
6545 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 garray_T ga;
6548 int c;
6549 int modifiers;
6550 int cpo_bslash;
6551 int cpo_special;
6552 int cpo_keycode;
6553
6554 ga_init(&ga);
6555 ga.ga_itemsize = 1;
6556 ga.ga_growsize = 40;
6557
6558 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6559 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6560 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6561
6562 for (; *str; ++str)
6563 {
6564 c = *str;
6565 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6566 {
6567 modifiers = 0;
6568 if (str[1] == KS_MODIFIER)
6569 {
6570 str++;
6571 modifiers = *++str;
6572 c = *++str;
6573 }
6574 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6575 {
6576 int i;
6577
6578 /* try to find special key in termcodes */
6579 for (i = 0; i < tc_len; ++i)
6580 if (termcodes[i].name[0] == str[1]
6581 && termcodes[i].name[1] == str[2])
6582 break;
6583 if (i < tc_len)
6584 {
6585 ga_concat(&ga, termcodes[i].code);
6586 str += 2;
6587 continue; /* for (str) */
6588 }
6589 }
6590 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6591 {
6592 if (expmap && cpo_special)
6593 {
6594 ga_clear(&ga);
6595 return NULL;
6596 }
6597 c = TO_SPECIAL(str[1], str[2]);
6598 if (c == K_ZERO) /* display <Nul> as ^@ */
6599 c = NUL;
6600 str += 2;
6601 }
6602 if (IS_SPECIAL(c) || modifiers) /* special key */
6603 {
6604 if (expmap && cpo_special)
6605 {
6606 ga_clear(&ga);
6607 return NULL;
6608 }
6609 ga_concat(&ga, get_special_key_name(c, modifiers));
6610 continue; /* for (str) */
6611 }
6612 }
6613 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6614 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6615 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6616 if (c)
6617 ga_append(&ga, c);
6618 }
6619 ga_append(&ga, NUL);
6620 return (char_u *)(ga.ga_data);
6621}
6622#endif
6623
6624#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6625static char ksme_str[20];
6626static char ksmr_str[20];
6627static char ksmd_str[20];
6628
6629/*
6630 * For Win32 console: update termcap codes for existing console attributes.
6631 */
6632 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006633update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 struct builtin_term *p;
6636
6637 p = find_builtin_term(DEFAULT_TERM);
6638 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6639 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6640 attr | 0x08); /* FOREGROUND_INTENSITY */
6641 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6642 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6643
6644 while (p->bt_string != NULL)
6645 {
6646 if (p->bt_entry == (int)KS_ME)
6647 p->bt_string = &ksme_str[0];
6648 else if (p->bt_entry == (int)KS_MR)
6649 p->bt_string = &ksmr_str[0];
6650 else if (p->bt_entry == (int)KS_MD)
6651 p->bt_string = &ksmd_str[0];
6652 ++p;
6653 }
6654}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006655
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006656# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006657# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006658struct ks_tbl_s
6659{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006660 int code; /* value of KS_ */
6661 char *vtp; /* code in vtp mode */
6662 char *vtp2; /* code in vtp2 mode */
6663 char buf[KSSIZE]; /* save buffer in non-vtp mode */
6664 char vbuf[KSSIZE]; /* save buffer in vtp mode */
6665 char v2buf[KSSIZE]; /* save buffer in vtp2 mode */
6666 char arr[KSSIZE]; /* real buffer */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006667};
6668
6669static struct ks_tbl_s ks_tbl[] =
6670{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006671 {(int)KS_ME, "\033|0m", "\033|0m"}, /* normal */
6672 {(int)KS_MR, "\033|7m", "\033|7m"}, /* reverse */
6673 {(int)KS_MD, "\033|1m", "\033|1m"}, /* bold */
6674 {(int)KS_SO, "\033|91m", "\033|91m"}, /* standout: bright red text */
6675 {(int)KS_SE, "\033|39m", "\033|39m"}, /* standout end: default color */
6676 {(int)KS_CZH, "\033|95m", "\033|95m"}, /* italic: bright magenta text */
6677 {(int)KS_CZR, "\033|0m", "\033|0m"}, /* italic end */
6678 {(int)KS_US, "\033|4m", "\033|4m"}, /* underscore */
6679 {(int)KS_UE, "\033|24m", "\033|24m"}, /* underscore end */
6680# ifdef TERMINFO
6681 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, /* set background color */
6682 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, /* set foreground color */
6683# else
6684 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, /* set background color */
6685 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, /* set foreground color */
6686# endif
6687 {(int)KS_CCO, "16", "256"}, /* colors */
6688 {(int)KS_NAME} /* terminator */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006689};
6690
6691 static struct builtin_term *
6692find_first_tcap(
6693 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006694 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006695{
6696 struct builtin_term *p;
6697
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006698 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006699 if (p->bt_entry == code)
6700 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006701 return NULL;
6702}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006703# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006704
6705/*
6706 * For Win32 console: replace the sequence immediately after termguicolors.
6707 */
6708 void
6709swap_tcap(void)
6710{
6711# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006712 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006713 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006714 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006715 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006716 int mode;
6717 enum
6718 {
6719 CMODEINDEX,
6720 CMODE24,
6721 CMODE256
6722 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006723
6724 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006725 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006726 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006727 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006728 {
6729 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006730 if (bt != NULL)
6731 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006732 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6733 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6734 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6735
6736 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6737 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006738 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006739 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006740 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006741 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006742 }
6743
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006744 if (p_tgc)
6745 mode = CMODE24;
6746 else if (t_colors >= 256)
6747 mode = CMODE256;
6748 else
6749 mode = CMODEINDEX;
6750
6751 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006752 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006753 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6754 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006755 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006756 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006757 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006758 case CMODEINDEX:
6759 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6760 break;
6761 case CMODE24:
6762 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6763 break;
6764 default:
6765 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006766 }
6767 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006768 }
6769
6770 if (mode != curr_mode)
6771 {
6772 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006773 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006774 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6775 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006776 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006777 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006778 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006779 case CMODEINDEX:
6780 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6781 break;
6782 case CMODE24:
6783 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6784 break;
6785 default:
6786 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006787 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006788 }
6789 }
6790
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006792 }
6793# endif
6794}
6795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006797
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006798#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006799 static int
6800hex_digit(int c)
6801{
6802 if (isdigit(c))
6803 return c - '0';
6804 c = TOLOWER_ASC(c);
6805 if (c >= 'a' && c <= 'f')
6806 return c - 'a' + 10;
6807 return 0x1ffffff;
6808}
6809
6810 guicolor_T
6811gui_get_color_cmn(char_u *name)
6812{
Bram Moolenaar28726652017-01-06 18:16:19 +01006813 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6814 * values as used by the MS-Windows GDI api. It should be used only for
6815 * MS-Windows GDI builds. */
6816# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6817# undef RGB
6818# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006819# ifndef RGB
6820# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6821# endif
6822# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006823 FILE *fd;
6824 char line[LINE_LEN];
6825 char_u *fname;
6826 int r, g, b, i;
6827 guicolor_T color;
6828
6829 struct rgbcolor_table_S {
6830 char_u *color_name;
6831 guicolor_T color;
6832 };
6833
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006834 /* Only non X11 colors (not present in rgb.txt) and colors in
6835 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006836 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006837 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6838 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6839 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6840 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6841 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6842 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6843 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6844 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6845 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6846 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6847 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6848 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6849 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006850 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6851 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006852 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006853 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006854 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006855 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6856 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6857 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6858 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6859 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6860 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6861 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6862 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6863 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006864 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006865 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006866 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6867 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006868 };
6869
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006870 static struct rgbcolor_table_S *colornames_table;
6871 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006872
6873 if (name[0] == '#' && STRLEN(name) == 7)
6874 {
6875 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006876 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006877 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6878 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6879 if (color > 0xffffff)
6880 return INVALCOLOR;
6881 return color;
6882 }
6883
6884 /* Check if the name is one of the colors we know */
6885 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6886 if (STRICMP(name, rgb_table[i].color_name) == 0)
6887 return rgb_table[i].color;
6888
6889 /*
6890 * Last attempt. Look in the file "$VIM/rgb.txt".
6891 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006892 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006893 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006894 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006895
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006896 /* colornames_table not yet initialized */
6897 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6898 if (fname == NULL)
6899 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006900
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006901 fd = fopen((char *)fname, "rt");
6902 vim_free(fname);
6903 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006904 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006905 if (p_verbose > 1)
6906 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6907 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006908 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006909
6910 for (counting = 1; counting >= 0; --counting)
6911 {
6912 if (!counting)
6913 {
6914 colornames_table = (struct rgbcolor_table_S *)alloc(
6915 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6916 if (colornames_table == NULL)
6917 {
6918 fclose(fd);
6919 return INVALCOLOR;
6920 }
6921 rewind(fd);
6922 }
6923 size = 0;
6924
6925 while (!feof(fd))
6926 {
6927 size_t len;
6928 int pos;
6929
6930 ignoredp = fgets(line, LINE_LEN, fd);
6931 len = strlen(line);
6932
6933 if (len <= 1 || line[len - 1] != '\n')
6934 continue;
6935
6936 line[len - 1] = '\0';
6937
6938 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6939 if (i != 3)
6940 continue;
6941
6942 if (!counting)
6943 {
6944 char_u *s = vim_strsave((char_u *)line + pos);
6945
6946 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006947 {
6948 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006949 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006950 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006951 colornames_table[size].color_name = s;
6952 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6953 }
6954 size++;
6955 }
6956 }
6957 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006958 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006959
6960 for (i = 0; i < size; i++)
6961 if (STRICMP(name, colornames_table[i].color_name) == 0)
6962 return colornames_table[i].color;
6963
Bram Moolenaarab302212016-04-26 20:59:29 +02006964 return INVALCOLOR;
6965}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006966
6967 guicolor_T
6968gui_get_rgb_color_cmn(int r, int g, int b)
6969{
6970 guicolor_T color = RGB(r, g, b);
6971
6972 if (color > 0xffffff)
6973 return INVALCOLOR;
6974 return color;
6975}
Bram Moolenaarab302212016-04-26 20:59:29 +02006976#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006977
6978#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
6979 || defined(PROTO)
6980static int cube_value[] = {
6981 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6982};
6983
6984static int grey_ramp[] = {
6985 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6986 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6987};
6988
6989# ifdef FEAT_TERMINAL
6990# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006991# else
6992# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006993# endif
6994
Bram Moolenaar9894e392018-05-05 14:29:06 +02006995static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006996// R G B idx
6997 { 0, 0, 0, 1}, // black
6998 {224, 0, 0, 2}, // dark red
6999 { 0, 224, 0, 3}, // dark green
7000 {224, 224, 0, 4}, // dark yellow / brown
7001 { 0, 0, 224, 5}, // dark blue
7002 {224, 0, 224, 6}, // dark magenta
7003 { 0, 224, 224, 7}, // dark cyan
7004 {224, 224, 224, 8}, // light grey
7005
7006 {128, 128, 128, 9}, // dark grey
7007 {255, 64, 64, 10}, // light red
7008 { 64, 255, 64, 11}, // light green
7009 {255, 255, 64, 12}, // yellow
7010 { 64, 64, 255, 13}, // light blue
7011 {255, 64, 255, 14}, // light magenta
7012 { 64, 255, 255, 15}, // light cyan
7013 {255, 255, 255, 16}, // white
7014};
7015
7016 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007017cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007018{
7019 int idx;
7020
7021 if (nr < 16)
7022 {
7023 *r = ansi_table[nr][0];
7024 *g = ansi_table[nr][1];
7025 *b = ansi_table[nr][2];
7026 *ansi_idx = ansi_table[nr][3];
7027 }
7028 else if (nr < 232)
7029 {
7030 /* 216 color cube */
7031 idx = nr - 16;
7032 *r = cube_value[idx / 36 % 6];
7033 *g = cube_value[idx / 6 % 6];
7034 *b = cube_value[idx % 6];
7035 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7036 }
7037 else if (nr < 256)
7038 {
7039 /* 24 grey scale ramp */
7040 idx = nr - 232;
7041 *r = grey_ramp[idx];
7042 *g = grey_ramp[idx];
7043 *b = grey_ramp[idx];
7044 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7045 }
7046 else
7047 {
7048 *r = 0;
7049 *g = 0;
7050 *b = 0;
7051 *ansi_idx = 0;
7052 }
7053}
7054#endif