blob: 37caf2b26cfb0aba8719f64199fd7f8e448e65d6 [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 Moolenaar69e05692018-05-10 14:11:52 +02001547 static void
1548get_term_entries(int *height, int *width)
1549{
1550 static struct {
1551 enum SpecialKey dest; /* index in term_strings[] */
1552 char *name; /* termcap name for string */
1553 } string_names[] =
1554 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1555 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1556 {KS_CL, "cl"}, {KS_CD, "cd"},
1557 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1558 {KS_ME, "me"}, {KS_MR, "mr"},
1559 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1560 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1561 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1562 {KS_STE,"Te"}, {KS_STS,"Ts"},
1563 {KS_CM, "cm"}, {KS_SR, "sr"},
1564 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1565 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1566 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1567 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1568 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1569 {KS_VS, "vs"}, {KS_CVS, "VS"},
1570 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1571 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1572 {KS_TS, "ts"}, {KS_FS, "fs"},
1573 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1574 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1575 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1576 {KS_8F, "8f"}, {KS_8B, "8b"},
1577 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1578 {KS_CPS, "PS"}, {KS_CPE, "PE"},
1579 {(enum SpecialKey)0, NULL}
1580 };
1581 int i;
1582 char_u *p;
1583 static char_u tstrbuf[TBUFSZ];
1584 char_u *tp = tstrbuf;
1585
1586 /*
1587 * get output strings
1588 */
1589 for (i = 0; string_names[i].name != NULL; ++i)
1590 {
1591 if (TERM_STR(string_names[i].dest) == NULL
1592 || TERM_STR(string_names[i].dest) == empty_option)
1593 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
1594 }
1595
1596 /* tgetflag() returns 1 if the flag is present, 0 if not and
1597 * possibly -1 if the flag doesn't exist. */
1598 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1599 T_MS = (char_u *)"y";
1600 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1601 T_XS = (char_u *)"y";
1602 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1603 T_XN = (char_u *)"y";
1604 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1605 T_DB = (char_u *)"y";
1606 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1607 T_DA = (char_u *)"y";
1608 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1609 T_UT = (char_u *)"y";
1610
1611 /*
1612 * get key codes
1613 */
1614 for (i = 0; key_names[i] != NULL; ++i)
1615 if (find_termcode((char_u *)key_names[i]) == NULL)
1616 {
1617 p = TGETSTR(key_names[i], &tp);
1618 /* if cursor-left == backspace, ignore it (televideo 925) */
1619 if (p != NULL
1620 && (*p != Ctrl_H
1621 || key_names[i][0] != 'k'
1622 || key_names[i][1] != 'l'))
1623 add_termcode((char_u *)key_names[i], p, FALSE);
1624 }
1625
1626 if (*height == 0)
1627 *height = tgetnum("li");
1628 if (*width == 0)
1629 *width = tgetnum("co");
1630
1631 /*
1632 * Get number of colors (if not done already).
1633 */
1634 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
1635 set_color_count(tgetnum("Co"));
1636
1637# ifndef hpux
1638 BC = (char *)TGETSTR("bc", &tp);
1639 UP = (char *)TGETSTR("up", &tp);
1640 p = TGETSTR("pc", &tp);
1641 if (p)
1642 PC = *p;
1643# endif
1644}
1645
1646 static void
1647report_term_error(char_u *error_msg, char_u *term)
1648{
1649 struct builtin_term *termp;
1650
1651 mch_errmsg("\r\n");
1652 if (error_msg != NULL)
1653 {
1654 mch_errmsg((char *)error_msg);
1655 mch_errmsg("\r\n");
1656 }
1657 mch_errmsg("'");
1658 mch_errmsg((char *)term);
1659 mch_errmsg(_("' not known. Available builtin terminals are:"));
1660 mch_errmsg("\r\n");
1661 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1662 {
1663 if (termp->bt_entry == (int)KS_NAME)
1664 {
1665#ifdef HAVE_TGETENT
1666 mch_errmsg(" builtin_");
1667#else
1668 mch_errmsg(" ");
1669#endif
1670 mch_errmsg(termp->bt_string);
1671 mch_errmsg("\r\n");
1672 }
1673 }
1674}
1675
1676 static void
1677report_default_term(char_u *term)
1678{
1679 mch_errmsg(_("defaulting to '"));
1680 mch_errmsg((char *)term);
1681 mch_errmsg("'\r\n");
1682 if (emsg_silent == 0)
1683 {
1684 screen_start(); /* don't know where cursor is now */
1685 out_flush();
1686 if (!is_not_a_term())
1687 ui_delay(2000L, TRUE);
1688 }
1689}
1690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691/*
1692 * Set terminal options for terminal "term".
1693 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1694 *
1695 * While doing this, until ttest(), some options may be NULL, be careful.
1696 */
1697 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001698set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 struct builtin_term *termp;
1701#ifdef HAVE_TGETENT
1702 int builtin_first = p_tbi;
1703 int try;
1704 int termcap_cleared = FALSE;
1705#endif
1706 int width = 0, height = 0;
1707 char_u *error_msg = NULL;
1708 char_u *bs_p, *del_p;
1709
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001710 /* In silect mode (ex -s) we don't use the 'term' option. */
1711 if (silent_mode)
1712 return OK;
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 detected_8bit = FALSE; /* reset 8-bit detection */
1715
1716 if (term_is_builtin(term))
1717 {
1718 term += 8;
1719#ifdef HAVE_TGETENT
1720 builtin_first = 1;
1721#endif
1722 }
1723
1724/*
1725 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1726 * If builtin_first is TRUE:
1727 * 0. try builtin termcap
1728 * 1. try external termcap
1729 * 2. if both fail default to a builtin terminal
1730 * If builtin_first is FALSE:
1731 * 1. try external termcap
1732 * 2. try builtin termcap, if both fail default to a builtin terminal
1733 */
1734#ifdef HAVE_TGETENT
1735 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1736 {
1737 /*
1738 * Use external termcap
1739 */
1740 if (try == 1)
1741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 /*
1745 * If the external termcap does not have a matching entry, try the
1746 * builtin ones.
1747 */
1748 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (!termcap_cleared)
1751 {
1752 clear_termoptions(); /* clear old options */
1753 termcap_cleared = TRUE;
1754 }
1755
Bram Moolenaar69e05692018-05-10 14:11:52 +02001756 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758 }
1759 else /* try == 0 || try == 2 */
1760#endif /* HAVE_TGETENT */
1761 /*
1762 * Use builtin termcap
1763 */
1764 {
1765#ifdef HAVE_TGETENT
1766 /*
1767 * If builtin termcap was already used, there is no need to search
1768 * for the builtin termcap again, quit now.
1769 */
1770 if (try == 2 && builtin_first && termcap_cleared)
1771 break;
1772#endif
1773 /*
1774 * search for 'term' in builtin_termcaps[]
1775 */
1776 termp = find_builtin_term(term);
1777 if (termp->bt_string == NULL) /* did not find it */
1778 {
1779#ifdef HAVE_TGETENT
1780 /*
1781 * If try == 0, first try the external termcap. If that is not
1782 * found we'll get back here with try == 2.
1783 * If termcap_cleared is set we used the external termcap,
1784 * don't complain about not finding the term in the builtin
1785 * termcap.
1786 */
1787 if (try == 0) /* try external one */
1788 continue;
1789 if (termcap_cleared) /* found in external termcap */
1790 break;
1791#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001792 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 /* when user typed :set term=xxx, quit here */
1795 if (starting != NO_SCREEN)
1796 {
1797 screen_start(); /* don't know where cursor is now */
1798 wait_return(TRUE);
1799 return FAIL;
1800 }
1801 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001802 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001803 set_string_option_direct((char_u *)"term", -1, term,
1804 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 display_errors();
1806 }
1807 out_flush();
1808#ifdef HAVE_TGETENT
1809 if (!termcap_cleared)
1810 {
1811#endif
1812 clear_termoptions(); /* clear old options */
1813#ifdef HAVE_TGETENT
1814 termcap_cleared = TRUE;
1815 }
1816#endif
1817 parse_builtin_tcap(term);
1818#ifdef FEAT_GUI
1819 if (term_is_gui(term))
1820 {
1821 out_flush();
1822 gui_init();
1823 /* If starting the GUI failed, don't do any of the other
1824 * things for this terminal */
1825 if (!gui.in_use)
1826 return FAIL;
1827#ifdef HAVE_TGETENT
1828 break; /* don't try using external termcap */
1829#endif
1830 }
1831#endif /* FEAT_GUI */
1832 }
1833#ifdef HAVE_TGETENT
1834 }
1835#endif
1836
1837/*
1838 * special: There is no info in the termcap about whether the cursor
1839 * positioning is relative to the start of the screen or to the start of the
1840 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1841 * relative.
1842 */
1843 if (STRCMP(term, "pcterm") == 0)
1844 T_CCS = (char_u *)"yes";
1845 else
1846 T_CCS = empty_option;
1847
1848#ifdef UNIX
1849/*
1850 * Any "stty" settings override the default for t_kb from the termcap.
1851 * This is in os_unix.c, because it depends a lot on the version of unix that
1852 * is being used.
1853 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1854 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001855# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 get_stty();
1859#endif
1860
1861/*
1862 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1863 * didn't work, use the default CTRL-H
1864 * The default for t_kD is DEL, unless t_kb is DEL.
1865 * The vim_strsave'd strings are probably lost forever, well it's only two
1866 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1867 * directly.
1868 */
1869#ifdef FEAT_GUI
1870 if (!gui.in_use)
1871#endif
1872 {
1873 bs_p = find_termcode((char_u *)"kb");
1874 del_p = find_termcode((char_u *)"kD");
1875 if (bs_p == NULL || *bs_p == NUL)
1876 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1877 if ((del_p == NULL || *del_p == NUL) &&
1878 (bs_p == NULL || *bs_p != DEL))
1879 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1880 }
1881
1882#if defined(UNIX) || defined(VMS)
1883 term_is_xterm = vim_is_xterm(term);
1884#endif
1885
1886#ifdef FEAT_MOUSE
1887# if defined(UNIX) || defined(VMS)
1888# ifdef FEAT_MOUSE_TTY
1889 /*
1890 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1891 * The termcode for the mouse is added as a side effect in option.c.
1892 */
1893 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001894 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001897 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 if (use_xterm_mouse())
1900 p = NULL; /* keep existing value, might be "xterm2" */
1901 else
1902 p = (char_u *)"xterm";
1903 }
1904# endif
1905 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001908 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1909 * "xterm2" in check_termcode(). */
1910 reset_option_was_set((char_u *)"ttym");
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (p == NULL
1913# ifdef FEAT_GUI
1914 || gui.in_use
1915# endif
1916 )
1917 check_mouse_termcode(); /* set mouse termcode anyway */
1918 }
1919# endif
1920# else
1921 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1922# endif
1923#endif /* FEAT_MOUSE */
1924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef USE_TERM_CONSOLE
1926 /* DEFAULT_TERM indicates that it is the machine console. */
1927 if (STRCMP(term, DEFAULT_TERM) != 0)
1928 term_console = FALSE;
1929 else
1930 {
1931 term_console = TRUE;
1932# ifdef AMIGA
1933 win_resize_on(); /* enable window resizing reports */
1934# endif
1935 }
1936#endif
1937
1938#if defined(UNIX) || defined(VMS)
1939 /*
1940 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1941 */
1942 if (vim_is_fastterm(term))
1943 p_tf = TRUE;
1944#endif
1945#ifdef USE_TERM_CONSOLE
1946 /*
1947 * 'ttyfast' is default on consoles
1948 */
1949 if (term_console)
1950 p_tf = TRUE;
1951#endif
1952
1953 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1954
1955 full_screen = TRUE; /* we can use termcap codes from now on */
1956 set_term_defaults(); /* use current values as defaults */
1957#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02001958 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001959 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
1961
1962 /*
1963 * Initialize the terminal with the appropriate termcap codes.
1964 * Set the mouse and window title if possible.
1965 * Don't do this when starting, need to parse the .vimrc first, because it
1966 * may redefine t_TI etc.
1967 */
1968 if (starting != NO_SCREEN)
1969 {
1970 starttermcap(); /* may change terminal mode */
1971#ifdef FEAT_MOUSE
1972 setmouse(); /* may start using the mouse */
1973#endif
1974#ifdef FEAT_TITLE
1975 maketitle(); /* may display window title */
1976#endif
1977 }
1978
1979 /* display initial screen after ttest() checking. jw. */
1980 if (width <= 0 || height <= 0)
1981 {
1982 /* termcap failed to report size */
1983 /* set defaults, in case ui_get_shellsize() also fails */
1984 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001985#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 height = 25; /* console is often 25 lines */
1987#else
1988 height = 24; /* most terminals are 24 lines */
1989#endif
1990 }
1991 set_shellsize(width, height, FALSE); /* may change Rows */
1992 if (starting != NO_SCREEN)
1993 {
1994 if (scroll_region)
1995 scroll_region_reset(); /* In case Rows changed */
1996 check_map_keycodes(); /* check mappings for terminal codes used */
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001999 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /*
2002 * Execute the TermChanged autocommands for each buffer that is
2003 * loaded.
2004 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002005 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002006 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 if (curbuf->b_ml.ml_mfp != NULL)
2009 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2010 curbuf);
2011 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002012 if (bufref_valid(&old_curbuf))
2013 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016
2017#ifdef FEAT_TERMRESPONSE
2018 may_req_termresponse();
2019#endif
2020
2021 return OK;
2022}
2023
2024#if defined(FEAT_MOUSE) || defined(PROTO)
2025
2026# ifdef FEAT_MOUSE_TTY
2027# define HMT_NORMAL 1
2028# define HMT_NETTERM 2
2029# define HMT_DEC 4
2030# define HMT_JSBTERM 8
2031# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002032# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002033# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002034# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035static int has_mouse_termcode = 0;
2036# endif
2037
Bram Moolenaar864207d2008-06-24 22:14:38 +00002038# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002040set_mouse_termcode(
2041 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2042 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 char_u name[2];
2045
2046 name[0] = n;
2047 name[1] = KE_FILLER;
2048 add_termcode(name, s, FALSE);
2049# ifdef FEAT_MOUSE_TTY
2050# ifdef FEAT_MOUSE_JSB
2051 if (n == KS_JSBTERM_MOUSE)
2052 has_mouse_termcode |= HMT_JSBTERM;
2053 else
2054# endif
2055# ifdef FEAT_MOUSE_NET
2056 if (n == KS_NETTERM_MOUSE)
2057 has_mouse_termcode |= HMT_NETTERM;
2058 else
2059# endif
2060# ifdef FEAT_MOUSE_DEC
2061 if (n == KS_DEC_MOUSE)
2062 has_mouse_termcode |= HMT_DEC;
2063 else
2064# endif
2065# ifdef FEAT_MOUSE_PTERM
2066 if (n == KS_PTERM_MOUSE)
2067 has_mouse_termcode |= HMT_PTERM;
2068 else
2069# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002070# ifdef FEAT_MOUSE_URXVT
2071 if (n == KS_URXVT_MOUSE)
2072 has_mouse_termcode |= HMT_URXVT;
2073 else
2074# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002075# ifdef FEAT_MOUSE_SGR
2076 if (n == KS_SGR_MOUSE)
2077 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002078 else if (n == KS_SGR_MOUSE_RELEASE)
2079 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002080 else
2081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 has_mouse_termcode |= HMT_NORMAL;
2083# endif
2084}
2085# endif
2086
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002087# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002088 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002090del_mouse_termcode(
2091 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
2093 char_u name[2];
2094
2095 name[0] = n;
2096 name[1] = KE_FILLER;
2097 del_termcode(name);
2098# ifdef FEAT_MOUSE_TTY
2099# ifdef FEAT_MOUSE_JSB
2100 if (n == KS_JSBTERM_MOUSE)
2101 has_mouse_termcode &= ~HMT_JSBTERM;
2102 else
2103# endif
2104# ifdef FEAT_MOUSE_NET
2105 if (n == KS_NETTERM_MOUSE)
2106 has_mouse_termcode &= ~HMT_NETTERM;
2107 else
2108# endif
2109# ifdef FEAT_MOUSE_DEC
2110 if (n == KS_DEC_MOUSE)
2111 has_mouse_termcode &= ~HMT_DEC;
2112 else
2113# endif
2114# ifdef FEAT_MOUSE_PTERM
2115 if (n == KS_PTERM_MOUSE)
2116 has_mouse_termcode &= ~HMT_PTERM;
2117 else
2118# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002119# ifdef FEAT_MOUSE_URXVT
2120 if (n == KS_URXVT_MOUSE)
2121 has_mouse_termcode &= ~HMT_URXVT;
2122 else
2123# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002124# ifdef FEAT_MOUSE_SGR
2125 if (n == KS_SGR_MOUSE)
2126 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002127 else if (n == KS_SGR_MOUSE_RELEASE)
2128 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002129 else
2130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 has_mouse_termcode &= ~HMT_NORMAL;
2132# endif
2133}
2134# endif
2135#endif
2136
2137#ifdef HAVE_TGETENT
2138/*
2139 * Call tgetent()
2140 * Return error message if it fails, NULL if it's OK.
2141 */
2142 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002143tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int i;
2146
2147 i = TGETENT(tbuf, term);
2148 if (i < 0 /* -1 is always an error */
2149# ifdef TGETENT_ZERO_ERR
2150 || i == 0 /* sometimes zero is also an error */
2151# endif
2152 )
2153 {
2154 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2155 * tgetent() with the always existing "dumb" entry to avoid a crash or
2156 * hang. */
2157 (void)TGETENT(tbuf, "dumb");
2158
2159 if (i < 0)
2160# ifdef TGETENT_ZERO_ERR
2161 return (char_u *)_("E557: Cannot open termcap file");
2162 if (i == 0)
2163# endif
2164#ifdef TERMINFO
2165 return (char_u *)_("E558: Terminal entry not found in terminfo");
2166#else
2167 return (char_u *)_("E559: Terminal entry not found in termcap");
2168#endif
2169 }
2170 return NULL;
2171}
2172
2173/*
2174 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2175 * Fix that here.
2176 */
2177 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002178vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 char *p;
2181
2182 p = tgetstr(s, (char **)pp);
2183 if (p == (char *)-1)
2184 p = NULL;
2185 return (char_u *)p;
2186}
2187#endif /* HAVE_TGETENT */
2188
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002189#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190/*
2191 * Get Columns and Rows from the termcap. Used after a window signal if the
2192 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2193 * and "li" entries never change. But on some systems this works.
2194 * Errors while getting the entries are ignored.
2195 */
2196 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002197getlinecol(
2198 long *cp, /* pointer to columns */
2199 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 char_u tbuf[TBUFSZ];
2202
2203 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2204 {
2205 if (*cp == 0)
2206 *cp = tgetnum("co");
2207 if (*rp == 0)
2208 *rp = tgetnum("li");
2209 }
2210}
2211#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2212
2213/*
2214 * Get a string entry from the termcap and add it to the list of termcodes.
2215 * Used for <t_xx> special keys.
2216 * Give an error message for failure when not sourcing.
2217 * If force given, replace an existing entry.
2218 * Return FAIL if the entry was not found, OK if the entry was added.
2219 */
2220 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002221add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 char_u *term;
2224 int key;
2225 struct builtin_term *termp;
2226#ifdef HAVE_TGETENT
2227 char_u *string;
2228 int i;
2229 int builtin_first;
2230 char_u tbuf[TBUFSZ];
2231 char_u tstrbuf[TBUFSZ];
2232 char_u *tp = tstrbuf;
2233 char_u *error_msg = NULL;
2234#endif
2235
2236/*
2237 * If the GUI is running or will start in a moment, we only support the keys
2238 * that the GUI can produce.
2239 */
2240#ifdef FEAT_GUI
2241 if (gui.in_use || gui.starting)
2242 return gui_mch_haskey(name);
2243#endif
2244
2245 if (!force && find_termcode(name) != NULL) /* it's already there */
2246 return OK;
2247
2248 term = T_NAME;
2249 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2250 return FAIL;
2251
2252 if (term_is_builtin(term)) /* name starts with "builtin_" */
2253 {
2254 term += 8;
2255#ifdef HAVE_TGETENT
2256 builtin_first = TRUE;
2257#endif
2258 }
2259#ifdef HAVE_TGETENT
2260 else
2261 builtin_first = p_tbi;
2262#endif
2263
2264#ifdef HAVE_TGETENT
2265/*
2266 * We can get the entry from the builtin termcap and from the external one.
2267 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2268 * builtin termcap first.
2269 * If 'ttybuiltin' is off, try external termcap first.
2270 */
2271 for (i = 0; i < 2; ++i)
2272 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002273 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#endif
2275 /*
2276 * Search in builtin termcap
2277 */
2278 {
2279 termp = find_builtin_term(term);
2280 if (termp->bt_string != NULL) /* found it */
2281 {
2282 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002283 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 while (termp->bt_entry != (int)KS_NAME)
2285 {
2286 if ((int)termp->bt_entry == key)
2287 {
2288 add_termcode(name, (char_u *)termp->bt_string,
2289 term_is_8bit(term));
2290 return OK;
2291 }
2292 ++termp;
2293 }
2294 }
2295 }
2296#ifdef HAVE_TGETENT
2297 else
2298 /*
2299 * Search in external termcap
2300 */
2301 {
2302 error_msg = tgetent_error(tbuf, term);
2303 if (error_msg == NULL)
2304 {
2305 string = TGETSTR((char *)name, &tp);
2306 if (string != NULL && *string != NUL)
2307 {
2308 add_termcode(name, string, FALSE);
2309 return OK;
2310 }
2311 }
2312 }
2313 }
2314#endif
2315
2316 if (sourcing_name == NULL)
2317 {
2318#ifdef HAVE_TGETENT
2319 if (error_msg != NULL)
2320 EMSG(error_msg);
2321 else
2322#endif
2323 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2324 }
2325 return FAIL;
2326}
2327
2328 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002329term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2332}
2333
2334/*
2335 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2336 * Assume that the terminal is using 8-bit controls when the name contains
2337 * "8bit", like in "xterm-8bit".
2338 */
2339 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002340term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2343}
2344
2345/*
2346 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002347 * <Esc>[ -> CSI <M_C_[>
2348 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 * <Esc>O -> <M-C-O>
2350 */
2351 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002352term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354 if (*p == ESC)
2355 {
2356 if (p[1] == '[')
2357 return CSI;
2358 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002359 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (p[1] == 'O')
2361 return 0x8f;
2362 }
2363 return 0;
2364}
2365
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002366#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002368term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2371}
2372#endif
2373
2374#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2375
2376 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002377tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 static char_u buf[16];
2380 char_u *p;
2381
2382 p = buf + 15;
2383 *p = '\0';
2384 do
2385 {
2386 --p;
2387 *p = (char_u) (i % 10 + '0');
2388 i /= 10;
2389 }
2390 while (i > 0 && p > buf);
2391 return p;
2392}
2393#endif
2394
2395#ifndef HAVE_TGETENT
2396
2397/*
2398 * minimal tgoto() implementation.
2399 * no padding and we only parse for %i %d and %+char
2400 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002401static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002403 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002404tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 static char buf[30];
2407 char *p, *s, *e;
2408
2409 if (!cm)
2410 return "OOPS";
2411 e = buf + 29;
2412 for (s = buf; s < e && *cm; cm++)
2413 {
2414 if (*cm != '%')
2415 {
2416 *s++ = *cm;
2417 continue;
2418 }
2419 switch (*++cm)
2420 {
2421 case 'd':
2422 p = (char *)tltoa((unsigned long)y);
2423 y = x;
2424 while (*p)
2425 *s++ = *p++;
2426 break;
2427 case 'i':
2428 x++;
2429 y++;
2430 break;
2431 case '+':
2432 *s++ = (char)(*++cm + y);
2433 y = x;
2434 break;
2435 case '%':
2436 *s++ = *cm;
2437 break;
2438 default:
2439 return "OOPS";
2440 }
2441 }
2442 *s = '\0';
2443 return buf;
2444}
2445
2446#endif /* HAVE_TGETENT */
2447
2448/*
2449 * Set the terminal name and initialize the terminal options.
2450 * If "name" is NULL or empty, get the terminal name from the environment.
2451 * If that fails, use the default terminal name.
2452 */
2453 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002454termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456 char_u *term;
2457
2458 if (name != NULL && *name == NUL)
2459 name = NULL; /* empty name is equal to no name */
2460 term = name;
2461
2462#ifdef __BEOS__
2463 /*
2464 * TERM environment variable is normally set to 'ansi' on the Bebox;
2465 * Since the BeBox doesn't quite support full ANSI yet, we use our
2466 * own custom 'ansi-beos' termcap instead, unless the -T option has
2467 * been given on the command line.
2468 */
2469 if (term == NULL
2470 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2471 term = DEFAULT_TERM;
2472#endif
2473#ifndef MSWIN
2474 if (term == NULL)
2475 term = mch_getenv((char_u *)"TERM");
2476#endif
2477 if (term == NULL || *term == NUL)
2478 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002479 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /* Set the default terminal name. */
2482 set_string_default("term", term);
2483 set_string_default("ttytype", term);
2484
2485 /*
2486 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2487 */
2488 set_termname(T_NAME != NULL ? T_NAME : term);
2489}
2490
2491/*
2492 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2493 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002494#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2496static char_u out_buf[OUT_SIZE + 1];
2497static int out_pos = 0; /* number of chars in out_buf */
2498
2499/*
2500 * out_flush(): flush the output buffer
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 int len;
2506
2507 if (out_pos != 0)
2508 {
2509 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2510 len = out_pos;
2511 out_pos = 0;
2512 ui_write(out_buf, len);
2513 }
2514}
2515
Bram Moolenaara338adc2018-01-31 20:51:47 +01002516/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002517 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2518 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002519 */
2520 void
2521out_flush_cursor(
2522 int force UNUSED, /* when TRUE, update cursor even when not moved */
2523 int clear_selection UNUSED) /* clear selection under cursor */
2524{
2525 mch_disable_flush();
2526 out_flush();
2527 mch_enable_flush();
2528#ifdef FEAT_GUI
2529 if (gui.in_use)
2530 {
2531 gui_update_cursor(force, clear_selection);
2532 gui_may_flush();
2533 }
2534#endif
2535}
2536
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538#if defined(FEAT_MBYTE) || defined(PROTO)
2539/*
2540 * Sometimes a byte out of a multi-byte character is written with out_char().
2541 * To avoid flushing half of the character, call this function first.
2542 */
2543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002544out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2547 out_flush();
2548}
2549#endif
2550
2551#ifdef FEAT_GUI
2552/*
2553 * out_trash(): Throw away the contents of the output buffer
2554 */
2555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 out_pos = 0;
2559}
2560#endif
2561
2562/*
2563 * out_char(c): put a byte into the output buffer.
2564 * Flush it if it becomes full.
2565 * This should not be used for outputting text on the screen (use functions
2566 * like msg_puts() and screen_putchar() for that).
2567 */
2568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002569out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
Bram Moolenaard0573012017-10-28 21:11:06 +02002571#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2573 out_char('\r');
2574#endif
2575
2576 out_buf[out_pos++] = c;
2577
2578 /* For testing we flush each time. */
2579 if (out_pos >= OUT_SIZE || p_wd)
2580 out_flush();
2581}
2582
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002583static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585/*
2586 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2587 */
2588 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002589out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaard0573012017-10-28 21:11:06 +02002591#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2593 out_char_nf('\r');
2594#endif
2595
2596 out_buf[out_pos++] = c;
2597
2598 if (out_pos >= OUT_SIZE)
2599 out_flush();
2600}
2601
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002602#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2603 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604/*
2605 * A never-padding out_str.
2606 * use this whenever you don't want to run the string through tputs.
2607 * tputs above is harmless, but tputs from the termcap library
2608 * is likely to strip off leading digits, that it mistakes for padding
2609 * information, and "%i", "%d", etc.
2610 * This should only be used for writing terminal codes, not for outputting
2611 * normal text (use functions like msg_puts() and screen_putchar() for that).
2612 */
2613 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002614out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2617 out_flush();
2618 while (*s)
2619 out_char_nf(*s++);
2620
2621 /* For testing we write one string at a time. */
2622 if (p_wd)
2623 out_flush();
2624}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002628 * A conditional-flushing out_str, mainly for visualbell.
2629 * Handles a delay internally, because termlib may not respect the delay or do
2630 * it at the wrong time.
2631 * Note: Only for terminal strings.
2632 */
2633 void
2634out_str_cf(char_u *s)
2635{
2636 if (s != NULL && *s)
2637 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002638#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002639 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002640#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002641
2642#ifdef FEAT_GUI
2643 /* Don't use tputs() when GUI is used, ncurses crashes. */
2644 if (gui.in_use)
2645 {
2646 out_str_nf(s);
2647 return;
2648 }
2649#endif
2650 if (out_pos > OUT_SIZE - 20)
2651 out_flush();
2652#ifdef HAVE_TGETENT
2653 for (p = s; *s; ++s)
2654 {
2655 /* flush just before delay command */
2656 if (*s == '$' && *(s + 1) == '<')
2657 {
2658 char_u save_c = *s;
2659 int duration = atoi((char *)s + 2);
2660
2661 *s = NUL;
2662 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2663 *s = save_c;
2664 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002665# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002666 /* Only sleep here if we can limit this happening in
2667 * vim_beep(). */
2668 p = vim_strchr(s, '>');
2669 if (p == NULL || duration <= 0)
2670 {
2671 /* can't parse the time, don't sleep here */
2672 p = s;
2673 }
2674 else
2675 {
2676 ++p;
2677 do_sleep(duration);
2678 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002679# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002680 /* Rely on the terminal library to sleep. */
2681 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002682# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002683 break;
2684 }
2685 }
2686 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2687#else
2688 while (*s)
2689 out_char_nf(*s++);
2690#endif
2691
2692 /* For testing we write one string at a time. */
2693 if (p_wd)
2694 out_flush();
2695 }
2696}
2697
2698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 * out_str(s): Put a character string a byte at a time into the output buffer.
2700 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2701 * This should only be used for writing terminal codes, not for outputting
2702 * normal text (use functions like msg_puts() and screen_putchar() for that).
2703 */
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 if (s != NULL && *s)
2708 {
2709#ifdef FEAT_GUI
2710 /* Don't use tputs() when GUI is used, ncurses crashes. */
2711 if (gui.in_use)
2712 {
2713 out_str_nf(s);
2714 return;
2715 }
2716#endif
2717 /* avoid terminal strings being split up */
2718 if (out_pos > OUT_SIZE - 20)
2719 out_flush();
2720#ifdef HAVE_TGETENT
2721 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2722#else
2723 while (*s)
2724 out_char_nf(*s++);
2725#endif
2726
2727 /* For testing we write one string at a time. */
2728 if (p_wd)
2729 out_flush();
2730 }
2731}
2732
2733/*
2734 * cursor positioning using termcap parser. (jw)
2735 */
2736 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002737term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 OUT_STR(tgoto((char *)T_CM, col, row));
2740}
2741
2742 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002743term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 OUT_STR(tgoto((char *)T_CRI, 0, i));
2746}
2747
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2752}
2753
2754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002755term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2758}
2759
2760#if defined(HAVE_TGETENT) || defined(PROTO)
2761 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002762term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 /* Can't handle a negative value here */
2765 if (x < 0)
2766 x = 0;
2767 if (y < 0)
2768 y = 0;
2769 OUT_STR(tgoto((char *)T_CWP, y, x));
2770}
2771
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002772# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2773/*
2774 * Return TRUE if we can request the terminal for a response.
2775 */
2776 static int
2777can_get_termresponse()
2778{
2779 return cur_tmode == TMODE_RAW
2780 && termcap_active
2781# ifdef UNIX
2782 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2783# endif
2784 && p_ek;
2785}
2786
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002787static int winpos_x = -1;
2788static int winpos_y = -1;
2789static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002790
2791/*
2792 * Try getting the Vim window position from the terminal.
2793 * Returns OK or FAIL.
2794 */
2795 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002796term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002797{
2798 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002799 int prev_winpos_x = winpos_x;
2800 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002801
2802 if (*T_CGP == NUL || !can_get_termresponse())
2803 return FAIL;
2804 winpos_x = -1;
2805 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002806 ++did_request_winpos;
2807 winpos_status = STATUS_SENT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002808 OUT_STR(T_CGP);
2809 out_flush();
2810
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002811 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002812 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002813 {
2814 (void)vpeekc_nomap();
2815 if (winpos_x >= 0 && winpos_y >= 0)
2816 {
2817 *x = winpos_x;
2818 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002819 return OK;
2820 }
2821 ui_delay(10, FALSE);
2822 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002823 /* Do not reset "did_request_winpos", if we timed out the response might
2824 * still come later and we must consume it. */
2825
2826 winpos_x = prev_winpos_x;
2827 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002828 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002829 {
2830 /* Polling: return previous values if we have them. */
2831 *x = winpos_x;
2832 *y = winpos_y;
2833 return OK;
2834 }
2835
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836 return FALSE;
2837}
2838# endif
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002841term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002843 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845#endif
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002848term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002851 int i = *s == CSI ? 1 : 2;
2852 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 /* Special handling of 16 colors, because termcap can't handle it */
2855 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2856 /* Also accept CSI instead of <Esc>[ */
2857 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002858 && ((s[0] == ESC && s[1] == '[')
2859#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2860 || (s[0] == ESC && s[1] == '|')
2861#endif
2862 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 && s[i] != NUL
2864 && (STRCMP(s + i + 1, "%p1%dm") == 0
2865 || STRCMP(s + i + 1, "%dm") == 0)
2866 && (s[i] == '3' || s[i] == '4'))
2867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002869 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002871 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002873 sprintf(buf, format,
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002874 i == 2 ?
2875#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2876 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
2877#endif
2878 IF_EB("\033[", ESC_STR "[") : "\233",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2880 : (n >= 16 ? "48;5;" : "10"));
2881 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2882 }
2883 else
2884 OUT_STR(tgoto((char *)s, 0, n));
2885}
2886
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002887 void
2888term_fg_color(int n)
2889{
2890 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2891 if (*T_CAF)
2892 term_color(T_CAF, n);
2893 else if (*T_CSF)
2894 term_color(T_CSF, n);
2895}
2896
2897 void
2898term_bg_color(int n)
2899{
2900 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2901 if (*T_CAB)
2902 term_color(T_CAB, n);
2903 else if (*T_CSB)
2904 term_color(T_CSB, n);
2905}
2906
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002907#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002908
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002909#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2910#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2911#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002912
2913 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002914term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002915{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002916#define MAX_COLOR_STR_LEN 100
2917 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002918
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002919 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002920 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002921 OUT_STR(buf);
2922}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002923
2924 void
2925term_fg_rgb_color(guicolor_T rgb)
2926{
2927 term_rgb_color(T_8F, rgb);
2928}
2929
2930 void
2931term_bg_rgb_color(guicolor_T rgb)
2932{
2933 term_rgb_color(T_8B, rgb);
2934}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002935#endif
2936
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002937#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2938 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939/*
2940 * Generic function to set window title, using t_ts and t_fs.
2941 */
2942 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002943term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 /* t_ts takes one argument: column in status line */
2946 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2947 out_str_nf(title);
2948 out_str(T_FS); /* set title end */
2949 out_flush();
2950}
2951#endif
2952
2953/*
2954 * Make sure we have a valid set or terminal options.
2955 * Replace all entries that are NULL by empty_option
2956 */
2957 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002958ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002960 char_u *env_colors;
2961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 check_options(); /* make sure no options are NULL */
2963
2964 /*
2965 * MUST have "cm": cursor motion.
2966 */
2967 if (*T_CM == NUL)
2968 EMSG(_("E437: terminal capability \"cm\" required"));
2969
2970 /*
2971 * if "cs" defined, use a scroll region, it's faster.
2972 */
2973 if (*T_CS != NUL)
2974 scroll_region = TRUE;
2975 else
2976 scroll_region = FALSE;
2977
2978 if (pairs)
2979 {
2980 /*
2981 * optional pairs
2982 */
2983 /* TP goes to normal mode for TI (invert) and TB (bold) */
2984 if (*T_ME == NUL)
2985 T_ME = T_MR = T_MD = T_MB = empty_option;
2986 if (*T_SO == NUL || *T_SE == NUL)
2987 T_SO = T_SE = empty_option;
2988 if (*T_US == NUL || *T_UE == NUL)
2989 T_US = T_UE = empty_option;
2990 if (*T_CZH == NUL || *T_CZR == NUL)
2991 T_CZH = T_CZR = empty_option;
2992
2993 /* T_VE is needed even though T_VI is not defined */
2994 if (*T_VE == NUL)
2995 T_VI = empty_option;
2996
2997 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2998 if (*T_ME == NUL)
2999 {
3000 T_ME = T_SE;
3001 T_MR = T_SO;
3002 T_MD = T_SO;
3003 }
3004
3005 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3006 if (*T_SO == NUL)
3007 {
3008 T_SE = T_ME;
3009 if (*T_MR == NUL)
3010 T_SO = T_MD;
3011 else
3012 T_SO = T_MR;
3013 }
3014
3015 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3016 if (*T_CZH == NUL)
3017 {
3018 T_CZR = T_ME;
3019 if (*T_MR == NUL)
3020 T_CZH = T_MD;
3021 else
3022 T_CZH = T_MR;
3023 }
3024
3025 /* "Sb" and "Sf" come in pairs */
3026 if (*T_CSB == NUL || *T_CSF == NUL)
3027 {
3028 T_CSB = empty_option;
3029 T_CSF = empty_option;
3030 }
3031
3032 /* "AB" and "AF" come in pairs */
3033 if (*T_CAB == NUL || *T_CAF == NUL)
3034 {
3035 T_CAB = empty_option;
3036 T_CAF = empty_option;
3037 }
3038
3039 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3040 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003041 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 /* Set 'weirdinvert' according to value of 't_xs' */
3044 p_wiv = (*T_XS != NUL);
3045 }
3046 need_gather = TRUE;
3047
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003048 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003050 env_colors = mch_getenv((char_u *)"COLORS");
3051 if (env_colors != NULL && isdigit(*env_colors))
3052 {
3053 int colors = atoi((char *)env_colors);
3054
3055 if (colors != t_colors)
3056 set_color_count(colors);
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059
3060#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3061 || defined(PROTO)
3062/*
3063 * Represent the given long_u as individual bytes, with the most significant
3064 * byte first, and store them in dst.
3065 */
3066 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003067add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 int i;
3070 int shift;
3071
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003072 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 shift = 8 * (sizeof(long_u) - i);
3075 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3076 }
3077}
3078
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003079static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081/*
3082 * Interpret the next string of bytes in buf as a long integer, with the most
3083 * significant byte first. Note that it is assumed that buf has been through
3084 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3085 * Puts result in val, and returns the number of bytes read from buf
3086 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3087 * were present.
3088 */
3089 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003090get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 int len;
3093 char_u bytes[sizeof(long_u)];
3094 int i;
3095 int shift;
3096
3097 *val = 0;
3098 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3099 if (len != -1)
3100 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003101 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 shift = 8 * (sizeof(long_u) - 1 - i);
3104 *val += (long_u)bytes[i] << shift;
3105 }
3106 }
3107 return len;
3108}
3109#endif
3110
3111#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003112 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3113 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114/*
3115 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3116 * that buf has been through inchar(). Returns the actual number of bytes used
3117 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3118 * available.
3119 */
3120 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003121get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 int len = 0;
3124 int i;
3125 char_u c;
3126
3127 for (i = 0; i < num_bytes; i++)
3128 {
3129 if ((c = buf[len++]) == NUL)
3130 return -1;
3131 if (c == K_SPECIAL)
3132 {
3133 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3134 return -1;
3135 if (buf[len++] == (int)KS_ZERO)
3136 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003137 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3138 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3139 if (buf[len++] == (int)KE_CSI)
3140 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003142 else if (c == CSI && buf[len] == KS_EXTRA
3143 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003144 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3145 * the start of a special key, see add_to_input_buf_csi(). */
3146 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 bytes[i] = c;
3148 }
3149 return len;
3150}
3151#endif
3152
3153/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003154 * Check if the new shell size is valid, correct it if it's too small or way
3155 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 */
3157 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003158check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (Rows < min_rows()) /* need room for one window and command line */
3161 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003162 limit_screen_size();
3163}
3164
3165/*
3166 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3167 */
3168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003169limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003170{
3171 if (Columns < MIN_COLUMNS)
3172 Columns = MIN_COLUMNS;
3173 else if (Columns > 10000)
3174 Columns = 10000;
3175 if (Rows > 1000)
3176 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177}
3178
Bram Moolenaar86b68352004-12-27 21:59:20 +00003179/*
3180 * Invoked just before the screen structures are going to be (re)allocated.
3181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003183win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 static int old_Rows = 0;
3186 static int old_Columns = 0;
3187
3188 if (old_Rows != Rows || old_Columns != Columns)
3189 ui_new_shellsize();
3190 if (old_Rows != Rows)
3191 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003192 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003193 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003194 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 old_Rows = Rows;
3196 shell_new_rows(); /* update window sizes */
3197 }
3198 if (old_Columns != Columns)
3199 {
3200 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203}
3204
3205/*
3206 * Call this function when the Vim shell has been resized in any way.
3207 * Will obtain the current size and redraw (also when size didn't change).
3208 */
3209 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003210shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 set_shellsize(0, 0, FALSE);
3213}
3214
3215/*
3216 * Check if the shell size changed. Handle a resize.
3217 * When the size didn't change, nothing happens.
3218 */
3219 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003220shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 int old_Rows = Rows;
3223 int old_Columns = Columns;
3224
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003225 if (!exiting
3226#ifdef FEAT_GUI
3227 /* Do not get the size when executing a shell command during
3228 * startup. */
3229 && !gui.starting
3230#endif
3231 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003232 {
3233 (void)ui_get_shellsize();
3234 check_shellsize();
3235 if (old_Rows != Rows || old_Columns != Columns)
3236 shell_resized();
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238}
3239
3240/*
3241 * Set size of the Vim shell.
3242 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3243 * window size (this is used for the :win command).
3244 * If 'mustset' is FALSE, we may try to get the real window size and if
3245 * it fails use 'width' and 'height'.
3246 */
3247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003248set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 static int busy = FALSE;
3251
3252 /*
3253 * Avoid recursiveness, can happen when setting the window size causes
3254 * another window-changed signal.
3255 */
3256 if (busy)
3257 return;
3258
3259 if (width < 0 || height < 0) /* just checking... */
3260 return;
3261
Bram Moolenaara971b822011-09-14 14:43:25 +02003262 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003264 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 State = SETWSIZE;
3266 return;
3267 }
3268
Bram Moolenaara971b822011-09-14 14:43:25 +02003269 /* curwin->w_buffer can be NULL when we are closing a window and the
3270 * buffer has already been closed and removing a scrollbar causes a resize
3271 * event. Don't resize then, it will happen after entering another buffer.
3272 */
3273 if (curwin->w_buffer == NULL)
3274 return;
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 ++busy;
3277
3278#ifdef AMIGA
3279 out_flush(); /* must do this before mch_get_shellsize() for
3280 some obscure reason */
3281#endif
3282
3283 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3284 {
3285 Rows = height;
3286 Columns = width;
3287 check_shellsize();
3288 ui_set_shellsize(mustset);
3289 }
3290 else
3291 check_shellsize();
3292
3293 /* The window layout used to be adjusted here, but it now happens in
3294 * screenalloc() (also invoked from screenclear()). That is because the
3295 * "busy" check above may skip this, but not screenalloc(). */
3296
3297 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3298 screenclear();
3299 else
3300 screen_start(); /* don't know where cursor is now */
3301
3302 if (starting != NO_SCREEN)
3303 {
3304#ifdef FEAT_TITLE
3305 maketitle();
3306#endif
3307 changed_line_abv_curs();
3308 invalidate_botline();
3309
3310 /*
3311 * We only redraw when it's needed:
3312 * - While at the more prompt or executing an external command, don't
3313 * redraw, but position the cursor.
3314 * - While editing the command line, only redraw that.
3315 * - in Ex mode, don't redraw anything.
3316 * - Otherwise, redraw right now, and position the cursor.
3317 * Always need to call update_screen() or screenalloc(), to make
3318 * sure Rows/Columns and the size of ScreenLines[] is correct!
3319 */
3320 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3321 || exmode_active)
3322 {
3323 screenalloc(FALSE);
3324 repeat_message();
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
3327 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003328 if (curwin->w_p_scb)
3329 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003330 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003331 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003332 update_screen(NOT_VALID);
3333 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003334 }
3335 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003336 {
3337 update_topline();
3338#if defined(FEAT_INS_EXPAND)
3339 if (pum_visible())
3340 {
3341 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003342 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003343 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003344#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003345 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003346 if (redrawing())
3347 setcursor();
3348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 cursor_on(); /* redrawing may have switched it off */
3351 }
3352 out_flush();
3353 --busy;
3354}
3355
3356/*
3357 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3358 * commands and Ex mode).
3359 */
3360 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003361settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363#ifdef FEAT_GUI
3364 /* don't set the term where gvim was started to any mode */
3365 if (gui.in_use)
3366 return;
3367#endif
3368
3369 if (full_screen)
3370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 /*
3372 * When returning after calling a shell we want to really set the
3373 * terminal to raw mode, even though we think it already is, because
3374 * the shell program may have reset the terminal mode.
3375 * When we think the terminal is normal, don't try to set it to
3376 * normal again, because that causes problems (logout!) on some
3377 * machines.
3378 */
3379 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3380 {
3381#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003382# ifdef FEAT_GUI
3383 if (!gui.in_use && !gui.starting)
3384# endif
3385 {
3386 /* May need to check for T_CRV response and termcodes, it
3387 * doesn't work in Cooked mode, an external program may get
3388 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003389 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3390 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003391#ifdef FEAT_TERMINAL
3392 || rfg_status == STATUS_SENT
3393#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003394 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003395 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003396 || rcs_status == STATUS_SENT
3397 || winpos_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003398 (void)vpeekc_nomap();
3399 check_for_codes_from_term();
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#endif
3402#ifdef FEAT_MOUSE_TTY
3403 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003404 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003406 if (tmode != TMODE_RAW)
3407 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003409 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 cur_tmode = tmode;
3411#ifdef FEAT_MOUSE
3412 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003413 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003415 if (tmode == TMODE_RAW)
3416 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 out_flush();
3418 }
3419#ifdef FEAT_TERMRESPONSE
3420 may_req_termresponse();
3421#endif
3422 }
3423}
3424
3425 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003426starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 if (full_screen && !termcap_active)
3429 {
3430 out_str(T_TI); /* start termcap mode */
3431 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003432 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 out_flush();
3434 termcap_active = TRUE;
3435 screen_start(); /* don't know where cursor is now */
3436#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003437# ifdef FEAT_GUI
3438 if (!gui.in_use && !gui.starting)
3439# endif
3440 {
3441 may_req_termresponse();
3442 /* Immediately check for a response. If t_Co changes, we don't
3443 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003444 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003445 check_for_codes_from_term();
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#endif
3448 }
3449}
3450
3451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 screen_stop_highlight();
3455 reset_cterm_colors();
3456 if (termcap_active)
3457 {
3458#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003459# ifdef FEAT_GUI
3460 if (!gui.in_use && !gui.starting)
3461# endif
3462 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003463 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003464 if (crv_status == STATUS_SENT
3465 || u7_status == STATUS_SENT
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003466# ifdef FEAT_TERMINAL
3467 || rfg_status == STATUS_SENT
3468# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003469 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003470 || rbm_status == STATUS_SENT
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003471 || rcs_status == STATUS_SENT
3472 || winpos_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003473 {
3474# ifdef UNIX
3475 /* Give the terminal a chance to respond. */
3476 mch_delay(100L, FALSE);
3477# endif
3478# ifdef TCIFLUSH
3479 /* Discard data received but not read. */
3480 if (exiting)
3481 tcflush(fileno(stdin), TCIFLUSH);
3482# endif
3483 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003484 /* Check for termcodes first, otherwise an external program may
3485 * get them. */
3486 check_for_codes_from_term();
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003489 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 out_str(T_KE); /* stop "keypad transmit" mode */
3491 out_flush();
3492 termcap_active = FALSE;
3493 cursor_on(); /* just in case it is still off */
3494 out_str(T_TE); /* stop termcap mode */
3495 screen_start(); /* don't know where cursor is now */
3496 out_flush();
3497 }
3498}
3499
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003500#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501/*
3502 * Request version string (for xterm) when needed.
3503 * Only do this after switching to raw mode, otherwise the result will be
3504 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003505 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003506 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 * Only do this after termcap mode has been started, otherwise the codes for
3508 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003509 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3510 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003511 * On Unix only do it when both output and input are a tty (avoid writing
3512 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 * The result is caught in check_termcode().
3514 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003516may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003518 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003519 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003520 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 && *T_CRV != NUL)
3522 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003523 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003525 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /* check for the characters now, otherwise they might be eaten by
3527 * get_keystroke() */
3528 out_flush();
3529 (void)vpeekc_nomap();
3530 }
3531}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003532
3533# if defined(FEAT_MBYTE) || defined(PROTO)
3534/*
3535 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003536 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003537 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003538 * If the terminal treats \u25bd as single width, the position is (1, 1),
3539 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003540 * This function has the side effect that changes cursor position, so
3541 * it must be called immediately after entering termcap mode.
3542 */
3543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003544may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003545{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003546 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003547 && can_get_termresponse()
3548 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003549 && *T_U7 != NUL
3550 && !option_was_set((char_u *)"ambiwidth"))
3551 {
3552 char_u buf[16];
3553
Bram Moolenaarb255b902018-04-24 21:40:10 +02003554 LOG_TR(("Sending U7 request"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02003555 /* Do this in the second row. In the first row the returned sequence
3556 * may be CSI 1;2R, which is the same as <S-F3>. */
3557 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003558 buf[mb_char2bytes(0x25bd, buf)] = 0;
3559 out_str(buf);
3560 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003561 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003562 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003563
3564 /* This overwrites a few characters on the screen, a redraw is needed
3565 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003566 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003567 out_str((char_u *)" ");
3568 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003569
Bram Moolenaar05684312017-12-09 15:11:24 +01003570 /* Need to reset the known cursor position. */
3571 screen_start();
3572
Bram Moolenaar9584b312013-03-13 19:29:28 +01003573 /* check for the characters now, otherwise they might be eaten by
3574 * get_keystroke() */
3575 out_flush();
3576 (void)vpeekc_nomap();
3577 }
3578}
3579# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003580
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003581/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003582 * Similar to requesting the version string: Request the terminal background
3583 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003584 */
3585 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003586may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003587{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003588 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003589 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003590 int didit = FALSE;
3591
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003592# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003593 /* Only request foreground if t_RF is set. */
3594 if (rfg_status == STATUS_GET && *T_RFG != NUL)
3595 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003596 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003597 out_str(T_RFG);
3598 rfg_status = STATUS_SENT;
3599 didit = TRUE;
3600 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003601# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003602
3603 /* Only request background if t_RB is set. */
3604 if (rbg_status == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003605 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003606 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003607 out_str(T_RBG);
3608 rbg_status = STATUS_SENT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003609 didit = TRUE;
3610 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003611
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003612 if (didit)
3613 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003614 /* check for the characters now, otherwise they might be eaten by
3615 * get_keystroke() */
3616 out_flush();
3617 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003618 }
3619 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003620}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003621
Bram Moolenaar2951b772013-07-03 12:45:31 +02003622# ifdef DEBUG_TERMRESPONSE
3623 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003624log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003625{
3626 static FILE *fd_tr = NULL;
3627 static proftime_T start;
3628 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003629 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003630
3631 if (fd_tr == NULL)
3632 {
3633 fd_tr = fopen("termresponse.log", "w");
3634 profile_start(&start);
3635 }
3636 now = start;
3637 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003638 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3639 must_redraw == NOT_VALID ? "NV"
3640 : must_redraw == CLEAR ? "CL" : " ");
3641 va_start(ap, fmt);
3642 vfprintf(fd_tr, fmt, ap);
3643 va_end(ap);
3644 fputc('\n', fd_tr);
3645 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003646}
3647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#endif
3649
3650/*
3651 * Return TRUE when saving and restoring the screen.
3652 */
3653 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003654swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 return (full_screen && *T_TI != NUL);
3657}
3658
Bram Moolenaarecadf432018-03-20 11:17:04 +01003659#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660/*
3661 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3662 */
3663 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003664setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666# ifdef FEAT_MOUSE_TTY
3667 int checkfor;
3668# endif
3669
3670# ifdef FEAT_MOUSESHAPE
3671 update_mouseshape(-1);
3672# endif
3673
3674# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3675# ifdef FEAT_GUI
3676 /* In the GUI the mouse is always enabled. */
3677 if (gui.in_use)
3678 return;
3679# endif
3680 /* be quick when mouse is off */
3681 if (*p_mouse == NUL || has_mouse_termcode == 0)
3682 return;
3683
3684 /* don't switch mouse on when not in raw mode (Ex mode) */
3685 if (cur_tmode != TMODE_RAW)
3686 {
3687 mch_setmouse(FALSE);
3688 return;
3689 }
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 if (VIsual_active)
3692 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003693 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 checkfor = MOUSE_RETURN;
3695 else if (State & INSERT)
3696 checkfor = MOUSE_INSERT;
3697 else if (State & CMDLINE)
3698 checkfor = MOUSE_COMMAND;
3699 else if (State == CONFIRM || State == EXTERNCMD)
3700 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3701 else
3702 checkfor = MOUSE_NORMAL; /* assume normal mode */
3703
3704 if (mouse_has(checkfor))
3705 mch_setmouse(TRUE);
3706 else
3707 mch_setmouse(FALSE);
3708# endif
3709}
3710
3711/*
3712 * Return TRUE if
3713 * - "c" is in 'mouse', or
3714 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3715 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3716 * normal editing mode (not at hit-return message).
3717 */
3718 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003719mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
3721 char_u *p;
3722
3723 for (p = p_mouse; *p; ++p)
3724 switch (*p)
3725 {
3726 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3727 return TRUE;
3728 break;
3729 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3730 return TRUE;
3731 break;
3732 default: if (c == *p) return TRUE; break;
3733 }
3734 return FALSE;
3735}
3736
3737/*
3738 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3739 */
3740 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003741mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 return (p_mousem[0] == 'p');
3744}
3745#endif
3746
3747/*
3748 * By outputting the 'cursor very visible' termcap code, for some windowed
3749 * terminals this makes the screen scrolled to the correct position.
3750 * Used when starting Vim or returning from a shell.
3751 */
3752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003753scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003755 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003758 out_str(T_CVS);
3759 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761}
3762
3763static int cursor_is_off = FALSE;
3764
3765/*
3766 * Enable the cursor.
3767 */
3768 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003769cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 if (cursor_is_off)
3772 {
3773 out_str(T_VE);
3774 cursor_is_off = FALSE;
3775 }
3776}
3777
3778/*
3779 * Disable the cursor.
3780 */
3781 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003782cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003784 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003786 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 cursor_is_off = TRUE;
3788 }
3789}
3790
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003791#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003793 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003794 */
3795 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003796term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003797{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003798 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003799 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003800
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003801 /* Only do something when redrawing the screen and we can restore the
3802 * mode. */
3803 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003804 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003805# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003806 if (forced && initial_cursor_shape > 0)
3807 /* Restore to initial values. */
3808 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003809# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003810 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003811 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003812
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003813 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003814 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003815 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003816 {
3817 if (*T_CSR != NUL)
3818 p = T_CSR; /* Replace mode cursor */
3819 else
3820 p = T_CSI; /* fall back to Insert mode cursor */
3821 if (*p != NUL)
3822 {
3823 out_str(p);
3824 showing_mode = REPLACE;
3825 }
3826 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003828 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003829 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003830 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003831 {
3832 out_str(T_CSI); /* Insert mode cursor */
3833 showing_mode = INSERT;
3834 }
3835 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003836 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003837 {
3838 out_str(T_CEI); /* non-Insert mode cursor */
3839 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003840 }
3841}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003842
3843# if defined(FEAT_TERMINAL) || defined(PROTO)
3844 void
3845term_cursor_color(char_u *color)
3846{
3847 if (*T_CSC != NUL)
3848 {
3849 out_str(T_CSC); /* set cursor color start */
3850 out_str_nf(color);
3851 out_str(T_CEC); /* set cursor color end */
3852 out_flush();
3853 }
3854}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003855# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003856
Bram Moolenaar4db25542017-08-28 22:43:05 +02003857 int
3858blink_state_is_inverted()
3859{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003860#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003861 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3862 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003863#else
3864 return FALSE;
3865#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003866}
3867
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003868/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003869 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003870 */
3871 void
3872term_cursor_shape(int shape, int blink)
3873{
3874 if (*T_CSH != NUL)
3875 {
3876 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3877 out_flush();
3878 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003879 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003880 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003881 int do_blink = blink;
3882
3883 /* t_SH is empty: try setting just the blink state.
3884 * The blink flags are XORed together, if the initial blinking from
3885 * style and shape differs, we need to invert the flag here. */
3886 if (blink_state_is_inverted())
3887 do_blink = !blink;
3888
3889 if (do_blink && *T_VS != NUL)
3890 {
3891 out_str(T_VS);
3892 out_flush();
3893 }
3894 else if (!do_blink && *T_CVS != NUL)
3895 {
3896 out_str(T_CVS);
3897 out_flush();
3898 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003899 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003900}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003901#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003902
3903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 * Set scrolling region for window 'wp'.
3905 * The region starts 'off' lines from the start of the window.
3906 * Also set the vertical scroll region for a vertically split window. Always
3907 * the full width of the window, excluding the vertical separator.
3908 */
3909 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003910scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3913 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003915 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3916 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 screen_start(); /* don't know where cursor is now */
3918}
3919
3920/*
3921 * Reset scrolling region to the whole screen.
3922 */
3923 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003924scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (*T_CSV != NUL)
3928 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 screen_start(); /* don't know where cursor is now */
3930}
3931
3932
3933/*
3934 * List of terminal codes that are currently recognized.
3935 */
3936
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003937static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 char_u name[2]; /* termcap name of entry */
3940 char_u *code; /* terminal code (in allocated memory) */
3941 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003942 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943} *termcodes = NULL;
3944
3945static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3946static int tc_len = 0; /* current number of entries in termcodes[] */
3947
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003948static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003951clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
3953 while (tc_len > 0)
3954 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003955 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 tc_max_len = 0;
3957
3958#ifdef HAVE_TGETENT
3959 BC = (char *)empty_option;
3960 UP = (char *)empty_option;
3961 PC = NUL; /* set pad character to NUL */
3962 ospeed = 0;
3963#endif
3964
3965 need_gather = TRUE; /* need to fill termleader[] */
3966}
3967
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968#define ATC_FROM_TERM 55
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970/*
3971 * Add a new entry to the list of terminal codes.
3972 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003973 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3974 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
3976 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003977add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 struct termcode *new_tc;
3980 int i, j;
3981 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003982 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 if (string == NULL || *string == NUL)
3985 {
3986 del_termcode(name);
3987 return;
3988 }
3989
Bram Moolenaar45500912014-07-09 20:51:07 +02003990#if defined(WIN3264) && !defined(FEAT_GUI)
3991 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (s == NULL)
3996 return;
3997
3998 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004001 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 s[0] = term_7to8bit(string);
4003 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004004
4005#if defined(WIN3264) && !defined(FEAT_GUI)
4006 if (s[0] == K_NUL)
4007 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004008 STRMOVE(s + 1, s);
4009 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02004010 }
4011#endif
4012
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004013 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 need_gather = TRUE; /* need to fill termleader[] */
4016
4017 /*
4018 * need to make space for more entries
4019 */
4020 if (tc_len == tc_max_len)
4021 {
4022 tc_max_len += 20;
4023 new_tc = (struct termcode *)alloc(
4024 (unsigned)(tc_max_len * sizeof(struct termcode)));
4025 if (new_tc == NULL)
4026 {
4027 tc_max_len -= 20;
4028 return;
4029 }
4030 for (i = 0; i < tc_len; ++i)
4031 new_tc[i] = termcodes[i];
4032 vim_free(termcodes);
4033 termcodes = new_tc;
4034 }
4035
4036 /*
4037 * Look for existing entry with the same name, it is replaced.
4038 * Look for an existing entry that is alphabetical higher, the new entry
4039 * is inserted in front of it.
4040 */
4041 for (i = 0; i < tc_len; ++i)
4042 {
4043 if (termcodes[i].name[0] < name[0])
4044 continue;
4045 if (termcodes[i].name[0] == name[0])
4046 {
4047 if (termcodes[i].name[1] < name[1])
4048 continue;
4049 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 */
4052 if (termcodes[i].name[1] == name[1])
4053 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004054 if (flags == ATC_FROM_TERM && (j = termcode_star(
4055 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004056 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057 /* Don't replace ESC[123;*X or ESC O*X with another when
4058 * invoked from got_code_from_term(). */
4059 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004060 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 && s[len - 1]
4062 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004063 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004064 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065 vim_free(s);
4066 return;
4067 }
4068 }
4069 else
4070 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004072 vim_free(termcodes[i].code);
4073 --tc_len;
4074 break;
4075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 }
4078 /*
4079 * Found alphabetical larger entry, move rest to insert new entry
4080 */
4081 for (j = tc_len; j > i; --j)
4082 termcodes[j] = termcodes[j - 1];
4083 break;
4084 }
4085
4086 termcodes[i].name[0] = name[0];
4087 termcodes[i].name[1] = name[1];
4088 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004089 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090
4091 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4092 * accept modifiers. */
4093 termcodes[i].modlen = 0;
4094 j = termcode_star(s, len);
4095 if (j > 0)
4096 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ++tc_len;
4098}
4099
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004101 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004102 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004103 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004104 */
4105 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004106termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107{
4108 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4109 if (len >= 3 && code[len - 2] == '*')
4110 {
4111 if (len >= 5 && code[len - 3] == ';')
4112 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004113 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004114 return 1;
4115 }
4116 return 0;
4117}
4118
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004120find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121{
4122 int i;
4123
4124 for (i = 0; i < tc_len; ++i)
4125 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4126 return termcodes[i].code;
4127 return NULL;
4128}
4129
4130#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4131 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004132get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 if (i >= tc_len)
4135 return NULL;
4136 return &termcodes[i].name[0];
4137}
4138#endif
4139
4140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004141del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 int i;
4144
4145 if (termcodes == NULL) /* nothing there yet */
4146 return;
4147
4148 need_gather = TRUE; /* need to fill termleader[] */
4149
4150 for (i = 0; i < tc_len; ++i)
4151 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4152 {
4153 del_termcode_idx(i);
4154 return;
4155 }
4156 /* not found. Give error message? */
4157}
4158
4159 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004160del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
4162 int i;
4163
4164 vim_free(termcodes[idx].code);
4165 --tc_len;
4166 for (i = idx; i < tc_len; ++i)
4167 termcodes[i] = termcodes[i + 1];
4168}
4169
4170#ifdef FEAT_TERMRESPONSE
4171/*
4172 * Called when detected that the terminal sends 8-bit codes.
4173 * Convert all 7-bit codes to their 8-bit equivalent.
4174 */
4175 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004176switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 int i;
4179 int c;
4180
4181 /* Only need to do something when not already using 8-bit codes. */
4182 if (!term_is_8bit(T_NAME))
4183 {
4184 for (i = 0; i < tc_len; ++i)
4185 {
4186 c = term_7to8bit(termcodes[i].code);
4187 if (c != 0)
4188 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004189 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 termcodes[i].code[0] = c;
4191 }
4192 }
4193 need_gather = TRUE; /* need to fill termleader[] */
4194 }
4195 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004196 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197}
4198#endif
4199
4200#ifdef CHECK_DOUBLE_CLICK
4201static linenr_T orig_topline = 0;
4202# ifdef FEAT_DIFF
4203static int orig_topfill = 0;
4204# endif
4205#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004206#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207/*
4208 * Checking for double clicks ourselves.
4209 * "orig_topline" is used to avoid detecting a double-click when the window
4210 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4211 */
4212/*
4213 * Set orig_topline. Used when jumping to another window, so that a double
4214 * click still works.
4215 */
4216 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004217set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219 orig_topline = wp->w_topline;
4220# ifdef FEAT_DIFF
4221 orig_topfill = wp->w_topfill;
4222# endif
4223}
4224#endif
4225
4226/*
4227 * Check if typebuf.tb_buf[] contains a terminal key code.
4228 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4229 * + max_offset].
4230 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004231 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 * With a match, the match is removed, the replacement code is inserted in
4233 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4234 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004235 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4236 * "buflen" is then the length of the string in buf[] and is updated for
4237 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
4239 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004240check_termcode(
4241 int max_offset,
4242 char_u *buf,
4243 int bufsize,
4244 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 char_u *tp;
4247 char_u *p;
4248 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004249 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004251 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 int offset;
4253 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004254 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004255 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int new_slen;
4258 int extra;
4259 char_u string[MAX_KEY_CODE_LEN + 1];
4260 int i, j;
4261 int idx = 0;
4262#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004263# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4264 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 char_u bytes[6];
4266 int num_bytes;
4267# endif
4268 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int is_click, is_drag;
4270 int wheel_code = 0;
4271 int current_button;
4272 static int held_button = MOUSE_RELEASE;
4273 static int orig_num_clicks = 1;
4274 static int orig_mouse_code = 0x0;
4275# ifdef CHECK_DOUBLE_CLICK
4276 static int orig_mouse_col = 0;
4277 static int orig_mouse_row = 0;
4278 static struct timeval orig_mouse_time = {0, 0};
4279 /* time of previous mouse click */
4280 struct timeval mouse_time; /* time of current mouse click */
4281 long timediff; /* elapsed time in msec */
4282# endif
4283#endif
4284 int cpo_koffset;
4285#ifdef FEAT_MOUSE_GPM
4286 extern int gpm_flag; /* gpm library variable */
4287#endif
4288
4289 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4290
4291 /*
4292 * Speed up the checks for terminal codes by gathering all first bytes
4293 * used in termleader[]. Often this is just a single <Esc>.
4294 */
4295 if (need_gather)
4296 gather_termleader();
4297
4298 /*
4299 * Check at several positions in typebuf.tb_buf[], to catch something like
4300 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4301 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004302 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 * This is used often, KEEP IT FAST!
4304 */
4305 for (offset = 0; offset < max_offset; ++offset)
4306 {
4307 if (buf == NULL)
4308 {
4309 if (offset >= typebuf.tb_len)
4310 break;
4311 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4312 len = typebuf.tb_len - offset; /* length of the input */
4313 }
4314 else
4315 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004316 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 break;
4318 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004319 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321
4322 /*
4323 * Don't check characters after K_SPECIAL, those are already
4324 * translated terminal chars (avoid translating ~@^Hx).
4325 */
4326 if (*tp == K_SPECIAL)
4327 {
4328 offset += 2; /* there are always 2 extra characters */
4329 continue;
4330 }
4331
4332 /*
4333 * Skip this position if the character does not appear as the first
4334 * character in term_strings. This speeds up a lot, since most
4335 * termcodes start with the same character (ESC or CSI).
4336 */
4337 i = *tp;
4338 for (p = termleader; *p && *p != i; ++p)
4339 ;
4340 if (*p == NUL)
4341 continue;
4342
4343 /*
4344 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4345 * are in Insert mode.
4346 */
4347 if (*tp == ESC && !p_ek && (State & INSERT))
4348 continue;
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004351 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354#ifdef FEAT_GUI
4355 if (gui.in_use)
4356 {
4357 /*
4358 * GUI special key codes are all of the form [CSI xx].
4359 */
4360 if (*tp == CSI) /* Special key from GUI */
4361 {
4362 if (len < 3)
4363 return -1; /* Shouldn't happen */
4364 slen = 3;
4365 key_name[0] = tp[1];
4366 key_name[1] = tp[2];
4367 }
4368 }
4369 else
4370#endif /* FEAT_GUI */
4371 {
4372 for (idx = 0; idx < tc_len; ++idx)
4373 {
4374 /*
4375 * Ignore the entry if we are not at the start of
4376 * typebuf.tb_buf[]
4377 * and there are not enough characters to make a match.
4378 * But only when the 'K' flag is in 'cpoptions'.
4379 */
4380 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004381 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (cpo_koffset && offset && len < slen)
4383 continue;
4384 if (STRNCMP(termcodes[idx].code, tp,
4385 (size_t)(slen > len ? len : slen)) == 0)
4386 {
4387 if (len < slen) /* got a partial sequence */
4388 return -1; /* need to get more chars */
4389
4390 /*
4391 * When found a keypad key, check if there is another key
4392 * that matches and use that one. This makes <Home> to be
4393 * found instead of <kHome> when they produce the same
4394 * key code.
4395 */
4396 if (termcodes[idx].name[0] == 'K'
4397 && VIM_ISDIGIT(termcodes[idx].name[1]))
4398 {
4399 for (j = idx + 1; j < tc_len; ++j)
4400 if (termcodes[j].len == slen &&
4401 STRNCMP(termcodes[idx].code,
4402 termcodes[j].code, slen) == 0)
4403 {
4404 idx = j;
4405 break;
4406 }
4407 }
4408
4409 key_name[0] = termcodes[idx].name[0];
4410 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 break;
4412 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004413
4414 /*
4415 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004416 * <Esc>[123;*X (modslen == slen - 3)
4417 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4418 * When there is a modifier the * matches a number.
4419 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004420 */
4421 if (termcodes[idx].modlen > 0)
4422 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 modslen = termcodes[idx].modlen;
4424 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004425 continue;
4426 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004427 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004428 {
4429 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004430
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004431 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004432 return -1; /* need to get more chars */
4433
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004434 if (tp[modslen] == termcodes[idx].code[slen - 1])
4435 slen = modslen + 1; /* no modifiers */
4436 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004437 continue; /* no match */
4438 else
4439 {
4440 /* Skip over the digits, the final char must
4441 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004442 for (j = slen - 2; j < len && (isdigit(tp[j])
4443 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004444 ;
4445 ++j;
4446 if (len < j) /* got a partial sequence */
4447 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004448 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4449 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004450
Bram Moolenaara529ce02017-06-22 22:37:57 +02004451 modifiers_start = tp + slen - 2;
4452
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004453 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004454 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004455 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004457 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004458 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004459 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004461 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004463
4464 slen = j;
4465 }
4466 key_name[0] = termcodes[idx].name[0];
4467 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004468 break;
4469 }
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
4473
4474#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004475 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004476 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004477 * detecting the start of these mouse codes they might as well be
4478 * another key code or terminal response. */
4479# ifdef FEAT_MOUSE_DEC
4480 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004481# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004482# ifdef FEAT_MOUSE_PTERM
4483 || key_name[0] == KS_PTERM_MOUSE
4484# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004487 /* Check for some responses from the terminal starting with
4488 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004489 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004490 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004491 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004492 * Also eat other possible responses to t_RV, rxvt returns
4493 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4494 * mrxvt has been reported to have "+" in the version. Assume
4495 * the escape sequence ends with a letter or one of "{|}~".
4496 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004497 * - Cursor position report: <Esc>[{row};{col}R
4498 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004499 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004500 *
4501 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004502 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004503 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004504
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004505 if ((*T_CRV != NUL || *T_U7 != NUL || did_request_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004506 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004507 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004508 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004510 int col = 0;
4511 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004512#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004513 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004514#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004517 for (i = 2 + (tp[0] != CSI); i < len
4518 && !(tp[i] >= '{' && tp[i] <= '~')
4519 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004520 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004521 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004522 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004523#ifdef FEAT_MBYTE
4524 row_char = tp[i - 1];
4525#endif
4526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004528 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004529 LOG_TR(("Not enough characters for CRV"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02004530 return -1;
4531 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004532 if (extra > 0)
4533 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004534
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004535#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004536 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4537 * u7_status is not "sent", it may be from a previous Vim that
4538 * just exited. But not for <S-F3>, it sends something
4539 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004540 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004541 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004542 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004543 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004544 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004545
Bram Moolenaarb255b902018-04-24 21:40:10 +02004546 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004547 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004548 did_cursorhold = TRUE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004549 if (col == 2)
4550 aw = "single";
4551 else if (col == 3)
4552 aw = "double";
4553 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4554 {
4555 /* Setting the option causes a screen redraw. Do
4556 * that right away if possible, keeping any
4557 * messages. */
4558 set_option_value((char_u *)"ambw", 0L,
4559 (char_u *)aw, 0);
4560# ifdef DEBUG_TERMRESPONSE
4561 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004562 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004563
Bram Moolenaarb255b902018-04-24 21:40:10 +02004564 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004565 }
4566# else
4567 redraw_asap(CLEAR);
4568# endif
4569 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004570 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004571 key_name[0] = (int)KS_EXTRA;
4572 key_name[1] = (int)KE_IGNORE;
4573 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004574# ifdef FEAT_EVAL
4575 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4576# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004577 }
4578 else
4579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004581 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004583 int version = col;
4584
Bram Moolenaarb255b902018-04-24 21:40:10 +02004585 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004586 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004587 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 /* If this code starts with CSI, you can bet that the
4590 * terminal uses 8-bit codes. */
4591 if (tp[0] == CSI)
4592 switch_to_8bit();
4593
4594 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004595 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 * Ignore it for when the user has set 'term' to xterm,
4597 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004598 if (version > 20000)
4599 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004601 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004603 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004604# ifdef FEAT_MOUSE_SGR
4605 int is_iterm2 = FALSE;
4606# endif
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004609 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004611 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 check_for_codes = TRUE;
4613 need_gather = TRUE;
4614 req_codes_from_term();
4615 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004616
4617 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004618 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004619 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004620 {
4621 /* If run from Vim $COLORS is set to the number of
4622 * colors the terminal supports. Otherwise assume
4623 * 256, libvterm supports even more. */
4624 if (mch_getenv((char_u *)"COLORS") == NULL)
4625 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004626# ifdef FEAT_MOUSE_SGR
4627 /* Libvterm can handle SGR mouse reporting. */
4628 if (!option_was_set((char_u *)"ttym"))
4629 set_option_value((char_u *)"ttym", 0L,
4630 (char_u *)"sgr", 0);
4631# endif
4632 }
4633
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004634 if (version == 95)
4635 {
4636 /* Mac Terminal.app sends 1;95;0 */
4637 if (STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
4638 {
4639 is_not_xterm = TRUE;
4640 is_mac_terminal = TRUE;
4641 }
4642# ifdef FEAT_MOUSE_SGR
Bram Moolenaar05684312017-12-09 15:11:24 +01004643 /* iTerm2 sends 0;95;0 */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004644 if (STRNCMP(tp + extra - 2, "0;95;0c", 7) == 0)
4645 is_iterm2 = TRUE;
4646# endif
4647 }
4648
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004649 /* Only set 'ttymouse' automatically if it was not set
4650 * by the user already. */
4651 if (!option_was_set((char_u *)"ttym"))
4652 {
4653# ifdef FEAT_MOUSE_SGR
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004654 /* Xterm version 277 supports SGR. Also support
Bram Moolenaar05684312017-12-09 15:11:24 +01004655 * Terminal.app and iTerm2. */
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004656 if (version >= 277 || is_iterm2 || is_mac_terminal)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004657 set_option_value((char_u *)"ttym", 0L,
4658 (char_u *)"sgr", 0);
4659 else
4660# endif
4661 /* if xterm version >= 95 use mouse dragging */
4662 if (version >= 95)
4663 set_option_value((char_u *)"ttym", 0L,
4664 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004665 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004666
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004667 /* Detect terminals that set $TERM to something like
4668 * "xterm-256colors" but are not fully xterm
4669 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004670
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004671 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004672 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004673 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004674 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004675 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004676 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004677 is_not_xterm = TRUE;
4678
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004679 /* PuTTY sends 0;136;0
4680 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004681 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004682 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004683 is_not_xterm = TRUE;
4684
4685 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004686 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004687 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004688 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004689
4690 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004691 * set. Only supported properly by xterm since version
4692 * 279 (otherwise it returns 0x18).
4693 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004694 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004695 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004696 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004697 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004698 && *T_CSH != NUL
4699 && *T_CRS != NUL)
4700 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004701 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004702 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004703 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004704 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004705 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004706
4707 /* Only request the cursor blink mode if t_RC set. Not
4708 * for Gnome terminal, it can't handle t_RC, it
4709 * echoes the characters to the screen. */
4710 if (rbm_status == STATUS_GET
4711 && !is_not_xterm
4712 && *T_CRC != NUL)
4713 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004714 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004715 out_str(T_CRC);
4716 rbm_status = STATUS_SENT;
4717 need_flush = TRUE;
4718 }
4719
4720 if (need_flush)
4721 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004723 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004725 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 apply_autocmds(EVENT_TERMRESPONSE,
4728 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 key_name[0] = (int)KS_EXTRA;
4730 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004732
Bram Moolenaar4db25542017-08-28 22:43:05 +02004733 /* Check blinking cursor from xterm:
4734 * {lead}?12;1$y set
4735 * {lead}?12;2$y not set
4736 *
4737 * {lead} can be <Esc>[ or CSI
4738 */
4739 else if (rbm_status == STATUS_SENT
4740 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4741 && i == j + 6
4742 && tp[j + 1] == '1'
4743 && tp[j + 2] == '2'
4744 && tp[j + 3] == ';'
4745 && tp[i - 1] == '$'
4746 && tp[i] == 'y')
4747 {
4748 initial_cursor_blink = (tp[j + 4] == '1');
4749 rbm_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004750 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004751 key_name[0] = (int)KS_EXTRA;
4752 key_name[1] = (int)KE_IGNORE;
4753 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004754# ifdef FEAT_EVAL
4755 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4756# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004757 }
4758
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004759 /*
4760 * Check for a window position response from the terminal:
4761 * {lead}3;{x}:{y}t
4762 */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004763 else if (did_request_winpos
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004764 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4765 || (len >= 3 && tp[0] == CSI))
4766 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4767 && tp[j + 1] == ';')
4768 {
4769 j += 2;
4770 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4771 ;
4772 if (i < len && tp[i] == ';')
4773 {
4774 winpos_x = atoi((char *)tp + j);
4775 j = i + 1;
4776 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4777 ;
4778 if (i < len && tp[i] == 't')
4779 {
4780 winpos_y = atoi((char *)tp + j);
4781 /* got finished code: consume it */
4782 key_name[0] = (int)KS_EXTRA;
4783 key_name[1] = (int)KE_IGNORE;
4784 slen = i + 1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004785
4786 if (--did_request_winpos <= 0)
4787 winpos_status = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004788 }
4789 }
4790 if (i == len)
4791 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004792 LOG_TR(("not enough characters for winpos"));
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004793 return -1;
4794 }
4795 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004796 }
4797
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004798 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004799 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004800 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004801 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004802 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004803 * {lead} can be <Esc>] or OSC
4804 * {tail} can be '\007', <Esc>\ or STERM.
4805 *
4806 * Consume any code that starts with "{lead}11;", it's also
4807 * possible that "rgba" is following.
4808 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004809 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004810 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4811 || tp[0] == OSC))
4812 {
4813 j = 1 + (tp[0] == ESC);
4814 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004815 || (argp[1] != '1' && argp[1] != '0')
4816 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004817 i = 0; /* no match */
4818 else
4819 for (i = j; i < len; ++i)
4820 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4821 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004822 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004823 int is_bg = argp[1] == '1';
4824
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004825 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004826 && tp[j + 11] == '/' && tp[j + 16] == '/')
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004827 {
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004828#ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004829 int rval = hexhex2nr(tp + j + 7);
4830 int gval = hexhex2nr(tp + j + 12);
4831 int bval = hexhex2nr(tp + j + 17);
Bram Moolenaar53ec7952017-11-05 21:24:23 +01004832#endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004833 if (is_bg)
4834 {
4835 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004836 + tp[j+17]) ? "light" : "dark";
4837
Bram Moolenaarb255b902018-04-24 21:40:10 +02004838 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004839 rbg_status = STATUS_GOT;
4840#ifdef FEAT_TERMINAL
4841 bg_r = rval;
4842 bg_g = gval;
4843 bg_b = bval;
4844#endif
4845 if (!option_was_set((char_u *)"bg")
4846 && STRCMP(p_bg, newval) != 0)
4847 {
4848 /* value differs, apply it */
4849 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004850 (char_u *)newval, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004851 reset_option_was_set((char_u *)"bg");
4852 redraw_asap(CLEAR);
4853 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004854 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004855#ifdef FEAT_TERMINAL
4856 else
4857 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004858 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004859 rfg_status = STATUS_GOT;
4860 fg_r = rval;
4861 fg_g = gval;
4862 fg_b = bval;
4863 }
4864#endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004865 }
4866
4867 /* got finished code: consume it */
4868 key_name[0] = (int)KS_EXTRA;
4869 key_name[1] = (int)KE_IGNORE;
4870 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004871# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004872 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4873 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004874# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004875 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004876 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004877 if (i == len)
4878 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004879 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004880 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004884 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004885 * {lead}{flag}+r<hex bytes><{tail}
4886 *
4887 * {lead} can be <Esc>P or DCS
4888 * {flag} can be '0' or '1'
4889 * {tail} can be Esc>\ or STERM
4890 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004891 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004892 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004893 *
4894 * {lead} can be <Esc>P or DCS
4895 * {tail} can be Esc>\ or STERM
4896 *
4897 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004898 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004899 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004900 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 || tp[0] == DCS))
4902 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004903 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004904 if (len < j + 3)
4905 i = len; /* need more chars */
4906 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004907 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004908 else if (argp[1] == '+')
4909 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004910 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004911 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004912 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 || tp[i] == STERM)
4914 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004915 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 got_code_from_term(tp + j, i);
4917 key_name[0] = (int)KS_EXTRA;
4918 key_name[1] = (int)KE_IGNORE;
4919 slen = i + 1 + (tp[i] == ESC);
4920 break;
4921 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004922 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004923 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004924 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004925 /* Probably the cursor shape response. Make sure that "i"
4926 * is equal to "len" when there are not sufficient
4927 * characters. */
4928 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004929 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004930 if (i - j == 3 && !isdigit(tp[i]))
4931 break;
4932 if (i - j == 4 && tp[i] != ' ')
4933 break;
4934 if (i - j == 5 && tp[i] != 'q')
4935 break;
4936 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4937 break;
4938 if ((i - j == 6 && tp[i] == STERM)
4939 || (i - j == 7 && tp[i] == '\\'))
4940 {
4941 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004942
Bram Moolenaar590ec872018-03-02 20:58:42 +01004943 /* 0, 1 = block blink, 2 = block
4944 * 3 = underline blink, 4 = underline
4945 * 5 = vertical bar blink, 6 = vertical bar */
4946 number = number == 0 ? 1 : number;
4947 initial_cursor_shape = (number + 1) / 2;
4948 /* The blink flag is actually inverted, compared to
4949 * the value set with T_SH. */
4950 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004951 (number & 1) ? FALSE : TRUE;
Bram Moolenaar590ec872018-03-02 20:58:42 +01004952 rcs_status = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004953 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004954
Bram Moolenaar590ec872018-03-02 20:58:42 +01004955 key_name[0] = (int)KS_EXTRA;
4956 key_name[1] = (int)KE_IGNORE;
4957 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004958# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004959 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004960# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004961 break;
4962 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004963 }
4964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004967 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004968 /* These codes arrive many together, each code can be
4969 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02004970 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004971 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975#endif
4976
4977 if (key_name[0] == NUL)
4978 continue; /* No match at this position, try next one */
4979
4980 /* We only get here when we have a complete termcode match */
4981
4982#ifdef FEAT_MOUSE
4983# ifdef FEAT_GUI
4984 /*
4985 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4986 * so that we know which window to scroll later.
4987 */
4988 if (gui.in_use
4989 && key_name[0] == (int)KS_EXTRA
4990 && (key_name[1] == (int)KE_X1MOUSE
4991 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004992 || key_name[1] == (int)KE_MOUSELEFT
4993 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 || key_name[1] == (int)KE_MOUSEDOWN
4995 || key_name[1] == (int)KE_MOUSEUP))
4996 {
4997 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4998 if (num_bytes == -1) /* not enough coordinates */
4999 return -1;
5000 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5001 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5002 slen += num_bytes;
5003 }
5004 else
5005# endif
5006 /*
5007 * If it is a mouse click, get the coordinates.
5008 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005009 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005011 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012# endif
5013# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005014 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015# endif
5016# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005017 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018# endif
5019# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005020 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005022# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005023 || key_name[0] == KS_URXVT_MOUSE
5024# endif
5025# ifdef FEAT_MOUSE_SGR
5026 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005027 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 )
5030 {
5031 is_click = is_drag = FALSE;
5032
Bram Moolenaar864207d2008-06-24 22:14:38 +00005033# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
5034 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (key_name[0] == (int)KS_MOUSE)
5036 {
5037 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005038 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 * s == encoded button state:
5040 * 0x20 = left button down
5041 * 0x21 = middle button down
5042 * 0x22 = right button down
5043 * 0x23 = any button release
5044 * 0x60 = button 4 down (scroll wheel down)
5045 * 0x61 = button 5 down (scroll wheel up)
5046 * add 0x04 for SHIFT
5047 * add 0x08 for ALT
5048 * add 0x10 for CTRL
5049 * add 0x20 for mouse drag (0x40 is drag with left button)
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005050 * add 0x40 for mouse move (0x80 is move, 0x81 too)
5051 * 0x43 (drag + release) is also move
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * c == column + ' ' + 1 == column + 33
5053 * r == row + ' ' + 1 == row + 33
5054 *
5055 * The coordinates are passed on through global variables.
5056 * Ugly, but this avoids trouble with mouse clicks at an
5057 * unexpected moment and allows for mapping them.
5058 */
5059 for (;;)
5060 {
5061#ifdef FEAT_GUI
5062 if (gui.in_use)
5063 {
5064 /* GUI uses more bits for columns > 223 */
5065 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
5066 if (num_bytes == -1) /* not enough coordinates */
5067 return -1;
5068 mouse_code = bytes[0];
5069 mouse_col = 128 * (bytes[1] - ' ' - 1)
5070 + bytes[2] - ' ' - 1;
5071 mouse_row = 128 * (bytes[3] - ' ' - 1)
5072 + bytes[4] - ' ' - 1;
5073 }
5074 else
5075#endif
5076 {
5077 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
5078 if (num_bytes == -1) /* not enough coordinates */
5079 return -1;
5080 mouse_code = bytes[0];
5081 mouse_col = bytes[1] - ' ' - 1;
5082 mouse_row = bytes[2] - ' ' - 1;
5083 }
5084 slen += num_bytes;
5085
5086 /* If the following bytes is also a mouse code and it has
5087 * the same code, dump this one and get the next. This
5088 * makes dragging a whole lot faster. */
5089#ifdef FEAT_GUI
5090 if (gui.in_use)
5091 j = 3;
5092 else
5093#endif
5094 j = termcodes[idx].len;
5095 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
5096 && tp[slen + j] == mouse_code
5097 && tp[slen + j + 1] != NUL
5098 && tp[slen + j + 2] != NUL
5099#ifdef FEAT_GUI
5100 && (!gui.in_use
5101 || (tp[slen + j + 3] != NUL
5102 && tp[slen + j + 4] != NUL))
5103#endif
5104 )
5105 slen += j;
5106 else
5107 break;
5108 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005111# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
5112 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005113 || key_name[0] == KS_SGR_MOUSE
5114 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005115 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005116 /* URXVT 1015 mouse reporting mode:
5117 * Almost identical to xterm mouse mode, except the values
5118 * are decimal instead of bytes.
5119 *
5120 * \033[%d;%d;%dM
5121 * ^-- row
5122 * ^----- column
5123 * ^-------- code
5124 *
5125 * SGR 1006 mouse reporting mode:
5126 * Almost identical to xterm mouse mode, except the values
5127 * are decimal instead of bytes.
5128 *
5129 * \033[<%d;%d;%dM
5130 * ^-- row
5131 * ^----- column
5132 * ^-------- code
5133 *
5134 * \033[<%d;%d;%dm : mouse release event
5135 * ^-- row
5136 * ^----- column
5137 * ^-------- code
5138 */
5139 p = modifiers_start;
5140 if (p == NULL)
5141 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005142
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005143 mouse_code = getdigits(&p);
5144 if (*p++ != ';')
5145 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005146
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005147 /* when mouse reporting is SGR, add 32 to mouse code */
5148 if (key_name[0] == KS_SGR_MOUSE
5149 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5150 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005151
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005152 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5153 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005154
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005155 mouse_col = getdigits(&p) - 1;
5156 if (*p++ != ';')
5157 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005158
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005159 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005160
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005161 /* The modifiers were the mouse coordinates, not the
5162 * modifier keys (alt/shift/ctrl/meta) state. */
5163 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005164 }
5165# endif
5166
5167 if (key_name[0] == (int)KS_MOUSE
5168#ifdef FEAT_MOUSE_URXVT
5169 || key_name[0] == (int)KS_URXVT_MOUSE
5170#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005171#ifdef FEAT_MOUSE_SGR
5172 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005173 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005174#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005175 )
5176 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005177# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 /*
5179 * Handle mouse events.
5180 * Recognize the xterm mouse wheel, but not in the GUI, the
5181 * Linux console with GPM and the MS-DOS or Win32 console
5182 * (multi-clicks use >= 0x60).
5183 */
5184 if (mouse_code >= MOUSEWHEEL_LOW
5185# ifdef FEAT_GUI
5186 && !gui.in_use
5187# endif
5188# ifdef FEAT_MOUSE_GPM
5189 && gpm_flag == 0
5190# endif
5191 )
5192 {
Bram Moolenaarbb160a12017-11-20 21:52:24 +01005193# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5194 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
5195 /* mouse-move event, using MOUSE_DRAG works */
5196 mouse_code = MOUSE_DRAG;
5197 else
5198# endif
5199 /* Keep the mouse_code before it's changed, so that we
5200 * remember that it was a mouse wheel click. */
5201 wheel_code = mouse_code;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203# ifdef FEAT_MOUSE_XTERM
5204 else if (held_button == MOUSE_RELEASE
5205# ifdef FEAT_GUI
5206 && !gui.in_use
5207# endif
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005208 && (mouse_code == 0x23 || mouse_code == 0x24
5209 || mouse_code == 0x40 || mouse_code == 0x41))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar1f8495c2018-04-04 21:53:11 +02005211 /* Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
5212 * And 0x40 and 0x41 are used by some xterm emulator. */
5213 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
5214 + MOUSEWHEEL_LOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216# endif
5217
5218# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5219 else if (use_xterm_mouse() > 1)
5220 {
5221 if (mouse_code & MOUSE_DRAG_XTERM)
5222 mouse_code |= MOUSE_DRAG;
5223 }
5224# endif
5225# ifdef FEAT_XCLIPBOARD
5226 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5227 {
5228 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5229 stop_xterm_trace();
5230 else
5231 start_xterm_trace(mouse_code);
5232 }
5233# endif
5234# endif
5235 }
5236# endif /* !UNIX || FEAT_MOUSE_XTERM */
5237# ifdef FEAT_MOUSE_NET
5238 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5239 {
5240 int mc, mr;
5241
5242 /* expect a rather limited sequence like: balancing {
5243 * \033}6,45\r
5244 * '6' is the row, 45 is the column
5245 */
5246 p = tp + slen;
5247 mr = getdigits(&p);
5248 if (*p++ != ',')
5249 return -1;
5250 mc = getdigits(&p);
5251 if (*p++ != '\r')
5252 return -1;
5253
5254 mouse_col = mc - 1;
5255 mouse_row = mr - 1;
5256 mouse_code = MOUSE_LEFT;
5257 slen += (int)(p - (tp + slen));
5258 }
5259# endif /* FEAT_MOUSE_NET */
5260# ifdef FEAT_MOUSE_JSB
5261 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5262 {
5263 int mult, val, iter, button, status;
5264
5265 /* JSBTERM Input Model
5266 * \033[0~zw uniq escape sequence
5267 * (L-x) Left button pressed - not pressed x not reporting
5268 * (M-x) Middle button pressed - not pressed x not reporting
5269 * (R-x) Right button pressed - not pressed x not reporting
5270 * (SDmdu) Single , Double click, m mouse move d button down
5271 * u button up
5272 * ### X cursor position padded to 3 digits
5273 * ### Y cursor position padded to 3 digits
5274 * (s-x) SHIFT key pressed - not pressed x not reporting
5275 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005276 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 */
5278
5279 p = tp + slen;
5280 button = mouse_code = 0;
5281 switch (*p++)
5282 {
5283 case 'L': button = 1; break;
5284 case '-': break;
5285 case 'x': break; /* ignore sequence */
5286 default: return -1; /* Unknown Result */
5287 }
5288 switch (*p++)
5289 {
5290 case 'M': button |= 2; break;
5291 case '-': break;
5292 case 'x': break; /* ignore sequence */
5293 default: return -1; /* Unknown Result */
5294 }
5295 switch (*p++)
5296 {
5297 case 'R': button |= 4; break;
5298 case '-': break;
5299 case 'x': break; /* ignore sequence */
5300 default: return -1; /* Unknown Result */
5301 }
5302 status = *p++;
5303 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5304 mult /= 10, p++)
5305 if (*p >= '0' && *p <= '9')
5306 val += (*p - '0') * mult;
5307 else
5308 return -1;
5309 mouse_col = val;
5310 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5311 mult /= 10, p++)
5312 if (*p >= '0' && *p <= '9')
5313 val += (*p - '0') * mult;
5314 else
5315 return -1;
5316 mouse_row = val;
5317 switch (*p++)
5318 {
5319 case 's': button |= 8; break; /* SHIFT key Pressed */
5320 case '-': break; /* Not Pressed */
5321 case 'x': break; /* Not Reporting */
5322 default: return -1; /* Unknown Result */
5323 }
5324 switch (*p++)
5325 {
5326 case 'c': button |= 16; break; /* CTRL key Pressed */
5327 case '-': break; /* Not Pressed */
5328 case 'x': break; /* Not Reporting */
5329 default: return -1; /* Unknown Result */
5330 }
5331 if (*p++ != '\033')
5332 return -1;
5333 if (*p++ != '\\')
5334 return -1;
5335 switch (status)
5336 {
5337 case 'D': /* Double Click */
5338 case 'S': /* Single Click */
5339 if (button & 1) mouse_code |= MOUSE_LEFT;
5340 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5341 if (button & 4) mouse_code |= MOUSE_RIGHT;
5342 if (button & 8) mouse_code |= MOUSE_SHIFT;
5343 if (button & 16) mouse_code |= MOUSE_CTRL;
5344 break;
5345 case 'm': /* Mouse move */
5346 if (button & 1) mouse_code |= MOUSE_LEFT;
5347 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5348 if (button & 4) mouse_code |= MOUSE_RIGHT;
5349 if (button & 8) mouse_code |= MOUSE_SHIFT;
5350 if (button & 16) mouse_code |= MOUSE_CTRL;
5351 if ((button & 7) != 0)
5352 {
5353 held_button = mouse_code;
5354 mouse_code |= MOUSE_DRAG;
5355 }
5356 is_drag = TRUE;
5357 showmode();
5358 break;
5359 case 'd': /* Button Down */
5360 if (button & 1) mouse_code |= MOUSE_LEFT;
5361 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5362 if (button & 4) mouse_code |= MOUSE_RIGHT;
5363 if (button & 8) mouse_code |= MOUSE_SHIFT;
5364 if (button & 16) mouse_code |= MOUSE_CTRL;
5365 break;
5366 case 'u': /* Button Up */
5367 if (button & 1)
5368 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5369 if (button & 2)
5370 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5371 if (button & 4)
5372 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5373 if (button & 8)
5374 mouse_code |= MOUSE_SHIFT;
5375 if (button & 16)
5376 mouse_code |= MOUSE_CTRL;
5377 break;
5378 default: return -1; /* Unknown Result */
5379 }
5380
5381 slen += (p - (tp + slen));
5382 }
5383# endif /* FEAT_MOUSE_JSB */
5384# ifdef FEAT_MOUSE_DEC
5385 if (key_name[0] == (int)KS_DEC_MOUSE)
5386 {
5387 /* The DEC Locator Input Model
5388 * Netterm delivers the code sequence:
5389 * \033[2;4;24;80&w (left button down)
5390 * \033[3;0;24;80&w (left button up)
5391 * \033[6;1;24;80&w (right button down)
5392 * \033[7;0;24;80&w (right button up)
5393 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5394 * Pe is the event code
5395 * Pb is the button code
5396 * Pr is the row coordinate
5397 * Pc is the column coordinate
5398 * Pp is the third coordinate (page number)
5399 * Pe, the event code indicates what event caused this report
5400 * The following event codes are defined:
5401 * 0 - request, the terminal received an explicit request
5402 * for a locator report, but the locator is unavailable
5403 * 1 - request, the terminal received an explicit request
5404 * for a locator report
5405 * 2 - left button down
5406 * 3 - left button up
5407 * 4 - middle button down
5408 * 5 - middle button up
5409 * 6 - right button down
5410 * 7 - right button up
5411 * 8 - fourth button down
5412 * 9 - fourth button up
5413 * 10 - locator outside filter rectangle
5414 * Pb, the button code, ASCII decimal 0-15 indicating which
5415 * buttons are down if any. The state of the four buttons
5416 * on the locator correspond to the low four bits of the
5417 * decimal value,
5418 * "1" means button depressed
5419 * 0 - no buttons down,
5420 * 1 - right,
5421 * 2 - middle,
5422 * 4 - left,
5423 * 8 - fourth
5424 * Pr is the row coordinate of the locator position in the page,
5425 * encoded as an ASCII decimal value.
5426 * If Pr is omitted, the locator position is undefined
5427 * (outside the terminal window for example).
5428 * Pc is the column coordinate of the locator position in the
5429 * page, encoded as an ASCII decimal value.
5430 * If Pc is omitted, the locator position is undefined
5431 * (outside the terminal window for example).
5432 * Pp is the page coordinate of the locator position
5433 * encoded as an ASCII decimal value.
5434 * The page coordinate may be omitted if the locator is on
5435 * page one (the default). We ignore it anyway.
5436 */
5437 int Pe, Pb, Pr, Pc;
5438
5439 p = tp + slen;
5440
5441 /* get event status */
5442 Pe = getdigits(&p);
5443 if (*p++ != ';')
5444 return -1;
5445
5446 /* get button status */
5447 Pb = getdigits(&p);
5448 if (*p++ != ';')
5449 return -1;
5450
5451 /* get row status */
5452 Pr = getdigits(&p);
5453 if (*p++ != ';')
5454 return -1;
5455
5456 /* get column status */
5457 Pc = getdigits(&p);
5458
5459 /* the page parameter is optional */
5460 if (*p == ';')
5461 {
5462 p++;
5463 (void)getdigits(&p);
5464 }
5465 if (*p++ != '&')
5466 return -1;
5467 if (*p++ != 'w')
5468 return -1;
5469
5470 mouse_code = 0;
5471 switch (Pe)
5472 {
5473 case 0: return -1; /* position request while unavailable */
5474 case 1: /* a response to a locator position request includes
5475 the status of all buttons */
5476 Pb &= 7; /* mask off and ignore fourth button */
5477 if (Pb & 4)
5478 mouse_code = MOUSE_LEFT;
5479 if (Pb & 2)
5480 mouse_code = MOUSE_MIDDLE;
5481 if (Pb & 1)
5482 mouse_code = MOUSE_RIGHT;
5483 if (Pb)
5484 {
5485 held_button = mouse_code;
5486 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005487 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 is_drag = TRUE;
5490 showmode();
5491 break;
5492 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005493 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 break;
5495 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5496 break;
5497 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005498 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 break;
5500 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5501 break;
5502 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005503 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 break;
5505 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5506 break;
5507 case 8: return -1; /* fourth button down */
5508 case 9: return -1; /* fourth button up */
5509 case 10: return -1; /* mouse outside of filter rectangle */
5510 default: return -1; /* should never occur */
5511 }
5512
5513 mouse_col = Pc - 1;
5514 mouse_row = Pr - 1;
5515
5516 slen += (int)(p - (tp + slen));
5517 }
5518# endif /* FEAT_MOUSE_DEC */
5519# ifdef FEAT_MOUSE_PTERM
5520 if (key_name[0] == (int)KS_PTERM_MOUSE)
5521 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005522 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 p = tp + slen;
5525
5526 action = getdigits(&p);
5527 if (*p++ != ';')
5528 return -1;
5529
5530 mouse_row = getdigits(&p);
5531 if (*p++ != ';')
5532 return -1;
5533 mouse_col = getdigits(&p);
5534 if (*p++ != ';')
5535 return -1;
5536
5537 button = getdigits(&p);
5538 mouse_code = 0;
5539
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005540 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 case 4: mouse_code = MOUSE_LEFT; break;
5543 case 1: mouse_code = MOUSE_RIGHT; break;
5544 case 2: mouse_code = MOUSE_MIDDLE; break;
5545 default: return -1;
5546 }
5547
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005548 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 case 31: /* Initial press */
5551 if (*p++ != ';')
5552 return -1;
5553
5554 num_clicks = getdigits(&p); /* Not used */
5555 break;
5556
5557 case 32: /* Release */
5558 mouse_code |= MOUSE_RELEASE;
5559 break;
5560
5561 case 33: /* Drag */
5562 held_button = mouse_code;
5563 mouse_code |= MOUSE_DRAG;
5564 break;
5565
5566 default:
5567 return -1;
5568 }
5569
5570 if (*p++ != 't')
5571 return -1;
5572
5573 slen += (p - (tp + slen));
5574 }
5575# endif /* FEAT_MOUSE_PTERM */
5576
5577 /* Interpret the mouse code */
5578 current_button = (mouse_code & MOUSE_CLICK_MASK);
5579 if (current_button == MOUSE_RELEASE
5580# ifdef FEAT_MOUSE_XTERM
5581 && wheel_code == 0
5582# endif
5583 )
5584 {
5585 /*
5586 * If we get a mouse drag or release event when
5587 * there is no mouse button held down (held_button ==
5588 * MOUSE_RELEASE), produce a K_IGNORE below.
5589 * (can happen when you hold down two buttons
5590 * and then let them go, or click in the menu bar, but not
5591 * on a menu, and drag into the text).
5592 */
5593 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5594 is_drag = TRUE;
5595 current_button = held_button;
5596 }
5597 else if (wheel_code == 0)
5598 {
5599# ifdef CHECK_DOUBLE_CLICK
5600# ifdef FEAT_MOUSE_GPM
5601# ifdef FEAT_GUI
5602 /*
5603 * Only for Unix, when GUI or gpm is not active, we handle
5604 * multi-clicks here.
5605 */
5606 if (gpm_flag == 0 && !gui.in_use)
5607# else
5608 if (gpm_flag == 0)
5609# endif
5610# else
5611# ifdef FEAT_GUI
5612 if (!gui.in_use)
5613# endif
5614# endif
5615 {
5616 /*
5617 * Compute the time elapsed since the previous mouse click.
5618 */
5619 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005620 if (orig_mouse_time.tv_sec == 0)
5621 {
5622 /*
5623 * Avoid computing the difference between mouse_time
5624 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005625 * difference would be huge and would cause
5626 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005627 */
5628 timediff = p_mouset;
5629 }
5630 else
5631 {
5632 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005633 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005634 if (timediff < 0)
5635 --orig_mouse_time.tv_sec;
5636 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005637 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 orig_mouse_time = mouse_time;
5640 if (mouse_code == orig_mouse_code
5641 && timediff < p_mouset
5642 && orig_num_clicks != 4
5643 && orig_mouse_col == mouse_col
5644 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005645 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005647 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005649 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005650 /* Double click in tab pages line also works
5651 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005652 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005653 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 ++orig_num_clicks;
5655 else
5656 orig_num_clicks = 1;
5657 orig_mouse_col = mouse_col;
5658 orig_mouse_row = mouse_row;
5659 orig_topline = curwin->w_topline;
5660#ifdef FEAT_DIFF
5661 orig_topfill = curwin->w_topfill;
5662#endif
5663 }
5664# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5665 else
5666 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5667# endif
5668# else
5669 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5670# endif
5671 is_click = TRUE;
5672 orig_mouse_code = mouse_code;
5673 }
5674 if (!is_drag)
5675 held_button = mouse_code & MOUSE_CLICK_MASK;
5676
5677 /*
5678 * Translate the actual mouse event into a pseudo mouse event.
5679 * First work out what modifiers are to be used.
5680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (orig_mouse_code & MOUSE_SHIFT)
5682 modifiers |= MOD_MASK_SHIFT;
5683 if (orig_mouse_code & MOUSE_CTRL)
5684 modifiers |= MOD_MASK_CTRL;
5685 if (orig_mouse_code & MOUSE_ALT)
5686 modifiers |= MOD_MASK_ALT;
5687 if (orig_num_clicks == 2)
5688 modifiers |= MOD_MASK_2CLICK;
5689 else if (orig_num_clicks == 3)
5690 modifiers |= MOD_MASK_3CLICK;
5691 else if (orig_num_clicks == 4)
5692 modifiers |= MOD_MASK_4CLICK;
5693
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005694 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5695 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 key_name[0] = (int)KS_EXTRA;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005697 if (wheel_code != 0
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005698 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005699 {
5700 if (wheel_code & MOUSE_CTRL)
5701 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005702 if (wheel_code & MOUSE_ALT)
5703 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 key_name[1] = (wheel_code & 1)
5705 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005706 held_button = MOUSE_RELEASE;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 else
5709 key_name[1] = get_pseudo_mouse_code(current_button,
5710 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005711
5712 /* Make sure the mouse position is valid. Some terminals may
5713 * return weird values. */
5714 if (mouse_col >= Columns)
5715 mouse_col = Columns - 1;
5716 if (mouse_row >= Rows)
5717 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719#endif /* FEAT_MOUSE */
5720
5721#ifdef FEAT_GUI
5722 /*
5723 * If using the GUI, then we get menu and scrollbar events.
5724 *
5725 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5726 * four bytes which are to be taken as a pointer to the vimmenu_T
5727 * structure.
5728 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005729 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005730 * is one byte with the tab index.
5731 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5733 * by one byte representing the scrollbar number, and then four bytes
5734 * representing a long_u which is the new value of the scrollbar.
5735 *
5736 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5737 * KE_FILLER followed by four bytes representing a long_u which is the
5738 * new value of the scrollbar.
5739 */
5740# ifdef FEAT_MENU
5741 else if (key_name[0] == (int)KS_MENU)
5742 {
5743 long_u val;
5744
5745 num_bytes = get_long_from_buf(tp + slen, &val);
5746 if (num_bytes == -1)
5747 return -1;
5748 current_menu = (vimmenu_T *)val;
5749 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005750
5751 /* The menu may have been deleted right after it was used, check
5752 * for that. */
5753 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5754 {
5755 key_name[0] = KS_EXTRA;
5756 key_name[1] = (int)KE_IGNORE;
5757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005760# ifdef FEAT_GUI_TABLINE
5761 else if (key_name[0] == (int)KS_TABLINE)
5762 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005763 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005764 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5765 if (num_bytes == -1)
5766 return -1;
5767 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005768 if (current_tab == 255) /* -1 in a byte gives 255 */
5769 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005770 slen += num_bytes;
5771 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005772 else if (key_name[0] == (int)KS_TABMENU)
5773 {
5774 /* Selecting tabline tab or using its menu. */
5775 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5776 if (num_bytes == -1)
5777 return -1;
5778 current_tab = (int)bytes[0];
5779 current_tabmenu = (int)bytes[1];
5780 slen += num_bytes;
5781 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783# ifndef USE_ON_FLY_SCROLL
5784 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5785 {
5786 long_u val;
5787
5788 /* Get the last scrollbar event in the queue of the same type */
5789 j = 0;
5790 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5791 && tp[j + 2] != NUL; ++i)
5792 {
5793 j += 3;
5794 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5795 if (num_bytes == -1)
5796 break;
5797 if (i == 0)
5798 current_scrollbar = (int)bytes[0];
5799 else if (current_scrollbar != (int)bytes[0])
5800 break;
5801 j += num_bytes;
5802 num_bytes = get_long_from_buf(tp + j, &val);
5803 if (num_bytes == -1)
5804 break;
5805 scrollbar_value = val;
5806 j += num_bytes;
5807 slen = j;
5808 }
5809 if (i == 0) /* not enough characters to make one */
5810 return -1;
5811 }
5812 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5813 {
5814 long_u val;
5815
5816 /* Get the last horiz. scrollbar event in the queue */
5817 j = 0;
5818 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5819 && tp[j + 2] != NUL; ++i)
5820 {
5821 j += 3;
5822 num_bytes = get_long_from_buf(tp + j, &val);
5823 if (num_bytes == -1)
5824 break;
5825 scrollbar_value = val;
5826 j += num_bytes;
5827 slen = j;
5828 }
5829 if (i == 0) /* not enough characters to make one */
5830 return -1;
5831 }
5832# endif /* !USE_ON_FLY_SCROLL */
5833#endif /* FEAT_GUI */
5834
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005835 /*
5836 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5837 */
5838 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5839
5840 /*
5841 * Add any modifier codes to our string.
5842 */
5843 new_slen = 0; /* Length of what will replace the termcode */
5844 if (modifiers != 0)
5845 {
5846 /* Some keys have the modifier included. Need to handle that here
5847 * to make mappings work. */
5848 key = simplify_key(key, &modifiers);
5849 if (modifiers != 0)
5850 {
5851 string[new_slen++] = K_SPECIAL;
5852 string[new_slen++] = (int)KS_MODIFIER;
5853 string[new_slen++] = modifiers;
5854 }
5855 }
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005858 key_name[0] = KEY2TERMCAP0(key);
5859 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005861 {
5862 /* from ":set <M-b>=xx" */
5863#ifdef FEAT_MBYTE
5864 if (has_mbyte)
5865 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5866 else
5867#endif
5868 string[new_slen++] = key_name[1];
5869 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005870 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5871 && key_name[1] == KE_IGNORE)
5872 {
5873 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5874 * to indicate what happened. */
5875 retval = KEYLEN_REMOVED;
5876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 else
5878 {
5879 string[new_slen++] = K_SPECIAL;
5880 string[new_slen++] = key_name[0];
5881 string[new_slen++] = key_name[1];
5882 }
5883 string[new_slen] = NUL;
5884 extra = new_slen - slen;
5885 if (buf == NULL)
5886 {
5887 if (extra < 0)
5888 /* remove matched chars, taking care of noremap */
5889 del_typebuf(-extra, offset);
5890 else if (extra > 0)
5891 /* insert the extra space we need */
5892 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5893
5894 /*
5895 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5896 * typebuf.tb_buf[]!
5897 */
5898 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5899 (size_t)new_slen);
5900 }
5901 else
5902 {
5903 if (extra < 0)
5904 /* remove matched characters */
5905 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005906 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005908 {
5909 /* Insert the extra space we need. If there is insufficient
5910 * space return -1. */
5911 if (*buflen + extra + new_slen >= bufsize)
5912 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005914 (size_t)(*buflen - offset));
5915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005917 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005919 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
5921
Bram Moolenaar2951b772013-07-03 12:45:31 +02005922#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005923 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 return 0; /* no match found */
5927}
5928
Bram Moolenaar9377df32017-10-15 13:22:01 +02005929#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005930/*
5931 * Get the text foreground color, if known.
5932 */
5933 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005934term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005935{
5936 if (rfg_status == STATUS_GOT)
5937 {
5938 *r = fg_r;
5939 *g = fg_g;
5940 *b = fg_b;
5941 }
5942}
5943
5944/*
5945 * Get the text background color, if known.
5946 */
5947 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005948term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005949{
5950 if (rbg_status == STATUS_GOT)
5951 {
5952 *r = bg_r;
5953 *g = bg_g;
5954 *b = bg_b;
5955 }
5956}
5957#endif
5958
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959/*
5960 * Replace any terminal code strings in from[] with the equivalent internal
5961 * vim representation. This is used for the "from" and "to" part of a
5962 * mapping, and the "to" part of a menu command.
5963 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5964 * '<'.
5965 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5966 *
5967 * The replacement is done in result[] and finally copied into allocated
5968 * memory. If this all works well *bufp is set to the allocated memory and a
5969 * pointer to it is returned. If something fails *bufp is set to NULL and from
5970 * is returned.
5971 *
5972 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5973 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5974 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5975 * instead of a CTRL-V.
5976 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005977 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005978replace_termcodes(
5979 char_u *from,
5980 char_u **bufp,
5981 int from_part,
5982 int do_lt, /* also translate <lt> */
5983 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 int i;
5986 int slen;
5987 int key;
5988 int dlen = 0;
5989 char_u *src;
5990 int do_backslash; /* backslash is a special character */
5991 int do_special; /* recognize <> key codes */
5992 int do_key_code; /* recognize raw key codes */
5993 char_u *result; /* buffer for resulting string */
5994
5995 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005996 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5998
5999 /*
6000 * Allocate space for the translation. Worst case a single character is
6001 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
6002 */
6003 result = alloc((unsigned)STRLEN(from) * 6 + 1);
6004 if (result == NULL) /* out of memory */
6005 {
6006 *bufp = NULL;
6007 return from;
6008 }
6009
6010 src = from;
6011
6012 /*
6013 * Check for #n at start only: function key n
6014 */
6015 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
6016 {
6017 result[dlen++] = K_SPECIAL;
6018 result[dlen++] = 'k';
6019 if (src[1] == '0')
6020 result[dlen++] = ';'; /* #0 is F10 is "k;" */
6021 else
6022 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
6023 src += 2;
6024 }
6025
6026 /*
6027 * Copy each byte from *from to result[dlen]
6028 */
6029 while (*src != NUL)
6030 {
6031 /*
6032 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006033 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
6035 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
6036 {
6037#ifdef FEAT_EVAL
6038 /*
6039 * Replace <SID> by K_SNR <script-nr> _.
6040 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
6041 */
6042 if (STRNICMP(src, "<SID>", 5) == 0)
6043 {
6044 if (current_SID <= 0)
6045 EMSG(_(e_usingsid));
6046 else
6047 {
6048 src += 5;
6049 result[dlen++] = K_SPECIAL;
6050 result[dlen++] = (int)KS_EXTRA;
6051 result[dlen++] = (int)KE_SNR;
6052 sprintf((char *)result + dlen, "%ld", (long)current_SID);
6053 dlen += (int)STRLEN(result + dlen);
6054 result[dlen++] = '_';
6055 continue;
6056 }
6057 }
6058#endif
6059
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006060 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (slen)
6062 {
6063 dlen += slen;
6064 continue;
6065 }
6066 }
6067
6068 /*
6069 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6070 * Note that this is also checked after replacing the <> form.
6071 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6072 * it could be a character in the file.
6073 */
6074 if (do_key_code)
6075 {
6076 i = find_term_bykeys(src);
6077 if (i >= 0)
6078 {
6079 result[dlen++] = K_SPECIAL;
6080 result[dlen++] = termcodes[i].name[0];
6081 result[dlen++] = termcodes[i].name[1];
6082 src += termcodes[i].len;
6083 /* If terminal code matched, continue after it. */
6084 continue;
6085 }
6086 }
6087
6088#ifdef FEAT_EVAL
6089 if (do_special)
6090 {
6091 char_u *p, *s, len;
6092
6093 /*
6094 * Replace <Leader> by the value of "mapleader".
6095 * Replace <LocalLeader> by the value of "maplocalleader".
6096 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6097 */
6098 if (STRNICMP(src, "<Leader>", 8) == 0)
6099 {
6100 len = 8;
6101 p = get_var_value((char_u *)"g:mapleader");
6102 }
6103 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6104 {
6105 len = 13;
6106 p = get_var_value((char_u *)"g:maplocalleader");
6107 }
6108 else
6109 {
6110 len = 0;
6111 p = NULL;
6112 }
6113 if (len != 0)
6114 {
6115 /* Allow up to 8 * 6 characters for "mapleader". */
6116 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6117 s = (char_u *)"\\";
6118 else
6119 s = p;
6120 while (*s != NUL)
6121 result[dlen++] = *s++;
6122 src += len;
6123 continue;
6124 }
6125 }
6126#endif
6127
6128 /*
6129 * Remove CTRL-V and ignore the next character.
6130 * For "from" side the CTRL-V at the end is included, for the "to"
6131 * part it is removed.
6132 * If 'cpoptions' does not contain 'B', also accept a backslash.
6133 */
6134 key = *src;
6135 if (key == Ctrl_V || (do_backslash && key == '\\'))
6136 {
6137 ++src; /* skip CTRL-V or backslash */
6138 if (*src == NUL)
6139 {
6140 if (from_part)
6141 result[dlen++] = key;
6142 break;
6143 }
6144 }
6145
6146#ifdef FEAT_MBYTE
6147 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006148 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149#endif
6150 {
6151 /*
6152 * If the character is K_SPECIAL, replace it with K_SPECIAL
6153 * KS_SPECIAL KE_FILLER.
6154 * If compiled with the GUI replace CSI with K_CSI.
6155 */
6156 if (*src == K_SPECIAL)
6157 {
6158 result[dlen++] = K_SPECIAL;
6159 result[dlen++] = KS_SPECIAL;
6160 result[dlen++] = KE_FILLER;
6161 }
6162# ifdef FEAT_GUI
6163 else if (*src == CSI)
6164 {
6165 result[dlen++] = K_SPECIAL;
6166 result[dlen++] = KS_EXTRA;
6167 result[dlen++] = (int)KE_CSI;
6168 }
6169# endif
6170 else
6171 result[dlen++] = *src;
6172 ++src;
6173 }
6174 }
6175 result[dlen] = NUL;
6176
6177 /*
6178 * Copy the new string to allocated memory.
6179 * If this fails, just return from.
6180 */
6181 if ((*bufp = vim_strsave(result)) != NULL)
6182 from = *bufp;
6183 vim_free(result);
6184 return from;
6185}
6186
6187/*
6188 * Find a termcode with keys 'src' (must be NUL terminated).
6189 * Return the index in termcodes[], or -1 if not found.
6190 */
6191 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006192find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006195 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 for (i = 0; i < tc_len; ++i)
6198 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006199 if (slen == termcodes[i].len
6200 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 return i;
6202 }
6203 return -1;
6204}
6205
6206/*
6207 * Gather the first characters in the terminal key codes into a string.
6208 * Used to speed up check_termcode().
6209 */
6210 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006211gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 int i;
6214 int len = 0;
6215
6216#ifdef FEAT_GUI
6217 if (gui.in_use)
6218 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6219#endif
6220#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006221 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 termleader[len++] = DCS; /* the termcode response starts with DCS
6223 in 8-bit mode */
6224#endif
6225 termleader[len] = NUL;
6226
6227 for (i = 0; i < tc_len; ++i)
6228 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6229 {
6230 termleader[len++] = termcodes[i].code[0];
6231 termleader[len] = NUL;
6232 }
6233
6234 need_gather = FALSE;
6235}
6236
6237/*
6238 * Show all termcodes (for ":set termcap")
6239 * This code looks a lot like showoptions(), but is different.
6240 */
6241 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006242show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 int col;
6245 int *items;
6246 int item_count;
6247 int run;
6248 int row, rows;
6249 int cols;
6250 int i;
6251 int len;
6252
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006253#define INC3 27 /* try to make three columns */
6254#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255#define GAP 2 /* spaces between columns */
6256
6257 if (tc_len == 0) /* no terminal codes (must be GUI) */
6258 return;
6259 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6260 if (items == NULL)
6261 return;
6262
6263 /* Highlight title */
6264 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6265
6266 /*
6267 * do the loop two times:
6268 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006269 * 2. display the medium items (medium length strings)
6270 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006272 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {
6274 /*
6275 * collect the items in items[]
6276 */
6277 item_count = 0;
6278 for (i = 0; i < tc_len; i++)
6279 {
6280 len = show_one_termcode(termcodes[i].name,
6281 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006282 if (len <= INC3 - GAP ? run == 1
6283 : len <= INC2 - GAP ? run == 2
6284 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 items[item_count++] = i;
6286 }
6287
6288 /*
6289 * display the items
6290 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006291 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006293 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (cols == 0)
6295 cols = 1;
6296 rows = (item_count + cols - 1) / cols;
6297 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006298 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 rows = item_count;
6300 for (row = 0; row < rows && !got_int; ++row)
6301 {
6302 msg_putchar('\n'); /* go to next line */
6303 if (got_int) /* 'q' typed in more */
6304 break;
6305 col = 0;
6306 for (i = row; i < item_count; i += rows)
6307 {
6308 msg_col = col; /* make columns */
6309 show_one_termcode(termcodes[items[i]].name,
6310 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006311 if (run == 2)
6312 col += INC2;
6313 else
6314 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 out_flush();
6317 ui_breakcheck();
6318 }
6319 }
6320 vim_free(items);
6321}
6322
6323/*
6324 * Show one termcode entry.
6325 * Output goes into IObuff[]
6326 */
6327 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006328show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 char_u *p;
6331 int len;
6332
6333 if (name[0] > '~')
6334 {
6335 IObuff[0] = ' ';
6336 IObuff[1] = ' ';
6337 IObuff[2] = ' ';
6338 IObuff[3] = ' ';
6339 }
6340 else
6341 {
6342 IObuff[0] = 't';
6343 IObuff[1] = '_';
6344 IObuff[2] = name[0];
6345 IObuff[3] = name[1];
6346 }
6347 IObuff[4] = ' ';
6348
6349 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6350 if (p[1] != 't')
6351 STRCPY(IObuff + 5, p);
6352 else
6353 IObuff[5] = NUL;
6354 len = (int)STRLEN(IObuff);
6355 do
6356 IObuff[len++] = ' ';
6357 while (len < 17);
6358 IObuff[len] = NUL;
6359 if (code == NULL)
6360 len += 4;
6361 else
6362 len += vim_strsize(code);
6363
6364 if (printit)
6365 {
6366 msg_puts(IObuff);
6367 if (code == NULL)
6368 msg_puts((char_u *)"NULL");
6369 else
6370 msg_outtrans(code);
6371 }
6372 return len;
6373}
6374
6375#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6376/*
6377 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6378 * termcap codes from the terminal itself.
6379 * We get them one by one to avoid a very long response string.
6380 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006381static int xt_index_in = 0;
6382static int xt_index_out = 0;
6383
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006385req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 xt_index_out = 0;
6388 xt_index_in = 0;
6389 req_more_codes_from_term();
6390}
6391
6392 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006393req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 char buf[11];
6396 int old_idx = xt_index_out;
6397
6398 /* Don't do anything when going to exit. */
6399 if (exiting)
6400 return;
6401
6402 /* Send up to 10 more requests out than we received. Avoid sending too
6403 * many, there can be a buffer overflow somewhere. */
6404 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6405 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006406 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006407
Bram Moolenaarb255b902018-04-24 21:40:10 +02006408 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6409 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 out_str_nf((char_u *)buf);
6411 ++xt_index_out;
6412 }
6413
6414 /* Send the codes out right away. */
6415 if (xt_index_out != old_idx)
6416 out_flush();
6417}
6418
6419/*
6420 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6421 * A "0" instead of the "1" indicates a code that isn't supported.
6422 * Both <name> and <string> are encoded in hex.
6423 * "code" points to the "0" or "1".
6424 */
6425 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006426got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428#define XT_LEN 100
6429 char_u name[3];
6430 char_u str[XT_LEN];
6431 int i;
6432 int j = 0;
6433 int c;
6434
6435 /* A '1' means the code is supported, a '0' means it isn't.
6436 * When half the length is > XT_LEN we can't use it.
6437 * Our names are currently all 2 characters. */
6438 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6439 {
6440 /* Get the name from the response and find it in the table. */
6441 name[0] = hexhex2nr(code + 3);
6442 name[1] = hexhex2nr(code + 5);
6443 name[2] = NUL;
6444 for (i = 0; key_names[i] != NULL; ++i)
6445 {
6446 if (STRCMP(key_names[i], name) == 0)
6447 {
6448 xt_index_in = i;
6449 break;
6450 }
6451 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006452
Bram Moolenaarb255b902018-04-24 21:40:10 +02006453 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if (key_names[i] != NULL)
6456 {
6457 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6458 str[j++] = c;
6459 str[j] = NUL;
6460 if (name[0] == 'C' && name[1] == 'o')
6461 {
6462 /* Color count is not a key code. */
6463 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006464 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
6466 else
6467 {
6468 /* First delete any existing entry with the same code. */
6469 i = find_term_bykeys(str);
6470 if (i >= 0)
6471 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006472 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 }
6474 }
6475 }
6476
6477 /* May request more codes now that we received one. */
6478 ++xt_index_in;
6479 req_more_codes_from_term();
6480}
6481
6482/*
6483 * Check if there are any unanswered requests and deal with them.
6484 * This is called before starting an external program or getting direct
6485 * keyboard input. We don't want responses to be send to that program or
6486 * handled as typed text.
6487 */
6488 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006489check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 int c;
6492
6493 /* If no codes requested or all are answered, no need to wait. */
6494 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6495 return;
6496
6497 /* Vgetc() will check for and handle any response.
6498 * Keep calling vpeekc() until we don't get any responses. */
6499 ++no_mapping;
6500 ++allow_keys;
6501 for (;;)
6502 {
6503 c = vpeekc();
6504 if (c == NUL) /* nothing available */
6505 break;
6506
6507 /* If a response is recognized it's replaced with K_IGNORE, must read
6508 * it from the input stream. If there is no K_IGNORE we can't do
6509 * anything, break here (there might be some responses further on, but
6510 * we don't want to throw away any typed chars). */
6511 if (c != K_SPECIAL && c != K_IGNORE)
6512 break;
6513 c = vgetc();
6514 if (c != K_IGNORE)
6515 {
6516 vungetc(c);
6517 break;
6518 }
6519 }
6520 --no_mapping;
6521 --allow_keys;
6522}
6523#endif
6524
6525#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6526/*
6527 * Translate an internal mapping/abbreviation representation into the
6528 * corresponding external one recognized by :map/:abbrev commands;
6529 * respects the current B/k/< settings of 'cpoption'.
6530 *
6531 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006532 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 *
6534 * It uses a growarray to build the translation string since the
6535 * latter can be wider than the original description. The caller has to
6536 * free the string afterwards.
6537 *
6538 * Returns NULL when there is a problem.
6539 */
6540 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006541translate_mapping(
6542 char_u *str,
6543 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 garray_T ga;
6546 int c;
6547 int modifiers;
6548 int cpo_bslash;
6549 int cpo_special;
6550 int cpo_keycode;
6551
6552 ga_init(&ga);
6553 ga.ga_itemsize = 1;
6554 ga.ga_growsize = 40;
6555
6556 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6557 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6558 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6559
6560 for (; *str; ++str)
6561 {
6562 c = *str;
6563 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6564 {
6565 modifiers = 0;
6566 if (str[1] == KS_MODIFIER)
6567 {
6568 str++;
6569 modifiers = *++str;
6570 c = *++str;
6571 }
6572 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6573 {
6574 int i;
6575
6576 /* try to find special key in termcodes */
6577 for (i = 0; i < tc_len; ++i)
6578 if (termcodes[i].name[0] == str[1]
6579 && termcodes[i].name[1] == str[2])
6580 break;
6581 if (i < tc_len)
6582 {
6583 ga_concat(&ga, termcodes[i].code);
6584 str += 2;
6585 continue; /* for (str) */
6586 }
6587 }
6588 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6589 {
6590 if (expmap && cpo_special)
6591 {
6592 ga_clear(&ga);
6593 return NULL;
6594 }
6595 c = TO_SPECIAL(str[1], str[2]);
6596 if (c == K_ZERO) /* display <Nul> as ^@ */
6597 c = NUL;
6598 str += 2;
6599 }
6600 if (IS_SPECIAL(c) || modifiers) /* special key */
6601 {
6602 if (expmap && cpo_special)
6603 {
6604 ga_clear(&ga);
6605 return NULL;
6606 }
6607 ga_concat(&ga, get_special_key_name(c, modifiers));
6608 continue; /* for (str) */
6609 }
6610 }
6611 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6612 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6613 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6614 if (c)
6615 ga_append(&ga, c);
6616 }
6617 ga_append(&ga, NUL);
6618 return (char_u *)(ga.ga_data);
6619}
6620#endif
6621
6622#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6623static char ksme_str[20];
6624static char ksmr_str[20];
6625static char ksmd_str[20];
6626
6627/*
6628 * For Win32 console: update termcap codes for existing console attributes.
6629 */
6630 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006631update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 struct builtin_term *p;
6634
6635 p = find_builtin_term(DEFAULT_TERM);
6636 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6637 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6638 attr | 0x08); /* FOREGROUND_INTENSITY */
6639 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6640 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6641
6642 while (p->bt_string != NULL)
6643 {
6644 if (p->bt_entry == (int)KS_ME)
6645 p->bt_string = &ksme_str[0];
6646 else if (p->bt_entry == (int)KS_MR)
6647 p->bt_string = &ksmr_str[0];
6648 else if (p->bt_entry == (int)KS_MD)
6649 p->bt_string = &ksmd_str[0];
6650 ++p;
6651 }
6652}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006653
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006654# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006655# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006656struct ks_tbl_s
6657{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006658 int code; /* value of KS_ */
6659 char *vtp; /* code in vtp mode */
6660 char *vtp2; /* code in vtp2 mode */
6661 char buf[KSSIZE]; /* save buffer in non-vtp mode */
6662 char vbuf[KSSIZE]; /* save buffer in vtp mode */
6663 char v2buf[KSSIZE]; /* save buffer in vtp2 mode */
6664 char arr[KSSIZE]; /* real buffer */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006665};
6666
6667static struct ks_tbl_s ks_tbl[] =
6668{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006669 {(int)KS_ME, "\033|0m", "\033|0m"}, /* normal */
6670 {(int)KS_MR, "\033|7m", "\033|7m"}, /* reverse */
6671 {(int)KS_MD, "\033|1m", "\033|1m"}, /* bold */
6672 {(int)KS_SO, "\033|91m", "\033|91m"}, /* standout: bright red text */
6673 {(int)KS_SE, "\033|39m", "\033|39m"}, /* standout end: default color */
6674 {(int)KS_CZH, "\033|95m", "\033|95m"}, /* italic: bright magenta text */
6675 {(int)KS_CZR, "\033|0m", "\033|0m"}, /* italic end */
6676 {(int)KS_US, "\033|4m", "\033|4m"}, /* underscore */
6677 {(int)KS_UE, "\033|24m", "\033|24m"}, /* underscore end */
6678# ifdef TERMINFO
6679 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, /* set background color */
6680 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, /* set foreground color */
6681# else
6682 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, /* set background color */
6683 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, /* set foreground color */
6684# endif
6685 {(int)KS_CCO, "16", "256"}, /* colors */
6686 {(int)KS_NAME} /* terminator */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006687};
6688
6689 static struct builtin_term *
6690find_first_tcap(
6691 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006692 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006693{
6694 struct builtin_term *p;
6695
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006696 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006697 if (p->bt_entry == code)
6698 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006699 return NULL;
6700}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006701# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006702
6703/*
6704 * For Win32 console: replace the sequence immediately after termguicolors.
6705 */
6706 void
6707swap_tcap(void)
6708{
6709# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006710 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006712 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006713 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006714 int mode;
6715 enum
6716 {
6717 CMODEINDEX,
6718 CMODE24,
6719 CMODE256
6720 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006721
6722 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006723 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006724 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006725 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006726 {
6727 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006728 if (bt != NULL)
6729 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006730 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6731 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6732 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6733
6734 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6735 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006736 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006737 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006738 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006739 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006740 }
6741
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006742 if (p_tgc)
6743 mode = CMODE24;
6744 else if (t_colors >= 256)
6745 mode = CMODE256;
6746 else
6747 mode = CMODEINDEX;
6748
6749 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006750 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006751 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6752 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006753 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006754 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006755 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006756 case CMODEINDEX:
6757 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6758 break;
6759 case CMODE24:
6760 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6761 break;
6762 default:
6763 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006764 }
6765 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006766 }
6767
6768 if (mode != curr_mode)
6769 {
6770 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006771 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006772 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6773 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006774 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006775 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006776 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006777 case CMODEINDEX:
6778 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6779 break;
6780 case CMODE24:
6781 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6782 break;
6783 default:
6784 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006785 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006786 }
6787 }
6788
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006789 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006790 }
6791# endif
6792}
6793
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006795
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006796#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006797 static int
6798hex_digit(int c)
6799{
6800 if (isdigit(c))
6801 return c - '0';
6802 c = TOLOWER_ASC(c);
6803 if (c >= 'a' && c <= 'f')
6804 return c - 'a' + 10;
6805 return 0x1ffffff;
6806}
6807
6808 guicolor_T
6809gui_get_color_cmn(char_u *name)
6810{
Bram Moolenaar28726652017-01-06 18:16:19 +01006811 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6812 * values as used by the MS-Windows GDI api. It should be used only for
6813 * MS-Windows GDI builds. */
6814# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6815# undef RGB
6816# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006817# ifndef RGB
6818# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6819# endif
6820# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006821 FILE *fd;
6822 char line[LINE_LEN];
6823 char_u *fname;
6824 int r, g, b, i;
6825 guicolor_T color;
6826
6827 struct rgbcolor_table_S {
6828 char_u *color_name;
6829 guicolor_T color;
6830 };
6831
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006832 /* Only non X11 colors (not present in rgb.txt) and colors in
6833 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006834 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006835 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6836 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6837 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6838 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6839 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6840 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6841 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6842 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6843 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6844 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6845 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6846 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6847 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006848 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6849 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006850 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006851 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006852 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006853 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6854 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6855 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6856 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6857 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6858 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6859 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6860 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6861 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006862 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006863 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006864 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6865 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006866 };
6867
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006868 static struct rgbcolor_table_S *colornames_table;
6869 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006870
6871 if (name[0] == '#' && STRLEN(name) == 7)
6872 {
6873 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006874 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006875 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6876 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6877 if (color > 0xffffff)
6878 return INVALCOLOR;
6879 return color;
6880 }
6881
6882 /* Check if the name is one of the colors we know */
6883 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6884 if (STRICMP(name, rgb_table[i].color_name) == 0)
6885 return rgb_table[i].color;
6886
6887 /*
6888 * Last attempt. Look in the file "$VIM/rgb.txt".
6889 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006890 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006891 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006892 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006893
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006894 /* colornames_table not yet initialized */
6895 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6896 if (fname == NULL)
6897 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006898
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006899 fd = fopen((char *)fname, "rt");
6900 vim_free(fname);
6901 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006902 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006903 if (p_verbose > 1)
6904 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6905 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006906 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006907
6908 for (counting = 1; counting >= 0; --counting)
6909 {
6910 if (!counting)
6911 {
6912 colornames_table = (struct rgbcolor_table_S *)alloc(
6913 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6914 if (colornames_table == NULL)
6915 {
6916 fclose(fd);
6917 return INVALCOLOR;
6918 }
6919 rewind(fd);
6920 }
6921 size = 0;
6922
6923 while (!feof(fd))
6924 {
6925 size_t len;
6926 int pos;
6927
6928 ignoredp = fgets(line, LINE_LEN, fd);
6929 len = strlen(line);
6930
6931 if (len <= 1 || line[len - 1] != '\n')
6932 continue;
6933
6934 line[len - 1] = '\0';
6935
6936 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6937 if (i != 3)
6938 continue;
6939
6940 if (!counting)
6941 {
6942 char_u *s = vim_strsave((char_u *)line + pos);
6943
6944 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006945 {
6946 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006947 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006948 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006949 colornames_table[size].color_name = s;
6950 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6951 }
6952 size++;
6953 }
6954 }
6955 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006956 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006957
6958 for (i = 0; i < size; i++)
6959 if (STRICMP(name, colornames_table[i].color_name) == 0)
6960 return colornames_table[i].color;
6961
Bram Moolenaarab302212016-04-26 20:59:29 +02006962 return INVALCOLOR;
6963}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006964
6965 guicolor_T
6966gui_get_rgb_color_cmn(int r, int g, int b)
6967{
6968 guicolor_T color = RGB(r, g, b);
6969
6970 if (color > 0xffffff)
6971 return INVALCOLOR;
6972 return color;
6973}
Bram Moolenaarab302212016-04-26 20:59:29 +02006974#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006975
6976#if (defined(WIN3264) && !defined(FEAT_GUI_W32)) || defined(FEAT_TERMINAL) \
6977 || defined(PROTO)
6978static int cube_value[] = {
6979 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6980};
6981
6982static int grey_ramp[] = {
6983 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6984 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6985};
6986
6987# ifdef FEAT_TERMINAL
6988# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006989# else
6990# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006991# endif
6992
Bram Moolenaar9894e392018-05-05 14:29:06 +02006993static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006994// R G B idx
6995 { 0, 0, 0, 1}, // black
6996 {224, 0, 0, 2}, // dark red
6997 { 0, 224, 0, 3}, // dark green
6998 {224, 224, 0, 4}, // dark yellow / brown
6999 { 0, 0, 224, 5}, // dark blue
7000 {224, 0, 224, 6}, // dark magenta
7001 { 0, 224, 224, 7}, // dark cyan
7002 {224, 224, 224, 8}, // light grey
7003
7004 {128, 128, 128, 9}, // dark grey
7005 {255, 64, 64, 10}, // light red
7006 { 64, 255, 64, 11}, // light green
7007 {255, 255, 64, 12}, // yellow
7008 { 64, 64, 255, 13}, // light blue
7009 {255, 64, 255, 14}, // light magenta
7010 { 64, 255, 255, 15}, // light cyan
7011 {255, 255, 255, 16}, // white
7012};
7013
7014 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007015cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007016{
7017 int idx;
7018
7019 if (nr < 16)
7020 {
7021 *r = ansi_table[nr][0];
7022 *g = ansi_table[nr][1];
7023 *b = ansi_table[nr][2];
7024 *ansi_idx = ansi_table[nr][3];
7025 }
7026 else if (nr < 232)
7027 {
7028 /* 216 color cube */
7029 idx = nr - 16;
7030 *r = cube_value[idx / 36 % 6];
7031 *g = cube_value[idx / 6 % 6];
7032 *b = cube_value[idx % 6];
7033 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7034 }
7035 else if (nr < 256)
7036 {
7037 /* 24 grey scale ramp */
7038 idx = nr - 232;
7039 *r = grey_ramp[idx];
7040 *g = grey_ramp[idx];
7041 *b = grey_ramp[idx];
7042 *ansi_idx = VTERM_ANSI_INDEX_NONE;
7043 }
7044 else
7045 {
7046 *r = 0;
7047 *g = 0;
7048 *b = 0;
7049 *ansi_idx = 0;
7050 }
7051}
7052#endif