blob: fec50d583a2ca6cebad9797b9706de3365bde99a [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);
79static void term_color(char_u *s, int n);
80static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static void req_codes_from_term(void);
83static void req_more_codes_from_term(void);
84static void got_code_from_term(char_u *code, int len);
85static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void del_termcode_idx(int idx);
93static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
98
99#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200117
118# define STATUS_GET 1 /* send request when switching to RAW mode */
119# define STATUS_SENT 2 /* did send request, waiting for response */
120# define STATUS_GOT 3 /* received response */
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200123static int crv_status = STATUS_GET;
124
Bram Moolenaar9584b312013-03-13 19:29:28 +0100125/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126static int u7_status = STATUS_GET;
127
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200128/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200129static int rbg_status = STATUS_GET;
130
Bram Moolenaar4db25542017-08-28 22:43:05 +0200131/* Request cursor blinking mode report: */
132static int rbm_status = STATUS_GET;
133
134/* Request cursor style report: */
135static int rcs_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136# endif
137
138/*
139 * Don't declare these variables if termcap.h contains them.
140 * Autoconf checks if these variables should be declared extern (not all
141 * systems have them).
142 * Some versions define ospeed to be speed_t, but that is incompatible with
143 * BSD, where ospeed is short and speed_t is long.
144 */
145# ifndef HAVE_OSPEED
146# ifdef OSPEED_EXTERN
147extern short ospeed;
148# else
149short ospeed;
150# endif
151# endif
152# ifndef HAVE_UP_BC_PC
153# ifdef UP_BC_PC_EXTERN
154extern char *UP, *BC, PC;
155# else
156char *UP, *BC, PC;
157# endif
158# endif
159
160# define TGETSTR(s, p) vim_tgetstr((s), (p))
161# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif /* HAVE_TGETENT */
164
165static int detected_8bit = FALSE; /* detected 8-bit terminal */
166
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200167#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200168/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200169 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200170static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200171
172/* The blink flag from the style response may be inverted from the actual
173 * blinking state, xterm XORs the flags. */
174static int initial_cursor_shape_blink = FALSE;
175
176/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200177static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200178#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200179
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000180static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181{
182
183#if defined(FEAT_GUI)
184/*
185 * GUI pseudo term-cap.
186 */
187 {(int)KS_NAME, "gui"},
188 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
189 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
190# ifdef TERMINFO
191 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
192# else
193 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
194# endif
195 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
196# ifdef TERMINFO
197 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
198 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100199# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
201# endif
202# else
203 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
204 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100205# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
207# endif
208# endif
209 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
210 /* attributes switched on with 'h', off with * 'H' */
211 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
212 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
213 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
214 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
215 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
216 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
217 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000218 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
219 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
221 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
222 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
223 {(int)KS_MS, "y"},
224 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100225 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {(int)KS_LE, "\b"}, /* cursor-left = BS */
227 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
228# ifdef TERMINFO
229 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
230# else
231 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
232# endif
233 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000234 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
236
237#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
239# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
240/*
241 * Amiga console window, default for Amiga
242 */
243 {(int)KS_NAME, "amiga"},
244 {(int)KS_CE, "\033[K"},
245 {(int)KS_CD, "\033[J"},
246 {(int)KS_AL, "\033[L"},
247# ifdef TERMINFO
248 {(int)KS_CAL, "\033[%p1%dL"},
249# else
250 {(int)KS_CAL, "\033[%dL"},
251# endif
252 {(int)KS_DL, "\033[M"},
253# ifdef TERMINFO
254 {(int)KS_CDL, "\033[%p1%dM"},
255# else
256 {(int)KS_CDL, "\033[%dM"},
257# endif
258 {(int)KS_CL, "\014"},
259 {(int)KS_VI, "\033[0 p"},
260 {(int)KS_VE, "\033[1 p"},
261 {(int)KS_ME, "\033[0m"},
262 {(int)KS_MR, "\033[7m"},
263 {(int)KS_MD, "\033[1m"},
264 {(int)KS_SE, "\033[0m"},
265 {(int)KS_SO, "\033[33m"},
266 {(int)KS_US, "\033[4m"},
267 {(int)KS_UE, "\033[0m"},
268 {(int)KS_CZH, "\033[3m"},
269 {(int)KS_CZR, "\033[0m"},
270#if defined(__MORPHOS__) || defined(__AROS__)
271 {(int)KS_CCO, "8"}, /* allow 8 colors */
272# ifdef TERMINFO
273 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
274 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
275# else
276 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
277 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
278# endif
279 {(int)KS_OP, "\033[m"}, /* reset colors */
280#endif
281 {(int)KS_MS, "y"},
282 {(int)KS_UT, "y"}, /* guessed */
283 {(int)KS_LE, "\b"},
284# ifdef TERMINFO
285 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
286# else
287 {(int)KS_CM, "\033[%i%d;%dH"},
288# endif
289#if defined(__MORPHOS__)
290 {(int)KS_SR, "\033M"},
291#endif
292# ifdef TERMINFO
293 {(int)KS_CRI, "\033[%p1%dC"},
294# else
295 {(int)KS_CRI, "\033[%dC"},
296# endif
297 {K_UP, "\233A"},
298 {K_DOWN, "\233B"},
299 {K_LEFT, "\233D"},
300 {K_RIGHT, "\233C"},
301 {K_S_UP, "\233T"},
302 {K_S_DOWN, "\233S"},
303 {K_S_LEFT, "\233 A"},
304 {K_S_RIGHT, "\233 @"},
305 {K_S_TAB, "\233Z"},
306 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
307 {K_F2, "\233\061~"},
308 {K_F3, "\233\062~"},
309 {K_F4, "\233\063~"},
310 {K_F5, "\233\064~"},
311 {K_F6, "\233\065~"},
312 {K_F7, "\233\066~"},
313 {K_F8, "\233\067~"},
314 {K_F9, "\233\070~"},
315 {K_F10, "\233\071~"},
316 {K_S_F1, "\233\061\060~"},
317 {K_S_F2, "\233\061\061~"},
318 {K_S_F3, "\233\061\062~"},
319 {K_S_F4, "\233\061\063~"},
320 {K_S_F5, "\233\061\064~"},
321 {K_S_F6, "\233\061\065~"},
322 {K_S_F7, "\233\061\066~"},
323 {K_S_F8, "\233\061\067~"},
324 {K_S_F9, "\233\061\070~"},
325 {K_S_F10, "\233\061\071~"},
326 {K_HELP, "\233?~"},
327 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
328 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
329 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
330 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
331 {K_END, "\233\064\065~"}, /* 101 key keyboard */
332
333 {BT_EXTRA_KEYS, ""},
334 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
335 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
336 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
337# endif
338
339# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
340/*
341 * almost standard ANSI terminal, default for bebox
342 */
343 {(int)KS_NAME, "beos-ansi"},
344 {(int)KS_CE, "\033[K"},
345 {(int)KS_CD, "\033[J"},
346 {(int)KS_AL, "\033[L"},
347# ifdef TERMINFO
348 {(int)KS_CAL, "\033[%p1%dL"},
349# else
350 {(int)KS_CAL, "\033[%dL"},
351# endif
352 {(int)KS_DL, "\033[M"},
353# ifdef TERMINFO
354 {(int)KS_CDL, "\033[%p1%dM"},
355# else
356 {(int)KS_CDL, "\033[%dM"},
357# endif
358#ifdef BEOS_PR_OR_BETTER
359# ifdef TERMINFO
360 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
361# else
362 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
363# endif
364#endif
365 {(int)KS_CL, "\033[H\033[2J"},
366#ifdef notyet
367 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
368 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
369#endif
370 {(int)KS_ME, "\033[m"}, /* normal mode */
371 {(int)KS_MR, "\033[7m"}, /* reverse */
372 {(int)KS_MD, "\033[1m"}, /* bold */
373 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
374 {(int)KS_SE, "\033[m"}, /* standout end */
375 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
376 {(int)KS_CZR, "\033[m"}, /* italic end */
377 {(int)KS_US, "\033[4m"}, /* underscore mode */
378 {(int)KS_UE, "\033[m"}, /* underscore end */
379 {(int)KS_CCO, "8"}, /* allow 8 colors */
380# ifdef TERMINFO
381 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
382 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
383# else
384 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
385 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
386# endif
387 {(int)KS_OP, "\033[m"}, /* reset colors */
388 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
389 {(int)KS_UT, "y"}, /* guessed */
390 {(int)KS_LE, "\b"},
391# ifdef TERMINFO
392 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
393# else
394 {(int)KS_CM, "\033[%i%d;%dH"},
395# endif
396 {(int)KS_SR, "\033M"},
397# ifdef TERMINFO
398 {(int)KS_CRI, "\033[%p1%dC"},
399# else
400 {(int)KS_CRI, "\033[%dC"},
401# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200402# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405
406 {K_UP, "\033[A"},
407 {K_DOWN, "\033[B"},
408 {K_LEFT, "\033[D"},
409 {K_RIGHT, "\033[C"},
410# endif
411
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200412# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413/*
414 * standard ANSI terminal, default for unix
415 */
416 {(int)KS_NAME, "ansi"},
417 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
418 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
419# ifdef TERMINFO
420 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
421# else
422 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
423# endif
424 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
425# ifdef TERMINFO
426 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
427# else
428 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
429# endif
430 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
431 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
432 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
433 {(int)KS_MS, "y"},
434 {(int)KS_UT, "y"}, /* guessed */
435 {(int)KS_LE, "\b"},
436# ifdef TERMINFO
437 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
438# else
439 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
440# endif
441# ifdef TERMINFO
442 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
443# else
444 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
445# endif
446# endif
447
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200448# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449/*
450 * These codes are valid when nansi.sys or equivalent has been installed.
451 * Function keys on a PC are preceded with a NUL. These are converted into
452 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
453 * CTRL-arrow is used instead of SHIFT-arrow.
454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {(int)KS_NAME, "pcansi"},
456 {(int)KS_DL, "\033[M"},
457 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {(int)KS_CE, "\033[K"},
459 {(int)KS_CL, "\033[2J"},
460 {(int)KS_ME, "\033[0m"},
461 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
462 {(int)KS_MD, "\033[1m"}, /* bold: white text */
463 {(int)KS_SE, "\033[0m"}, /* standout end */
464 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
465 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
466 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
467 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
468 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
469 {(int)KS_CCO, "8"}, /* allow 8 colors */
470# ifdef TERMINFO
471 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
472 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
473# else
474 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
475 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
476# endif
477 {(int)KS_OP, "\033[0m"}, /* reset colors */
478 {(int)KS_MS, "y"},
479 {(int)KS_UT, "y"}, /* guessed */
480 {(int)KS_LE, "\b"},
481# ifdef TERMINFO
482 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
483# else
484 {(int)KS_CM, "\033[%i%d;%dH"},
485# endif
486# ifdef TERMINFO
487 {(int)KS_CRI, "\033[%p1%dC"},
488# else
489 {(int)KS_CRI, "\033[%dC"},
490# endif
491 {K_UP, "\316H"},
492 {K_DOWN, "\316P"},
493 {K_LEFT, "\316K"},
494 {K_RIGHT, "\316M"},
495 {K_S_LEFT, "\316s"},
496 {K_S_RIGHT, "\316t"},
497 {K_F1, "\316;"},
498 {K_F2, "\316<"},
499 {K_F3, "\316="},
500 {K_F4, "\316>"},
501 {K_F5, "\316?"},
502 {K_F6, "\316@"},
503 {K_F7, "\316A"},
504 {K_F8, "\316B"},
505 {K_F9, "\316C"},
506 {K_F10, "\316D"},
507 {K_F11, "\316\205"}, /* guessed */
508 {K_F12, "\316\206"}, /* guessed */
509 {K_S_F1, "\316T"},
510 {K_S_F2, "\316U"},
511 {K_S_F3, "\316V"},
512 {K_S_F4, "\316W"},
513 {K_S_F5, "\316X"},
514 {K_S_F6, "\316Y"},
515 {K_S_F7, "\316Z"},
516 {K_S_F8, "\316["},
517 {K_S_F9, "\316\\"},
518 {K_S_F10, "\316]"},
519 {K_S_F11, "\316\207"}, /* guessed */
520 {K_S_F12, "\316\210"}, /* guessed */
521 {K_INS, "\316R"},
522 {K_DEL, "\316S"},
523 {K_HOME, "\316G"},
524 {K_END, "\316O"},
525 {K_PAGEDOWN, "\316Q"},
526 {K_PAGEUP, "\316I"},
527# endif
528
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200529# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/*
531 * These codes are valid for the Win32 Console . The entries that start with
532 * ESC | are translated into console calls in os_win32.c. The function keys
533 * are also translated in os_win32.c.
534 */
535 {(int)KS_NAME, "win32"},
536 {(int)KS_CE, "\033|K"}, /* clear to end of line */
537 {(int)KS_AL, "\033|L"}, /* add new blank line */
538# ifdef TERMINFO
539 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
540# else
541 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
542# endif
543 {(int)KS_DL, "\033|M"}, /* delete line */
544# ifdef TERMINFO
545 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
546# else
547 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
548# endif
549 {(int)KS_CL, "\033|J"}, /* clear screen */
550 {(int)KS_CD, "\033|j"}, /* clear to end of display */
551 {(int)KS_VI, "\033|v"}, /* cursor invisible */
552 {(int)KS_VE, "\033|V"}, /* cursor visible */
553
554 {(int)KS_ME, "\033|0m"}, /* normal */
555 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
556 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
557#if 1
558 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
559 {(int)KS_SE, "\033|0m"}, /* standout end */
560#else
561 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
562 {(int)KS_SE, "\033|f"}, /* standout end */
563#endif
564 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
565 {(int)KS_CZR, "\033|0m"}, /* italic end */
566 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
567 {(int)KS_UE, "\033|0m"}, /* underscore end */
568 {(int)KS_CCO, "16"}, /* allow 16 colors */
569# ifdef TERMINFO
570 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
571 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
572# else
573 {(int)KS_CAB, "\033|%db"}, /* set background color */
574 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
575# endif
576
577 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
578 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100579 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {(int)KS_LE, "\b"},
581# ifdef TERMINFO
582 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
583# else
584 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
585# endif
586 {(int)KS_VB, "\033|B"}, /* visual bell */
587 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
588 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
589# ifdef TERMINFO
590 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
591# else
592 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
593# endif
594
595 {K_UP, "\316H"},
596 {K_DOWN, "\316P"},
597 {K_LEFT, "\316K"},
598 {K_RIGHT, "\316M"},
599 {K_S_UP, "\316\304"},
600 {K_S_DOWN, "\316\317"},
601 {K_S_LEFT, "\316\311"},
602 {K_C_LEFT, "\316s"},
603 {K_S_RIGHT, "\316\313"},
604 {K_C_RIGHT, "\316t"},
605 {K_S_TAB, "\316\017"},
606 {K_F1, "\316;"},
607 {K_F2, "\316<"},
608 {K_F3, "\316="},
609 {K_F4, "\316>"},
610 {K_F5, "\316?"},
611 {K_F6, "\316@"},
612 {K_F7, "\316A"},
613 {K_F8, "\316B"},
614 {K_F9, "\316C"},
615 {K_F10, "\316D"},
616 {K_F11, "\316\205"},
617 {K_F12, "\316\206"},
618 {K_S_F1, "\316T"},
619 {K_S_F2, "\316U"},
620 {K_S_F3, "\316V"},
621 {K_S_F4, "\316W"},
622 {K_S_F5, "\316X"},
623 {K_S_F6, "\316Y"},
624 {K_S_F7, "\316Z"},
625 {K_S_F8, "\316["},
626 {K_S_F9, "\316\\"},
627 {K_S_F10, "\316]"},
628 {K_S_F11, "\316\207"},
629 {K_S_F12, "\316\210"},
630 {K_INS, "\316R"},
631 {K_DEL, "\316S"},
632 {K_HOME, "\316G"},
633 {K_S_HOME, "\316\302"},
634 {K_C_HOME, "\316w"},
635 {K_END, "\316O"},
636 {K_S_END, "\316\315"},
637 {K_C_END, "\316u"},
638 {K_PAGEDOWN, "\316Q"},
639 {K_PAGEUP, "\316I"},
640 {K_KPLUS, "\316N"},
641 {K_KMINUS, "\316J"},
642 {K_KMULTIPLY, "\316\067"},
643 {K_K0, "\316\332"},
644 {K_K1, "\316\336"},
645 {K_K2, "\316\342"},
646 {K_K3, "\316\346"},
647 {K_K4, "\316\352"},
648 {K_K5, "\316\356"},
649 {K_K6, "\316\362"},
650 {K_K7, "\316\366"},
651 {K_K8, "\316\372"},
652 {K_K9, "\316\376"},
653# endif
654
655# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
656/*
657 * VT320 is working as an ANSI terminal compatible DEC terminal.
658 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
659 * Note: K_F1...K_F5 are for internal use, should not be defined.
660 * TODO:- rewrite ESC[ codes to CSI
661 * - keyboard languages (CSI ? 26 n)
662 */
663 {(int)KS_NAME, "vt320"},
664 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
665 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
666# ifdef TERMINFO
667 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
668# else
669 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
670# endif
671 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
672# ifdef TERMINFO
673 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
674# else
675 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
676# endif
677 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000678 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
679 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
681 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
683 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
684 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
685 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
686 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
687 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
688 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
689 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
690 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
691 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {(int)KS_MS, "y"},
693 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100694 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {(int)KS_LE, "\b"},
696# ifdef TERMINFO
697 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
698 ESC_STR "[%i%p1%d;%p2%dH")},
699# else
700 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
701# endif
702# ifdef TERMINFO
703 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
704# else
705 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
706# endif
707 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
708 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
709 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
710 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000711 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
712 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
713 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
714 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
715 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
717 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
718 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
719 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
720 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
723 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
724 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
725 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
726 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
727 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
728 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
729 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
730 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
731 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
732 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
733 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
734 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
735 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
736 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
737 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
738 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
739 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
740 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
741 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
742 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
743# endif
744
745# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
746/*
747 * Ordinary vt52
748 */
749 {(int)KS_NAME, "vt52"},
750 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
751 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100752# ifdef TERMINFO
753 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
754 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
755# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100759 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
761 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {K_UP, IF_EB("\033A", ESC_STR "A")},
763 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
764 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
765 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100766 {K_F1, IF_EB("\033P", ESC_STR "P")},
767 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
768 {K_F3, IF_EB("\033R", ESC_STR "R")},
769# ifdef __MINT__
770 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
771 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
772 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
773 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
774 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
776 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
777 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
778 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {K_F4, IF_EB("\033S", ESC_STR "S")},
780 {K_F5, IF_EB("\033T", ESC_STR "T")},
781 {K_F6, IF_EB("\033U", ESC_STR "U")},
782 {K_F7, IF_EB("\033V", ESC_STR "V")},
783 {K_F8, IF_EB("\033W", ESC_STR "W")},
784 {K_F9, IF_EB("\033X", ESC_STR "X")},
785 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
786 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
787 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
788 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
789 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
790 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
791 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
792 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
793 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
794 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
795 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
796 {K_INS, IF_EB("\033I", ESC_STR "I")},
797 {K_HOME, IF_EB("\033E", ESC_STR "E")},
798 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
799 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
800# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {(int)KS_MS, "y"},
803# endif
804# endif
805
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200806# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200807 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
809 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
810# ifdef TERMINFO
811 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
812# else
813 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
814# endif
815 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
816# ifdef TERMINFO
817 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
818# else
819 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
820# endif
821# ifdef TERMINFO
822 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
823 ESC_STR "[%i%p1%d;%p2%dr")},
824# else
825 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
826# endif
827 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
828 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
829 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
830 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
831 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
832 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
833 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
834 {(int)KS_MS, "y"},
835 {(int)KS_UT, "y"},
836 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200837 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
838 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200839 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200840 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200841# ifdef TERMINFO
842 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
843# else
844 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
845# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200846 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200847 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848# ifdef TERMINFO
849 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
850 ESC_STR "[%i%p1%d;%p2%dH")},
851# else
852 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
853# endif
854 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
855# ifdef TERMINFO
856 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
857# else
858 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
859# endif
860 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
861 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
862# ifdef FEAT_XTERM_SAVE
863 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
864 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
865 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
866# endif
867 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
868 {(int)KS_CIE, "\007"},
869 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
870 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200871 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
872 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# ifdef TERMINFO
874 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
875 ESC_STR "[8;%p1%d;%p2%dt")},
876 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
877 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200878 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# else
880 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
881 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200882 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883# endif
884 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200885 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100886 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200887# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200888 /* These are printf strings, not terminal codes. */
889 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
890 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
891# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100892 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
893 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000894
895 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
896 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
897 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
898 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
899 /* An extra set of cursor keys for vt100 mode */
900 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
901 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
902 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
903 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000905 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
906 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
907 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
908 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000909 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
910 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
911 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
912 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
913 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
914 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
915 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
916 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
917 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
918 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
919 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
920 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000922 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
923 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
924 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
925 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000926 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
927 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000929 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000930 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000931 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000932 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
933 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000934 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000935 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000936 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000937 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
938 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100939 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
940 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
941 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
942 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
943 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
944 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
945 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
946 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
947 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000950 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
951 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
952 /* F14 and F15 are missing, because they send the same codes as the undo
953 * and help key, although they don't work on all keyboards. */
954 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
955 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
956 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
957 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
958 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
959
960 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
961 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
962 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
963 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
964 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
965 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
966 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
967 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
968 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
969 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
970
971 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
972 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
973 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
974 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
975 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
976 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
977 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978# endif
979
980# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
981/*
982 * iris-ansi for Silicon Graphics machines.
983 */
984 {(int)KS_NAME, "iris-ansi"},
985 {(int)KS_CE, "\033[K"},
986 {(int)KS_CD, "\033[J"},
987 {(int)KS_AL, "\033[L"},
988# ifdef TERMINFO
989 {(int)KS_CAL, "\033[%p1%dL"},
990# else
991 {(int)KS_CAL, "\033[%dL"},
992# endif
993 {(int)KS_DL, "\033[M"},
994# ifdef TERMINFO
995 {(int)KS_CDL, "\033[%p1%dM"},
996# else
997 {(int)KS_CDL, "\033[%dM"},
998# endif
999#if 0 /* The scroll region is not working as Vim expects. */
1000# ifdef TERMINFO
1001 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1002# else
1003 {(int)KS_CS, "\033[%i%d;%dr"},
1004# endif
1005#endif
1006 {(int)KS_CL, "\033[H\033[2J"},
1007 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1008 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1009 {(int)KS_TI, "\033[=6h"},
1010 {(int)KS_TE, "\033[=6l"},
1011 {(int)KS_SE, "\033[21;27m"},
1012 {(int)KS_SO, "\033[1;7m"},
1013 {(int)KS_ME, "\033[m"},
1014 {(int)KS_MR, "\033[7m"},
1015 {(int)KS_MD, "\033[1m"},
1016 {(int)KS_CCO, "8"}, /* allow 8 colors */
1017 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1018 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1019 {(int)KS_US, "\033[4m"}, /* underline on */
1020 {(int)KS_UE, "\033[24m"}, /* underline off */
1021# ifdef TERMINFO
1022 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1023 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1024 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1025 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1026# else
1027 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1028 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1029 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1030 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1031# endif
1032 {(int)KS_MS, "y"}, /* guessed */
1033 {(int)KS_UT, "y"}, /* guessed */
1034 {(int)KS_LE, "\b"},
1035# ifdef TERMINFO
1036 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1037# else
1038 {(int)KS_CM, "\033[%i%d;%dH"},
1039# endif
1040 {(int)KS_SR, "\033M"},
1041# ifdef TERMINFO
1042 {(int)KS_CRI, "\033[%p1%dC"},
1043# else
1044 {(int)KS_CRI, "\033[%dC"},
1045# endif
1046 {(int)KS_CIS, "\033P3.y"},
1047 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1048 {(int)KS_TS, "\033P1.y"},
1049 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1050# ifdef TERMINFO
1051 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1052 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1053# else
1054 {(int)KS_CWS, "\033[203;%d;%d/y"},
1055 {(int)KS_CWP, "\033[205;%d;%d/y"},
1056# endif
1057 {K_UP, "\033[A"},
1058 {K_DOWN, "\033[B"},
1059 {K_LEFT, "\033[D"},
1060 {K_RIGHT, "\033[C"},
1061 {K_S_UP, "\033[161q"},
1062 {K_S_DOWN, "\033[164q"},
1063 {K_S_LEFT, "\033[158q"},
1064 {K_S_RIGHT, "\033[167q"},
1065 {K_F1, "\033[001q"},
1066 {K_F2, "\033[002q"},
1067 {K_F3, "\033[003q"},
1068 {K_F4, "\033[004q"},
1069 {K_F5, "\033[005q"},
1070 {K_F6, "\033[006q"},
1071 {K_F7, "\033[007q"},
1072 {K_F8, "\033[008q"},
1073 {K_F9, "\033[009q"},
1074 {K_F10, "\033[010q"},
1075 {K_F11, "\033[011q"},
1076 {K_F12, "\033[012q"},
1077 {K_S_F1, "\033[013q"},
1078 {K_S_F2, "\033[014q"},
1079 {K_S_F3, "\033[015q"},
1080 {K_S_F4, "\033[016q"},
1081 {K_S_F5, "\033[017q"},
1082 {K_S_F6, "\033[018q"},
1083 {K_S_F7, "\033[019q"},
1084 {K_S_F8, "\033[020q"},
1085 {K_S_F9, "\033[021q"},
1086 {K_S_F10, "\033[022q"},
1087 {K_S_F11, "\033[023q"},
1088 {K_S_F12, "\033[024q"},
1089 {K_INS, "\033[139q"},
1090 {K_HOME, "\033[H"},
1091 {K_END, "\033[146q"},
1092 {K_PAGEUP, "\033[150q"},
1093 {K_PAGEDOWN, "\033[154q"},
1094# endif
1095
1096# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1097/*
1098 * for debugging
1099 */
1100 {(int)KS_NAME, "debug"},
1101 {(int)KS_CE, "[CE]"},
1102 {(int)KS_CD, "[CD]"},
1103 {(int)KS_AL, "[AL]"},
1104# ifdef TERMINFO
1105 {(int)KS_CAL, "[CAL%p1%d]"},
1106# else
1107 {(int)KS_CAL, "[CAL%d]"},
1108# endif
1109 {(int)KS_DL, "[DL]"},
1110# ifdef TERMINFO
1111 {(int)KS_CDL, "[CDL%p1%d]"},
1112# else
1113 {(int)KS_CDL, "[CDL%d]"},
1114# endif
1115# ifdef TERMINFO
1116 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1117# else
1118 {(int)KS_CS, "[%dCS%d]"},
1119# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001120# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121# ifdef TERMINFO
1122 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1123# else
1124 {(int)KS_CSV, "[%dCSV%d]"},
1125# endif
1126# endif
1127# ifdef TERMINFO
1128 {(int)KS_CAB, "[CAB%p1%d]"},
1129 {(int)KS_CAF, "[CAF%p1%d]"},
1130 {(int)KS_CSB, "[CSB%p1%d]"},
1131 {(int)KS_CSF, "[CSF%p1%d]"},
1132# else
1133 {(int)KS_CAB, "[CAB%d]"},
1134 {(int)KS_CAF, "[CAF%d]"},
1135 {(int)KS_CSB, "[CSB%d]"},
1136 {(int)KS_CSF, "[CSF%d]"},
1137# endif
1138 {(int)KS_OP, "[OP]"},
1139 {(int)KS_LE, "[LE]"},
1140 {(int)KS_CL, "[CL]"},
1141 {(int)KS_VI, "[VI]"},
1142 {(int)KS_VE, "[VE]"},
1143 {(int)KS_VS, "[VS]"},
1144 {(int)KS_ME, "[ME]"},
1145 {(int)KS_MR, "[MR]"},
1146 {(int)KS_MB, "[MB]"},
1147 {(int)KS_MD, "[MD]"},
1148 {(int)KS_SE, "[SE]"},
1149 {(int)KS_SO, "[SO]"},
1150 {(int)KS_UE, "[UE]"},
1151 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001152 {(int)KS_UCE, "[UCE]"},
1153 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {(int)KS_MS, "[MS]"},
1155 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001156 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157# ifdef TERMINFO
1158 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1159# else
1160 {(int)KS_CM, "[%dCM%d]"},
1161# endif
1162 {(int)KS_SR, "[SR]"},
1163# ifdef TERMINFO
1164 {(int)KS_CRI, "[CRI%p1%d]"},
1165# else
1166 {(int)KS_CRI, "[CRI%d]"},
1167# endif
1168 {(int)KS_VB, "[VB]"},
1169 {(int)KS_KS, "[KS]"},
1170 {(int)KS_KE, "[KE]"},
1171 {(int)KS_TI, "[TI]"},
1172 {(int)KS_TE, "[TE]"},
1173 {(int)KS_CIS, "[CIS]"},
1174 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001175 {(int)KS_CSC, "[CSC]"},
1176 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {(int)KS_TS, "[TS]"},
1178 {(int)KS_FS, "[FS]"},
1179# ifdef TERMINFO
1180 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1181 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1182# else
1183 {(int)KS_CWS, "[%dCWS%d]"},
1184 {(int)KS_CWP, "[%dCWP%d]"},
1185# endif
1186 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001187 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001188 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {K_UP, "[KU]"},
1190 {K_DOWN, "[KD]"},
1191 {K_LEFT, "[KL]"},
1192 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001193 {K_XUP, "[xKU]"},
1194 {K_XDOWN, "[xKD]"},
1195 {K_XLEFT, "[xKL]"},
1196 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {K_S_UP, "[S-KU]"},
1198 {K_S_DOWN, "[S-KD]"},
1199 {K_S_LEFT, "[S-KL]"},
1200 {K_C_LEFT, "[C-KL]"},
1201 {K_S_RIGHT, "[S-KR]"},
1202 {K_C_RIGHT, "[C-KR]"},
1203 {K_F1, "[F1]"},
1204 {K_XF1, "[xF1]"},
1205 {K_F2, "[F2]"},
1206 {K_XF2, "[xF2]"},
1207 {K_F3, "[F3]"},
1208 {K_XF3, "[xF3]"},
1209 {K_F4, "[F4]"},
1210 {K_XF4, "[xF4]"},
1211 {K_F5, "[F5]"},
1212 {K_F6, "[F6]"},
1213 {K_F7, "[F7]"},
1214 {K_F8, "[F8]"},
1215 {K_F9, "[F9]"},
1216 {K_F10, "[F10]"},
1217 {K_F11, "[F11]"},
1218 {K_F12, "[F12]"},
1219 {K_S_F1, "[S-F1]"},
1220 {K_S_XF1, "[S-xF1]"},
1221 {K_S_F2, "[S-F2]"},
1222 {K_S_XF2, "[S-xF2]"},
1223 {K_S_F3, "[S-F3]"},
1224 {K_S_XF3, "[S-xF3]"},
1225 {K_S_F4, "[S-F4]"},
1226 {K_S_XF4, "[S-xF4]"},
1227 {K_S_F5, "[S-F5]"},
1228 {K_S_F6, "[S-F6]"},
1229 {K_S_F7, "[S-F7]"},
1230 {K_S_F8, "[S-F8]"},
1231 {K_S_F9, "[S-F9]"},
1232 {K_S_F10, "[S-F10]"},
1233 {K_S_F11, "[S-F11]"},
1234 {K_S_F12, "[S-F12]"},
1235 {K_HELP, "[HELP]"},
1236 {K_UNDO, "[UNDO]"},
1237 {K_BS, "[BS]"},
1238 {K_INS, "[INS]"},
1239 {K_KINS, "[KINS]"},
1240 {K_DEL, "[DEL]"},
1241 {K_KDEL, "[KDEL]"},
1242 {K_HOME, "[HOME]"},
1243 {K_S_HOME, "[C-HOME]"},
1244 {K_C_HOME, "[C-HOME]"},
1245 {K_KHOME, "[KHOME]"},
1246 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001247 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {K_END, "[END]"},
1249 {K_S_END, "[C-END]"},
1250 {K_C_END, "[C-END]"},
1251 {K_KEND, "[KEND]"},
1252 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001253 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {K_PAGEUP, "[PAGEUP]"},
1255 {K_PAGEDOWN, "[PAGEDOWN]"},
1256 {K_KPAGEUP, "[KPAGEUP]"},
1257 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1258 {K_MOUSE, "[MOUSE]"},
1259 {K_KPLUS, "[KPLUS]"},
1260 {K_KMINUS, "[KMINUS]"},
1261 {K_KDIVIDE, "[KDIVIDE]"},
1262 {K_KMULTIPLY, "[KMULTIPLY]"},
1263 {K_KENTER, "[KENTER]"},
1264 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001265 {K_PS, "[PASTE-START]"},
1266 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {K_K0, "[K0]"},
1268 {K_K1, "[K1]"},
1269 {K_K2, "[K2]"},
1270 {K_K3, "[K3]"},
1271 {K_K4, "[K4]"},
1272 {K_K5, "[K5]"},
1273 {K_K6, "[K6]"},
1274 {K_K7, "[K7]"},
1275 {K_K8, "[K8]"},
1276 {K_K9, "[K9]"},
1277# endif
1278
1279#endif /* NO_BUILTIN_TCAPS */
1280
1281/*
1282 * The most minimal terminal: only clear screen and cursor positioning
1283 * Always included.
1284 */
1285 {(int)KS_NAME, "dumb"},
1286 {(int)KS_CL, "\014"},
1287#ifdef TERMINFO
1288 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1289 ESC_STR "[%i%p1%d;%p2%dH")},
1290#else
1291 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1292#endif
1293
1294/*
1295 * end marker
1296 */
1297 {(int)KS_NAME, NULL}
1298
1299}; /* end of builtin_termcaps */
1300
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001301#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001302 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001303termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001304{
Bram Moolenaarab302212016-04-26 20:59:29 +02001305 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001306}
1307
1308 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001309termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001310{
1311 guicolor_T t;
1312
1313 if (*name == NUL)
1314 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001315 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001316
1317 if (t == INVALCOLOR)
1318 EMSG2(_("E254: Cannot allocate color %s"), name);
1319 return t;
1320}
1321
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001322 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001323termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001324{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001325 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001326}
1327#endif
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329/*
1330 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#ifdef AMIGA
1333# define DEFAULT_TERM (char_u *)"amiga"
1334#endif
1335
1336#ifdef MSWIN
1337# define DEFAULT_TERM (char_u *)"win32"
1338#endif
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#if defined(UNIX) && !defined(__MINT__)
1341# define DEFAULT_TERM (char_u *)"ansi"
1342#endif
1343
1344#ifdef __MINT__
1345# define DEFAULT_TERM (char_u *)"vt52"
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#ifdef VMS
1349# define DEFAULT_TERM (char_u *)"vt320"
1350#endif
1351
1352#ifdef __BEOS__
1353# undef DEFAULT_TERM
1354# define DEFAULT_TERM (char_u *)"beos-ansi"
1355#endif
1356
1357#ifndef DEFAULT_TERM
1358# define DEFAULT_TERM (char_u *)"dumb"
1359#endif
1360
1361/*
1362 * Term_strings contains currently used terminal output strings.
1363 * It is initialized with the default values by parse_builtin_tcap().
1364 * The values can be changed by setting the option with the same name.
1365 */
1366char_u *(term_strings[(int)KS_LAST + 1]);
1367
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001368static int need_gather = FALSE; /* need to fill termleader[] */
1369static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001371static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar833e0e32017-08-26 15:16:03 +02001372# ifdef MACOS
1373static int is_terminal_app = FALSE; /* recognized Terminal.app */
1374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#endif
1376
1377 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001378find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 struct builtin_term *p;
1381
1382 p = builtin_termcaps;
1383 while (p->bt_string != NULL)
1384 {
1385 if (p->bt_entry == (int)KS_NAME)
1386 {
1387#ifdef UNIX
1388 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1389 return p;
1390 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1391 return p;
1392 else
1393#endif
1394#ifdef VMS
1395 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1396 return p;
1397 else
1398#endif
1399 if (STRCMP(term, p->bt_string) == 0)
1400 return p;
1401 }
1402 ++p;
1403 }
1404 return p;
1405}
1406
1407/*
1408 * Parsing of the builtin termcap entries.
1409 * Caller should check if 'name' is a valid builtin term.
1410 * The terminal's name is not set, as this is already done in termcapinit().
1411 */
1412 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001413parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 struct builtin_term *p;
1416 char_u name[2];
1417 int term_8bit;
1418
1419 p = find_builtin_term(term);
1420 term_8bit = term_is_8bit(term);
1421
1422 /* Do not parse if builtin term not found */
1423 if (p->bt_string == NULL)
1424 return;
1425
1426 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1427 {
1428 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1429 {
1430 /* Only set the value if it wasn't set yet. */
1431 if (term_strings[p->bt_entry] == NULL
1432 || term_strings[p->bt_entry] == empty_option)
1433 {
1434 /* 8bit terminal: use CSI instead of <Esc>[ */
1435 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1436 {
1437 char_u *s, *t;
1438
1439 s = vim_strsave((char_u *)p->bt_string);
1440 if (s != NULL)
1441 {
1442 for (t = s; *t; ++t)
1443 if (term_7to8bit(t))
1444 {
1445 *t = term_7to8bit(t);
1446 STRCPY(t + 1, t + 2);
1447 }
1448 term_strings[p->bt_entry] = s;
1449 set_term_option_alloced(&term_strings[p->bt_entry]);
1450 }
1451 }
1452 else
1453 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1454 }
1455 }
1456 else
1457 {
1458 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1459 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1460 if (find_termcode(name) == NULL)
1461 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1462 }
1463 }
1464}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466/*
1467 * Set number of colors.
1468 * Store it as a number in t_colors.
1469 * Store it as a string in T_CCO (using nr_colors[]).
1470 */
1471 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001472set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 char_u nr_colors[20]; /* string for number of colors */
1475
1476 t_colors = nr;
1477 if (t_colors > 1)
1478 sprintf((char *)nr_colors, "%d", t_colors);
1479 else
1480 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001481 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001483
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001484#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001485/*
1486 * Set the color count to "val" and redraw if it changed.
1487 */
1488 static void
1489may_adjust_color_count(int val)
1490{
1491 if (val != t_colors)
1492 {
1493 /* Nr of colors changed, initialize highlighting and
1494 * redraw everything. This causes a redraw, which usually
1495 * clears the message. Try keeping the message if it
1496 * might work. */
1497 set_keep_msg_from_hist();
1498 set_color_count(val);
1499 init_highlight(TRUE, FALSE);
1500# ifdef DEBUG_TERMRESPONSE
1501 {
1502 char buf[100];
1503 int r = redraw_asap(CLEAR);
1504
1505 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1506 log_tr(buf);
1507 }
1508# else
1509 redraw_asap(CLEAR);
1510# endif
1511 }
1512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#endif
1514
1515#ifdef HAVE_TGETENT
1516static char *(key_names[]) =
1517{
1518#ifdef FEAT_TERMRESPONSE
1519 /* Do this one first, it may cause a screen redraw. */
1520 "Co",
1521#endif
1522 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 "#2", "#4", "%i", "*7",
1524 "k1", "k2", "k3", "k4", "k5", "k6",
1525 "k7", "k8", "k9", "k;", "F1", "F2",
1526 "%1", "&8", "kb", "kI", "kD", "kh",
1527 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1528 NULL
1529};
1530#endif
1531
1532/*
1533 * Set terminal options for terminal "term".
1534 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1535 *
1536 * While doing this, until ttest(), some options may be NULL, be careful.
1537 */
1538 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001539set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 struct builtin_term *termp;
1542#ifdef HAVE_TGETENT
1543 int builtin_first = p_tbi;
1544 int try;
1545 int termcap_cleared = FALSE;
1546#endif
1547 int width = 0, height = 0;
1548 char_u *error_msg = NULL;
1549 char_u *bs_p, *del_p;
1550
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001551 /* In silect mode (ex -s) we don't use the 'term' option. */
1552 if (silent_mode)
1553 return OK;
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 detected_8bit = FALSE; /* reset 8-bit detection */
1556
1557 if (term_is_builtin(term))
1558 {
1559 term += 8;
1560#ifdef HAVE_TGETENT
1561 builtin_first = 1;
1562#endif
1563 }
1564
1565/*
1566 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1567 * If builtin_first is TRUE:
1568 * 0. try builtin termcap
1569 * 1. try external termcap
1570 * 2. if both fail default to a builtin terminal
1571 * If builtin_first is FALSE:
1572 * 1. try external termcap
1573 * 2. try builtin termcap, if both fail default to a builtin terminal
1574 */
1575#ifdef HAVE_TGETENT
1576 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1577 {
1578 /*
1579 * Use external termcap
1580 */
1581 if (try == 1)
1582 {
1583 char_u *p;
1584 static char_u tstrbuf[TBUFSZ];
1585 int i;
1586 char_u tbuf[TBUFSZ];
1587 char_u *tp;
1588 static struct {
1589 enum SpecialKey dest; /* index in term_strings[] */
1590 char *name; /* termcap name for string */
1591 } string_names[] =
1592 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1593 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1594 {KS_CL, "cl"}, {KS_CD, "cd"},
1595 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001596 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1598 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001599 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1600 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1602 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1603 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1604 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1605 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001606 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001608 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {KS_TS, "ts"}, {KS_FS, "fs"},
1610 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001611 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001612 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001613 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001614 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1615 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {(enum SpecialKey)0, NULL}
1617 };
1618
1619 /*
1620 * If the external termcap does not have a matching entry, try the
1621 * builtin ones.
1622 */
1623 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1624 {
1625 tp = tstrbuf;
1626 if (!termcap_cleared)
1627 {
1628 clear_termoptions(); /* clear old options */
1629 termcap_cleared = TRUE;
1630 }
1631
1632 /* get output strings */
1633 for (i = 0; string_names[i].name != NULL; ++i)
1634 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001635 if (TERM_STR(string_names[i].dest) == NULL
1636 || TERM_STR(string_names[i].dest) == empty_option)
1637 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 TGETSTR(string_names[i].name, &tp);
1639 }
1640
1641 /* tgetflag() returns 1 if the flag is present, 0 if not and
1642 * possibly -1 if the flag doesn't exist. */
1643 if ((T_MS == NULL || T_MS == empty_option)
1644 && tgetflag("ms") > 0)
1645 T_MS = (char_u *)"y";
1646 if ((T_XS == NULL || T_XS == empty_option)
1647 && tgetflag("xs") > 0)
1648 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001649 if ((T_XN == NULL || T_XN == empty_option)
1650 && tgetflag("xn") > 0)
1651 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if ((T_DB == NULL || T_DB == empty_option)
1653 && tgetflag("db") > 0)
1654 T_DB = (char_u *)"y";
1655 if ((T_DA == NULL || T_DA == empty_option)
1656 && tgetflag("da") > 0)
1657 T_DA = (char_u *)"y";
1658 if ((T_UT == NULL || T_UT == empty_option)
1659 && tgetflag("ut") > 0)
1660 T_UT = (char_u *)"y";
1661
1662
1663 /*
1664 * get key codes
1665 */
1666 for (i = 0; key_names[i] != NULL; ++i)
1667 {
1668 if (find_termcode((char_u *)key_names[i]) == NULL)
1669 {
1670 p = TGETSTR(key_names[i], &tp);
1671 /* if cursor-left == backspace, ignore it (televideo
1672 * 925) */
1673 if (p != NULL
1674 && (*p != Ctrl_H
1675 || key_names[i][0] != 'k'
1676 || key_names[i][1] != 'l'))
1677 add_termcode((char_u *)key_names[i], p, FALSE);
1678 }
1679 }
1680
1681 if (height == 0)
1682 height = tgetnum("li");
1683 if (width == 0)
1684 width = tgetnum("co");
1685
1686 /*
1687 * Get number of colors (if not done already).
1688 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001689 if (TERM_STR(KS_CCO) == NULL
1690 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 set_color_count(tgetnum("Co"));
1692
1693# ifndef hpux
1694 BC = (char *)TGETSTR("bc", &tp);
1695 UP = (char *)TGETSTR("up", &tp);
1696 p = TGETSTR("pc", &tp);
1697 if (p)
1698 PC = *p;
1699# endif /* hpux */
1700 }
1701 }
1702 else /* try == 0 || try == 2 */
1703#endif /* HAVE_TGETENT */
1704 /*
1705 * Use builtin termcap
1706 */
1707 {
1708#ifdef HAVE_TGETENT
1709 /*
1710 * If builtin termcap was already used, there is no need to search
1711 * for the builtin termcap again, quit now.
1712 */
1713 if (try == 2 && builtin_first && termcap_cleared)
1714 break;
1715#endif
1716 /*
1717 * search for 'term' in builtin_termcaps[]
1718 */
1719 termp = find_builtin_term(term);
1720 if (termp->bt_string == NULL) /* did not find it */
1721 {
1722#ifdef HAVE_TGETENT
1723 /*
1724 * If try == 0, first try the external termcap. If that is not
1725 * found we'll get back here with try == 2.
1726 * If termcap_cleared is set we used the external termcap,
1727 * don't complain about not finding the term in the builtin
1728 * termcap.
1729 */
1730 if (try == 0) /* try external one */
1731 continue;
1732 if (termcap_cleared) /* found in external termcap */
1733 break;
1734#endif
1735
1736 mch_errmsg("\r\n");
1737 if (error_msg != NULL)
1738 {
1739 mch_errmsg((char *)error_msg);
1740 mch_errmsg("\r\n");
1741 }
1742 mch_errmsg("'");
1743 mch_errmsg((char *)term);
1744 mch_errmsg(_("' not known. Available builtin terminals are:"));
1745 mch_errmsg("\r\n");
1746 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1747 ++termp)
1748 {
1749 if (termp->bt_entry == (int)KS_NAME)
1750 {
1751#ifdef HAVE_TGETENT
1752 mch_errmsg(" builtin_");
1753#else
1754 mch_errmsg(" ");
1755#endif
1756 mch_errmsg(termp->bt_string);
1757 mch_errmsg("\r\n");
1758 }
1759 }
1760 /* when user typed :set term=xxx, quit here */
1761 if (starting != NO_SCREEN)
1762 {
1763 screen_start(); /* don't know where cursor is now */
1764 wait_return(TRUE);
1765 return FAIL;
1766 }
1767 term = DEFAULT_TERM;
1768 mch_errmsg(_("defaulting to '"));
1769 mch_errmsg((char *)term);
1770 mch_errmsg("'\r\n");
1771 if (emsg_silent == 0)
1772 {
1773 screen_start(); /* don't know where cursor is now */
1774 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001775 if (!is_not_a_term())
1776 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001778 set_string_option_direct((char_u *)"term", -1, term,
1779 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 display_errors();
1781 }
1782 out_flush();
1783#ifdef HAVE_TGETENT
1784 if (!termcap_cleared)
1785 {
1786#endif
1787 clear_termoptions(); /* clear old options */
1788#ifdef HAVE_TGETENT
1789 termcap_cleared = TRUE;
1790 }
1791#endif
1792 parse_builtin_tcap(term);
1793#ifdef FEAT_GUI
1794 if (term_is_gui(term))
1795 {
1796 out_flush();
1797 gui_init();
1798 /* If starting the GUI failed, don't do any of the other
1799 * things for this terminal */
1800 if (!gui.in_use)
1801 return FAIL;
1802#ifdef HAVE_TGETENT
1803 break; /* don't try using external termcap */
1804#endif
1805 }
1806#endif /* FEAT_GUI */
1807 }
1808#ifdef HAVE_TGETENT
1809 }
1810#endif
1811
1812/*
1813 * special: There is no info in the termcap about whether the cursor
1814 * positioning is relative to the start of the screen or to the start of the
1815 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1816 * relative.
1817 */
1818 if (STRCMP(term, "pcterm") == 0)
1819 T_CCS = (char_u *)"yes";
1820 else
1821 T_CCS = empty_option;
1822
1823#ifdef UNIX
1824/*
1825 * Any "stty" settings override the default for t_kb from the termcap.
1826 * This is in os_unix.c, because it depends a lot on the version of unix that
1827 * is being used.
1828 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1829 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001830# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 get_stty();
1834#endif
1835
1836/*
1837 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1838 * didn't work, use the default CTRL-H
1839 * The default for t_kD is DEL, unless t_kb is DEL.
1840 * The vim_strsave'd strings are probably lost forever, well it's only two
1841 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1842 * directly.
1843 */
1844#ifdef FEAT_GUI
1845 if (!gui.in_use)
1846#endif
1847 {
1848 bs_p = find_termcode((char_u *)"kb");
1849 del_p = find_termcode((char_u *)"kD");
1850 if (bs_p == NULL || *bs_p == NUL)
1851 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1852 if ((del_p == NULL || *del_p == NUL) &&
1853 (bs_p == NULL || *bs_p != DEL))
1854 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1855 }
1856
1857#if defined(UNIX) || defined(VMS)
1858 term_is_xterm = vim_is_xterm(term);
1859#endif
1860
1861#ifdef FEAT_MOUSE
1862# if defined(UNIX) || defined(VMS)
1863# ifdef FEAT_MOUSE_TTY
1864 /*
1865 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1866 * The termcode for the mouse is added as a side effect in option.c.
1867 */
1868 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001869 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001872 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 if (use_xterm_mouse())
1875 p = NULL; /* keep existing value, might be "xterm2" */
1876 else
1877 p = (char_u *)"xterm";
1878 }
1879# endif
1880 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001883 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1884 * "xterm2" in check_termcode(). */
1885 reset_option_was_set((char_u *)"ttym");
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (p == NULL
1888# ifdef FEAT_GUI
1889 || gui.in_use
1890# endif
1891 )
1892 check_mouse_termcode(); /* set mouse termcode anyway */
1893 }
1894# endif
1895# else
1896 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1897# endif
1898#endif /* FEAT_MOUSE */
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#ifdef USE_TERM_CONSOLE
1901 /* DEFAULT_TERM indicates that it is the machine console. */
1902 if (STRCMP(term, DEFAULT_TERM) != 0)
1903 term_console = FALSE;
1904 else
1905 {
1906 term_console = TRUE;
1907# ifdef AMIGA
1908 win_resize_on(); /* enable window resizing reports */
1909# endif
1910 }
1911#endif
1912
1913#if defined(UNIX) || defined(VMS)
1914 /*
1915 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1916 */
1917 if (vim_is_fastterm(term))
1918 p_tf = TRUE;
1919#endif
1920#ifdef USE_TERM_CONSOLE
1921 /*
1922 * 'ttyfast' is default on consoles
1923 */
1924 if (term_console)
1925 p_tf = TRUE;
1926#endif
1927
1928 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1929
1930 full_screen = TRUE; /* we can use termcap codes from now on */
1931 set_term_defaults(); /* use current values as defaults */
1932#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001933 LOG_TR("setting crv_status to STATUS_GET");
1934 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#endif
1936
1937 /*
1938 * Initialize the terminal with the appropriate termcap codes.
1939 * Set the mouse and window title if possible.
1940 * Don't do this when starting, need to parse the .vimrc first, because it
1941 * may redefine t_TI etc.
1942 */
1943 if (starting != NO_SCREEN)
1944 {
1945 starttermcap(); /* may change terminal mode */
1946#ifdef FEAT_MOUSE
1947 setmouse(); /* may start using the mouse */
1948#endif
1949#ifdef FEAT_TITLE
1950 maketitle(); /* may display window title */
1951#endif
1952 }
1953
1954 /* display initial screen after ttest() checking. jw. */
1955 if (width <= 0 || height <= 0)
1956 {
1957 /* termcap failed to report size */
1958 /* set defaults, in case ui_get_shellsize() also fails */
1959 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001960#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 height = 25; /* console is often 25 lines */
1962#else
1963 height = 24; /* most terminals are 24 lines */
1964#endif
1965 }
1966 set_shellsize(width, height, FALSE); /* may change Rows */
1967 if (starting != NO_SCREEN)
1968 {
1969 if (scroll_region)
1970 scroll_region_reset(); /* In case Rows changed */
1971 check_map_keycodes(); /* check mappings for terminal codes used */
1972
1973#ifdef FEAT_AUTOCMD
1974 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001975 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 /*
1978 * Execute the TermChanged autocommands for each buffer that is
1979 * loaded.
1980 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001981 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001982 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 if (curbuf->b_ml.ml_mfp != NULL)
1985 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1986 curbuf);
1987 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001988 if (bufref_valid(&old_curbuf))
1989 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991#endif
1992 }
1993
1994#ifdef FEAT_TERMRESPONSE
1995 may_req_termresponse();
1996#endif
1997
1998 return OK;
1999}
2000
2001#if defined(FEAT_MOUSE) || defined(PROTO)
2002
2003# ifdef FEAT_MOUSE_TTY
2004# define HMT_NORMAL 1
2005# define HMT_NETTERM 2
2006# define HMT_DEC 4
2007# define HMT_JSBTERM 8
2008# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002009# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002010# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002011# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012static int has_mouse_termcode = 0;
2013# endif
2014
Bram Moolenaar864207d2008-06-24 22:14:38 +00002015# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002017set_mouse_termcode(
2018 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2019 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
2021 char_u name[2];
2022
2023 name[0] = n;
2024 name[1] = KE_FILLER;
2025 add_termcode(name, s, FALSE);
2026# ifdef FEAT_MOUSE_TTY
2027# ifdef FEAT_MOUSE_JSB
2028 if (n == KS_JSBTERM_MOUSE)
2029 has_mouse_termcode |= HMT_JSBTERM;
2030 else
2031# endif
2032# ifdef FEAT_MOUSE_NET
2033 if (n == KS_NETTERM_MOUSE)
2034 has_mouse_termcode |= HMT_NETTERM;
2035 else
2036# endif
2037# ifdef FEAT_MOUSE_DEC
2038 if (n == KS_DEC_MOUSE)
2039 has_mouse_termcode |= HMT_DEC;
2040 else
2041# endif
2042# ifdef FEAT_MOUSE_PTERM
2043 if (n == KS_PTERM_MOUSE)
2044 has_mouse_termcode |= HMT_PTERM;
2045 else
2046# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002047# ifdef FEAT_MOUSE_URXVT
2048 if (n == KS_URXVT_MOUSE)
2049 has_mouse_termcode |= HMT_URXVT;
2050 else
2051# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002052# ifdef FEAT_MOUSE_SGR
2053 if (n == KS_SGR_MOUSE)
2054 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002055 else if (n == KS_SGR_MOUSE_RELEASE)
2056 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002057 else
2058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 has_mouse_termcode |= HMT_NORMAL;
2060# endif
2061}
2062# endif
2063
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002064# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002065 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002067del_mouse_termcode(
2068 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 char_u name[2];
2071
2072 name[0] = n;
2073 name[1] = KE_FILLER;
2074 del_termcode(name);
2075# ifdef FEAT_MOUSE_TTY
2076# ifdef FEAT_MOUSE_JSB
2077 if (n == KS_JSBTERM_MOUSE)
2078 has_mouse_termcode &= ~HMT_JSBTERM;
2079 else
2080# endif
2081# ifdef FEAT_MOUSE_NET
2082 if (n == KS_NETTERM_MOUSE)
2083 has_mouse_termcode &= ~HMT_NETTERM;
2084 else
2085# endif
2086# ifdef FEAT_MOUSE_DEC
2087 if (n == KS_DEC_MOUSE)
2088 has_mouse_termcode &= ~HMT_DEC;
2089 else
2090# endif
2091# ifdef FEAT_MOUSE_PTERM
2092 if (n == KS_PTERM_MOUSE)
2093 has_mouse_termcode &= ~HMT_PTERM;
2094 else
2095# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002096# ifdef FEAT_MOUSE_URXVT
2097 if (n == KS_URXVT_MOUSE)
2098 has_mouse_termcode &= ~HMT_URXVT;
2099 else
2100# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002101# ifdef FEAT_MOUSE_SGR
2102 if (n == KS_SGR_MOUSE)
2103 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002104 else if (n == KS_SGR_MOUSE_RELEASE)
2105 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002106 else
2107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 has_mouse_termcode &= ~HMT_NORMAL;
2109# endif
2110}
2111# endif
2112#endif
2113
2114#ifdef HAVE_TGETENT
2115/*
2116 * Call tgetent()
2117 * Return error message if it fails, NULL if it's OK.
2118 */
2119 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002120tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 int i;
2123
2124 i = TGETENT(tbuf, term);
2125 if (i < 0 /* -1 is always an error */
2126# ifdef TGETENT_ZERO_ERR
2127 || i == 0 /* sometimes zero is also an error */
2128# endif
2129 )
2130 {
2131 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2132 * tgetent() with the always existing "dumb" entry to avoid a crash or
2133 * hang. */
2134 (void)TGETENT(tbuf, "dumb");
2135
2136 if (i < 0)
2137# ifdef TGETENT_ZERO_ERR
2138 return (char_u *)_("E557: Cannot open termcap file");
2139 if (i == 0)
2140# endif
2141#ifdef TERMINFO
2142 return (char_u *)_("E558: Terminal entry not found in terminfo");
2143#else
2144 return (char_u *)_("E559: Terminal entry not found in termcap");
2145#endif
2146 }
2147 return NULL;
2148}
2149
2150/*
2151 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2152 * Fix that here.
2153 */
2154 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002155vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 char *p;
2158
2159 p = tgetstr(s, (char **)pp);
2160 if (p == (char *)-1)
2161 p = NULL;
2162 return (char_u *)p;
2163}
2164#endif /* HAVE_TGETENT */
2165
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002166#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167/*
2168 * Get Columns and Rows from the termcap. Used after a window signal if the
2169 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2170 * and "li" entries never change. But on some systems this works.
2171 * Errors while getting the entries are ignored.
2172 */
2173 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002174getlinecol(
2175 long *cp, /* pointer to columns */
2176 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 char_u tbuf[TBUFSZ];
2179
2180 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2181 {
2182 if (*cp == 0)
2183 *cp = tgetnum("co");
2184 if (*rp == 0)
2185 *rp = tgetnum("li");
2186 }
2187}
2188#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2189
2190/*
2191 * Get a string entry from the termcap and add it to the list of termcodes.
2192 * Used for <t_xx> special keys.
2193 * Give an error message for failure when not sourcing.
2194 * If force given, replace an existing entry.
2195 * Return FAIL if the entry was not found, OK if the entry was added.
2196 */
2197 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002198add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 char_u *term;
2201 int key;
2202 struct builtin_term *termp;
2203#ifdef HAVE_TGETENT
2204 char_u *string;
2205 int i;
2206 int builtin_first;
2207 char_u tbuf[TBUFSZ];
2208 char_u tstrbuf[TBUFSZ];
2209 char_u *tp = tstrbuf;
2210 char_u *error_msg = NULL;
2211#endif
2212
2213/*
2214 * If the GUI is running or will start in a moment, we only support the keys
2215 * that the GUI can produce.
2216 */
2217#ifdef FEAT_GUI
2218 if (gui.in_use || gui.starting)
2219 return gui_mch_haskey(name);
2220#endif
2221
2222 if (!force && find_termcode(name) != NULL) /* it's already there */
2223 return OK;
2224
2225 term = T_NAME;
2226 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2227 return FAIL;
2228
2229 if (term_is_builtin(term)) /* name starts with "builtin_" */
2230 {
2231 term += 8;
2232#ifdef HAVE_TGETENT
2233 builtin_first = TRUE;
2234#endif
2235 }
2236#ifdef HAVE_TGETENT
2237 else
2238 builtin_first = p_tbi;
2239#endif
2240
2241#ifdef HAVE_TGETENT
2242/*
2243 * We can get the entry from the builtin termcap and from the external one.
2244 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2245 * builtin termcap first.
2246 * If 'ttybuiltin' is off, try external termcap first.
2247 */
2248 for (i = 0; i < 2; ++i)
2249 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002250 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252 /*
2253 * Search in builtin termcap
2254 */
2255 {
2256 termp = find_builtin_term(term);
2257 if (termp->bt_string != NULL) /* found it */
2258 {
2259 key = TERMCAP2KEY(name[0], name[1]);
2260 while (termp->bt_entry != (int)KS_NAME)
2261 {
2262 if ((int)termp->bt_entry == key)
2263 {
2264 add_termcode(name, (char_u *)termp->bt_string,
2265 term_is_8bit(term));
2266 return OK;
2267 }
2268 ++termp;
2269 }
2270 }
2271 }
2272#ifdef HAVE_TGETENT
2273 else
2274 /*
2275 * Search in external termcap
2276 */
2277 {
2278 error_msg = tgetent_error(tbuf, term);
2279 if (error_msg == NULL)
2280 {
2281 string = TGETSTR((char *)name, &tp);
2282 if (string != NULL && *string != NUL)
2283 {
2284 add_termcode(name, string, FALSE);
2285 return OK;
2286 }
2287 }
2288 }
2289 }
2290#endif
2291
2292 if (sourcing_name == NULL)
2293 {
2294#ifdef HAVE_TGETENT
2295 if (error_msg != NULL)
2296 EMSG(error_msg);
2297 else
2298#endif
2299 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2300 }
2301 return FAIL;
2302}
2303
2304 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002305term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2308}
2309
2310/*
2311 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2312 * Assume that the terminal is using 8-bit controls when the name contains
2313 * "8bit", like in "xterm-8bit".
2314 */
2315 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2319}
2320
2321/*
2322 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002323 * <Esc>[ -> CSI <M_C_[>
2324 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * <Esc>O -> <M-C-O>
2326 */
2327 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002328term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 if (*p == ESC)
2331 {
2332 if (p[1] == '[')
2333 return CSI;
2334 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002335 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (p[1] == 'O')
2337 return 0x8f;
2338 }
2339 return 0;
2340}
2341
2342#ifdef FEAT_GUI
2343 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002344term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2347}
2348#endif
2349
2350#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2351
2352 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002353tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 static char_u buf[16];
2356 char_u *p;
2357
2358 p = buf + 15;
2359 *p = '\0';
2360 do
2361 {
2362 --p;
2363 *p = (char_u) (i % 10 + '0');
2364 i /= 10;
2365 }
2366 while (i > 0 && p > buf);
2367 return p;
2368}
2369#endif
2370
2371#ifndef HAVE_TGETENT
2372
2373/*
2374 * minimal tgoto() implementation.
2375 * no padding and we only parse for %i %d and %+char
2376 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002377static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002379 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002380tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 static char buf[30];
2383 char *p, *s, *e;
2384
2385 if (!cm)
2386 return "OOPS";
2387 e = buf + 29;
2388 for (s = buf; s < e && *cm; cm++)
2389 {
2390 if (*cm != '%')
2391 {
2392 *s++ = *cm;
2393 continue;
2394 }
2395 switch (*++cm)
2396 {
2397 case 'd':
2398 p = (char *)tltoa((unsigned long)y);
2399 y = x;
2400 while (*p)
2401 *s++ = *p++;
2402 break;
2403 case 'i':
2404 x++;
2405 y++;
2406 break;
2407 case '+':
2408 *s++ = (char)(*++cm + y);
2409 y = x;
2410 break;
2411 case '%':
2412 *s++ = *cm;
2413 break;
2414 default:
2415 return "OOPS";
2416 }
2417 }
2418 *s = '\0';
2419 return buf;
2420}
2421
2422#endif /* HAVE_TGETENT */
2423
2424/*
2425 * Set the terminal name and initialize the terminal options.
2426 * If "name" is NULL or empty, get the terminal name from the environment.
2427 * If that fails, use the default terminal name.
2428 */
2429 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002430termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 char_u *term;
2433
2434 if (name != NULL && *name == NUL)
2435 name = NULL; /* empty name is equal to no name */
2436 term = name;
2437
2438#ifdef __BEOS__
2439 /*
2440 * TERM environment variable is normally set to 'ansi' on the Bebox;
2441 * Since the BeBox doesn't quite support full ANSI yet, we use our
2442 * own custom 'ansi-beos' termcap instead, unless the -T option has
2443 * been given on the command line.
2444 */
2445 if (term == NULL
2446 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2447 term = DEFAULT_TERM;
2448#endif
2449#ifndef MSWIN
2450 if (term == NULL)
2451 term = mch_getenv((char_u *)"TERM");
2452#endif
2453 if (term == NULL || *term == NUL)
2454 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002455 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /* Set the default terminal name. */
2458 set_string_default("term", term);
2459 set_string_default("ttytype", term);
2460
2461 /*
2462 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2463 */
2464 set_termname(T_NAME != NULL ? T_NAME : term);
2465}
2466
2467/*
2468 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2469 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002470#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2472static char_u out_buf[OUT_SIZE + 1];
2473static int out_pos = 0; /* number of chars in out_buf */
2474
2475/*
2476 * out_flush(): flush the output buffer
2477 */
2478 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002479out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 int len;
2482
2483 if (out_pos != 0)
2484 {
2485 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2486 len = out_pos;
2487 out_pos = 0;
2488 ui_write(out_buf, len);
2489 }
2490}
2491
2492#if defined(FEAT_MBYTE) || defined(PROTO)
2493/*
2494 * Sometimes a byte out of a multi-byte character is written with out_char().
2495 * To avoid flushing half of the character, call this function first.
2496 */
2497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002498out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2501 out_flush();
2502}
2503#endif
2504
2505#ifdef FEAT_GUI
2506/*
2507 * out_trash(): Throw away the contents of the output buffer
2508 */
2509 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002510out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 out_pos = 0;
2513}
2514#endif
2515
2516/*
2517 * out_char(c): put a byte into the output buffer.
2518 * Flush it if it becomes full.
2519 * This should not be used for outputting text on the screen (use functions
2520 * like msg_puts() and screen_putchar() for that).
2521 */
2522 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002523out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2526 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2527 out_char('\r');
2528#endif
2529
2530 out_buf[out_pos++] = c;
2531
2532 /* For testing we flush each time. */
2533 if (out_pos >= OUT_SIZE || p_wd)
2534 out_flush();
2535}
2536
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002537static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539/*
2540 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2541 */
2542 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002543out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2546 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2547 out_char_nf('\r');
2548#endif
2549
2550 out_buf[out_pos++] = c;
2551
2552 if (out_pos >= OUT_SIZE)
2553 out_flush();
2554}
2555
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002556#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2557 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558/*
2559 * A never-padding out_str.
2560 * use this whenever you don't want to run the string through tputs.
2561 * tputs above is harmless, but tputs from the termcap library
2562 * is likely to strip off leading digits, that it mistakes for padding
2563 * information, and "%i", "%d", etc.
2564 * This should only be used for writing terminal codes, not for outputting
2565 * normal text (use functions like msg_puts() and screen_putchar() for that).
2566 */
2567 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002568out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2571 out_flush();
2572 while (*s)
2573 out_char_nf(*s++);
2574
2575 /* For testing we write one string at a time. */
2576 if (p_wd)
2577 out_flush();
2578}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002582 * A conditional-flushing out_str, mainly for visualbell.
2583 * Handles a delay internally, because termlib may not respect the delay or do
2584 * it at the wrong time.
2585 * Note: Only for terminal strings.
2586 */
2587 void
2588out_str_cf(char_u *s)
2589{
2590 if (s != NULL && *s)
2591 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002592#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002593 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002594#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002595
2596#ifdef FEAT_GUI
2597 /* Don't use tputs() when GUI is used, ncurses crashes. */
2598 if (gui.in_use)
2599 {
2600 out_str_nf(s);
2601 return;
2602 }
2603#endif
2604 if (out_pos > OUT_SIZE - 20)
2605 out_flush();
2606#ifdef HAVE_TGETENT
2607 for (p = s; *s; ++s)
2608 {
2609 /* flush just before delay command */
2610 if (*s == '$' && *(s + 1) == '<')
2611 {
2612 char_u save_c = *s;
2613 int duration = atoi((char *)s + 2);
2614
2615 *s = NUL;
2616 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2617 *s = save_c;
2618 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002619# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002620 /* Only sleep here if we can limit this happening in
2621 * vim_beep(). */
2622 p = vim_strchr(s, '>');
2623 if (p == NULL || duration <= 0)
2624 {
2625 /* can't parse the time, don't sleep here */
2626 p = s;
2627 }
2628 else
2629 {
2630 ++p;
2631 do_sleep(duration);
2632 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002633# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002634 /* Rely on the terminal library to sleep. */
2635 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002636# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002637 break;
2638 }
2639 }
2640 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2641#else
2642 while (*s)
2643 out_char_nf(*s++);
2644#endif
2645
2646 /* For testing we write one string at a time. */
2647 if (p_wd)
2648 out_flush();
2649 }
2650}
2651
2652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 * out_str(s): Put a character string a byte at a time into the output buffer.
2654 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2655 * This should only be used for writing terminal codes, not for outputting
2656 * normal text (use functions like msg_puts() and screen_putchar() for that).
2657 */
2658 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002659out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 if (s != NULL && *s)
2662 {
2663#ifdef FEAT_GUI
2664 /* Don't use tputs() when GUI is used, ncurses crashes. */
2665 if (gui.in_use)
2666 {
2667 out_str_nf(s);
2668 return;
2669 }
2670#endif
2671 /* avoid terminal strings being split up */
2672 if (out_pos > OUT_SIZE - 20)
2673 out_flush();
2674#ifdef HAVE_TGETENT
2675 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2676#else
2677 while (*s)
2678 out_char_nf(*s++);
2679#endif
2680
2681 /* For testing we write one string at a time. */
2682 if (p_wd)
2683 out_flush();
2684 }
2685}
2686
2687/*
2688 * cursor positioning using termcap parser. (jw)
2689 */
2690 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002691term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 OUT_STR(tgoto((char *)T_CM, col, row));
2694}
2695
2696 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002697term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 OUT_STR(tgoto((char *)T_CRI, 0, i));
2700}
2701
2702 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002703term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2706}
2707
2708 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002709term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2712}
2713
2714#if defined(HAVE_TGETENT) || defined(PROTO)
2715 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002716term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
2718 /* Can't handle a negative value here */
2719 if (x < 0)
2720 x = 0;
2721 if (y < 0)
2722 y = 0;
2723 OUT_STR(tgoto((char *)T_CWP, y, x));
2724}
2725
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002726# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2727/*
2728 * Return TRUE if we can request the terminal for a response.
2729 */
2730 static int
2731can_get_termresponse()
2732{
2733 return cur_tmode == TMODE_RAW
2734 && termcap_active
2735# ifdef UNIX
2736 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2737# endif
2738 && p_ek;
2739}
2740
2741static int winpos_x;
2742static int winpos_y;
2743static int waiting_for_winpos = FALSE;
2744
2745/*
2746 * Try getting the Vim window position from the terminal.
2747 * Returns OK or FAIL.
2748 */
2749 int
2750term_get_winpos(int *x, int *y)
2751{
2752 int count = 0;
2753
2754 if (*T_CGP == NUL || !can_get_termresponse())
2755 return FAIL;
2756 winpos_x = -1;
2757 winpos_y = -1;
2758 waiting_for_winpos = TRUE;
2759 OUT_STR(T_CGP);
2760 out_flush();
2761
2762 /* Try reading the result for 100 msec. */
2763 while (count++ < 10)
2764 {
2765 (void)vpeekc_nomap();
2766 if (winpos_x >= 0 && winpos_y >= 0)
2767 {
2768 *x = winpos_x;
2769 *y = winpos_y;
2770 waiting_for_winpos = FALSE;
2771 return OK;
2772 }
2773 ui_delay(10, FALSE);
2774 }
2775 waiting_for_winpos = FALSE;
2776 return FALSE;
2777}
2778# endif
2779
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002781term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002783 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
2785#endif
2786
2787 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2791 if (*T_CAF)
2792 term_color(T_CAF, n);
2793 else if (*T_CSF)
2794 term_color(T_CSF, n);
2795}
2796
2797 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002798term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2801 if (*T_CAB)
2802 term_color(T_CAB, n);
2803 else if (*T_CSB)
2804 term_color(T_CSB, n);
2805}
2806
2807 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002808term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 char buf[20];
2811 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2812
2813 /* Special handling of 16 colors, because termcap can't handle it */
2814 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2815 /* Also accept CSI instead of <Esc>[ */
2816 if (n >= 8 && t_colors >= 16
2817 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2818 && s[i] != NUL
2819 && (STRCMP(s + i + 1, "%p1%dm") == 0
2820 || STRCMP(s + i + 1, "%dm") == 0)
2821 && (s[i] == '3' || s[i] == '4'))
2822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002824 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002826 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002828 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2830 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2831 : (n >= 16 ? "48;5;" : "10"));
2832 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2833 }
2834 else
2835 OUT_STR(tgoto((char *)s, 0, n));
2836}
2837
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002838#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002839
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002840#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2841#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2842#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002843
2844 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002845term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002846{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002847#define MAX_COLOR_STR_LEN 100
2848 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002849
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002850 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002851 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002852 OUT_STR(buf);
2853}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002854
2855 void
2856term_fg_rgb_color(guicolor_T rgb)
2857{
2858 term_rgb_color(T_8F, rgb);
2859}
2860
2861 void
2862term_bg_rgb_color(guicolor_T rgb)
2863{
2864 term_rgb_color(T_8B, rgb);
2865}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002866#endif
2867
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002868#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2869 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
2871 * Generic function to set window title, using t_ts and t_fs.
2872 */
2873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002874term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 /* t_ts takes one argument: column in status line */
2877 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2878 out_str_nf(title);
2879 out_str(T_FS); /* set title end */
2880 out_flush();
2881}
2882#endif
2883
2884/*
2885 * Make sure we have a valid set or terminal options.
2886 * Replace all entries that are NULL by empty_option
2887 */
2888 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002889ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002891 char_u *env_colors;
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 check_options(); /* make sure no options are NULL */
2894
2895 /*
2896 * MUST have "cm": cursor motion.
2897 */
2898 if (*T_CM == NUL)
2899 EMSG(_("E437: terminal capability \"cm\" required"));
2900
2901 /*
2902 * if "cs" defined, use a scroll region, it's faster.
2903 */
2904 if (*T_CS != NUL)
2905 scroll_region = TRUE;
2906 else
2907 scroll_region = FALSE;
2908
2909 if (pairs)
2910 {
2911 /*
2912 * optional pairs
2913 */
2914 /* TP goes to normal mode for TI (invert) and TB (bold) */
2915 if (*T_ME == NUL)
2916 T_ME = T_MR = T_MD = T_MB = empty_option;
2917 if (*T_SO == NUL || *T_SE == NUL)
2918 T_SO = T_SE = empty_option;
2919 if (*T_US == NUL || *T_UE == NUL)
2920 T_US = T_UE = empty_option;
2921 if (*T_CZH == NUL || *T_CZR == NUL)
2922 T_CZH = T_CZR = empty_option;
2923
2924 /* T_VE is needed even though T_VI is not defined */
2925 if (*T_VE == NUL)
2926 T_VI = empty_option;
2927
2928 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2929 if (*T_ME == NUL)
2930 {
2931 T_ME = T_SE;
2932 T_MR = T_SO;
2933 T_MD = T_SO;
2934 }
2935
2936 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2937 if (*T_SO == NUL)
2938 {
2939 T_SE = T_ME;
2940 if (*T_MR == NUL)
2941 T_SO = T_MD;
2942 else
2943 T_SO = T_MR;
2944 }
2945
2946 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2947 if (*T_CZH == NUL)
2948 {
2949 T_CZR = T_ME;
2950 if (*T_MR == NUL)
2951 T_CZH = T_MD;
2952 else
2953 T_CZH = T_MR;
2954 }
2955
2956 /* "Sb" and "Sf" come in pairs */
2957 if (*T_CSB == NUL || *T_CSF == NUL)
2958 {
2959 T_CSB = empty_option;
2960 T_CSF = empty_option;
2961 }
2962
2963 /* "AB" and "AF" come in pairs */
2964 if (*T_CAB == NUL || *T_CAF == NUL)
2965 {
2966 T_CAB = empty_option;
2967 T_CAF = empty_option;
2968 }
2969
2970 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2971 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002972 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 /* Set 'weirdinvert' according to value of 't_xs' */
2975 p_wiv = (*T_XS != NUL);
2976 }
2977 need_gather = TRUE;
2978
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002979 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002981 env_colors = mch_getenv((char_u *)"COLORS");
2982 if (env_colors != NULL && isdigit(*env_colors))
2983 {
2984 int colors = atoi((char *)env_colors);
2985
2986 if (colors != t_colors)
2987 set_color_count(colors);
2988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990
2991#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2992 || defined(PROTO)
2993/*
2994 * Represent the given long_u as individual bytes, with the most significant
2995 * byte first, and store them in dst.
2996 */
2997 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002998add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 int i;
3001 int shift;
3002
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003003 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 {
3005 shift = 8 * (sizeof(long_u) - i);
3006 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3007 }
3008}
3009
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003010static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012/*
3013 * Interpret the next string of bytes in buf as a long integer, with the most
3014 * significant byte first. Note that it is assumed that buf has been through
3015 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3016 * Puts result in val, and returns the number of bytes read from buf
3017 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3018 * were present.
3019 */
3020 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003021get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int len;
3024 char_u bytes[sizeof(long_u)];
3025 int i;
3026 int shift;
3027
3028 *val = 0;
3029 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3030 if (len != -1)
3031 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003032 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 shift = 8 * (sizeof(long_u) - 1 - i);
3035 *val += (long_u)bytes[i] << shift;
3036 }
3037 }
3038 return len;
3039}
3040#endif
3041
3042#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003043 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3044 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045/*
3046 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3047 * that buf has been through inchar(). Returns the actual number of bytes used
3048 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3049 * available.
3050 */
3051 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003052get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int len = 0;
3055 int i;
3056 char_u c;
3057
3058 for (i = 0; i < num_bytes; i++)
3059 {
3060 if ((c = buf[len++]) == NUL)
3061 return -1;
3062 if (c == K_SPECIAL)
3063 {
3064 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3065 return -1;
3066 if (buf[len++] == (int)KS_ZERO)
3067 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003068 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3069 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3070 if (buf[len++] == (int)KE_CSI)
3071 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003073 else if (c == CSI && buf[len] == KS_EXTRA
3074 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3076 * the start of a special key, see add_to_input_buf_csi(). */
3077 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 bytes[i] = c;
3079 }
3080 return len;
3081}
3082#endif
3083
3084/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003085 * Check if the new shell size is valid, correct it if it's too small or way
3086 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 */
3088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003089check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (Rows < min_rows()) /* need room for one window and command line */
3092 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003093 limit_screen_size();
3094}
3095
3096/*
3097 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3098 */
3099 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003100limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003101{
3102 if (Columns < MIN_COLUMNS)
3103 Columns = MIN_COLUMNS;
3104 else if (Columns > 10000)
3105 Columns = 10000;
3106 if (Rows > 1000)
3107 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108}
3109
Bram Moolenaar86b68352004-12-27 21:59:20 +00003110/*
3111 * Invoked just before the screen structures are going to be (re)allocated.
3112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003114win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 static int old_Rows = 0;
3117 static int old_Columns = 0;
3118
3119 if (old_Rows != Rows || old_Columns != Columns)
3120 ui_new_shellsize();
3121 if (old_Rows != Rows)
3122 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003123 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003124 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003125 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 old_Rows = Rows;
3127 shell_new_rows(); /* update window sizes */
3128 }
3129 if (old_Columns != Columns)
3130 {
3131 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003132#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 shell_new_columns(); /* update window sizes */
3134#endif
3135 }
3136}
3137
3138/*
3139 * Call this function when the Vim shell has been resized in any way.
3140 * Will obtain the current size and redraw (also when size didn't change).
3141 */
3142 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003143shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 set_shellsize(0, 0, FALSE);
3146}
3147
3148/*
3149 * Check if the shell size changed. Handle a resize.
3150 * When the size didn't change, nothing happens.
3151 */
3152 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003153shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 int old_Rows = Rows;
3156 int old_Columns = Columns;
3157
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003158 if (!exiting
3159#ifdef FEAT_GUI
3160 /* Do not get the size when executing a shell command during
3161 * startup. */
3162 && !gui.starting
3163#endif
3164 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003165 {
3166 (void)ui_get_shellsize();
3167 check_shellsize();
3168 if (old_Rows != Rows || old_Columns != Columns)
3169 shell_resized();
3170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171}
3172
3173/*
3174 * Set size of the Vim shell.
3175 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3176 * window size (this is used for the :win command).
3177 * If 'mustset' is FALSE, we may try to get the real window size and if
3178 * it fails use 'width' and 'height'.
3179 */
3180 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003181set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 static int busy = FALSE;
3184
3185 /*
3186 * Avoid recursiveness, can happen when setting the window size causes
3187 * another window-changed signal.
3188 */
3189 if (busy)
3190 return;
3191
3192 if (width < 0 || height < 0) /* just checking... */
3193 return;
3194
Bram Moolenaara971b822011-09-14 14:43:25 +02003195 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003197 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 State = SETWSIZE;
3199 return;
3200 }
3201
Bram Moolenaara971b822011-09-14 14:43:25 +02003202 /* curwin->w_buffer can be NULL when we are closing a window and the
3203 * buffer has already been closed and removing a scrollbar causes a resize
3204 * event. Don't resize then, it will happen after entering another buffer.
3205 */
3206 if (curwin->w_buffer == NULL)
3207 return;
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 ++busy;
3210
3211#ifdef AMIGA
3212 out_flush(); /* must do this before mch_get_shellsize() for
3213 some obscure reason */
3214#endif
3215
3216 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3217 {
3218 Rows = height;
3219 Columns = width;
3220 check_shellsize();
3221 ui_set_shellsize(mustset);
3222 }
3223 else
3224 check_shellsize();
3225
3226 /* The window layout used to be adjusted here, but it now happens in
3227 * screenalloc() (also invoked from screenclear()). That is because the
3228 * "busy" check above may skip this, but not screenalloc(). */
3229
3230 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3231 screenclear();
3232 else
3233 screen_start(); /* don't know where cursor is now */
3234
3235 if (starting != NO_SCREEN)
3236 {
3237#ifdef FEAT_TITLE
3238 maketitle();
3239#endif
3240 changed_line_abv_curs();
3241 invalidate_botline();
3242
3243 /*
3244 * We only redraw when it's needed:
3245 * - While at the more prompt or executing an external command, don't
3246 * redraw, but position the cursor.
3247 * - While editing the command line, only redraw that.
3248 * - in Ex mode, don't redraw anything.
3249 * - Otherwise, redraw right now, and position the cursor.
3250 * Always need to call update_screen() or screenalloc(), to make
3251 * sure Rows/Columns and the size of ScreenLines[] is correct!
3252 */
3253 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3254 || exmode_active)
3255 {
3256 screenalloc(FALSE);
3257 repeat_message();
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else
3260 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003261#ifdef FEAT_SCROLLBIND
3262 if (curwin->w_p_scb)
3263 do_check_scrollbind(TRUE);
3264#endif
3265 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003266 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003267 update_screen(NOT_VALID);
3268 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003269 }
3270 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003271 {
3272 update_topline();
3273#if defined(FEAT_INS_EXPAND)
3274 if (pum_visible())
3275 {
3276 redraw_later(NOT_VALID);
3277 ins_compl_show_pum(); /* This includes the redraw. */
3278 }
3279 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003280#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003281 update_screen(NOT_VALID);
3282 if (redrawing())
3283 setcursor();
3284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286 cursor_on(); /* redrawing may have switched it off */
3287 }
3288 out_flush();
3289 --busy;
3290}
3291
3292/*
3293 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3294 * commands and Ex mode).
3295 */
3296 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003297settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299#ifdef FEAT_GUI
3300 /* don't set the term where gvim was started to any mode */
3301 if (gui.in_use)
3302 return;
3303#endif
3304
3305 if (full_screen)
3306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /*
3308 * When returning after calling a shell we want to really set the
3309 * terminal to raw mode, even though we think it already is, because
3310 * the shell program may have reset the terminal mode.
3311 * When we think the terminal is normal, don't try to set it to
3312 * normal again, because that causes problems (logout!) on some
3313 * machines.
3314 */
3315 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3316 {
3317#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003318# ifdef FEAT_GUI
3319 if (!gui.in_use && !gui.starting)
3320# endif
3321 {
3322 /* May need to check for T_CRV response and termcodes, it
3323 * doesn't work in Cooked mode, an external program may get
3324 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003325 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3326 || u7_status == STATUS_SENT
3327 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003328 || rbm_status == STATUS_SENT
3329 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003330 (void)vpeekc_nomap();
3331 check_for_codes_from_term();
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
3334#ifdef FEAT_MOUSE_TTY
3335 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003336 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003338 if (tmode != TMODE_RAW)
3339 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003341 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 cur_tmode = tmode;
3343#ifdef FEAT_MOUSE
3344 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003345 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003347 if (tmode == TMODE_RAW)
3348 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 out_flush();
3350 }
3351#ifdef FEAT_TERMRESPONSE
3352 may_req_termresponse();
3353#endif
3354 }
3355}
3356
3357 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003358starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (full_screen && !termcap_active)
3361 {
3362 out_str(T_TI); /* start termcap mode */
3363 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003364 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 out_flush();
3366 termcap_active = TRUE;
3367 screen_start(); /* don't know where cursor is now */
3368#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003369# ifdef FEAT_GUI
3370 if (!gui.in_use && !gui.starting)
3371# endif
3372 {
3373 may_req_termresponse();
3374 /* Immediately check for a response. If t_Co changes, we don't
3375 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003376 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003377 check_for_codes_from_term();
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380 }
3381}
3382
3383 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003384stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 screen_stop_highlight();
3387 reset_cterm_colors();
3388 if (termcap_active)
3389 {
3390#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003391# ifdef FEAT_GUI
3392 if (!gui.in_use && !gui.starting)
3393# endif
3394 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003395 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003396 if (crv_status == STATUS_SENT
3397 || u7_status == STATUS_SENT
3398 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003399 || rbm_status == STATUS_SENT
3400 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003401 {
3402# ifdef UNIX
3403 /* Give the terminal a chance to respond. */
3404 mch_delay(100L, FALSE);
3405# endif
3406# ifdef TCIFLUSH
3407 /* Discard data received but not read. */
3408 if (exiting)
3409 tcflush(fileno(stdin), TCIFLUSH);
3410# endif
3411 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003412 /* Check for termcodes first, otherwise an external program may
3413 * get them. */
3414 check_for_codes_from_term();
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003417 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 out_str(T_KE); /* stop "keypad transmit" mode */
3419 out_flush();
3420 termcap_active = FALSE;
3421 cursor_on(); /* just in case it is still off */
3422 out_str(T_TE); /* stop termcap mode */
3423 screen_start(); /* don't know where cursor is now */
3424 out_flush();
3425 }
3426}
3427
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003428#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429/*
3430 * Request version string (for xterm) when needed.
3431 * Only do this after switching to raw mode, otherwise the result will be
3432 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003433 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003434 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * Only do this after termcap mode has been started, otherwise the codes for
3436 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003437 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3438 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003439 * On Unix only do it when both output and input are a tty (avoid writing
3440 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 * The result is caught in check_termcode().
3442 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003443 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003444may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003446 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003447 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003448 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 && *T_CRV != NUL)
3450 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003451 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003453 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 /* check for the characters now, otherwise they might be eaten by
3455 * get_keystroke() */
3456 out_flush();
3457 (void)vpeekc_nomap();
3458 }
3459}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003460
3461# if defined(FEAT_MBYTE) || defined(PROTO)
3462/*
3463 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003464 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003465 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003466 * If the terminal treats \u25bd as single width, the position is (1, 1),
3467 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003468 * This function has the side effect that changes cursor position, so
3469 * it must be called immediately after entering termcap mode.
3470 */
3471 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003472may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003473{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003474 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003475 && can_get_termresponse()
3476 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003477 && *T_U7 != NUL
3478 && !option_was_set((char_u *)"ambiwidth"))
3479 {
3480 char_u buf[16];
3481
Bram Moolenaar2951b772013-07-03 12:45:31 +02003482 LOG_TR("Sending U7 request");
3483 /* Do this in the second row. In the first row the returned sequence
3484 * may be CSI 1;2R, which is the same as <S-F3>. */
3485 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003486 buf[mb_char2bytes(0x25bd, buf)] = 0;
3487 out_str(buf);
3488 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003489 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003490 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003491
3492 /* This overwrites a few characters on the screen, a redraw is needed
3493 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003494 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003495 out_str((char_u *)" ");
3496 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003497
Bram Moolenaar9584b312013-03-13 19:29:28 +01003498 /* check for the characters now, otherwise they might be eaten by
3499 * get_keystroke() */
3500 out_flush();
3501 (void)vpeekc_nomap();
3502 }
3503}
3504# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003505
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003506/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003507 * Similar to requesting the version string: Request the terminal background
3508 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003509 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003510 */
3511 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003512may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003513{
Bram Moolenaar4db25542017-08-28 22:43:05 +02003514 int did_one = FALSE;
3515
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003516 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003517 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003518 /* Only request background if t_RB is set and 'background' wasn't
3519 * changed. */
3520 if (rbg_status == STATUS_GET
3521 && *T_RBG != NUL
3522 && !option_was_set((char_u *)"bg"))
3523 {
3524 LOG_TR("Sending BG request");
3525 out_str(T_RBG);
3526 rbg_status = STATUS_SENT;
Bram Moolenaar4db25542017-08-28 22:43:05 +02003527 did_one = TRUE;
3528 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003529
Bram Moolenaar4db25542017-08-28 22:43:05 +02003530 /* Only request cursor blinking mode if t_RC is set. */
3531 if (rbm_status == STATUS_GET && *T_CRC != NUL)
3532 {
3533 LOG_TR("Sending BC request");
3534 out_str(T_CRC);
3535 rbm_status = STATUS_SENT;
3536 did_one = TRUE;
3537 }
3538
3539 if (did_one)
3540 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003541 /* check for the characters now, otherwise they might be eaten by
3542 * get_keystroke() */
3543 out_flush();
3544 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003545 }
3546 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003547}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003548
Bram Moolenaar2951b772013-07-03 12:45:31 +02003549# ifdef DEBUG_TERMRESPONSE
3550 static void
3551log_tr(char *msg)
3552{
3553 static FILE *fd_tr = NULL;
3554 static proftime_T start;
3555 proftime_T now;
3556
3557 if (fd_tr == NULL)
3558 {
3559 fd_tr = fopen("termresponse.log", "w");
3560 profile_start(&start);
3561 }
3562 now = start;
3563 profile_end(&now);
3564 fprintf(fd_tr, "%s: %s %s\n",
3565 profile_msg(&now),
3566 must_redraw == NOT_VALID ? "NV"
3567 : must_redraw == CLEAR ? "CL" : " ",
3568 msg);
3569}
3570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571#endif
3572
3573/*
3574 * Return TRUE when saving and restoring the screen.
3575 */
3576 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003577swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 return (full_screen && *T_TI != NUL);
3580}
3581
3582#ifdef FEAT_MOUSE
3583/*
3584 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3585 */
3586 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003587setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589# ifdef FEAT_MOUSE_TTY
3590 int checkfor;
3591# endif
3592
3593# ifdef FEAT_MOUSESHAPE
3594 update_mouseshape(-1);
3595# endif
3596
3597# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3598# ifdef FEAT_GUI
3599 /* In the GUI the mouse is always enabled. */
3600 if (gui.in_use)
3601 return;
3602# endif
3603 /* be quick when mouse is off */
3604 if (*p_mouse == NUL || has_mouse_termcode == 0)
3605 return;
3606
3607 /* don't switch mouse on when not in raw mode (Ex mode) */
3608 if (cur_tmode != TMODE_RAW)
3609 {
3610 mch_setmouse(FALSE);
3611 return;
3612 }
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (VIsual_active)
3615 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003616 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 checkfor = MOUSE_RETURN;
3618 else if (State & INSERT)
3619 checkfor = MOUSE_INSERT;
3620 else if (State & CMDLINE)
3621 checkfor = MOUSE_COMMAND;
3622 else if (State == CONFIRM || State == EXTERNCMD)
3623 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3624 else
3625 checkfor = MOUSE_NORMAL; /* assume normal mode */
3626
3627 if (mouse_has(checkfor))
3628 mch_setmouse(TRUE);
3629 else
3630 mch_setmouse(FALSE);
3631# endif
3632}
3633
3634/*
3635 * Return TRUE if
3636 * - "c" is in 'mouse', or
3637 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3638 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3639 * normal editing mode (not at hit-return message).
3640 */
3641 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003642mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 char_u *p;
3645
3646 for (p = p_mouse; *p; ++p)
3647 switch (*p)
3648 {
3649 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3650 return TRUE;
3651 break;
3652 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3653 return TRUE;
3654 break;
3655 default: if (c == *p) return TRUE; break;
3656 }
3657 return FALSE;
3658}
3659
3660/*
3661 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3662 */
3663 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003664mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 return (p_mousem[0] == 'p');
3667}
3668#endif
3669
3670/*
3671 * By outputting the 'cursor very visible' termcap code, for some windowed
3672 * terminals this makes the screen scrolled to the correct position.
3673 * Used when starting Vim or returning from a shell.
3674 */
3675 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003676scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003678 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
3680 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003681 out_str(T_CVS);
3682 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684}
3685
3686static int cursor_is_off = FALSE;
3687
3688/*
3689 * Enable the cursor.
3690 */
3691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003692cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (cursor_is_off)
3695 {
3696 out_str(T_VE);
3697 cursor_is_off = FALSE;
3698 }
3699}
3700
3701/*
3702 * Disable the cursor.
3703 */
3704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003705cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003707 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003709 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 cursor_is_off = TRUE;
3711 }
3712}
3713
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003714#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003716 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003717 */
3718 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003719term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003720{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003721 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003722 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003723
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003724 /* Only do something when redrawing the screen and we can restore the
3725 * mode. */
3726 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003727 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003728# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003729 if (forced && initial_cursor_shape > 0)
3730 /* Restore to initial values. */
3731 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003732# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003733 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003734 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003735
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003736 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003737 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003738 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003739 {
3740 if (*T_CSR != NUL)
3741 p = T_CSR; /* Replace mode cursor */
3742 else
3743 p = T_CSI; /* fall back to Insert mode cursor */
3744 if (*p != NUL)
3745 {
3746 out_str(p);
3747 showing_mode = REPLACE;
3748 }
3749 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003750 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003751 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003752 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003753 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003754 {
3755 out_str(T_CSI); /* Insert mode cursor */
3756 showing_mode = INSERT;
3757 }
3758 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003759 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003760 {
3761 out_str(T_CEI); /* non-Insert mode cursor */
3762 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003763 }
3764}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003765
3766# if defined(FEAT_TERMINAL) || defined(PROTO)
3767 void
3768term_cursor_color(char_u *color)
3769{
3770 if (*T_CSC != NUL)
3771 {
3772 out_str(T_CSC); /* set cursor color start */
3773 out_str_nf(color);
3774 out_str(T_CEC); /* set cursor color end */
3775 out_flush();
3776 }
3777}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003778# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003779
Bram Moolenaar4db25542017-08-28 22:43:05 +02003780 int
3781blink_state_is_inverted()
3782{
3783 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3784 && initial_cursor_blink != initial_cursor_shape_blink;
3785}
3786
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003787/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003788 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003789 */
3790 void
3791term_cursor_shape(int shape, int blink)
3792{
3793 if (*T_CSH != NUL)
3794 {
3795 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3796 out_flush();
3797 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003798 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003799 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003800 int do_blink = blink;
3801
3802 /* t_SH is empty: try setting just the blink state.
3803 * The blink flags are XORed together, if the initial blinking from
3804 * style and shape differs, we need to invert the flag here. */
3805 if (blink_state_is_inverted())
3806 do_blink = !blink;
3807
3808 if (do_blink && *T_VS != NUL)
3809 {
3810 out_str(T_VS);
3811 out_flush();
3812 }
3813 else if (!do_blink && *T_CVS != NUL)
3814 {
3815 out_str(T_CVS);
3816 out_flush();
3817 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003818 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003819}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003820#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003821
3822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 * Set scrolling region for window 'wp'.
3824 * The region starts 'off' lines from the start of the window.
3825 * Also set the vertical scroll region for a vertically split window. Always
3826 * the full width of the window, excluding the vertical separator.
3827 */
3828 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003829scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3832 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003833#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (*T_CSV != NUL && wp->w_width != Columns)
3835 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3836 W_WINCOL(wp)));
3837#endif
3838 screen_start(); /* don't know where cursor is now */
3839}
3840
3841/*
3842 * Reset scrolling region to the whole screen.
3843 */
3844 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003845scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
3847 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003848#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (*T_CSV != NUL)
3850 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3851#endif
3852 screen_start(); /* don't know where cursor is now */
3853}
3854
3855
3856/*
3857 * List of terminal codes that are currently recognized.
3858 */
3859
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003860static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 char_u name[2]; /* termcap name of entry */
3863 char_u *code; /* terminal code (in allocated memory) */
3864 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003865 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866} *termcodes = NULL;
3867
3868static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3869static int tc_len = 0; /* current number of entries in termcodes[] */
3870
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003871static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 while (tc_len > 0)
3877 vim_free(termcodes[--tc_len].code);
3878 vim_free(termcodes);
3879 termcodes = NULL;
3880 tc_max_len = 0;
3881
3882#ifdef HAVE_TGETENT
3883 BC = (char *)empty_option;
3884 UP = (char *)empty_option;
3885 PC = NUL; /* set pad character to NUL */
3886 ospeed = 0;
3887#endif
3888
3889 need_gather = TRUE; /* need to fill termleader[] */
3890}
3891
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003892#define ATC_FROM_TERM 55
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
3895 * Add a new entry to the list of terminal codes.
3896 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003897 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3898 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
3900 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003901add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 struct termcode *new_tc;
3904 int i, j;
3905 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003906 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 if (string == NULL || *string == NUL)
3909 {
3910 del_termcode(name);
3911 return;
3912 }
3913
Bram Moolenaar45500912014-07-09 20:51:07 +02003914#if defined(WIN3264) && !defined(FEAT_GUI)
3915 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (s == NULL)
3920 return;
3921
3922 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003923 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003925 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 s[0] = term_7to8bit(string);
3927 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003928
3929#if defined(WIN3264) && !defined(FEAT_GUI)
3930 if (s[0] == K_NUL)
3931 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003932 STRMOVE(s + 1, s);
3933 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003934 }
3935#endif
3936
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003937 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 need_gather = TRUE; /* need to fill termleader[] */
3940
3941 /*
3942 * need to make space for more entries
3943 */
3944 if (tc_len == tc_max_len)
3945 {
3946 tc_max_len += 20;
3947 new_tc = (struct termcode *)alloc(
3948 (unsigned)(tc_max_len * sizeof(struct termcode)));
3949 if (new_tc == NULL)
3950 {
3951 tc_max_len -= 20;
3952 return;
3953 }
3954 for (i = 0; i < tc_len; ++i)
3955 new_tc[i] = termcodes[i];
3956 vim_free(termcodes);
3957 termcodes = new_tc;
3958 }
3959
3960 /*
3961 * Look for existing entry with the same name, it is replaced.
3962 * Look for an existing entry that is alphabetical higher, the new entry
3963 * is inserted in front of it.
3964 */
3965 for (i = 0; i < tc_len; ++i)
3966 {
3967 if (termcodes[i].name[0] < name[0])
3968 continue;
3969 if (termcodes[i].name[0] == name[0])
3970 {
3971 if (termcodes[i].name[1] < name[1])
3972 continue;
3973 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
3976 if (termcodes[i].name[1] == name[1])
3977 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003978 if (flags == ATC_FROM_TERM && (j = termcode_star(
3979 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003980 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 /* Don't replace ESC[123;*X or ESC O*X with another when
3982 * invoked from got_code_from_term(). */
3983 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003984 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 && s[len - 1]
3986 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003987 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003989 vim_free(s);
3990 return;
3991 }
3992 }
3993 else
3994 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003996 vim_free(termcodes[i].code);
3997 --tc_len;
3998 break;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 }
4002 /*
4003 * Found alphabetical larger entry, move rest to insert new entry
4004 */
4005 for (j = tc_len; j > i; --j)
4006 termcodes[j] = termcodes[j - 1];
4007 break;
4008 }
4009
4010 termcodes[i].name[0] = name[0];
4011 termcodes[i].name[1] = name[1];
4012 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004013 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014
4015 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4016 * accept modifiers. */
4017 termcodes[i].modlen = 0;
4018 j = termcode_star(s, len);
4019 if (j > 0)
4020 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 ++tc_len;
4022}
4023
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004024/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004025 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004026 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004027 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004028 */
4029 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004030termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004031{
4032 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4033 if (len >= 3 && code[len - 2] == '*')
4034 {
4035 if (len >= 5 && code[len - 3] == ';')
4036 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004037 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004038 return 1;
4039 }
4040 return 0;
4041}
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004044find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 int i;
4047
4048 for (i = 0; i < tc_len; ++i)
4049 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4050 return termcodes[i].code;
4051 return NULL;
4052}
4053
4054#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4055 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004056get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 if (i >= tc_len)
4059 return NULL;
4060 return &termcodes[i].name[0];
4061}
4062#endif
4063
4064 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004065del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
4067 int i;
4068
4069 if (termcodes == NULL) /* nothing there yet */
4070 return;
4071
4072 need_gather = TRUE; /* need to fill termleader[] */
4073
4074 for (i = 0; i < tc_len; ++i)
4075 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4076 {
4077 del_termcode_idx(i);
4078 return;
4079 }
4080 /* not found. Give error message? */
4081}
4082
4083 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004084del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 int i;
4087
4088 vim_free(termcodes[idx].code);
4089 --tc_len;
4090 for (i = idx; i < tc_len; ++i)
4091 termcodes[i] = termcodes[i + 1];
4092}
4093
4094#ifdef FEAT_TERMRESPONSE
4095/*
4096 * Called when detected that the terminal sends 8-bit codes.
4097 * Convert all 7-bit codes to their 8-bit equivalent.
4098 */
4099 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004100switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 int i;
4103 int c;
4104
4105 /* Only need to do something when not already using 8-bit codes. */
4106 if (!term_is_8bit(T_NAME))
4107 {
4108 for (i = 0; i < tc_len; ++i)
4109 {
4110 c = term_7to8bit(termcodes[i].code);
4111 if (c != 0)
4112 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004113 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 termcodes[i].code[0] = c;
4115 }
4116 }
4117 need_gather = TRUE; /* need to fill termleader[] */
4118 }
4119 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004120 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122#endif
4123
4124#ifdef CHECK_DOUBLE_CLICK
4125static linenr_T orig_topline = 0;
4126# ifdef FEAT_DIFF
4127static int orig_topfill = 0;
4128# endif
4129#endif
4130#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4131/*
4132 * Checking for double clicks ourselves.
4133 * "orig_topline" is used to avoid detecting a double-click when the window
4134 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4135 */
4136/*
4137 * Set orig_topline. Used when jumping to another window, so that a double
4138 * click still works.
4139 */
4140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004141set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 orig_topline = wp->w_topline;
4144# ifdef FEAT_DIFF
4145 orig_topfill = wp->w_topfill;
4146# endif
4147}
4148#endif
4149
4150/*
4151 * Check if typebuf.tb_buf[] contains a terminal key code.
4152 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4153 * + max_offset].
4154 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004155 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 * With a match, the match is removed, the replacement code is inserted in
4157 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4158 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004159 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4160 * "buflen" is then the length of the string in buf[] and is updated for
4161 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 */
4163 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004164check_termcode(
4165 int max_offset,
4166 char_u *buf,
4167 int bufsize,
4168 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 char_u *tp;
4171 char_u *p;
4172 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004175 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 int offset;
4177 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004178 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004179 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004180 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int new_slen;
4182 int extra;
4183 char_u string[MAX_KEY_CODE_LEN + 1];
4184 int i, j;
4185 int idx = 0;
4186#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004187# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4188 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 char_u bytes[6];
4190 int num_bytes;
4191# endif
4192 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 int is_click, is_drag;
4194 int wheel_code = 0;
4195 int current_button;
4196 static int held_button = MOUSE_RELEASE;
4197 static int orig_num_clicks = 1;
4198 static int orig_mouse_code = 0x0;
4199# ifdef CHECK_DOUBLE_CLICK
4200 static int orig_mouse_col = 0;
4201 static int orig_mouse_row = 0;
4202 static struct timeval orig_mouse_time = {0, 0};
4203 /* time of previous mouse click */
4204 struct timeval mouse_time; /* time of current mouse click */
4205 long timediff; /* elapsed time in msec */
4206# endif
4207#endif
4208 int cpo_koffset;
4209#ifdef FEAT_MOUSE_GPM
4210 extern int gpm_flag; /* gpm library variable */
4211#endif
4212
4213 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4214
4215 /*
4216 * Speed up the checks for terminal codes by gathering all first bytes
4217 * used in termleader[]. Often this is just a single <Esc>.
4218 */
4219 if (need_gather)
4220 gather_termleader();
4221
4222 /*
4223 * Check at several positions in typebuf.tb_buf[], to catch something like
4224 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4225 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004226 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 * This is used often, KEEP IT FAST!
4228 */
4229 for (offset = 0; offset < max_offset; ++offset)
4230 {
4231 if (buf == NULL)
4232 {
4233 if (offset >= typebuf.tb_len)
4234 break;
4235 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4236 len = typebuf.tb_len - offset; /* length of the input */
4237 }
4238 else
4239 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004240 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 break;
4242 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004243 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245
4246 /*
4247 * Don't check characters after K_SPECIAL, those are already
4248 * translated terminal chars (avoid translating ~@^Hx).
4249 */
4250 if (*tp == K_SPECIAL)
4251 {
4252 offset += 2; /* there are always 2 extra characters */
4253 continue;
4254 }
4255
4256 /*
4257 * Skip this position if the character does not appear as the first
4258 * character in term_strings. This speeds up a lot, since most
4259 * termcodes start with the same character (ESC or CSI).
4260 */
4261 i = *tp;
4262 for (p = termleader; *p && *p != i; ++p)
4263 ;
4264 if (*p == NUL)
4265 continue;
4266
4267 /*
4268 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4269 * are in Insert mode.
4270 */
4271 if (*tp == ESC && !p_ek && (State & INSERT))
4272 continue;
4273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004275 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278#ifdef FEAT_GUI
4279 if (gui.in_use)
4280 {
4281 /*
4282 * GUI special key codes are all of the form [CSI xx].
4283 */
4284 if (*tp == CSI) /* Special key from GUI */
4285 {
4286 if (len < 3)
4287 return -1; /* Shouldn't happen */
4288 slen = 3;
4289 key_name[0] = tp[1];
4290 key_name[1] = tp[2];
4291 }
4292 }
4293 else
4294#endif /* FEAT_GUI */
4295 {
4296 for (idx = 0; idx < tc_len; ++idx)
4297 {
4298 /*
4299 * Ignore the entry if we are not at the start of
4300 * typebuf.tb_buf[]
4301 * and there are not enough characters to make a match.
4302 * But only when the 'K' flag is in 'cpoptions'.
4303 */
4304 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004305 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (cpo_koffset && offset && len < slen)
4307 continue;
4308 if (STRNCMP(termcodes[idx].code, tp,
4309 (size_t)(slen > len ? len : slen)) == 0)
4310 {
4311 if (len < slen) /* got a partial sequence */
4312 return -1; /* need to get more chars */
4313
4314 /*
4315 * When found a keypad key, check if there is another key
4316 * that matches and use that one. This makes <Home> to be
4317 * found instead of <kHome> when they produce the same
4318 * key code.
4319 */
4320 if (termcodes[idx].name[0] == 'K'
4321 && VIM_ISDIGIT(termcodes[idx].name[1]))
4322 {
4323 for (j = idx + 1; j < tc_len; ++j)
4324 if (termcodes[j].len == slen &&
4325 STRNCMP(termcodes[idx].code,
4326 termcodes[j].code, slen) == 0)
4327 {
4328 idx = j;
4329 break;
4330 }
4331 }
4332
4333 key_name[0] = termcodes[idx].name[0];
4334 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004337
4338 /*
4339 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004340 * <Esc>[123;*X (modslen == slen - 3)
4341 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4342 * When there is a modifier the * matches a number.
4343 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344 */
4345 if (termcodes[idx].modlen > 0)
4346 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004347 modslen = termcodes[idx].modlen;
4348 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 continue;
4350 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004351 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004352 {
4353 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004356 return -1; /* need to get more chars */
4357
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004358 if (tp[modslen] == termcodes[idx].code[slen - 1])
4359 slen = modslen + 1; /* no modifiers */
4360 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004361 continue; /* no match */
4362 else
4363 {
4364 /* Skip over the digits, the final char must
4365 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004366 for (j = slen - 2; j < len && (isdigit(tp[j])
4367 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004368 ;
4369 ++j;
4370 if (len < j) /* got a partial sequence */
4371 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4373 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004374
Bram Moolenaara529ce02017-06-22 22:37:57 +02004375 modifiers_start = tp + slen - 2;
4376
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004377 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004378 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004379 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004380 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004381 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004382 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004383 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004384 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004385 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004386 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004387
4388 slen = j;
4389 }
4390 key_name[0] = termcodes[idx].name[0];
4391 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004392 break;
4393 }
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396 }
4397
4398#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004399 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004400 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004401 * detecting the start of these mouse codes they might as well be
4402 * another key code or terminal response. */
4403# ifdef FEAT_MOUSE_DEC
4404 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004405# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004406# ifdef FEAT_MOUSE_PTERM
4407 || key_name[0] == KS_PTERM_MOUSE
4408# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004409 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004411 /* Check for some responses from the terminal starting with
4412 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004413 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004414 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004415 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004416 * Also eat other possible responses to t_RV, rxvt returns
4417 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4418 * mrxvt has been reported to have "+" in the version. Assume
4419 * the escape sequence ends with a letter or one of "{|}~".
4420 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004421 * - Cursor position report: <Esc>[{row};{col}R
4422 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004423 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004424 *
4425 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004426 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004427 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004428
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004429 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004430 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004431 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004432 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004434 int col = 0;
4435 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004436#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004437 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004438#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004441 for (i = 2 + (tp[0] != CSI); i < len
4442 && !(tp[i] >= '{' && tp[i] <= '~')
4443 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004444 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004445 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004446 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004447#ifdef FEAT_MBYTE
4448 row_char = tp[i - 1];
4449#endif
4450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004452 {
4453 LOG_TR("Not enough characters for CRV");
4454 return -1;
4455 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004456 if (extra > 0)
4457 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004458
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004459#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004460 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4461 * u7_status is not "sent", it may be from a previous Vim that
4462 * just exited. But not for <S-F3>, it sends something
4463 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004464 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004465 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004466 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004467 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004468 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004469
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004470 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004471 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004472# ifdef FEAT_AUTOCMD
4473 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004474# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004475 if (col == 2)
4476 aw = "single";
4477 else if (col == 3)
4478 aw = "double";
4479 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4480 {
4481 /* Setting the option causes a screen redraw. Do
4482 * that right away if possible, keeping any
4483 * messages. */
4484 set_option_value((char_u *)"ambw", 0L,
4485 (char_u *)aw, 0);
4486# ifdef DEBUG_TERMRESPONSE
4487 {
4488 char buf[100];
4489 int r = redraw_asap(CLEAR);
4490
4491 sprintf(buf,
4492 "set 'ambiwidth', redraw_asap(): %d",
4493 r);
4494 log_tr(buf);
4495 }
4496# else
4497 redraw_asap(CLEAR);
4498# endif
4499 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004500 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004501 key_name[0] = (int)KS_EXTRA;
4502 key_name[1] = (int)KE_IGNORE;
4503 slen = i + 1;
4504 }
4505 else
4506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004508 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004510 LOG_TR("Received CRV response");
4511 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004512# ifdef FEAT_AUTOCMD
4513 did_cursorhold = TRUE;
4514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 /* If this code starts with CSI, you can bet that the
4517 * terminal uses 8-bit codes. */
4518 if (tp[0] == CSI)
4519 switch_to_8bit();
4520
4521 /* rxvt sends its version number: "20703" is 2.7.3.
4522 * Ignore it for when the user has set 'term' to xterm,
4523 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004524 if (col > 20000)
4525 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004527 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004529 /* Only set 'ttymouse' automatically if it was not set
4530 * by the user already. */
4531 if (!option_was_set((char_u *)"ttym"))
4532 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004533# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004534 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004535 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004536 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004537 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004538# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004539 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004540 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004541 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004543 }
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004546 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004548 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 check_for_codes = TRUE;
4550 need_gather = TRUE;
4551 req_codes_from_term();
4552 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004553
4554 /* libvterm sends 0;100;0 */
4555 if (col == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004556 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004557 {
4558 /* If run from Vim $COLORS is set to the number of
4559 * colors the terminal supports. Otherwise assume
4560 * 256, libvterm supports even more. */
4561 if (mch_getenv((char_u *)"COLORS") == NULL)
4562 may_adjust_color_count(256);
4563 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004564
4565# ifdef MACOS
4566 /* Mac Terminal.app sends 1;95;0 */
4567 if (col == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004568 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004569 {
4570 /* Terminal.app sets $TERM to "xterm-256colors",
4571 * but it's not fully xterm compatible. */
4572 is_terminal_app = TRUE;
4573 }
4574# endif
4575
4576 /* Only request the cursor style if t_SH and t_RS are
4577 * set. Not for Terminal.app, it can't handle t_RS, it
4578 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004579 if (rcs_status == STATUS_GET
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004580# ifdef MACOS
4581 && !is_terminal_app
4582# endif
4583 && *T_CSH != NUL
4584 && *T_CRS != NUL)
4585 {
4586 LOG_TR("Sending cursor style request");
4587 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004588 rcs_status = STATUS_SENT;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004589 out_flush();
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592# ifdef FEAT_EVAL
4593 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4594# endif
4595# ifdef FEAT_AUTOCMD
4596 apply_autocmds(EVENT_TERMRESPONSE,
4597 NULL, NULL, FALSE, curbuf);
4598# endif
4599 key_name[0] = (int)KS_EXTRA;
4600 key_name[1] = (int)KE_IGNORE;
4601 slen = i + 1;
4602 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004603
Bram Moolenaar4db25542017-08-28 22:43:05 +02004604 /* Check blinking cursor from xterm:
4605 * {lead}?12;1$y set
4606 * {lead}?12;2$y not set
4607 *
4608 * {lead} can be <Esc>[ or CSI
4609 */
4610 else if (rbm_status == STATUS_SENT
4611 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4612 && i == j + 6
4613 && tp[j + 1] == '1'
4614 && tp[j + 2] == '2'
4615 && tp[j + 3] == ';'
4616 && tp[i - 1] == '$'
4617 && tp[i] == 'y')
4618 {
4619 initial_cursor_blink = (tp[j + 4] == '1');
4620 rbm_status = STATUS_GOT;
4621 LOG_TR("Received cursor blinking mode response");
4622 key_name[0] = (int)KS_EXTRA;
4623 key_name[1] = (int)KE_IGNORE;
4624 slen = i + 1;
4625 }
4626
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004627 /*
4628 * Check for a window position response from the terminal:
4629 * {lead}3;{x}:{y}t
4630 */
4631 else if (waiting_for_winpos
4632 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4633 || (len >= 3 && tp[0] == CSI))
4634 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4635 && tp[j + 1] == ';')
4636 {
4637 j += 2;
4638 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4639 ;
4640 if (i < len && tp[i] == ';')
4641 {
4642 winpos_x = atoi((char *)tp + j);
4643 j = i + 1;
4644 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4645 ;
4646 if (i < len && tp[i] == 't')
4647 {
4648 winpos_y = atoi((char *)tp + j);
4649 /* got finished code: consume it */
4650 key_name[0] = (int)KS_EXTRA;
4651 key_name[1] = (int)KE_IGNORE;
4652 slen = i + 1;
4653 }
4654 }
4655 if (i == len)
4656 {
4657 LOG_TR("not enough characters for winpos");
4658 return -1;
4659 }
4660 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004661 }
4662
4663 /* Check for background color response from the terminal:
4664 *
4665 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4666 *
4667 * {lead} can be <Esc>] or OSC
4668 * {tail} can be '\007', <Esc>\ or STERM.
4669 *
4670 * Consume any code that starts with "{lead}11;", it's also
4671 * possible that "rgba" is following.
4672 */
4673 else if (*T_RBG != NUL
4674 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4675 || tp[0] == OSC))
4676 {
4677 j = 1 + (tp[0] == ESC);
4678 if (len >= j + 3 && (argp[0] != '1'
4679 || argp[1] != '1' || argp[2] != ';'))
4680 i = 0; /* no match */
4681 else
4682 for (i = j; i < len; ++i)
4683 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4684 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004685 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004686 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4687 && tp[j + 11] == '/' && tp[j + 16] == '/'
4688 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004689 {
4690 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4691 + tp[j+17]) ? "light" : "dark";
4692
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004693 LOG_TR("Received RBG response");
4694 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004695 if (STRCMP(p_bg, newval) != 0)
4696 {
4697 /* value differs, apply it */
4698 set_option_value((char_u *)"bg", 0L,
4699 (char_u *)newval, 0);
4700 reset_option_was_set((char_u *)"bg");
4701 redraw_asap(CLEAR);
4702 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004703 }
4704
4705 /* got finished code: consume it */
4706 key_name[0] = (int)KS_EXTRA;
4707 key_name[1] = (int)KE_IGNORE;
4708 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf6d9f962017-08-24 20:21:16 +02004709 if (tp[i] == 0x07 && i + 1 < len && tp[i + 1] == 0x18)
4710 /* Sometimes the 0x07 is followed by 0x18, unclear
4711 * when this happens. */
4712 ++slen;
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004713 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004714 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004715 if (i == len)
4716 {
4717 LOG_TR("not enough characters for RB");
4718 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004722 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004723 * {lead}{flag}+r<hex bytes><{tail}
4724 *
4725 * {lead} can be <Esc>P or DCS
4726 * {flag} can be '0' or '1'
4727 * {tail} can be Esc>\ or STERM
4728 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004729 * Check for cursor shape response from xterm:
4730 * {lead}1$r<number> q{tail}
4731 *
4732 * {lead} can be <Esc>P or DCS
4733 * {tail} can be Esc>\ or STERM
4734 *
4735 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004736 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004737 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004738 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 || tp[0] == DCS))
4740 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004741 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004742 if (len < j + 3)
4743 i = len; /* need more chars */
4744 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004745 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004746 else if (argp[1] == '+')
4747 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004748 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004749 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004750 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 || tp[i] == STERM)
4752 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004753 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 got_code_from_term(tp + j, i);
4755 key_name[0] = (int)KS_EXTRA;
4756 key_name[1] = (int)KE_IGNORE;
4757 slen = i + 1 + (tp[i] == ESC);
4758 break;
4759 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004760 }
4761 else if ((len >= j + 6 && isdigit(argp[3]))
4762 && argp[4] == ' '
4763 && argp[5] == 'q')
4764 {
4765 /* cursor shape response */
4766 i = j + 6;
4767 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4768 || tp[i] == STERM)
4769 {
4770 int number = argp[3] - '0';
4771
4772 /* 0, 1 = block blink, 2 = block
4773 * 3 = underline blink, 4 = underline
4774 * 5 = vertical bar blink, 6 = vertical bar */
4775 number = number == 0 ? 1 : number;
4776 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004777 /* The blink flag is actually inverted, compared to
4778 * the value set with T_SH. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004779 initial_cursor_shape_blink =
4780 (number & 1) ? FALSE : TRUE;
4781 rcs_status = STATUS_GOT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004782 LOG_TR("Received cursor shape response");
4783
4784 key_name[0] = (int)KS_EXTRA;
4785 key_name[1] = (int)KE_IGNORE;
4786 slen = i + 1 + (tp[i] == ESC);
4787 }
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004791 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004792 /* These codes arrive many together, each code can be
4793 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004794 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004795 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 }
4799#endif
4800
4801 if (key_name[0] == NUL)
4802 continue; /* No match at this position, try next one */
4803
4804 /* We only get here when we have a complete termcode match */
4805
4806#ifdef FEAT_MOUSE
4807# ifdef FEAT_GUI
4808 /*
4809 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4810 * so that we know which window to scroll later.
4811 */
4812 if (gui.in_use
4813 && key_name[0] == (int)KS_EXTRA
4814 && (key_name[1] == (int)KE_X1MOUSE
4815 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004816 || key_name[1] == (int)KE_MOUSELEFT
4817 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 || key_name[1] == (int)KE_MOUSEDOWN
4819 || key_name[1] == (int)KE_MOUSEUP))
4820 {
4821 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4822 if (num_bytes == -1) /* not enough coordinates */
4823 return -1;
4824 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4825 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4826 slen += num_bytes;
4827 }
4828 else
4829# endif
4830 /*
4831 * If it is a mouse click, get the coordinates.
4832 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004833 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004835 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836# endif
4837# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004838 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839# endif
4840# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004841 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842# endif
4843# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004844 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004846# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004847 || key_name[0] == KS_URXVT_MOUSE
4848# endif
4849# ifdef FEAT_MOUSE_SGR
4850 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004851 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 )
4854 {
4855 is_click = is_drag = FALSE;
4856
Bram Moolenaar864207d2008-06-24 22:14:38 +00004857# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4858 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (key_name[0] == (int)KS_MOUSE)
4860 {
4861 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004862 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 * s == encoded button state:
4864 * 0x20 = left button down
4865 * 0x21 = middle button down
4866 * 0x22 = right button down
4867 * 0x23 = any button release
4868 * 0x60 = button 4 down (scroll wheel down)
4869 * 0x61 = button 5 down (scroll wheel up)
4870 * add 0x04 for SHIFT
4871 * add 0x08 for ALT
4872 * add 0x10 for CTRL
4873 * add 0x20 for mouse drag (0x40 is drag with left button)
4874 * c == column + ' ' + 1 == column + 33
4875 * r == row + ' ' + 1 == row + 33
4876 *
4877 * The coordinates are passed on through global variables.
4878 * Ugly, but this avoids trouble with mouse clicks at an
4879 * unexpected moment and allows for mapping them.
4880 */
4881 for (;;)
4882 {
4883#ifdef FEAT_GUI
4884 if (gui.in_use)
4885 {
4886 /* GUI uses more bits for columns > 223 */
4887 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4888 if (num_bytes == -1) /* not enough coordinates */
4889 return -1;
4890 mouse_code = bytes[0];
4891 mouse_col = 128 * (bytes[1] - ' ' - 1)
4892 + bytes[2] - ' ' - 1;
4893 mouse_row = 128 * (bytes[3] - ' ' - 1)
4894 + bytes[4] - ' ' - 1;
4895 }
4896 else
4897#endif
4898 {
4899 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4900 if (num_bytes == -1) /* not enough coordinates */
4901 return -1;
4902 mouse_code = bytes[0];
4903 mouse_col = bytes[1] - ' ' - 1;
4904 mouse_row = bytes[2] - ' ' - 1;
4905 }
4906 slen += num_bytes;
4907
4908 /* If the following bytes is also a mouse code and it has
4909 * the same code, dump this one and get the next. This
4910 * makes dragging a whole lot faster. */
4911#ifdef FEAT_GUI
4912 if (gui.in_use)
4913 j = 3;
4914 else
4915#endif
4916 j = termcodes[idx].len;
4917 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4918 && tp[slen + j] == mouse_code
4919 && tp[slen + j + 1] != NUL
4920 && tp[slen + j + 2] != NUL
4921#ifdef FEAT_GUI
4922 && (!gui.in_use
4923 || (tp[slen + j + 3] != NUL
4924 && tp[slen + j + 4] != NUL))
4925#endif
4926 )
4927 slen += j;
4928 else
4929 break;
4930 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004933# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4934 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004935 || key_name[0] == KS_SGR_MOUSE
4936 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004937 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004938 /* URXVT 1015 mouse reporting mode:
4939 * Almost identical to xterm mouse mode, except the values
4940 * are decimal instead of bytes.
4941 *
4942 * \033[%d;%d;%dM
4943 * ^-- row
4944 * ^----- column
4945 * ^-------- code
4946 *
4947 * SGR 1006 mouse reporting mode:
4948 * Almost identical to xterm mouse mode, except the values
4949 * are decimal instead of bytes.
4950 *
4951 * \033[<%d;%d;%dM
4952 * ^-- row
4953 * ^----- column
4954 * ^-------- code
4955 *
4956 * \033[<%d;%d;%dm : mouse release event
4957 * ^-- row
4958 * ^----- column
4959 * ^-------- code
4960 */
4961 p = modifiers_start;
4962 if (p == NULL)
4963 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004964
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004965 mouse_code = getdigits(&p);
4966 if (*p++ != ';')
4967 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004968
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004969 /* when mouse reporting is SGR, add 32 to mouse code */
4970 if (key_name[0] == KS_SGR_MOUSE
4971 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4972 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004973
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004974 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4975 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004976
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004977 mouse_col = getdigits(&p) - 1;
4978 if (*p++ != ';')
4979 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004980
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004981 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004982
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004983 /* The modifiers were the mouse coordinates, not the
4984 * modifier keys (alt/shift/ctrl/meta) state. */
4985 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004986 }
4987# endif
4988
4989 if (key_name[0] == (int)KS_MOUSE
4990#ifdef FEAT_MOUSE_URXVT
4991 || key_name[0] == (int)KS_URXVT_MOUSE
4992#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004993#ifdef FEAT_MOUSE_SGR
4994 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004995 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004996#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004997 )
4998 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004999# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Handle mouse events.
5002 * Recognize the xterm mouse wheel, but not in the GUI, the
5003 * Linux console with GPM and the MS-DOS or Win32 console
5004 * (multi-clicks use >= 0x60).
5005 */
5006 if (mouse_code >= MOUSEWHEEL_LOW
5007# ifdef FEAT_GUI
5008 && !gui.in_use
5009# endif
5010# ifdef FEAT_MOUSE_GPM
5011 && gpm_flag == 0
5012# endif
5013 )
5014 {
5015 /* Keep the mouse_code before it's changed, so that we
5016 * remember that it was a mouse wheel click. */
5017 wheel_code = mouse_code;
5018 }
5019# ifdef FEAT_MOUSE_XTERM
5020 else if (held_button == MOUSE_RELEASE
5021# ifdef FEAT_GUI
5022 && !gui.in_use
5023# endif
5024 && (mouse_code == 0x23 || mouse_code == 0x24))
5025 {
5026 /* Apparently used by rxvt scroll wheel. */
5027 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5028 }
5029# endif
5030
5031# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5032 else if (use_xterm_mouse() > 1)
5033 {
5034 if (mouse_code & MOUSE_DRAG_XTERM)
5035 mouse_code |= MOUSE_DRAG;
5036 }
5037# endif
5038# ifdef FEAT_XCLIPBOARD
5039 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5040 {
5041 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5042 stop_xterm_trace();
5043 else
5044 start_xterm_trace(mouse_code);
5045 }
5046# endif
5047# endif
5048 }
5049# endif /* !UNIX || FEAT_MOUSE_XTERM */
5050# ifdef FEAT_MOUSE_NET
5051 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5052 {
5053 int mc, mr;
5054
5055 /* expect a rather limited sequence like: balancing {
5056 * \033}6,45\r
5057 * '6' is the row, 45 is the column
5058 */
5059 p = tp + slen;
5060 mr = getdigits(&p);
5061 if (*p++ != ',')
5062 return -1;
5063 mc = getdigits(&p);
5064 if (*p++ != '\r')
5065 return -1;
5066
5067 mouse_col = mc - 1;
5068 mouse_row = mr - 1;
5069 mouse_code = MOUSE_LEFT;
5070 slen += (int)(p - (tp + slen));
5071 }
5072# endif /* FEAT_MOUSE_NET */
5073# ifdef FEAT_MOUSE_JSB
5074 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5075 {
5076 int mult, val, iter, button, status;
5077
5078 /* JSBTERM Input Model
5079 * \033[0~zw uniq escape sequence
5080 * (L-x) Left button pressed - not pressed x not reporting
5081 * (M-x) Middle button pressed - not pressed x not reporting
5082 * (R-x) Right button pressed - not pressed x not reporting
5083 * (SDmdu) Single , Double click, m mouse move d button down
5084 * u button up
5085 * ### X cursor position padded to 3 digits
5086 * ### Y cursor position padded to 3 digits
5087 * (s-x) SHIFT key pressed - not pressed x not reporting
5088 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005089 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
5091
5092 p = tp + slen;
5093 button = mouse_code = 0;
5094 switch (*p++)
5095 {
5096 case 'L': button = 1; break;
5097 case '-': break;
5098 case 'x': break; /* ignore sequence */
5099 default: return -1; /* Unknown Result */
5100 }
5101 switch (*p++)
5102 {
5103 case 'M': button |= 2; break;
5104 case '-': break;
5105 case 'x': break; /* ignore sequence */
5106 default: return -1; /* Unknown Result */
5107 }
5108 switch (*p++)
5109 {
5110 case 'R': button |= 4; break;
5111 case '-': break;
5112 case 'x': break; /* ignore sequence */
5113 default: return -1; /* Unknown Result */
5114 }
5115 status = *p++;
5116 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5117 mult /= 10, p++)
5118 if (*p >= '0' && *p <= '9')
5119 val += (*p - '0') * mult;
5120 else
5121 return -1;
5122 mouse_col = val;
5123 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5124 mult /= 10, p++)
5125 if (*p >= '0' && *p <= '9')
5126 val += (*p - '0') * mult;
5127 else
5128 return -1;
5129 mouse_row = val;
5130 switch (*p++)
5131 {
5132 case 's': button |= 8; break; /* SHIFT key Pressed */
5133 case '-': break; /* Not Pressed */
5134 case 'x': break; /* Not Reporting */
5135 default: return -1; /* Unknown Result */
5136 }
5137 switch (*p++)
5138 {
5139 case 'c': button |= 16; break; /* CTRL key Pressed */
5140 case '-': break; /* Not Pressed */
5141 case 'x': break; /* Not Reporting */
5142 default: return -1; /* Unknown Result */
5143 }
5144 if (*p++ != '\033')
5145 return -1;
5146 if (*p++ != '\\')
5147 return -1;
5148 switch (status)
5149 {
5150 case 'D': /* Double Click */
5151 case 'S': /* Single Click */
5152 if (button & 1) mouse_code |= MOUSE_LEFT;
5153 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5154 if (button & 4) mouse_code |= MOUSE_RIGHT;
5155 if (button & 8) mouse_code |= MOUSE_SHIFT;
5156 if (button & 16) mouse_code |= MOUSE_CTRL;
5157 break;
5158 case 'm': /* Mouse move */
5159 if (button & 1) mouse_code |= MOUSE_LEFT;
5160 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5161 if (button & 4) mouse_code |= MOUSE_RIGHT;
5162 if (button & 8) mouse_code |= MOUSE_SHIFT;
5163 if (button & 16) mouse_code |= MOUSE_CTRL;
5164 if ((button & 7) != 0)
5165 {
5166 held_button = mouse_code;
5167 mouse_code |= MOUSE_DRAG;
5168 }
5169 is_drag = TRUE;
5170 showmode();
5171 break;
5172 case 'd': /* Button Down */
5173 if (button & 1) mouse_code |= MOUSE_LEFT;
5174 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5175 if (button & 4) mouse_code |= MOUSE_RIGHT;
5176 if (button & 8) mouse_code |= MOUSE_SHIFT;
5177 if (button & 16) mouse_code |= MOUSE_CTRL;
5178 break;
5179 case 'u': /* Button Up */
5180 if (button & 1)
5181 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5182 if (button & 2)
5183 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5184 if (button & 4)
5185 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5186 if (button & 8)
5187 mouse_code |= MOUSE_SHIFT;
5188 if (button & 16)
5189 mouse_code |= MOUSE_CTRL;
5190 break;
5191 default: return -1; /* Unknown Result */
5192 }
5193
5194 slen += (p - (tp + slen));
5195 }
5196# endif /* FEAT_MOUSE_JSB */
5197# ifdef FEAT_MOUSE_DEC
5198 if (key_name[0] == (int)KS_DEC_MOUSE)
5199 {
5200 /* The DEC Locator Input Model
5201 * Netterm delivers the code sequence:
5202 * \033[2;4;24;80&w (left button down)
5203 * \033[3;0;24;80&w (left button up)
5204 * \033[6;1;24;80&w (right button down)
5205 * \033[7;0;24;80&w (right button up)
5206 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5207 * Pe is the event code
5208 * Pb is the button code
5209 * Pr is the row coordinate
5210 * Pc is the column coordinate
5211 * Pp is the third coordinate (page number)
5212 * Pe, the event code indicates what event caused this report
5213 * The following event codes are defined:
5214 * 0 - request, the terminal received an explicit request
5215 * for a locator report, but the locator is unavailable
5216 * 1 - request, the terminal received an explicit request
5217 * for a locator report
5218 * 2 - left button down
5219 * 3 - left button up
5220 * 4 - middle button down
5221 * 5 - middle button up
5222 * 6 - right button down
5223 * 7 - right button up
5224 * 8 - fourth button down
5225 * 9 - fourth button up
5226 * 10 - locator outside filter rectangle
5227 * Pb, the button code, ASCII decimal 0-15 indicating which
5228 * buttons are down if any. The state of the four buttons
5229 * on the locator correspond to the low four bits of the
5230 * decimal value,
5231 * "1" means button depressed
5232 * 0 - no buttons down,
5233 * 1 - right,
5234 * 2 - middle,
5235 * 4 - left,
5236 * 8 - fourth
5237 * Pr is the row coordinate of the locator position in the page,
5238 * encoded as an ASCII decimal value.
5239 * If Pr is omitted, the locator position is undefined
5240 * (outside the terminal window for example).
5241 * Pc is the column coordinate of the locator position in the
5242 * page, encoded as an ASCII decimal value.
5243 * If Pc is omitted, the locator position is undefined
5244 * (outside the terminal window for example).
5245 * Pp is the page coordinate of the locator position
5246 * encoded as an ASCII decimal value.
5247 * The page coordinate may be omitted if the locator is on
5248 * page one (the default). We ignore it anyway.
5249 */
5250 int Pe, Pb, Pr, Pc;
5251
5252 p = tp + slen;
5253
5254 /* get event status */
5255 Pe = getdigits(&p);
5256 if (*p++ != ';')
5257 return -1;
5258
5259 /* get button status */
5260 Pb = getdigits(&p);
5261 if (*p++ != ';')
5262 return -1;
5263
5264 /* get row status */
5265 Pr = getdigits(&p);
5266 if (*p++ != ';')
5267 return -1;
5268
5269 /* get column status */
5270 Pc = getdigits(&p);
5271
5272 /* the page parameter is optional */
5273 if (*p == ';')
5274 {
5275 p++;
5276 (void)getdigits(&p);
5277 }
5278 if (*p++ != '&')
5279 return -1;
5280 if (*p++ != 'w')
5281 return -1;
5282
5283 mouse_code = 0;
5284 switch (Pe)
5285 {
5286 case 0: return -1; /* position request while unavailable */
5287 case 1: /* a response to a locator position request includes
5288 the status of all buttons */
5289 Pb &= 7; /* mask off and ignore fourth button */
5290 if (Pb & 4)
5291 mouse_code = MOUSE_LEFT;
5292 if (Pb & 2)
5293 mouse_code = MOUSE_MIDDLE;
5294 if (Pb & 1)
5295 mouse_code = MOUSE_RIGHT;
5296 if (Pb)
5297 {
5298 held_button = mouse_code;
5299 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005300 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 is_drag = TRUE;
5303 showmode();
5304 break;
5305 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005306 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
5308 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5309 break;
5310 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005311 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 break;
5313 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5314 break;
5315 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005316 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 break;
5318 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5319 break;
5320 case 8: return -1; /* fourth button down */
5321 case 9: return -1; /* fourth button up */
5322 case 10: return -1; /* mouse outside of filter rectangle */
5323 default: return -1; /* should never occur */
5324 }
5325
5326 mouse_col = Pc - 1;
5327 mouse_row = Pr - 1;
5328
5329 slen += (int)(p - (tp + slen));
5330 }
5331# endif /* FEAT_MOUSE_DEC */
5332# ifdef FEAT_MOUSE_PTERM
5333 if (key_name[0] == (int)KS_PTERM_MOUSE)
5334 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005335 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 p = tp + slen;
5338
5339 action = getdigits(&p);
5340 if (*p++ != ';')
5341 return -1;
5342
5343 mouse_row = getdigits(&p);
5344 if (*p++ != ';')
5345 return -1;
5346 mouse_col = getdigits(&p);
5347 if (*p++ != ';')
5348 return -1;
5349
5350 button = getdigits(&p);
5351 mouse_code = 0;
5352
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005353 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 case 4: mouse_code = MOUSE_LEFT; break;
5356 case 1: mouse_code = MOUSE_RIGHT; break;
5357 case 2: mouse_code = MOUSE_MIDDLE; break;
5358 default: return -1;
5359 }
5360
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005361 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 case 31: /* Initial press */
5364 if (*p++ != ';')
5365 return -1;
5366
5367 num_clicks = getdigits(&p); /* Not used */
5368 break;
5369
5370 case 32: /* Release */
5371 mouse_code |= MOUSE_RELEASE;
5372 break;
5373
5374 case 33: /* Drag */
5375 held_button = mouse_code;
5376 mouse_code |= MOUSE_DRAG;
5377 break;
5378
5379 default:
5380 return -1;
5381 }
5382
5383 if (*p++ != 't')
5384 return -1;
5385
5386 slen += (p - (tp + slen));
5387 }
5388# endif /* FEAT_MOUSE_PTERM */
5389
5390 /* Interpret the mouse code */
5391 current_button = (mouse_code & MOUSE_CLICK_MASK);
5392 if (current_button == MOUSE_RELEASE
5393# ifdef FEAT_MOUSE_XTERM
5394 && wheel_code == 0
5395# endif
5396 )
5397 {
5398 /*
5399 * If we get a mouse drag or release event when
5400 * there is no mouse button held down (held_button ==
5401 * MOUSE_RELEASE), produce a K_IGNORE below.
5402 * (can happen when you hold down two buttons
5403 * and then let them go, or click in the menu bar, but not
5404 * on a menu, and drag into the text).
5405 */
5406 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5407 is_drag = TRUE;
5408 current_button = held_button;
5409 }
5410 else if (wheel_code == 0)
5411 {
5412# ifdef CHECK_DOUBLE_CLICK
5413# ifdef FEAT_MOUSE_GPM
5414# ifdef FEAT_GUI
5415 /*
5416 * Only for Unix, when GUI or gpm is not active, we handle
5417 * multi-clicks here.
5418 */
5419 if (gpm_flag == 0 && !gui.in_use)
5420# else
5421 if (gpm_flag == 0)
5422# endif
5423# else
5424# ifdef FEAT_GUI
5425 if (!gui.in_use)
5426# endif
5427# endif
5428 {
5429 /*
5430 * Compute the time elapsed since the previous mouse click.
5431 */
5432 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005433 if (orig_mouse_time.tv_sec == 0)
5434 {
5435 /*
5436 * Avoid computing the difference between mouse_time
5437 * and orig_mouse_time for the first click, as the
5438 * difference would be huge and would cause multiplication
5439 * overflow.
5440 */
5441 timediff = p_mouset;
5442 }
5443 else
5444 {
5445 timediff = (mouse_time.tv_usec
5446 - orig_mouse_time.tv_usec) / 1000;
5447 if (timediff < 0)
5448 --orig_mouse_time.tv_sec;
5449 timediff += (mouse_time.tv_sec
5450 - orig_mouse_time.tv_sec) * 1000;
5451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 orig_mouse_time = mouse_time;
5453 if (mouse_code == orig_mouse_code
5454 && timediff < p_mouset
5455 && orig_num_clicks != 4
5456 && orig_mouse_col == mouse_col
5457 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005458 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005460 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005462 )
5463#ifdef FEAT_WINDOWS
5464 /* Double click in tab pages line also works
5465 * when window contents changes. */
5466 || (mouse_row == 0 && firstwin->w_winrow > 0)
5467#endif
5468 )
5469 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 ++orig_num_clicks;
5471 else
5472 orig_num_clicks = 1;
5473 orig_mouse_col = mouse_col;
5474 orig_mouse_row = mouse_row;
5475 orig_topline = curwin->w_topline;
5476#ifdef FEAT_DIFF
5477 orig_topfill = curwin->w_topfill;
5478#endif
5479 }
5480# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5481 else
5482 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5483# endif
5484# else
5485 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5486# endif
5487 is_click = TRUE;
5488 orig_mouse_code = mouse_code;
5489 }
5490 if (!is_drag)
5491 held_button = mouse_code & MOUSE_CLICK_MASK;
5492
5493 /*
5494 * Translate the actual mouse event into a pseudo mouse event.
5495 * First work out what modifiers are to be used.
5496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (orig_mouse_code & MOUSE_SHIFT)
5498 modifiers |= MOD_MASK_SHIFT;
5499 if (orig_mouse_code & MOUSE_CTRL)
5500 modifiers |= MOD_MASK_CTRL;
5501 if (orig_mouse_code & MOUSE_ALT)
5502 modifiers |= MOD_MASK_ALT;
5503 if (orig_num_clicks == 2)
5504 modifiers |= MOD_MASK_2CLICK;
5505 else if (orig_num_clicks == 3)
5506 modifiers |= MOD_MASK_3CLICK;
5507 else if (orig_num_clicks == 4)
5508 modifiers |= MOD_MASK_4CLICK;
5509
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005510 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5511 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005513 if (wheel_code != 0
5514 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005515 {
5516 if (wheel_code & MOUSE_CTRL)
5517 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005518 if (wheel_code & MOUSE_ALT)
5519 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 key_name[1] = (wheel_code & 1)
5521 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 else
5524 key_name[1] = get_pseudo_mouse_code(current_button,
5525 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005526
5527 /* Make sure the mouse position is valid. Some terminals may
5528 * return weird values. */
5529 if (mouse_col >= Columns)
5530 mouse_col = Columns - 1;
5531 if (mouse_row >= Rows)
5532 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534#endif /* FEAT_MOUSE */
5535
5536#ifdef FEAT_GUI
5537 /*
5538 * If using the GUI, then we get menu and scrollbar events.
5539 *
5540 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5541 * four bytes which are to be taken as a pointer to the vimmenu_T
5542 * structure.
5543 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005544 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005545 * is one byte with the tab index.
5546 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5548 * by one byte representing the scrollbar number, and then four bytes
5549 * representing a long_u which is the new value of the scrollbar.
5550 *
5551 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5552 * KE_FILLER followed by four bytes representing a long_u which is the
5553 * new value of the scrollbar.
5554 */
5555# ifdef FEAT_MENU
5556 else if (key_name[0] == (int)KS_MENU)
5557 {
5558 long_u val;
5559
5560 num_bytes = get_long_from_buf(tp + slen, &val);
5561 if (num_bytes == -1)
5562 return -1;
5563 current_menu = (vimmenu_T *)val;
5564 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005565
5566 /* The menu may have been deleted right after it was used, check
5567 * for that. */
5568 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5569 {
5570 key_name[0] = KS_EXTRA;
5571 key_name[1] = (int)KE_IGNORE;
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005575# ifdef FEAT_GUI_TABLINE
5576 else if (key_name[0] == (int)KS_TABLINE)
5577 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005578 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005579 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5580 if (num_bytes == -1)
5581 return -1;
5582 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005583 if (current_tab == 255) /* -1 in a byte gives 255 */
5584 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005585 slen += num_bytes;
5586 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005587 else if (key_name[0] == (int)KS_TABMENU)
5588 {
5589 /* Selecting tabline tab or using its menu. */
5590 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5591 if (num_bytes == -1)
5592 return -1;
5593 current_tab = (int)bytes[0];
5594 current_tabmenu = (int)bytes[1];
5595 slen += num_bytes;
5596 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598# ifndef USE_ON_FLY_SCROLL
5599 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5600 {
5601 long_u val;
5602
5603 /* Get the last scrollbar event in the queue of the same type */
5604 j = 0;
5605 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5606 && tp[j + 2] != NUL; ++i)
5607 {
5608 j += 3;
5609 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5610 if (num_bytes == -1)
5611 break;
5612 if (i == 0)
5613 current_scrollbar = (int)bytes[0];
5614 else if (current_scrollbar != (int)bytes[0])
5615 break;
5616 j += num_bytes;
5617 num_bytes = get_long_from_buf(tp + j, &val);
5618 if (num_bytes == -1)
5619 break;
5620 scrollbar_value = val;
5621 j += num_bytes;
5622 slen = j;
5623 }
5624 if (i == 0) /* not enough characters to make one */
5625 return -1;
5626 }
5627 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5628 {
5629 long_u val;
5630
5631 /* Get the last horiz. scrollbar event in the queue */
5632 j = 0;
5633 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5634 && tp[j + 2] != NUL; ++i)
5635 {
5636 j += 3;
5637 num_bytes = get_long_from_buf(tp + j, &val);
5638 if (num_bytes == -1)
5639 break;
5640 scrollbar_value = val;
5641 j += num_bytes;
5642 slen = j;
5643 }
5644 if (i == 0) /* not enough characters to make one */
5645 return -1;
5646 }
5647# endif /* !USE_ON_FLY_SCROLL */
5648#endif /* FEAT_GUI */
5649
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005650 /*
5651 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5652 */
5653 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5654
5655 /*
5656 * Add any modifier codes to our string.
5657 */
5658 new_slen = 0; /* Length of what will replace the termcode */
5659 if (modifiers != 0)
5660 {
5661 /* Some keys have the modifier included. Need to handle that here
5662 * to make mappings work. */
5663 key = simplify_key(key, &modifiers);
5664 if (modifiers != 0)
5665 {
5666 string[new_slen++] = K_SPECIAL;
5667 string[new_slen++] = (int)KS_MODIFIER;
5668 string[new_slen++] = modifiers;
5669 }
5670 }
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005673 key_name[0] = KEY2TERMCAP0(key);
5674 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005676 {
5677 /* from ":set <M-b>=xx" */
5678#ifdef FEAT_MBYTE
5679 if (has_mbyte)
5680 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5681 else
5682#endif
5683 string[new_slen++] = key_name[1];
5684 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005685 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5686 && key_name[1] == KE_IGNORE)
5687 {
5688 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5689 * to indicate what happened. */
5690 retval = KEYLEN_REMOVED;
5691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
5693 {
5694 string[new_slen++] = K_SPECIAL;
5695 string[new_slen++] = key_name[0];
5696 string[new_slen++] = key_name[1];
5697 }
5698 string[new_slen] = NUL;
5699 extra = new_slen - slen;
5700 if (buf == NULL)
5701 {
5702 if (extra < 0)
5703 /* remove matched chars, taking care of noremap */
5704 del_typebuf(-extra, offset);
5705 else if (extra > 0)
5706 /* insert the extra space we need */
5707 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5708
5709 /*
5710 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5711 * typebuf.tb_buf[]!
5712 */
5713 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5714 (size_t)new_slen);
5715 }
5716 else
5717 {
5718 if (extra < 0)
5719 /* remove matched characters */
5720 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005721 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005723 {
5724 /* Insert the extra space we need. If there is insufficient
5725 * space return -1. */
5726 if (*buflen + extra + new_slen >= bufsize)
5727 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005729 (size_t)(*buflen - offset));
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005732 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005734 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
5736
Bram Moolenaar2951b772013-07-03 12:45:31 +02005737#ifdef FEAT_TERMRESPONSE
5738 LOG_TR("normal character");
5739#endif
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return 0; /* no match found */
5742}
5743
5744/*
5745 * Replace any terminal code strings in from[] with the equivalent internal
5746 * vim representation. This is used for the "from" and "to" part of a
5747 * mapping, and the "to" part of a menu command.
5748 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5749 * '<'.
5750 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5751 *
5752 * The replacement is done in result[] and finally copied into allocated
5753 * memory. If this all works well *bufp is set to the allocated memory and a
5754 * pointer to it is returned. If something fails *bufp is set to NULL and from
5755 * is returned.
5756 *
5757 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5758 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5759 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5760 * instead of a CTRL-V.
5761 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005762 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005763replace_termcodes(
5764 char_u *from,
5765 char_u **bufp,
5766 int from_part,
5767 int do_lt, /* also translate <lt> */
5768 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 int i;
5771 int slen;
5772 int key;
5773 int dlen = 0;
5774 char_u *src;
5775 int do_backslash; /* backslash is a special character */
5776 int do_special; /* recognize <> key codes */
5777 int do_key_code; /* recognize raw key codes */
5778 char_u *result; /* buffer for resulting string */
5779
5780 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005781 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5783
5784 /*
5785 * Allocate space for the translation. Worst case a single character is
5786 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5787 */
5788 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5789 if (result == NULL) /* out of memory */
5790 {
5791 *bufp = NULL;
5792 return from;
5793 }
5794
5795 src = from;
5796
5797 /*
5798 * Check for #n at start only: function key n
5799 */
5800 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5801 {
5802 result[dlen++] = K_SPECIAL;
5803 result[dlen++] = 'k';
5804 if (src[1] == '0')
5805 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5806 else
5807 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5808 src += 2;
5809 }
5810
5811 /*
5812 * Copy each byte from *from to result[dlen]
5813 */
5814 while (*src != NUL)
5815 {
5816 /*
5817 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005818 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 */
5820 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5821 {
5822#ifdef FEAT_EVAL
5823 /*
5824 * Replace <SID> by K_SNR <script-nr> _.
5825 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5826 */
5827 if (STRNICMP(src, "<SID>", 5) == 0)
5828 {
5829 if (current_SID <= 0)
5830 EMSG(_(e_usingsid));
5831 else
5832 {
5833 src += 5;
5834 result[dlen++] = K_SPECIAL;
5835 result[dlen++] = (int)KS_EXTRA;
5836 result[dlen++] = (int)KE_SNR;
5837 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5838 dlen += (int)STRLEN(result + dlen);
5839 result[dlen++] = '_';
5840 continue;
5841 }
5842 }
5843#endif
5844
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005845 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (slen)
5847 {
5848 dlen += slen;
5849 continue;
5850 }
5851 }
5852
5853 /*
5854 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5855 * Note that this is also checked after replacing the <> form.
5856 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5857 * it could be a character in the file.
5858 */
5859 if (do_key_code)
5860 {
5861 i = find_term_bykeys(src);
5862 if (i >= 0)
5863 {
5864 result[dlen++] = K_SPECIAL;
5865 result[dlen++] = termcodes[i].name[0];
5866 result[dlen++] = termcodes[i].name[1];
5867 src += termcodes[i].len;
5868 /* If terminal code matched, continue after it. */
5869 continue;
5870 }
5871 }
5872
5873#ifdef FEAT_EVAL
5874 if (do_special)
5875 {
5876 char_u *p, *s, len;
5877
5878 /*
5879 * Replace <Leader> by the value of "mapleader".
5880 * Replace <LocalLeader> by the value of "maplocalleader".
5881 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5882 */
5883 if (STRNICMP(src, "<Leader>", 8) == 0)
5884 {
5885 len = 8;
5886 p = get_var_value((char_u *)"g:mapleader");
5887 }
5888 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5889 {
5890 len = 13;
5891 p = get_var_value((char_u *)"g:maplocalleader");
5892 }
5893 else
5894 {
5895 len = 0;
5896 p = NULL;
5897 }
5898 if (len != 0)
5899 {
5900 /* Allow up to 8 * 6 characters for "mapleader". */
5901 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5902 s = (char_u *)"\\";
5903 else
5904 s = p;
5905 while (*s != NUL)
5906 result[dlen++] = *s++;
5907 src += len;
5908 continue;
5909 }
5910 }
5911#endif
5912
5913 /*
5914 * Remove CTRL-V and ignore the next character.
5915 * For "from" side the CTRL-V at the end is included, for the "to"
5916 * part it is removed.
5917 * If 'cpoptions' does not contain 'B', also accept a backslash.
5918 */
5919 key = *src;
5920 if (key == Ctrl_V || (do_backslash && key == '\\'))
5921 {
5922 ++src; /* skip CTRL-V or backslash */
5923 if (*src == NUL)
5924 {
5925 if (from_part)
5926 result[dlen++] = key;
5927 break;
5928 }
5929 }
5930
5931#ifdef FEAT_MBYTE
5932 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005933 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#endif
5935 {
5936 /*
5937 * If the character is K_SPECIAL, replace it with K_SPECIAL
5938 * KS_SPECIAL KE_FILLER.
5939 * If compiled with the GUI replace CSI with K_CSI.
5940 */
5941 if (*src == K_SPECIAL)
5942 {
5943 result[dlen++] = K_SPECIAL;
5944 result[dlen++] = KS_SPECIAL;
5945 result[dlen++] = KE_FILLER;
5946 }
5947# ifdef FEAT_GUI
5948 else if (*src == CSI)
5949 {
5950 result[dlen++] = K_SPECIAL;
5951 result[dlen++] = KS_EXTRA;
5952 result[dlen++] = (int)KE_CSI;
5953 }
5954# endif
5955 else
5956 result[dlen++] = *src;
5957 ++src;
5958 }
5959 }
5960 result[dlen] = NUL;
5961
5962 /*
5963 * Copy the new string to allocated memory.
5964 * If this fails, just return from.
5965 */
5966 if ((*bufp = vim_strsave(result)) != NULL)
5967 from = *bufp;
5968 vim_free(result);
5969 return from;
5970}
5971
5972/*
5973 * Find a termcode with keys 'src' (must be NUL terminated).
5974 * Return the index in termcodes[], or -1 if not found.
5975 */
5976 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005977find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978{
5979 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005980 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 for (i = 0; i < tc_len; ++i)
5983 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005984 if (slen == termcodes[i].len
5985 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return i;
5987 }
5988 return -1;
5989}
5990
5991/*
5992 * Gather the first characters in the terminal key codes into a string.
5993 * Used to speed up check_termcode().
5994 */
5995 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005996gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 int i;
5999 int len = 0;
6000
6001#ifdef FEAT_GUI
6002 if (gui.in_use)
6003 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6004#endif
6005#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006006 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 termleader[len++] = DCS; /* the termcode response starts with DCS
6008 in 8-bit mode */
6009#endif
6010 termleader[len] = NUL;
6011
6012 for (i = 0; i < tc_len; ++i)
6013 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6014 {
6015 termleader[len++] = termcodes[i].code[0];
6016 termleader[len] = NUL;
6017 }
6018
6019 need_gather = FALSE;
6020}
6021
6022/*
6023 * Show all termcodes (for ":set termcap")
6024 * This code looks a lot like showoptions(), but is different.
6025 */
6026 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006027show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 int col;
6030 int *items;
6031 int item_count;
6032 int run;
6033 int row, rows;
6034 int cols;
6035 int i;
6036 int len;
6037
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006038#define INC3 27 /* try to make three columns */
6039#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040#define GAP 2 /* spaces between columns */
6041
6042 if (tc_len == 0) /* no terminal codes (must be GUI) */
6043 return;
6044 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6045 if (items == NULL)
6046 return;
6047
6048 /* Highlight title */
6049 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6050
6051 /*
6052 * do the loop two times:
6053 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006054 * 2. display the medium items (medium length strings)
6055 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006057 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 {
6059 /*
6060 * collect the items in items[]
6061 */
6062 item_count = 0;
6063 for (i = 0; i < tc_len; i++)
6064 {
6065 len = show_one_termcode(termcodes[i].name,
6066 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006067 if (len <= INC3 - GAP ? run == 1
6068 : len <= INC2 - GAP ? run == 2
6069 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 items[item_count++] = i;
6071 }
6072
6073 /*
6074 * display the items
6075 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006076 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006078 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (cols == 0)
6080 cols = 1;
6081 rows = (item_count + cols - 1) / cols;
6082 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006083 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 rows = item_count;
6085 for (row = 0; row < rows && !got_int; ++row)
6086 {
6087 msg_putchar('\n'); /* go to next line */
6088 if (got_int) /* 'q' typed in more */
6089 break;
6090 col = 0;
6091 for (i = row; i < item_count; i += rows)
6092 {
6093 msg_col = col; /* make columns */
6094 show_one_termcode(termcodes[items[i]].name,
6095 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006096 if (run == 2)
6097 col += INC2;
6098 else
6099 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101 out_flush();
6102 ui_breakcheck();
6103 }
6104 }
6105 vim_free(items);
6106}
6107
6108/*
6109 * Show one termcode entry.
6110 * Output goes into IObuff[]
6111 */
6112 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006113show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115 char_u *p;
6116 int len;
6117
6118 if (name[0] > '~')
6119 {
6120 IObuff[0] = ' ';
6121 IObuff[1] = ' ';
6122 IObuff[2] = ' ';
6123 IObuff[3] = ' ';
6124 }
6125 else
6126 {
6127 IObuff[0] = 't';
6128 IObuff[1] = '_';
6129 IObuff[2] = name[0];
6130 IObuff[3] = name[1];
6131 }
6132 IObuff[4] = ' ';
6133
6134 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6135 if (p[1] != 't')
6136 STRCPY(IObuff + 5, p);
6137 else
6138 IObuff[5] = NUL;
6139 len = (int)STRLEN(IObuff);
6140 do
6141 IObuff[len++] = ' ';
6142 while (len < 17);
6143 IObuff[len] = NUL;
6144 if (code == NULL)
6145 len += 4;
6146 else
6147 len += vim_strsize(code);
6148
6149 if (printit)
6150 {
6151 msg_puts(IObuff);
6152 if (code == NULL)
6153 msg_puts((char_u *)"NULL");
6154 else
6155 msg_outtrans(code);
6156 }
6157 return len;
6158}
6159
6160#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6161/*
6162 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6163 * termcap codes from the terminal itself.
6164 * We get them one by one to avoid a very long response string.
6165 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006166static int xt_index_in = 0;
6167static int xt_index_out = 0;
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006170req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 xt_index_out = 0;
6173 xt_index_in = 0;
6174 req_more_codes_from_term();
6175}
6176
6177 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006178req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 char buf[11];
6181 int old_idx = xt_index_out;
6182
6183 /* Don't do anything when going to exit. */
6184 if (exiting)
6185 return;
6186
6187 /* Send up to 10 more requests out than we received. Avoid sending too
6188 * many, there can be a buffer overflow somewhere. */
6189 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6190 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006191# ifdef DEBUG_TERMRESPONSE
6192 char dbuf[100];
6193
6194 sprintf(dbuf, "Requesting XT %d: %s",
6195 xt_index_out, key_names[xt_index_out]);
6196 log_tr(dbuf);
6197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 sprintf(buf, "\033P+q%02x%02x\033\\",
6199 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6200 out_str_nf((char_u *)buf);
6201 ++xt_index_out;
6202 }
6203
6204 /* Send the codes out right away. */
6205 if (xt_index_out != old_idx)
6206 out_flush();
6207}
6208
6209/*
6210 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6211 * A "0" instead of the "1" indicates a code that isn't supported.
6212 * Both <name> and <string> are encoded in hex.
6213 * "code" points to the "0" or "1".
6214 */
6215 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006216got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
6218#define XT_LEN 100
6219 char_u name[3];
6220 char_u str[XT_LEN];
6221 int i;
6222 int j = 0;
6223 int c;
6224
6225 /* A '1' means the code is supported, a '0' means it isn't.
6226 * When half the length is > XT_LEN we can't use it.
6227 * Our names are currently all 2 characters. */
6228 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6229 {
6230 /* Get the name from the response and find it in the table. */
6231 name[0] = hexhex2nr(code + 3);
6232 name[1] = hexhex2nr(code + 5);
6233 name[2] = NUL;
6234 for (i = 0; key_names[i] != NULL; ++i)
6235 {
6236 if (STRCMP(key_names[i], name) == 0)
6237 {
6238 xt_index_in = i;
6239 break;
6240 }
6241 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006242# ifdef DEBUG_TERMRESPONSE
6243 {
6244 char buf[100];
6245
6246 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6247 log_tr(buf);
6248 }
6249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (key_names[i] != NULL)
6251 {
6252 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6253 str[j++] = c;
6254 str[j] = NUL;
6255 if (name[0] == 'C' && name[1] == 'o')
6256 {
6257 /* Color count is not a key code. */
6258 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006259 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
6261 else
6262 {
6263 /* First delete any existing entry with the same code. */
6264 i = find_term_bykeys(str);
6265 if (i >= 0)
6266 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006267 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269 }
6270 }
6271
6272 /* May request more codes now that we received one. */
6273 ++xt_index_in;
6274 req_more_codes_from_term();
6275}
6276
6277/*
6278 * Check if there are any unanswered requests and deal with them.
6279 * This is called before starting an external program or getting direct
6280 * keyboard input. We don't want responses to be send to that program or
6281 * handled as typed text.
6282 */
6283 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006284check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 int c;
6287
6288 /* If no codes requested or all are answered, no need to wait. */
6289 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6290 return;
6291
6292 /* Vgetc() will check for and handle any response.
6293 * Keep calling vpeekc() until we don't get any responses. */
6294 ++no_mapping;
6295 ++allow_keys;
6296 for (;;)
6297 {
6298 c = vpeekc();
6299 if (c == NUL) /* nothing available */
6300 break;
6301
6302 /* If a response is recognized it's replaced with K_IGNORE, must read
6303 * it from the input stream. If there is no K_IGNORE we can't do
6304 * anything, break here (there might be some responses further on, but
6305 * we don't want to throw away any typed chars). */
6306 if (c != K_SPECIAL && c != K_IGNORE)
6307 break;
6308 c = vgetc();
6309 if (c != K_IGNORE)
6310 {
6311 vungetc(c);
6312 break;
6313 }
6314 }
6315 --no_mapping;
6316 --allow_keys;
6317}
6318#endif
6319
6320#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6321/*
6322 * Translate an internal mapping/abbreviation representation into the
6323 * corresponding external one recognized by :map/:abbrev commands;
6324 * respects the current B/k/< settings of 'cpoption'.
6325 *
6326 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006327 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 *
6329 * It uses a growarray to build the translation string since the
6330 * latter can be wider than the original description. The caller has to
6331 * free the string afterwards.
6332 *
6333 * Returns NULL when there is a problem.
6334 */
6335 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006336translate_mapping(
6337 char_u *str,
6338 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 garray_T ga;
6341 int c;
6342 int modifiers;
6343 int cpo_bslash;
6344 int cpo_special;
6345 int cpo_keycode;
6346
6347 ga_init(&ga);
6348 ga.ga_itemsize = 1;
6349 ga.ga_growsize = 40;
6350
6351 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6352 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6353 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6354
6355 for (; *str; ++str)
6356 {
6357 c = *str;
6358 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6359 {
6360 modifiers = 0;
6361 if (str[1] == KS_MODIFIER)
6362 {
6363 str++;
6364 modifiers = *++str;
6365 c = *++str;
6366 }
6367 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6368 {
6369 int i;
6370
6371 /* try to find special key in termcodes */
6372 for (i = 0; i < tc_len; ++i)
6373 if (termcodes[i].name[0] == str[1]
6374 && termcodes[i].name[1] == str[2])
6375 break;
6376 if (i < tc_len)
6377 {
6378 ga_concat(&ga, termcodes[i].code);
6379 str += 2;
6380 continue; /* for (str) */
6381 }
6382 }
6383 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6384 {
6385 if (expmap && cpo_special)
6386 {
6387 ga_clear(&ga);
6388 return NULL;
6389 }
6390 c = TO_SPECIAL(str[1], str[2]);
6391 if (c == K_ZERO) /* display <Nul> as ^@ */
6392 c = NUL;
6393 str += 2;
6394 }
6395 if (IS_SPECIAL(c) || modifiers) /* special key */
6396 {
6397 if (expmap && cpo_special)
6398 {
6399 ga_clear(&ga);
6400 return NULL;
6401 }
6402 ga_concat(&ga, get_special_key_name(c, modifiers));
6403 continue; /* for (str) */
6404 }
6405 }
6406 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6407 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6408 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6409 if (c)
6410 ga_append(&ga, c);
6411 }
6412 ga_append(&ga, NUL);
6413 return (char_u *)(ga.ga_data);
6414}
6415#endif
6416
6417#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6418static char ksme_str[20];
6419static char ksmr_str[20];
6420static char ksmd_str[20];
6421
6422/*
6423 * For Win32 console: update termcap codes for existing console attributes.
6424 */
6425 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006426update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 struct builtin_term *p;
6429
6430 p = find_builtin_term(DEFAULT_TERM);
6431 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6432 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6433 attr | 0x08); /* FOREGROUND_INTENSITY */
6434 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6435 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6436
6437 while (p->bt_string != NULL)
6438 {
6439 if (p->bt_entry == (int)KS_ME)
6440 p->bt_string = &ksme_str[0];
6441 else if (p->bt_entry == (int)KS_MR)
6442 p->bt_string = &ksmr_str[0];
6443 else if (p->bt_entry == (int)KS_MD)
6444 p->bt_string = &ksmd_str[0];
6445 ++p;
6446 }
6447}
6448#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006449
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006450#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006451 static int
6452hex_digit(int c)
6453{
6454 if (isdigit(c))
6455 return c - '0';
6456 c = TOLOWER_ASC(c);
6457 if (c >= 'a' && c <= 'f')
6458 return c - 'a' + 10;
6459 return 0x1ffffff;
6460}
6461
6462 guicolor_T
6463gui_get_color_cmn(char_u *name)
6464{
Bram Moolenaar28726652017-01-06 18:16:19 +01006465 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6466 * values as used by the MS-Windows GDI api. It should be used only for
6467 * MS-Windows GDI builds. */
6468# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6469# undef RGB
6470# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006471# ifndef RGB
6472# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6473# endif
6474# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006475 FILE *fd;
6476 char line[LINE_LEN];
6477 char_u *fname;
6478 int r, g, b, i;
6479 guicolor_T color;
6480
6481 struct rgbcolor_table_S {
6482 char_u *color_name;
6483 guicolor_T color;
6484 };
6485
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006486 /* Only non X11 colors (not present in rgb.txt) and colors in
6487 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006488 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006489 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6490 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6491 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6492 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6493 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6494 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6495 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6496 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6497 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6498 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6499 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6500 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6501 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006502 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6503 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006504 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006505 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006506 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6507 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6508 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6509 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6510 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6511 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6512 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6513 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6514 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006515 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006516 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006517 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6518 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006519 };
6520
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006521 static struct rgbcolor_table_S *colornames_table;
6522 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006523
6524 if (name[0] == '#' && STRLEN(name) == 7)
6525 {
6526 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006527 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006528 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6529 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6530 if (color > 0xffffff)
6531 return INVALCOLOR;
6532 return color;
6533 }
6534
6535 /* Check if the name is one of the colors we know */
6536 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6537 if (STRICMP(name, rgb_table[i].color_name) == 0)
6538 return rgb_table[i].color;
6539
6540 /*
6541 * Last attempt. Look in the file "$VIM/rgb.txt".
6542 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006543 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006544 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006545 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006546
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006547 /* colornames_table not yet initialized */
6548 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6549 if (fname == NULL)
6550 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006551
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006552 fd = fopen((char *)fname, "rt");
6553 vim_free(fname);
6554 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006555 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006556 if (p_verbose > 1)
6557 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6558 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006559 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006560
6561 for (counting = 1; counting >= 0; --counting)
6562 {
6563 if (!counting)
6564 {
6565 colornames_table = (struct rgbcolor_table_S *)alloc(
6566 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6567 if (colornames_table == NULL)
6568 {
6569 fclose(fd);
6570 return INVALCOLOR;
6571 }
6572 rewind(fd);
6573 }
6574 size = 0;
6575
6576 while (!feof(fd))
6577 {
6578 size_t len;
6579 int pos;
6580
6581 ignoredp = fgets(line, LINE_LEN, fd);
6582 len = strlen(line);
6583
6584 if (len <= 1 || line[len - 1] != '\n')
6585 continue;
6586
6587 line[len - 1] = '\0';
6588
6589 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6590 if (i != 3)
6591 continue;
6592
6593 if (!counting)
6594 {
6595 char_u *s = vim_strsave((char_u *)line + pos);
6596
6597 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006598 {
6599 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006600 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006601 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006602 colornames_table[size].color_name = s;
6603 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6604 }
6605 size++;
6606 }
6607 }
6608 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006609 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006610
6611 for (i = 0; i < size; i++)
6612 if (STRICMP(name, colornames_table[i].color_name) == 0)
6613 return colornames_table[i].color;
6614
Bram Moolenaarab302212016-04-26 20:59:29 +02006615 return INVALCOLOR;
6616}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006617
6618 guicolor_T
6619gui_get_rgb_color_cmn(int r, int g, int b)
6620{
6621 guicolor_T color = RGB(r, g, b);
6622
6623 if (color > 0xffffff)
6624 return INVALCOLOR;
6625 return color;
6626}
Bram Moolenaarab302212016-04-26 20:59:29 +02006627#endif