blob: 424489206f683418337e71db9509ce78c14f7753 [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 Moolenaarf3af54e2017-08-30 14:53:06 +02001372static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#endif
1374
1375 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001376find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 struct builtin_term *p;
1379
1380 p = builtin_termcaps;
1381 while (p->bt_string != NULL)
1382 {
1383 if (p->bt_entry == (int)KS_NAME)
1384 {
1385#ifdef UNIX
1386 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1387 return p;
1388 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1389 return p;
1390 else
1391#endif
1392#ifdef VMS
1393 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1394 return p;
1395 else
1396#endif
1397 if (STRCMP(term, p->bt_string) == 0)
1398 return p;
1399 }
1400 ++p;
1401 }
1402 return p;
1403}
1404
1405/*
1406 * Parsing of the builtin termcap entries.
1407 * Caller should check if 'name' is a valid builtin term.
1408 * The terminal's name is not set, as this is already done in termcapinit().
1409 */
1410 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001411parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 struct builtin_term *p;
1414 char_u name[2];
1415 int term_8bit;
1416
1417 p = find_builtin_term(term);
1418 term_8bit = term_is_8bit(term);
1419
1420 /* Do not parse if builtin term not found */
1421 if (p->bt_string == NULL)
1422 return;
1423
1424 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1425 {
1426 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1427 {
1428 /* Only set the value if it wasn't set yet. */
1429 if (term_strings[p->bt_entry] == NULL
1430 || term_strings[p->bt_entry] == empty_option)
1431 {
1432 /* 8bit terminal: use CSI instead of <Esc>[ */
1433 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1434 {
1435 char_u *s, *t;
1436
1437 s = vim_strsave((char_u *)p->bt_string);
1438 if (s != NULL)
1439 {
1440 for (t = s; *t; ++t)
1441 if (term_7to8bit(t))
1442 {
1443 *t = term_7to8bit(t);
1444 STRCPY(t + 1, t + 2);
1445 }
1446 term_strings[p->bt_entry] = s;
1447 set_term_option_alloced(&term_strings[p->bt_entry]);
1448 }
1449 }
1450 else
1451 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1452 }
1453 }
1454 else
1455 {
1456 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1457 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1458 if (find_termcode(name) == NULL)
1459 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1460 }
1461 }
1462}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464/*
1465 * Set number of colors.
1466 * Store it as a number in t_colors.
1467 * Store it as a string in T_CCO (using nr_colors[]).
1468 */
1469 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001470set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 char_u nr_colors[20]; /* string for number of colors */
1473
1474 t_colors = nr;
1475 if (t_colors > 1)
1476 sprintf((char *)nr_colors, "%d", t_colors);
1477 else
1478 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001479 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001481
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001482#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001483/*
1484 * Set the color count to "val" and redraw if it changed.
1485 */
1486 static void
1487may_adjust_color_count(int val)
1488{
1489 if (val != t_colors)
1490 {
1491 /* Nr of colors changed, initialize highlighting and
1492 * redraw everything. This causes a redraw, which usually
1493 * clears the message. Try keeping the message if it
1494 * might work. */
1495 set_keep_msg_from_hist();
1496 set_color_count(val);
1497 init_highlight(TRUE, FALSE);
1498# ifdef DEBUG_TERMRESPONSE
1499 {
1500 char buf[100];
1501 int r = redraw_asap(CLEAR);
1502
1503 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1504 log_tr(buf);
1505 }
1506# else
1507 redraw_asap(CLEAR);
1508# endif
1509 }
1510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512
1513#ifdef HAVE_TGETENT
1514static char *(key_names[]) =
1515{
1516#ifdef FEAT_TERMRESPONSE
1517 /* Do this one first, it may cause a screen redraw. */
1518 "Co",
1519#endif
1520 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 "#2", "#4", "%i", "*7",
1522 "k1", "k2", "k3", "k4", "k5", "k6",
1523 "k7", "k8", "k9", "k;", "F1", "F2",
1524 "%1", "&8", "kb", "kI", "kD", "kh",
1525 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1526 NULL
1527};
1528#endif
1529
1530/*
1531 * Set terminal options for terminal "term".
1532 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1533 *
1534 * While doing this, until ttest(), some options may be NULL, be careful.
1535 */
1536 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001537set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 struct builtin_term *termp;
1540#ifdef HAVE_TGETENT
1541 int builtin_first = p_tbi;
1542 int try;
1543 int termcap_cleared = FALSE;
1544#endif
1545 int width = 0, height = 0;
1546 char_u *error_msg = NULL;
1547 char_u *bs_p, *del_p;
1548
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001549 /* In silect mode (ex -s) we don't use the 'term' option. */
1550 if (silent_mode)
1551 return OK;
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 detected_8bit = FALSE; /* reset 8-bit detection */
1554
1555 if (term_is_builtin(term))
1556 {
1557 term += 8;
1558#ifdef HAVE_TGETENT
1559 builtin_first = 1;
1560#endif
1561 }
1562
1563/*
1564 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1565 * If builtin_first is TRUE:
1566 * 0. try builtin termcap
1567 * 1. try external termcap
1568 * 2. if both fail default to a builtin terminal
1569 * If builtin_first is FALSE:
1570 * 1. try external termcap
1571 * 2. try builtin termcap, if both fail default to a builtin terminal
1572 */
1573#ifdef HAVE_TGETENT
1574 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1575 {
1576 /*
1577 * Use external termcap
1578 */
1579 if (try == 1)
1580 {
1581 char_u *p;
1582 static char_u tstrbuf[TBUFSZ];
1583 int i;
1584 char_u tbuf[TBUFSZ];
1585 char_u *tp;
1586 static struct {
1587 enum SpecialKey dest; /* index in term_strings[] */
1588 char *name; /* termcap name for string */
1589 } string_names[] =
1590 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1591 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1592 {KS_CL, "cl"}, {KS_CD, "cd"},
1593 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001594 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1596 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001597 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1598 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1600 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1601 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1602 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1603 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001604 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001606 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {KS_TS, "ts"}, {KS_FS, "fs"},
1608 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001609 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001610 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001611 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001612 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1613 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {(enum SpecialKey)0, NULL}
1615 };
1616
1617 /*
1618 * If the external termcap does not have a matching entry, try the
1619 * builtin ones.
1620 */
1621 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1622 {
1623 tp = tstrbuf;
1624 if (!termcap_cleared)
1625 {
1626 clear_termoptions(); /* clear old options */
1627 termcap_cleared = TRUE;
1628 }
1629
1630 /* get output strings */
1631 for (i = 0; string_names[i].name != NULL; ++i)
1632 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001633 if (TERM_STR(string_names[i].dest) == NULL
1634 || TERM_STR(string_names[i].dest) == empty_option)
1635 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 TGETSTR(string_names[i].name, &tp);
1637 }
1638
1639 /* tgetflag() returns 1 if the flag is present, 0 if not and
1640 * possibly -1 if the flag doesn't exist. */
1641 if ((T_MS == NULL || T_MS == empty_option)
1642 && tgetflag("ms") > 0)
1643 T_MS = (char_u *)"y";
1644 if ((T_XS == NULL || T_XS == empty_option)
1645 && tgetflag("xs") > 0)
1646 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001647 if ((T_XN == NULL || T_XN == empty_option)
1648 && tgetflag("xn") > 0)
1649 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if ((T_DB == NULL || T_DB == empty_option)
1651 && tgetflag("db") > 0)
1652 T_DB = (char_u *)"y";
1653 if ((T_DA == NULL || T_DA == empty_option)
1654 && tgetflag("da") > 0)
1655 T_DA = (char_u *)"y";
1656 if ((T_UT == NULL || T_UT == empty_option)
1657 && tgetflag("ut") > 0)
1658 T_UT = (char_u *)"y";
1659
1660
1661 /*
1662 * get key codes
1663 */
1664 for (i = 0; key_names[i] != NULL; ++i)
1665 {
1666 if (find_termcode((char_u *)key_names[i]) == NULL)
1667 {
1668 p = TGETSTR(key_names[i], &tp);
1669 /* if cursor-left == backspace, ignore it (televideo
1670 * 925) */
1671 if (p != NULL
1672 && (*p != Ctrl_H
1673 || key_names[i][0] != 'k'
1674 || key_names[i][1] != 'l'))
1675 add_termcode((char_u *)key_names[i], p, FALSE);
1676 }
1677 }
1678
1679 if (height == 0)
1680 height = tgetnum("li");
1681 if (width == 0)
1682 width = tgetnum("co");
1683
1684 /*
1685 * Get number of colors (if not done already).
1686 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001687 if (TERM_STR(KS_CCO) == NULL
1688 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 set_color_count(tgetnum("Co"));
1690
1691# ifndef hpux
1692 BC = (char *)TGETSTR("bc", &tp);
1693 UP = (char *)TGETSTR("up", &tp);
1694 p = TGETSTR("pc", &tp);
1695 if (p)
1696 PC = *p;
1697# endif /* hpux */
1698 }
1699 }
1700 else /* try == 0 || try == 2 */
1701#endif /* HAVE_TGETENT */
1702 /*
1703 * Use builtin termcap
1704 */
1705 {
1706#ifdef HAVE_TGETENT
1707 /*
1708 * If builtin termcap was already used, there is no need to search
1709 * for the builtin termcap again, quit now.
1710 */
1711 if (try == 2 && builtin_first && termcap_cleared)
1712 break;
1713#endif
1714 /*
1715 * search for 'term' in builtin_termcaps[]
1716 */
1717 termp = find_builtin_term(term);
1718 if (termp->bt_string == NULL) /* did not find it */
1719 {
1720#ifdef HAVE_TGETENT
1721 /*
1722 * If try == 0, first try the external termcap. If that is not
1723 * found we'll get back here with try == 2.
1724 * If termcap_cleared is set we used the external termcap,
1725 * don't complain about not finding the term in the builtin
1726 * termcap.
1727 */
1728 if (try == 0) /* try external one */
1729 continue;
1730 if (termcap_cleared) /* found in external termcap */
1731 break;
1732#endif
1733
1734 mch_errmsg("\r\n");
1735 if (error_msg != NULL)
1736 {
1737 mch_errmsg((char *)error_msg);
1738 mch_errmsg("\r\n");
1739 }
1740 mch_errmsg("'");
1741 mch_errmsg((char *)term);
1742 mch_errmsg(_("' not known. Available builtin terminals are:"));
1743 mch_errmsg("\r\n");
1744 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1745 ++termp)
1746 {
1747 if (termp->bt_entry == (int)KS_NAME)
1748 {
1749#ifdef HAVE_TGETENT
1750 mch_errmsg(" builtin_");
1751#else
1752 mch_errmsg(" ");
1753#endif
1754 mch_errmsg(termp->bt_string);
1755 mch_errmsg("\r\n");
1756 }
1757 }
1758 /* when user typed :set term=xxx, quit here */
1759 if (starting != NO_SCREEN)
1760 {
1761 screen_start(); /* don't know where cursor is now */
1762 wait_return(TRUE);
1763 return FAIL;
1764 }
1765 term = DEFAULT_TERM;
1766 mch_errmsg(_("defaulting to '"));
1767 mch_errmsg((char *)term);
1768 mch_errmsg("'\r\n");
1769 if (emsg_silent == 0)
1770 {
1771 screen_start(); /* don't know where cursor is now */
1772 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001773 if (!is_not_a_term())
1774 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001776 set_string_option_direct((char_u *)"term", -1, term,
1777 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 display_errors();
1779 }
1780 out_flush();
1781#ifdef HAVE_TGETENT
1782 if (!termcap_cleared)
1783 {
1784#endif
1785 clear_termoptions(); /* clear old options */
1786#ifdef HAVE_TGETENT
1787 termcap_cleared = TRUE;
1788 }
1789#endif
1790 parse_builtin_tcap(term);
1791#ifdef FEAT_GUI
1792 if (term_is_gui(term))
1793 {
1794 out_flush();
1795 gui_init();
1796 /* If starting the GUI failed, don't do any of the other
1797 * things for this terminal */
1798 if (!gui.in_use)
1799 return FAIL;
1800#ifdef HAVE_TGETENT
1801 break; /* don't try using external termcap */
1802#endif
1803 }
1804#endif /* FEAT_GUI */
1805 }
1806#ifdef HAVE_TGETENT
1807 }
1808#endif
1809
1810/*
1811 * special: There is no info in the termcap about whether the cursor
1812 * positioning is relative to the start of the screen or to the start of the
1813 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1814 * relative.
1815 */
1816 if (STRCMP(term, "pcterm") == 0)
1817 T_CCS = (char_u *)"yes";
1818 else
1819 T_CCS = empty_option;
1820
1821#ifdef UNIX
1822/*
1823 * Any "stty" settings override the default for t_kb from the termcap.
1824 * This is in os_unix.c, because it depends a lot on the version of unix that
1825 * is being used.
1826 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1827 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001828# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 get_stty();
1832#endif
1833
1834/*
1835 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1836 * didn't work, use the default CTRL-H
1837 * The default for t_kD is DEL, unless t_kb is DEL.
1838 * The vim_strsave'd strings are probably lost forever, well it's only two
1839 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1840 * directly.
1841 */
1842#ifdef FEAT_GUI
1843 if (!gui.in_use)
1844#endif
1845 {
1846 bs_p = find_termcode((char_u *)"kb");
1847 del_p = find_termcode((char_u *)"kD");
1848 if (bs_p == NULL || *bs_p == NUL)
1849 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1850 if ((del_p == NULL || *del_p == NUL) &&
1851 (bs_p == NULL || *bs_p != DEL))
1852 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1853 }
1854
1855#if defined(UNIX) || defined(VMS)
1856 term_is_xterm = vim_is_xterm(term);
1857#endif
1858
1859#ifdef FEAT_MOUSE
1860# if defined(UNIX) || defined(VMS)
1861# ifdef FEAT_MOUSE_TTY
1862 /*
1863 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1864 * The termcode for the mouse is added as a side effect in option.c.
1865 */
1866 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001867 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001870 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
1872 if (use_xterm_mouse())
1873 p = NULL; /* keep existing value, might be "xterm2" */
1874 else
1875 p = (char_u *)"xterm";
1876 }
1877# endif
1878 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001881 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1882 * "xterm2" in check_termcode(). */
1883 reset_option_was_set((char_u *)"ttym");
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (p == NULL
1886# ifdef FEAT_GUI
1887 || gui.in_use
1888# endif
1889 )
1890 check_mouse_termcode(); /* set mouse termcode anyway */
1891 }
1892# endif
1893# else
1894 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1895# endif
1896#endif /* FEAT_MOUSE */
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#ifdef USE_TERM_CONSOLE
1899 /* DEFAULT_TERM indicates that it is the machine console. */
1900 if (STRCMP(term, DEFAULT_TERM) != 0)
1901 term_console = FALSE;
1902 else
1903 {
1904 term_console = TRUE;
1905# ifdef AMIGA
1906 win_resize_on(); /* enable window resizing reports */
1907# endif
1908 }
1909#endif
1910
1911#if defined(UNIX) || defined(VMS)
1912 /*
1913 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1914 */
1915 if (vim_is_fastterm(term))
1916 p_tf = TRUE;
1917#endif
1918#ifdef USE_TERM_CONSOLE
1919 /*
1920 * 'ttyfast' is default on consoles
1921 */
1922 if (term_console)
1923 p_tf = TRUE;
1924#endif
1925
1926 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1927
1928 full_screen = TRUE; /* we can use termcap codes from now on */
1929 set_term_defaults(); /* use current values as defaults */
1930#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001931 LOG_TR("setting crv_status to STATUS_GET");
1932 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933#endif
1934
1935 /*
1936 * Initialize the terminal with the appropriate termcap codes.
1937 * Set the mouse and window title if possible.
1938 * Don't do this when starting, need to parse the .vimrc first, because it
1939 * may redefine t_TI etc.
1940 */
1941 if (starting != NO_SCREEN)
1942 {
1943 starttermcap(); /* may change terminal mode */
1944#ifdef FEAT_MOUSE
1945 setmouse(); /* may start using the mouse */
1946#endif
1947#ifdef FEAT_TITLE
1948 maketitle(); /* may display window title */
1949#endif
1950 }
1951
1952 /* display initial screen after ttest() checking. jw. */
1953 if (width <= 0 || height <= 0)
1954 {
1955 /* termcap failed to report size */
1956 /* set defaults, in case ui_get_shellsize() also fails */
1957 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001958#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 height = 25; /* console is often 25 lines */
1960#else
1961 height = 24; /* most terminals are 24 lines */
1962#endif
1963 }
1964 set_shellsize(width, height, FALSE); /* may change Rows */
1965 if (starting != NO_SCREEN)
1966 {
1967 if (scroll_region)
1968 scroll_region_reset(); /* In case Rows changed */
1969 check_map_keycodes(); /* check mappings for terminal codes used */
1970
1971#ifdef FEAT_AUTOCMD
1972 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001973 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 /*
1976 * Execute the TermChanged autocommands for each buffer that is
1977 * loaded.
1978 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001979 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001980 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
1982 if (curbuf->b_ml.ml_mfp != NULL)
1983 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1984 curbuf);
1985 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001986 if (bufref_valid(&old_curbuf))
1987 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989#endif
1990 }
1991
1992#ifdef FEAT_TERMRESPONSE
1993 may_req_termresponse();
1994#endif
1995
1996 return OK;
1997}
1998
1999#if defined(FEAT_MOUSE) || defined(PROTO)
2000
2001# ifdef FEAT_MOUSE_TTY
2002# define HMT_NORMAL 1
2003# define HMT_NETTERM 2
2004# define HMT_DEC 4
2005# define HMT_JSBTERM 8
2006# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002007# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002008# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002009# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010static int has_mouse_termcode = 0;
2011# endif
2012
Bram Moolenaar864207d2008-06-24 22:14:38 +00002013# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002015set_mouse_termcode(
2016 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2017 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 char_u name[2];
2020
2021 name[0] = n;
2022 name[1] = KE_FILLER;
2023 add_termcode(name, s, FALSE);
2024# ifdef FEAT_MOUSE_TTY
2025# ifdef FEAT_MOUSE_JSB
2026 if (n == KS_JSBTERM_MOUSE)
2027 has_mouse_termcode |= HMT_JSBTERM;
2028 else
2029# endif
2030# ifdef FEAT_MOUSE_NET
2031 if (n == KS_NETTERM_MOUSE)
2032 has_mouse_termcode |= HMT_NETTERM;
2033 else
2034# endif
2035# ifdef FEAT_MOUSE_DEC
2036 if (n == KS_DEC_MOUSE)
2037 has_mouse_termcode |= HMT_DEC;
2038 else
2039# endif
2040# ifdef FEAT_MOUSE_PTERM
2041 if (n == KS_PTERM_MOUSE)
2042 has_mouse_termcode |= HMT_PTERM;
2043 else
2044# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002045# ifdef FEAT_MOUSE_URXVT
2046 if (n == KS_URXVT_MOUSE)
2047 has_mouse_termcode |= HMT_URXVT;
2048 else
2049# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002050# ifdef FEAT_MOUSE_SGR
2051 if (n == KS_SGR_MOUSE)
2052 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002053 else if (n == KS_SGR_MOUSE_RELEASE)
2054 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002055 else
2056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 has_mouse_termcode |= HMT_NORMAL;
2058# endif
2059}
2060# endif
2061
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002062# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002063 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002065del_mouse_termcode(
2066 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 char_u name[2];
2069
2070 name[0] = n;
2071 name[1] = KE_FILLER;
2072 del_termcode(name);
2073# ifdef FEAT_MOUSE_TTY
2074# ifdef FEAT_MOUSE_JSB
2075 if (n == KS_JSBTERM_MOUSE)
2076 has_mouse_termcode &= ~HMT_JSBTERM;
2077 else
2078# endif
2079# ifdef FEAT_MOUSE_NET
2080 if (n == KS_NETTERM_MOUSE)
2081 has_mouse_termcode &= ~HMT_NETTERM;
2082 else
2083# endif
2084# ifdef FEAT_MOUSE_DEC
2085 if (n == KS_DEC_MOUSE)
2086 has_mouse_termcode &= ~HMT_DEC;
2087 else
2088# endif
2089# ifdef FEAT_MOUSE_PTERM
2090 if (n == KS_PTERM_MOUSE)
2091 has_mouse_termcode &= ~HMT_PTERM;
2092 else
2093# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002094# ifdef FEAT_MOUSE_URXVT
2095 if (n == KS_URXVT_MOUSE)
2096 has_mouse_termcode &= ~HMT_URXVT;
2097 else
2098# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002099# ifdef FEAT_MOUSE_SGR
2100 if (n == KS_SGR_MOUSE)
2101 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002102 else if (n == KS_SGR_MOUSE_RELEASE)
2103 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002104 else
2105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 has_mouse_termcode &= ~HMT_NORMAL;
2107# endif
2108}
2109# endif
2110#endif
2111
2112#ifdef HAVE_TGETENT
2113/*
2114 * Call tgetent()
2115 * Return error message if it fails, NULL if it's OK.
2116 */
2117 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002118tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 int i;
2121
2122 i = TGETENT(tbuf, term);
2123 if (i < 0 /* -1 is always an error */
2124# ifdef TGETENT_ZERO_ERR
2125 || i == 0 /* sometimes zero is also an error */
2126# endif
2127 )
2128 {
2129 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2130 * tgetent() with the always existing "dumb" entry to avoid a crash or
2131 * hang. */
2132 (void)TGETENT(tbuf, "dumb");
2133
2134 if (i < 0)
2135# ifdef TGETENT_ZERO_ERR
2136 return (char_u *)_("E557: Cannot open termcap file");
2137 if (i == 0)
2138# endif
2139#ifdef TERMINFO
2140 return (char_u *)_("E558: Terminal entry not found in terminfo");
2141#else
2142 return (char_u *)_("E559: Terminal entry not found in termcap");
2143#endif
2144 }
2145 return NULL;
2146}
2147
2148/*
2149 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2150 * Fix that here.
2151 */
2152 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002153vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 char *p;
2156
2157 p = tgetstr(s, (char **)pp);
2158 if (p == (char *)-1)
2159 p = NULL;
2160 return (char_u *)p;
2161}
2162#endif /* HAVE_TGETENT */
2163
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002164#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165/*
2166 * Get Columns and Rows from the termcap. Used after a window signal if the
2167 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2168 * and "li" entries never change. But on some systems this works.
2169 * Errors while getting the entries are ignored.
2170 */
2171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002172getlinecol(
2173 long *cp, /* pointer to columns */
2174 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 char_u tbuf[TBUFSZ];
2177
2178 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2179 {
2180 if (*cp == 0)
2181 *cp = tgetnum("co");
2182 if (*rp == 0)
2183 *rp = tgetnum("li");
2184 }
2185}
2186#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2187
2188/*
2189 * Get a string entry from the termcap and add it to the list of termcodes.
2190 * Used for <t_xx> special keys.
2191 * Give an error message for failure when not sourcing.
2192 * If force given, replace an existing entry.
2193 * Return FAIL if the entry was not found, OK if the entry was added.
2194 */
2195 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002196add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 char_u *term;
2199 int key;
2200 struct builtin_term *termp;
2201#ifdef HAVE_TGETENT
2202 char_u *string;
2203 int i;
2204 int builtin_first;
2205 char_u tbuf[TBUFSZ];
2206 char_u tstrbuf[TBUFSZ];
2207 char_u *tp = tstrbuf;
2208 char_u *error_msg = NULL;
2209#endif
2210
2211/*
2212 * If the GUI is running or will start in a moment, we only support the keys
2213 * that the GUI can produce.
2214 */
2215#ifdef FEAT_GUI
2216 if (gui.in_use || gui.starting)
2217 return gui_mch_haskey(name);
2218#endif
2219
2220 if (!force && find_termcode(name) != NULL) /* it's already there */
2221 return OK;
2222
2223 term = T_NAME;
2224 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2225 return FAIL;
2226
2227 if (term_is_builtin(term)) /* name starts with "builtin_" */
2228 {
2229 term += 8;
2230#ifdef HAVE_TGETENT
2231 builtin_first = TRUE;
2232#endif
2233 }
2234#ifdef HAVE_TGETENT
2235 else
2236 builtin_first = p_tbi;
2237#endif
2238
2239#ifdef HAVE_TGETENT
2240/*
2241 * We can get the entry from the builtin termcap and from the external one.
2242 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2243 * builtin termcap first.
2244 * If 'ttybuiltin' is off, try external termcap first.
2245 */
2246 for (i = 0; i < 2; ++i)
2247 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002248 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250 /*
2251 * Search in builtin termcap
2252 */
2253 {
2254 termp = find_builtin_term(term);
2255 if (termp->bt_string != NULL) /* found it */
2256 {
2257 key = TERMCAP2KEY(name[0], name[1]);
2258 while (termp->bt_entry != (int)KS_NAME)
2259 {
2260 if ((int)termp->bt_entry == key)
2261 {
2262 add_termcode(name, (char_u *)termp->bt_string,
2263 term_is_8bit(term));
2264 return OK;
2265 }
2266 ++termp;
2267 }
2268 }
2269 }
2270#ifdef HAVE_TGETENT
2271 else
2272 /*
2273 * Search in external termcap
2274 */
2275 {
2276 error_msg = tgetent_error(tbuf, term);
2277 if (error_msg == NULL)
2278 {
2279 string = TGETSTR((char *)name, &tp);
2280 if (string != NULL && *string != NUL)
2281 {
2282 add_termcode(name, string, FALSE);
2283 return OK;
2284 }
2285 }
2286 }
2287 }
2288#endif
2289
2290 if (sourcing_name == NULL)
2291 {
2292#ifdef HAVE_TGETENT
2293 if (error_msg != NULL)
2294 EMSG(error_msg);
2295 else
2296#endif
2297 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2298 }
2299 return FAIL;
2300}
2301
2302 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002303term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2306}
2307
2308/*
2309 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2310 * Assume that the terminal is using 8-bit controls when the name contains
2311 * "8bit", like in "xterm-8bit".
2312 */
2313 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002314term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2317}
2318
2319/*
2320 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002321 * <Esc>[ -> CSI <M_C_[>
2322 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * <Esc>O -> <M-C-O>
2324 */
2325 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002326term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 if (*p == ESC)
2329 {
2330 if (p[1] == '[')
2331 return CSI;
2332 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002333 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (p[1] == 'O')
2335 return 0x8f;
2336 }
2337 return 0;
2338}
2339
2340#ifdef FEAT_GUI
2341 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2345}
2346#endif
2347
2348#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2349
2350 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002351tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
2353 static char_u buf[16];
2354 char_u *p;
2355
2356 p = buf + 15;
2357 *p = '\0';
2358 do
2359 {
2360 --p;
2361 *p = (char_u) (i % 10 + '0');
2362 i /= 10;
2363 }
2364 while (i > 0 && p > buf);
2365 return p;
2366}
2367#endif
2368
2369#ifndef HAVE_TGETENT
2370
2371/*
2372 * minimal tgoto() implementation.
2373 * no padding and we only parse for %i %d and %+char
2374 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002375static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002377 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002378tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 static char buf[30];
2381 char *p, *s, *e;
2382
2383 if (!cm)
2384 return "OOPS";
2385 e = buf + 29;
2386 for (s = buf; s < e && *cm; cm++)
2387 {
2388 if (*cm != '%')
2389 {
2390 *s++ = *cm;
2391 continue;
2392 }
2393 switch (*++cm)
2394 {
2395 case 'd':
2396 p = (char *)tltoa((unsigned long)y);
2397 y = x;
2398 while (*p)
2399 *s++ = *p++;
2400 break;
2401 case 'i':
2402 x++;
2403 y++;
2404 break;
2405 case '+':
2406 *s++ = (char)(*++cm + y);
2407 y = x;
2408 break;
2409 case '%':
2410 *s++ = *cm;
2411 break;
2412 default:
2413 return "OOPS";
2414 }
2415 }
2416 *s = '\0';
2417 return buf;
2418}
2419
2420#endif /* HAVE_TGETENT */
2421
2422/*
2423 * Set the terminal name and initialize the terminal options.
2424 * If "name" is NULL or empty, get the terminal name from the environment.
2425 * If that fails, use the default terminal name.
2426 */
2427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002428termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 char_u *term;
2431
2432 if (name != NULL && *name == NUL)
2433 name = NULL; /* empty name is equal to no name */
2434 term = name;
2435
2436#ifdef __BEOS__
2437 /*
2438 * TERM environment variable is normally set to 'ansi' on the Bebox;
2439 * Since the BeBox doesn't quite support full ANSI yet, we use our
2440 * own custom 'ansi-beos' termcap instead, unless the -T option has
2441 * been given on the command line.
2442 */
2443 if (term == NULL
2444 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2445 term = DEFAULT_TERM;
2446#endif
2447#ifndef MSWIN
2448 if (term == NULL)
2449 term = mch_getenv((char_u *)"TERM");
2450#endif
2451 if (term == NULL || *term == NUL)
2452 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002453 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 /* Set the default terminal name. */
2456 set_string_default("term", term);
2457 set_string_default("ttytype", term);
2458
2459 /*
2460 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2461 */
2462 set_termname(T_NAME != NULL ? T_NAME : term);
2463}
2464
2465/*
2466 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2467 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002468#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2470static char_u out_buf[OUT_SIZE + 1];
2471static int out_pos = 0; /* number of chars in out_buf */
2472
2473/*
2474 * out_flush(): flush the output buffer
2475 */
2476 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002477out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 int len;
2480
2481 if (out_pos != 0)
2482 {
2483 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2484 len = out_pos;
2485 out_pos = 0;
2486 ui_write(out_buf, len);
2487 }
2488}
2489
2490#if defined(FEAT_MBYTE) || defined(PROTO)
2491/*
2492 * Sometimes a byte out of a multi-byte character is written with out_char().
2493 * To avoid flushing half of the character, call this function first.
2494 */
2495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002496out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2499 out_flush();
2500}
2501#endif
2502
2503#ifdef FEAT_GUI
2504/*
2505 * out_trash(): Throw away the contents of the output buffer
2506 */
2507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002508out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 out_pos = 0;
2511}
2512#endif
2513
2514/*
2515 * out_char(c): put a byte into the output buffer.
2516 * Flush it if it becomes full.
2517 * This should not be used for outputting text on the screen (use functions
2518 * like msg_puts() and screen_putchar() for that).
2519 */
2520 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002521out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2524 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2525 out_char('\r');
2526#endif
2527
2528 out_buf[out_pos++] = c;
2529
2530 /* For testing we flush each time. */
2531 if (out_pos >= OUT_SIZE || p_wd)
2532 out_flush();
2533}
2534
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002535static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537/*
2538 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2539 */
2540 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002541out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2544 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2545 out_char_nf('\r');
2546#endif
2547
2548 out_buf[out_pos++] = c;
2549
2550 if (out_pos >= OUT_SIZE)
2551 out_flush();
2552}
2553
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002554#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2555 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556/*
2557 * A never-padding out_str.
2558 * use this whenever you don't want to run the string through tputs.
2559 * tputs above is harmless, but tputs from the termcap library
2560 * is likely to strip off leading digits, that it mistakes for padding
2561 * information, and "%i", "%d", etc.
2562 * This should only be used for writing terminal codes, not for outputting
2563 * normal text (use functions like msg_puts() and screen_putchar() for that).
2564 */
2565 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002566out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2569 out_flush();
2570 while (*s)
2571 out_char_nf(*s++);
2572
2573 /* For testing we write one string at a time. */
2574 if (p_wd)
2575 out_flush();
2576}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002580 * A conditional-flushing out_str, mainly for visualbell.
2581 * Handles a delay internally, because termlib may not respect the delay or do
2582 * it at the wrong time.
2583 * Note: Only for terminal strings.
2584 */
2585 void
2586out_str_cf(char_u *s)
2587{
2588 if (s != NULL && *s)
2589 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002590#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002591 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002592#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002593
2594#ifdef FEAT_GUI
2595 /* Don't use tputs() when GUI is used, ncurses crashes. */
2596 if (gui.in_use)
2597 {
2598 out_str_nf(s);
2599 return;
2600 }
2601#endif
2602 if (out_pos > OUT_SIZE - 20)
2603 out_flush();
2604#ifdef HAVE_TGETENT
2605 for (p = s; *s; ++s)
2606 {
2607 /* flush just before delay command */
2608 if (*s == '$' && *(s + 1) == '<')
2609 {
2610 char_u save_c = *s;
2611 int duration = atoi((char *)s + 2);
2612
2613 *s = NUL;
2614 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2615 *s = save_c;
2616 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002617# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002618 /* Only sleep here if we can limit this happening in
2619 * vim_beep(). */
2620 p = vim_strchr(s, '>');
2621 if (p == NULL || duration <= 0)
2622 {
2623 /* can't parse the time, don't sleep here */
2624 p = s;
2625 }
2626 else
2627 {
2628 ++p;
2629 do_sleep(duration);
2630 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002631# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002632 /* Rely on the terminal library to sleep. */
2633 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002634# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002635 break;
2636 }
2637 }
2638 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2639#else
2640 while (*s)
2641 out_char_nf(*s++);
2642#endif
2643
2644 /* For testing we write one string at a time. */
2645 if (p_wd)
2646 out_flush();
2647 }
2648}
2649
2650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * out_str(s): Put a character string a byte at a time into the output buffer.
2652 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2653 * This should only be used for writing terminal codes, not for outputting
2654 * normal text (use functions like msg_puts() and screen_putchar() for that).
2655 */
2656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002657out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 if (s != NULL && *s)
2660 {
2661#ifdef FEAT_GUI
2662 /* Don't use tputs() when GUI is used, ncurses crashes. */
2663 if (gui.in_use)
2664 {
2665 out_str_nf(s);
2666 return;
2667 }
2668#endif
2669 /* avoid terminal strings being split up */
2670 if (out_pos > OUT_SIZE - 20)
2671 out_flush();
2672#ifdef HAVE_TGETENT
2673 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2674#else
2675 while (*s)
2676 out_char_nf(*s++);
2677#endif
2678
2679 /* For testing we write one string at a time. */
2680 if (p_wd)
2681 out_flush();
2682 }
2683}
2684
2685/*
2686 * cursor positioning using termcap parser. (jw)
2687 */
2688 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002689term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 OUT_STR(tgoto((char *)T_CM, col, row));
2692}
2693
2694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002695term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 OUT_STR(tgoto((char *)T_CRI, 0, i));
2698}
2699
2700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2704}
2705
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2710}
2711
2712#if defined(HAVE_TGETENT) || defined(PROTO)
2713 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 /* Can't handle a negative value here */
2717 if (x < 0)
2718 x = 0;
2719 if (y < 0)
2720 y = 0;
2721 OUT_STR(tgoto((char *)T_CWP, y, x));
2722}
2723
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002724# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2725/*
2726 * Return TRUE if we can request the terminal for a response.
2727 */
2728 static int
2729can_get_termresponse()
2730{
2731 return cur_tmode == TMODE_RAW
2732 && termcap_active
2733# ifdef UNIX
2734 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2735# endif
2736 && p_ek;
2737}
2738
2739static int winpos_x;
2740static int winpos_y;
2741static int waiting_for_winpos = FALSE;
2742
2743/*
2744 * Try getting the Vim window position from the terminal.
2745 * Returns OK or FAIL.
2746 */
2747 int
2748term_get_winpos(int *x, int *y)
2749{
2750 int count = 0;
2751
2752 if (*T_CGP == NUL || !can_get_termresponse())
2753 return FAIL;
2754 winpos_x = -1;
2755 winpos_y = -1;
2756 waiting_for_winpos = TRUE;
2757 OUT_STR(T_CGP);
2758 out_flush();
2759
2760 /* Try reading the result for 100 msec. */
2761 while (count++ < 10)
2762 {
2763 (void)vpeekc_nomap();
2764 if (winpos_x >= 0 && winpos_y >= 0)
2765 {
2766 *x = winpos_x;
2767 *y = winpos_y;
2768 waiting_for_winpos = FALSE;
2769 return OK;
2770 }
2771 ui_delay(10, FALSE);
2772 }
2773 waiting_for_winpos = FALSE;
2774 return FALSE;
2775}
2776# endif
2777
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002779term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002781 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782}
2783#endif
2784
2785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002786term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2789 if (*T_CAF)
2790 term_color(T_CAF, n);
2791 else if (*T_CSF)
2792 term_color(T_CSF, n);
2793}
2794
2795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2799 if (*T_CAB)
2800 term_color(T_CAB, n);
2801 else if (*T_CSB)
2802 term_color(T_CSB, n);
2803}
2804
2805 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002806term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 char buf[20];
2809 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2810
2811 /* Special handling of 16 colors, because termcap can't handle it */
2812 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2813 /* Also accept CSI instead of <Esc>[ */
2814 if (n >= 8 && t_colors >= 16
2815 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2816 && s[i] != NUL
2817 && (STRCMP(s + i + 1, "%p1%dm") == 0
2818 || STRCMP(s + i + 1, "%dm") == 0)
2819 && (s[i] == '3' || s[i] == '4'))
2820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002822 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002824 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002826 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2828 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2829 : (n >= 16 ? "48;5;" : "10"));
2830 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2831 }
2832 else
2833 OUT_STR(tgoto((char *)s, 0, n));
2834}
2835
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002836#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002837
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002838#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2839#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2840#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002841
2842 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002843term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002844{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002845#define MAX_COLOR_STR_LEN 100
2846 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002847
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002848 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002849 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002850 OUT_STR(buf);
2851}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002852
2853 void
2854term_fg_rgb_color(guicolor_T rgb)
2855{
2856 term_rgb_color(T_8F, rgb);
2857}
2858
2859 void
2860term_bg_rgb_color(guicolor_T rgb)
2861{
2862 term_rgb_color(T_8B, rgb);
2863}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002864#endif
2865
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002866#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2867 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868/*
2869 * Generic function to set window title, using t_ts and t_fs.
2870 */
2871 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002872term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 /* t_ts takes one argument: column in status line */
2875 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2876 out_str_nf(title);
2877 out_str(T_FS); /* set title end */
2878 out_flush();
2879}
2880#endif
2881
2882/*
2883 * Make sure we have a valid set or terminal options.
2884 * Replace all entries that are NULL by empty_option
2885 */
2886 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002887ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002889 char_u *env_colors;
2890
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 check_options(); /* make sure no options are NULL */
2892
2893 /*
2894 * MUST have "cm": cursor motion.
2895 */
2896 if (*T_CM == NUL)
2897 EMSG(_("E437: terminal capability \"cm\" required"));
2898
2899 /*
2900 * if "cs" defined, use a scroll region, it's faster.
2901 */
2902 if (*T_CS != NUL)
2903 scroll_region = TRUE;
2904 else
2905 scroll_region = FALSE;
2906
2907 if (pairs)
2908 {
2909 /*
2910 * optional pairs
2911 */
2912 /* TP goes to normal mode for TI (invert) and TB (bold) */
2913 if (*T_ME == NUL)
2914 T_ME = T_MR = T_MD = T_MB = empty_option;
2915 if (*T_SO == NUL || *T_SE == NUL)
2916 T_SO = T_SE = empty_option;
2917 if (*T_US == NUL || *T_UE == NUL)
2918 T_US = T_UE = empty_option;
2919 if (*T_CZH == NUL || *T_CZR == NUL)
2920 T_CZH = T_CZR = empty_option;
2921
2922 /* T_VE is needed even though T_VI is not defined */
2923 if (*T_VE == NUL)
2924 T_VI = empty_option;
2925
2926 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2927 if (*T_ME == NUL)
2928 {
2929 T_ME = T_SE;
2930 T_MR = T_SO;
2931 T_MD = T_SO;
2932 }
2933
2934 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2935 if (*T_SO == NUL)
2936 {
2937 T_SE = T_ME;
2938 if (*T_MR == NUL)
2939 T_SO = T_MD;
2940 else
2941 T_SO = T_MR;
2942 }
2943
2944 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2945 if (*T_CZH == NUL)
2946 {
2947 T_CZR = T_ME;
2948 if (*T_MR == NUL)
2949 T_CZH = T_MD;
2950 else
2951 T_CZH = T_MR;
2952 }
2953
2954 /* "Sb" and "Sf" come in pairs */
2955 if (*T_CSB == NUL || *T_CSF == NUL)
2956 {
2957 T_CSB = empty_option;
2958 T_CSF = empty_option;
2959 }
2960
2961 /* "AB" and "AF" come in pairs */
2962 if (*T_CAB == NUL || *T_CAF == NUL)
2963 {
2964 T_CAB = empty_option;
2965 T_CAF = empty_option;
2966 }
2967
2968 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2969 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002970 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /* Set 'weirdinvert' according to value of 't_xs' */
2973 p_wiv = (*T_XS != NUL);
2974 }
2975 need_gather = TRUE;
2976
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002977 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002979 env_colors = mch_getenv((char_u *)"COLORS");
2980 if (env_colors != NULL && isdigit(*env_colors))
2981 {
2982 int colors = atoi((char *)env_colors);
2983
2984 if (colors != t_colors)
2985 set_color_count(colors);
2986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2990 || defined(PROTO)
2991/*
2992 * Represent the given long_u as individual bytes, with the most significant
2993 * byte first, and store them in dst.
2994 */
2995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 int i;
2999 int shift;
3000
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003001 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 {
3003 shift = 8 * (sizeof(long_u) - i);
3004 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3005 }
3006}
3007
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003008static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010/*
3011 * Interpret the next string of bytes in buf as a long integer, with the most
3012 * significant byte first. Note that it is assumed that buf has been through
3013 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3014 * Puts result in val, and returns the number of bytes read from buf
3015 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3016 * were present.
3017 */
3018 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003019get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 int len;
3022 char_u bytes[sizeof(long_u)];
3023 int i;
3024 int shift;
3025
3026 *val = 0;
3027 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3028 if (len != -1)
3029 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003030 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 {
3032 shift = 8 * (sizeof(long_u) - 1 - i);
3033 *val += (long_u)bytes[i] << shift;
3034 }
3035 }
3036 return len;
3037}
3038#endif
3039
3040#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003041 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3042 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3045 * that buf has been through inchar(). Returns the actual number of bytes used
3046 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3047 * available.
3048 */
3049 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003050get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 int len = 0;
3053 int i;
3054 char_u c;
3055
3056 for (i = 0; i < num_bytes; i++)
3057 {
3058 if ((c = buf[len++]) == NUL)
3059 return -1;
3060 if (c == K_SPECIAL)
3061 {
3062 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3063 return -1;
3064 if (buf[len++] == (int)KS_ZERO)
3065 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003066 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3067 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3068 if (buf[len++] == (int)KE_CSI)
3069 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003071 else if (c == CSI && buf[len] == KS_EXTRA
3072 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3074 * the start of a special key, see add_to_input_buf_csi(). */
3075 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 bytes[i] = c;
3077 }
3078 return len;
3079}
3080#endif
3081
3082/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003083 * Check if the new shell size is valid, correct it if it's too small or way
3084 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003087check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (Rows < min_rows()) /* need room for one window and command line */
3090 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003091 limit_screen_size();
3092}
3093
3094/*
3095 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3096 */
3097 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003098limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003099{
3100 if (Columns < MIN_COLUMNS)
3101 Columns = MIN_COLUMNS;
3102 else if (Columns > 10000)
3103 Columns = 10000;
3104 if (Rows > 1000)
3105 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
Bram Moolenaar86b68352004-12-27 21:59:20 +00003108/*
3109 * Invoked just before the screen structures are going to be (re)allocated.
3110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 static int old_Rows = 0;
3115 static int old_Columns = 0;
3116
3117 if (old_Rows != Rows || old_Columns != Columns)
3118 ui_new_shellsize();
3119 if (old_Rows != Rows)
3120 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003121 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003122 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003123 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 old_Rows = Rows;
3125 shell_new_rows(); /* update window sizes */
3126 }
3127 if (old_Columns != Columns)
3128 {
3129 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003130#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 shell_new_columns(); /* update window sizes */
3132#endif
3133 }
3134}
3135
3136/*
3137 * Call this function when the Vim shell has been resized in any way.
3138 * Will obtain the current size and redraw (also when size didn't change).
3139 */
3140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003141shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 set_shellsize(0, 0, FALSE);
3144}
3145
3146/*
3147 * Check if the shell size changed. Handle a resize.
3148 * When the size didn't change, nothing happens.
3149 */
3150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003151shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int old_Rows = Rows;
3154 int old_Columns = Columns;
3155
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003156 if (!exiting
3157#ifdef FEAT_GUI
3158 /* Do not get the size when executing a shell command during
3159 * startup. */
3160 && !gui.starting
3161#endif
3162 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003163 {
3164 (void)ui_get_shellsize();
3165 check_shellsize();
3166 if (old_Rows != Rows || old_Columns != Columns)
3167 shell_resized();
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169}
3170
3171/*
3172 * Set size of the Vim shell.
3173 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3174 * window size (this is used for the :win command).
3175 * If 'mustset' is FALSE, we may try to get the real window size and if
3176 * it fails use 'width' and 'height'.
3177 */
3178 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003179set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 static int busy = FALSE;
3182
3183 /*
3184 * Avoid recursiveness, can happen when setting the window size causes
3185 * another window-changed signal.
3186 */
3187 if (busy)
3188 return;
3189
3190 if (width < 0 || height < 0) /* just checking... */
3191 return;
3192
Bram Moolenaara971b822011-09-14 14:43:25 +02003193 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003195 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 State = SETWSIZE;
3197 return;
3198 }
3199
Bram Moolenaara971b822011-09-14 14:43:25 +02003200 /* curwin->w_buffer can be NULL when we are closing a window and the
3201 * buffer has already been closed and removing a scrollbar causes a resize
3202 * event. Don't resize then, it will happen after entering another buffer.
3203 */
3204 if (curwin->w_buffer == NULL)
3205 return;
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 ++busy;
3208
3209#ifdef AMIGA
3210 out_flush(); /* must do this before mch_get_shellsize() for
3211 some obscure reason */
3212#endif
3213
3214 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3215 {
3216 Rows = height;
3217 Columns = width;
3218 check_shellsize();
3219 ui_set_shellsize(mustset);
3220 }
3221 else
3222 check_shellsize();
3223
3224 /* The window layout used to be adjusted here, but it now happens in
3225 * screenalloc() (also invoked from screenclear()). That is because the
3226 * "busy" check above may skip this, but not screenalloc(). */
3227
3228 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3229 screenclear();
3230 else
3231 screen_start(); /* don't know where cursor is now */
3232
3233 if (starting != NO_SCREEN)
3234 {
3235#ifdef FEAT_TITLE
3236 maketitle();
3237#endif
3238 changed_line_abv_curs();
3239 invalidate_botline();
3240
3241 /*
3242 * We only redraw when it's needed:
3243 * - While at the more prompt or executing an external command, don't
3244 * redraw, but position the cursor.
3245 * - While editing the command line, only redraw that.
3246 * - in Ex mode, don't redraw anything.
3247 * - Otherwise, redraw right now, and position the cursor.
3248 * Always need to call update_screen() or screenalloc(), to make
3249 * sure Rows/Columns and the size of ScreenLines[] is correct!
3250 */
3251 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3252 || exmode_active)
3253 {
3254 screenalloc(FALSE);
3255 repeat_message();
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 else
3258 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003259#ifdef FEAT_SCROLLBIND
3260 if (curwin->w_p_scb)
3261 do_check_scrollbind(TRUE);
3262#endif
3263 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003264 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003265 update_screen(NOT_VALID);
3266 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003267 }
3268 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003269 {
3270 update_topline();
3271#if defined(FEAT_INS_EXPAND)
3272 if (pum_visible())
3273 {
3274 redraw_later(NOT_VALID);
3275 ins_compl_show_pum(); /* This includes the redraw. */
3276 }
3277 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003278#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003279 update_screen(NOT_VALID);
3280 if (redrawing())
3281 setcursor();
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 cursor_on(); /* redrawing may have switched it off */
3285 }
3286 out_flush();
3287 --busy;
3288}
3289
3290/*
3291 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3292 * commands and Ex mode).
3293 */
3294 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003295settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297#ifdef FEAT_GUI
3298 /* don't set the term where gvim was started to any mode */
3299 if (gui.in_use)
3300 return;
3301#endif
3302
3303 if (full_screen)
3304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 /*
3306 * When returning after calling a shell we want to really set the
3307 * terminal to raw mode, even though we think it already is, because
3308 * the shell program may have reset the terminal mode.
3309 * When we think the terminal is normal, don't try to set it to
3310 * normal again, because that causes problems (logout!) on some
3311 * machines.
3312 */
3313 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3314 {
3315#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003316# ifdef FEAT_GUI
3317 if (!gui.in_use && !gui.starting)
3318# endif
3319 {
3320 /* May need to check for T_CRV response and termcodes, it
3321 * doesn't work in Cooked mode, an external program may get
3322 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003323 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3324 || u7_status == STATUS_SENT
3325 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003326 || rbm_status == STATUS_SENT
3327 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003328 (void)vpeekc_nomap();
3329 check_for_codes_from_term();
3330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif
3332#ifdef FEAT_MOUSE_TTY
3333 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003334 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003336 if (tmode != TMODE_RAW)
3337 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003339 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 cur_tmode = tmode;
3341#ifdef FEAT_MOUSE
3342 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003343 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003345 if (tmode == TMODE_RAW)
3346 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 out_flush();
3348 }
3349#ifdef FEAT_TERMRESPONSE
3350 may_req_termresponse();
3351#endif
3352 }
3353}
3354
3355 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 if (full_screen && !termcap_active)
3359 {
3360 out_str(T_TI); /* start termcap mode */
3361 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003362 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 out_flush();
3364 termcap_active = TRUE;
3365 screen_start(); /* don't know where cursor is now */
3366#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003367# ifdef FEAT_GUI
3368 if (!gui.in_use && !gui.starting)
3369# endif
3370 {
3371 may_req_termresponse();
3372 /* Immediately check for a response. If t_Co changes, we don't
3373 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003374 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003375 check_for_codes_from_term();
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#endif
3378 }
3379}
3380
3381 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003382stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 screen_stop_highlight();
3385 reset_cterm_colors();
3386 if (termcap_active)
3387 {
3388#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003389# ifdef FEAT_GUI
3390 if (!gui.in_use && !gui.starting)
3391# endif
3392 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003393 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003394 if (crv_status == STATUS_SENT
3395 || u7_status == STATUS_SENT
3396 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003397 || rbm_status == STATUS_SENT
3398 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003399 {
3400# ifdef UNIX
3401 /* Give the terminal a chance to respond. */
3402 mch_delay(100L, FALSE);
3403# endif
3404# ifdef TCIFLUSH
3405 /* Discard data received but not read. */
3406 if (exiting)
3407 tcflush(fileno(stdin), TCIFLUSH);
3408# endif
3409 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003410 /* Check for termcodes first, otherwise an external program may
3411 * get them. */
3412 check_for_codes_from_term();
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003415 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 out_str(T_KE); /* stop "keypad transmit" mode */
3417 out_flush();
3418 termcap_active = FALSE;
3419 cursor_on(); /* just in case it is still off */
3420 out_str(T_TE); /* stop termcap mode */
3421 screen_start(); /* don't know where cursor is now */
3422 out_flush();
3423 }
3424}
3425
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003426#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427/*
3428 * Request version string (for xterm) when needed.
3429 * Only do this after switching to raw mode, otherwise the result will be
3430 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003431 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003432 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 * Only do this after termcap mode has been started, otherwise the codes for
3434 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003435 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3436 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003437 * On Unix only do it when both output and input are a tty (avoid writing
3438 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * The result is caught in check_termcode().
3440 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003444 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003445 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003446 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 && *T_CRV != NUL)
3448 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003449 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003451 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 /* check for the characters now, otherwise they might be eaten by
3453 * get_keystroke() */
3454 out_flush();
3455 (void)vpeekc_nomap();
3456 }
3457}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003458
3459# if defined(FEAT_MBYTE) || defined(PROTO)
3460/*
3461 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003462 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003463 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003464 * If the terminal treats \u25bd as single width, the position is (1, 1),
3465 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003466 * This function has the side effect that changes cursor position, so
3467 * it must be called immediately after entering termcap mode.
3468 */
3469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003470may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003471{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003472 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003473 && can_get_termresponse()
3474 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003475 && *T_U7 != NUL
3476 && !option_was_set((char_u *)"ambiwidth"))
3477 {
3478 char_u buf[16];
3479
Bram Moolenaar2951b772013-07-03 12:45:31 +02003480 LOG_TR("Sending U7 request");
3481 /* Do this in the second row. In the first row the returned sequence
3482 * may be CSI 1;2R, which is the same as <S-F3>. */
3483 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003484 buf[mb_char2bytes(0x25bd, buf)] = 0;
3485 out_str(buf);
3486 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003487 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003488 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003489
3490 /* This overwrites a few characters on the screen, a redraw is needed
3491 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003492 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003493 out_str((char_u *)" ");
3494 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003495
Bram Moolenaar9584b312013-03-13 19:29:28 +01003496 /* check for the characters now, otherwise they might be eaten by
3497 * get_keystroke() */
3498 out_flush();
3499 (void)vpeekc_nomap();
3500 }
3501}
3502# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003503
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003504/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003505 * Similar to requesting the version string: Request the terminal background
3506 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003507 */
3508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003509may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003510{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003511 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003512 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003513 /* Only request background if t_RB is set and 'background' wasn't
3514 * changed. */
3515 if (rbg_status == STATUS_GET
3516 && *T_RBG != NUL
3517 && !option_was_set((char_u *)"bg"))
3518 {
3519 LOG_TR("Sending BG request");
3520 out_str(T_RBG);
3521 rbg_status = STATUS_SENT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003522
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003523 /* check for the characters now, otherwise they might be eaten by
3524 * get_keystroke() */
3525 out_flush();
3526 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003527 }
3528 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003529}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003530
Bram Moolenaar2951b772013-07-03 12:45:31 +02003531# ifdef DEBUG_TERMRESPONSE
3532 static void
3533log_tr(char *msg)
3534{
3535 static FILE *fd_tr = NULL;
3536 static proftime_T start;
3537 proftime_T now;
3538
3539 if (fd_tr == NULL)
3540 {
3541 fd_tr = fopen("termresponse.log", "w");
3542 profile_start(&start);
3543 }
3544 now = start;
3545 profile_end(&now);
3546 fprintf(fd_tr, "%s: %s %s\n",
3547 profile_msg(&now),
3548 must_redraw == NOT_VALID ? "NV"
3549 : must_redraw == CLEAR ? "CL" : " ",
3550 msg);
3551}
3552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
3554
3555/*
3556 * Return TRUE when saving and restoring the screen.
3557 */
3558 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 return (full_screen && *T_TI != NUL);
3562}
3563
3564#ifdef FEAT_MOUSE
3565/*
3566 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3567 */
3568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003569setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571# ifdef FEAT_MOUSE_TTY
3572 int checkfor;
3573# endif
3574
3575# ifdef FEAT_MOUSESHAPE
3576 update_mouseshape(-1);
3577# endif
3578
3579# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3580# ifdef FEAT_GUI
3581 /* In the GUI the mouse is always enabled. */
3582 if (gui.in_use)
3583 return;
3584# endif
3585 /* be quick when mouse is off */
3586 if (*p_mouse == NUL || has_mouse_termcode == 0)
3587 return;
3588
3589 /* don't switch mouse on when not in raw mode (Ex mode) */
3590 if (cur_tmode != TMODE_RAW)
3591 {
3592 mch_setmouse(FALSE);
3593 return;
3594 }
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (VIsual_active)
3597 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003598 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 checkfor = MOUSE_RETURN;
3600 else if (State & INSERT)
3601 checkfor = MOUSE_INSERT;
3602 else if (State & CMDLINE)
3603 checkfor = MOUSE_COMMAND;
3604 else if (State == CONFIRM || State == EXTERNCMD)
3605 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3606 else
3607 checkfor = MOUSE_NORMAL; /* assume normal mode */
3608
3609 if (mouse_has(checkfor))
3610 mch_setmouse(TRUE);
3611 else
3612 mch_setmouse(FALSE);
3613# endif
3614}
3615
3616/*
3617 * Return TRUE if
3618 * - "c" is in 'mouse', or
3619 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3620 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3621 * normal editing mode (not at hit-return message).
3622 */
3623 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003624mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 char_u *p;
3627
3628 for (p = p_mouse; *p; ++p)
3629 switch (*p)
3630 {
3631 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3632 return TRUE;
3633 break;
3634 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3635 return TRUE;
3636 break;
3637 default: if (c == *p) return TRUE; break;
3638 }
3639 return FALSE;
3640}
3641
3642/*
3643 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3644 */
3645 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003646mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 return (p_mousem[0] == 'p');
3649}
3650#endif
3651
3652/*
3653 * By outputting the 'cursor very visible' termcap code, for some windowed
3654 * terminals this makes the screen scrolled to the correct position.
3655 * Used when starting Vim or returning from a shell.
3656 */
3657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003658scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003660 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003663 out_str(T_CVS);
3664 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666}
3667
3668static int cursor_is_off = FALSE;
3669
3670/*
3671 * Enable the cursor.
3672 */
3673 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003674cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (cursor_is_off)
3677 {
3678 out_str(T_VE);
3679 cursor_is_off = FALSE;
3680 }
3681}
3682
3683/*
3684 * Disable the cursor.
3685 */
3686 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003687cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003689 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003691 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 cursor_is_off = TRUE;
3693 }
3694}
3695
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003696#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003698 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003699 */
3700 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003701term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003702{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003703 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003704 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003705
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003706 /* Only do something when redrawing the screen and we can restore the
3707 * mode. */
3708 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003709 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003710# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003711 if (forced && initial_cursor_shape > 0)
3712 /* Restore to initial values. */
3713 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003714# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003715 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003716 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003717
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003718 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003719 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003720 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003721 {
3722 if (*T_CSR != NUL)
3723 p = T_CSR; /* Replace mode cursor */
3724 else
3725 p = T_CSI; /* fall back to Insert mode cursor */
3726 if (*p != NUL)
3727 {
3728 out_str(p);
3729 showing_mode = REPLACE;
3730 }
3731 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003732 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003733 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003734 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003735 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003736 {
3737 out_str(T_CSI); /* Insert mode cursor */
3738 showing_mode = INSERT;
3739 }
3740 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003741 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003742 {
3743 out_str(T_CEI); /* non-Insert mode cursor */
3744 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003745 }
3746}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003747
3748# if defined(FEAT_TERMINAL) || defined(PROTO)
3749 void
3750term_cursor_color(char_u *color)
3751{
3752 if (*T_CSC != NUL)
3753 {
3754 out_str(T_CSC); /* set cursor color start */
3755 out_str_nf(color);
3756 out_str(T_CEC); /* set cursor color end */
3757 out_flush();
3758 }
3759}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003760# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003761
Bram Moolenaar4db25542017-08-28 22:43:05 +02003762 int
3763blink_state_is_inverted()
3764{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003765#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003766 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3767 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003768#else
3769 return FALSE;
3770#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003771}
3772
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003773/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003774 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003775 */
3776 void
3777term_cursor_shape(int shape, int blink)
3778{
3779 if (*T_CSH != NUL)
3780 {
3781 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3782 out_flush();
3783 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003784 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003785 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003786 int do_blink = blink;
3787
3788 /* t_SH is empty: try setting just the blink state.
3789 * The blink flags are XORed together, if the initial blinking from
3790 * style and shape differs, we need to invert the flag here. */
3791 if (blink_state_is_inverted())
3792 do_blink = !blink;
3793
3794 if (do_blink && *T_VS != NUL)
3795 {
3796 out_str(T_VS);
3797 out_flush();
3798 }
3799 else if (!do_blink && *T_CVS != NUL)
3800 {
3801 out_str(T_CVS);
3802 out_flush();
3803 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003804 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003805}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003806#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003807
3808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 * Set scrolling region for window 'wp'.
3810 * The region starts 'off' lines from the start of the window.
3811 * Also set the vertical scroll region for a vertically split window. Always
3812 * the full width of the window, excluding the vertical separator.
3813 */
3814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003815scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3818 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003819#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (*T_CSV != NUL && wp->w_width != Columns)
3821 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3822 W_WINCOL(wp)));
3823#endif
3824 screen_start(); /* don't know where cursor is now */
3825}
3826
3827/*
3828 * Reset scrolling region to the whole screen.
3829 */
3830 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003831scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003834#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (*T_CSV != NUL)
3836 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3837#endif
3838 screen_start(); /* don't know where cursor is now */
3839}
3840
3841
3842/*
3843 * List of terminal codes that are currently recognized.
3844 */
3845
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003846static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848 char_u name[2]; /* termcap name of entry */
3849 char_u *code; /* terminal code (in allocated memory) */
3850 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003851 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852} *termcodes = NULL;
3853
3854static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3855static int tc_len = 0; /* current number of entries in termcodes[] */
3856
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003857static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003860clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 while (tc_len > 0)
3863 vim_free(termcodes[--tc_len].code);
3864 vim_free(termcodes);
3865 termcodes = NULL;
3866 tc_max_len = 0;
3867
3868#ifdef HAVE_TGETENT
3869 BC = (char *)empty_option;
3870 UP = (char *)empty_option;
3871 PC = NUL; /* set pad character to NUL */
3872 ospeed = 0;
3873#endif
3874
3875 need_gather = TRUE; /* need to fill termleader[] */
3876}
3877
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003878#define ATC_FROM_TERM 55
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880/*
3881 * Add a new entry to the list of terminal codes.
3882 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003883 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3884 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 */
3886 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003887add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 struct termcode *new_tc;
3890 int i, j;
3891 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003892 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894 if (string == NULL || *string == NUL)
3895 {
3896 del_termcode(name);
3897 return;
3898 }
3899
Bram Moolenaar45500912014-07-09 20:51:07 +02003900#if defined(WIN3264) && !defined(FEAT_GUI)
3901 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3902#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (s == NULL)
3906 return;
3907
3908 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003909 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003911 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 s[0] = term_7to8bit(string);
3913 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003914
3915#if defined(WIN3264) && !defined(FEAT_GUI)
3916 if (s[0] == K_NUL)
3917 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003918 STRMOVE(s + 1, s);
3919 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003920 }
3921#endif
3922
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003923 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 need_gather = TRUE; /* need to fill termleader[] */
3926
3927 /*
3928 * need to make space for more entries
3929 */
3930 if (tc_len == tc_max_len)
3931 {
3932 tc_max_len += 20;
3933 new_tc = (struct termcode *)alloc(
3934 (unsigned)(tc_max_len * sizeof(struct termcode)));
3935 if (new_tc == NULL)
3936 {
3937 tc_max_len -= 20;
3938 return;
3939 }
3940 for (i = 0; i < tc_len; ++i)
3941 new_tc[i] = termcodes[i];
3942 vim_free(termcodes);
3943 termcodes = new_tc;
3944 }
3945
3946 /*
3947 * Look for existing entry with the same name, it is replaced.
3948 * Look for an existing entry that is alphabetical higher, the new entry
3949 * is inserted in front of it.
3950 */
3951 for (i = 0; i < tc_len; ++i)
3952 {
3953 if (termcodes[i].name[0] < name[0])
3954 continue;
3955 if (termcodes[i].name[0] == name[0])
3956 {
3957 if (termcodes[i].name[1] < name[1])
3958 continue;
3959 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003960 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
3962 if (termcodes[i].name[1] == name[1])
3963 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003964 if (flags == ATC_FROM_TERM && (j = termcode_star(
3965 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003966 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 /* Don't replace ESC[123;*X or ESC O*X with another when
3968 * invoked from got_code_from_term(). */
3969 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003970 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971 && s[len - 1]
3972 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003973 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003975 vim_free(s);
3976 return;
3977 }
3978 }
3979 else
3980 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003982 vim_free(termcodes[i].code);
3983 --tc_len;
3984 break;
3985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988 /*
3989 * Found alphabetical larger entry, move rest to insert new entry
3990 */
3991 for (j = tc_len; j > i; --j)
3992 termcodes[j] = termcodes[j - 1];
3993 break;
3994 }
3995
3996 termcodes[i].name[0] = name[0];
3997 termcodes[i].name[1] = name[1];
3998 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003999 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004000
4001 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4002 * accept modifiers. */
4003 termcodes[i].modlen = 0;
4004 j = termcode_star(s, len);
4005 if (j > 0)
4006 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 ++tc_len;
4008}
4009
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004010/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004011 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004012 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004013 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 */
4015 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004016termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017{
4018 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4019 if (len >= 3 && code[len - 2] == '*')
4020 {
4021 if (len >= 5 && code[len - 3] == ';')
4022 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004023 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004024 return 1;
4025 }
4026 return 0;
4027}
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004030find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 int i;
4033
4034 for (i = 0; i < tc_len; ++i)
4035 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4036 return termcodes[i].code;
4037 return NULL;
4038}
4039
4040#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4041 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004042get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 if (i >= tc_len)
4045 return NULL;
4046 return &termcodes[i].name[0];
4047}
4048#endif
4049
4050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004051del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
4053 int i;
4054
4055 if (termcodes == NULL) /* nothing there yet */
4056 return;
4057
4058 need_gather = TRUE; /* need to fill termleader[] */
4059
4060 for (i = 0; i < tc_len; ++i)
4061 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4062 {
4063 del_termcode_idx(i);
4064 return;
4065 }
4066 /* not found. Give error message? */
4067}
4068
4069 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004070del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 int i;
4073
4074 vim_free(termcodes[idx].code);
4075 --tc_len;
4076 for (i = idx; i < tc_len; ++i)
4077 termcodes[i] = termcodes[i + 1];
4078}
4079
4080#ifdef FEAT_TERMRESPONSE
4081/*
4082 * Called when detected that the terminal sends 8-bit codes.
4083 * Convert all 7-bit codes to their 8-bit equivalent.
4084 */
4085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 int i;
4089 int c;
4090
4091 /* Only need to do something when not already using 8-bit codes. */
4092 if (!term_is_8bit(T_NAME))
4093 {
4094 for (i = 0; i < tc_len; ++i)
4095 {
4096 c = term_7to8bit(termcodes[i].code);
4097 if (c != 0)
4098 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004099 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 termcodes[i].code[0] = c;
4101 }
4102 }
4103 need_gather = TRUE; /* need to fill termleader[] */
4104 }
4105 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004106 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107}
4108#endif
4109
4110#ifdef CHECK_DOUBLE_CLICK
4111static linenr_T orig_topline = 0;
4112# ifdef FEAT_DIFF
4113static int orig_topfill = 0;
4114# endif
4115#endif
4116#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4117/*
4118 * Checking for double clicks ourselves.
4119 * "orig_topline" is used to avoid detecting a double-click when the window
4120 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4121 */
4122/*
4123 * Set orig_topline. Used when jumping to another window, so that a double
4124 * click still works.
4125 */
4126 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004127set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 orig_topline = wp->w_topline;
4130# ifdef FEAT_DIFF
4131 orig_topfill = wp->w_topfill;
4132# endif
4133}
4134#endif
4135
4136/*
4137 * Check if typebuf.tb_buf[] contains a terminal key code.
4138 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4139 * + max_offset].
4140 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004141 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * With a match, the match is removed, the replacement code is inserted in
4143 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4144 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004145 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4146 * "buflen" is then the length of the string in buf[] and is updated for
4147 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
4149 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004150check_termcode(
4151 int max_offset,
4152 char_u *buf,
4153 int bufsize,
4154 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 char_u *tp;
4157 char_u *p;
4158 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004159 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004161 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int offset;
4163 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004164 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004165 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int new_slen;
4168 int extra;
4169 char_u string[MAX_KEY_CODE_LEN + 1];
4170 int i, j;
4171 int idx = 0;
4172#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004173# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4174 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 char_u bytes[6];
4176 int num_bytes;
4177# endif
4178 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 int is_click, is_drag;
4180 int wheel_code = 0;
4181 int current_button;
4182 static int held_button = MOUSE_RELEASE;
4183 static int orig_num_clicks = 1;
4184 static int orig_mouse_code = 0x0;
4185# ifdef CHECK_DOUBLE_CLICK
4186 static int orig_mouse_col = 0;
4187 static int orig_mouse_row = 0;
4188 static struct timeval orig_mouse_time = {0, 0};
4189 /* time of previous mouse click */
4190 struct timeval mouse_time; /* time of current mouse click */
4191 long timediff; /* elapsed time in msec */
4192# endif
4193#endif
4194 int cpo_koffset;
4195#ifdef FEAT_MOUSE_GPM
4196 extern int gpm_flag; /* gpm library variable */
4197#endif
4198
4199 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4200
4201 /*
4202 * Speed up the checks for terminal codes by gathering all first bytes
4203 * used in termleader[]. Often this is just a single <Esc>.
4204 */
4205 if (need_gather)
4206 gather_termleader();
4207
4208 /*
4209 * Check at several positions in typebuf.tb_buf[], to catch something like
4210 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4211 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004212 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * This is used often, KEEP IT FAST!
4214 */
4215 for (offset = 0; offset < max_offset; ++offset)
4216 {
4217 if (buf == NULL)
4218 {
4219 if (offset >= typebuf.tb_len)
4220 break;
4221 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4222 len = typebuf.tb_len - offset; /* length of the input */
4223 }
4224 else
4225 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004226 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 break;
4228 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004229 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 /*
4233 * Don't check characters after K_SPECIAL, those are already
4234 * translated terminal chars (avoid translating ~@^Hx).
4235 */
4236 if (*tp == K_SPECIAL)
4237 {
4238 offset += 2; /* there are always 2 extra characters */
4239 continue;
4240 }
4241
4242 /*
4243 * Skip this position if the character does not appear as the first
4244 * character in term_strings. This speeds up a lot, since most
4245 * termcodes start with the same character (ESC or CSI).
4246 */
4247 i = *tp;
4248 for (p = termleader; *p && *p != i; ++p)
4249 ;
4250 if (*p == NUL)
4251 continue;
4252
4253 /*
4254 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4255 * are in Insert mode.
4256 */
4257 if (*tp == ESC && !p_ek && (State & INSERT))
4258 continue;
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004261 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004262 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264#ifdef FEAT_GUI
4265 if (gui.in_use)
4266 {
4267 /*
4268 * GUI special key codes are all of the form [CSI xx].
4269 */
4270 if (*tp == CSI) /* Special key from GUI */
4271 {
4272 if (len < 3)
4273 return -1; /* Shouldn't happen */
4274 slen = 3;
4275 key_name[0] = tp[1];
4276 key_name[1] = tp[2];
4277 }
4278 }
4279 else
4280#endif /* FEAT_GUI */
4281 {
4282 for (idx = 0; idx < tc_len; ++idx)
4283 {
4284 /*
4285 * Ignore the entry if we are not at the start of
4286 * typebuf.tb_buf[]
4287 * and there are not enough characters to make a match.
4288 * But only when the 'K' flag is in 'cpoptions'.
4289 */
4290 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004291 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (cpo_koffset && offset && len < slen)
4293 continue;
4294 if (STRNCMP(termcodes[idx].code, tp,
4295 (size_t)(slen > len ? len : slen)) == 0)
4296 {
4297 if (len < slen) /* got a partial sequence */
4298 return -1; /* need to get more chars */
4299
4300 /*
4301 * When found a keypad key, check if there is another key
4302 * that matches and use that one. This makes <Home> to be
4303 * found instead of <kHome> when they produce the same
4304 * key code.
4305 */
4306 if (termcodes[idx].name[0] == 'K'
4307 && VIM_ISDIGIT(termcodes[idx].name[1]))
4308 {
4309 for (j = idx + 1; j < tc_len; ++j)
4310 if (termcodes[j].len == slen &&
4311 STRNCMP(termcodes[idx].code,
4312 termcodes[j].code, slen) == 0)
4313 {
4314 idx = j;
4315 break;
4316 }
4317 }
4318
4319 key_name[0] = termcodes[idx].name[0];
4320 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004323
4324 /*
4325 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004326 * <Esc>[123;*X (modslen == slen - 3)
4327 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4328 * When there is a modifier the * matches a number.
4329 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004330 */
4331 if (termcodes[idx].modlen > 0)
4332 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004333 modslen = termcodes[idx].modlen;
4334 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004335 continue;
4336 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004337 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004338 {
4339 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004340
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342 return -1; /* need to get more chars */
4343
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344 if (tp[modslen] == termcodes[idx].code[slen - 1])
4345 slen = modslen + 1; /* no modifiers */
4346 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004347 continue; /* no match */
4348 else
4349 {
4350 /* Skip over the digits, the final char must
4351 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004352 for (j = slen - 2; j < len && (isdigit(tp[j])
4353 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354 ;
4355 ++j;
4356 if (len < j) /* got a partial sequence */
4357 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004358 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4359 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004360
Bram Moolenaara529ce02017-06-22 22:37:57 +02004361 modifiers_start = tp + slen - 2;
4362
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004363 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004364 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004365 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004367 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004368 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004369 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004371 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004373
4374 slen = j;
4375 }
4376 key_name[0] = termcodes[idx].name[0];
4377 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004378 break;
4379 }
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 }
4383
4384#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004385 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004386 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004387 * detecting the start of these mouse codes they might as well be
4388 * another key code or terminal response. */
4389# ifdef FEAT_MOUSE_DEC
4390 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004391# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004392# ifdef FEAT_MOUSE_PTERM
4393 || key_name[0] == KS_PTERM_MOUSE
4394# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004395 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004397 /* Check for some responses from the terminal starting with
4398 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004399 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004400 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004401 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004402 * Also eat other possible responses to t_RV, rxvt returns
4403 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4404 * mrxvt has been reported to have "+" in the version. Assume
4405 * the escape sequence ends with a letter or one of "{|}~".
4406 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004407 * - Cursor position report: <Esc>[{row};{col}R
4408 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004409 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004410 *
4411 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004412 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004413 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004414
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004415 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004416 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004417 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004418 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004420 int col = 0;
4421 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004422#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004423 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004424#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004427 for (i = 2 + (tp[0] != CSI); i < len
4428 && !(tp[i] >= '{' && tp[i] <= '~')
4429 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004430 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004431 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004432 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004433#ifdef FEAT_MBYTE
4434 row_char = tp[i - 1];
4435#endif
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004438 {
4439 LOG_TR("Not enough characters for CRV");
4440 return -1;
4441 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004442 if (extra > 0)
4443 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004444
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004445#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004446 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4447 * u7_status is not "sent", it may be from a previous Vim that
4448 * just exited. But not for <S-F3>, it sends something
4449 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004450 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004451 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004452 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004453 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004454 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004455
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004456 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004457 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004458# ifdef FEAT_AUTOCMD
4459 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004460# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004461 if (col == 2)
4462 aw = "single";
4463 else if (col == 3)
4464 aw = "double";
4465 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4466 {
4467 /* Setting the option causes a screen redraw. Do
4468 * that right away if possible, keeping any
4469 * messages. */
4470 set_option_value((char_u *)"ambw", 0L,
4471 (char_u *)aw, 0);
4472# ifdef DEBUG_TERMRESPONSE
4473 {
4474 char buf[100];
4475 int r = redraw_asap(CLEAR);
4476
4477 sprintf(buf,
4478 "set 'ambiwidth', redraw_asap(): %d",
4479 r);
4480 log_tr(buf);
4481 }
4482# else
4483 redraw_asap(CLEAR);
4484# endif
4485 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004486 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004487 key_name[0] = (int)KS_EXTRA;
4488 key_name[1] = (int)KE_IGNORE;
4489 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004490# ifdef FEAT_EVAL
4491 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4492# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004493 }
4494 else
4495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004497 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004499 LOG_TR("Received CRV response");
4500 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004501# ifdef FEAT_AUTOCMD
4502 did_cursorhold = TRUE;
4503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /* If this code starts with CSI, you can bet that the
4506 * terminal uses 8-bit codes. */
4507 if (tp[0] == CSI)
4508 switch_to_8bit();
4509
4510 /* rxvt sends its version number: "20703" is 2.7.3.
4511 * Ignore it for when the user has set 'term' to xterm,
4512 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004513 if (col > 20000)
4514 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004516 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004518 int need_flush = FALSE;
4519
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004520 /* Only set 'ttymouse' automatically if it was not set
4521 * by the user already. */
4522 if (!option_was_set((char_u *)"ttym"))
4523 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004524# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004525 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004526 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004527 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004528 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004529# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004530 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004531 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004532 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004534 }
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004537 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004539 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 check_for_codes = TRUE;
4541 need_gather = TRUE;
4542 req_codes_from_term();
4543 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004544
4545 /* libvterm sends 0;100;0 */
4546 if (col == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004547 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004548 {
4549 /* If run from Vim $COLORS is set to the number of
4550 * colors the terminal supports. Otherwise assume
4551 * 256, libvterm supports even more. */
4552 if (mch_getenv((char_u *)"COLORS") == NULL)
4553 may_adjust_color_count(256);
4554 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004555
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004556 /* Detect terminals that set $TERM to something like
4557 * "xterm-256colors" but are not fully xterm
4558 * compatible. */
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004559# ifdef MACOS
4560 /* Mac Terminal.app sends 1;95;0 */
4561 if (col == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004562 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004563 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004564# endif
Bram Moolenaardc5471d2017-08-30 18:59:03 +02004565 /* Gnome Terminal.app sends 1;3801;0 or 1;4402;0,
4566 * assuming any version number over 3000 is not an
4567 * xterm. */
4568 if (col >= 3000)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004569 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004570
4571 /* Only request the cursor style if t_SH and t_RS are
4572 * set. Not for Terminal.app, it can't handle t_RS, it
4573 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004574 if (rcs_status == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004575 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004576 && *T_CSH != NUL
4577 && *T_CRS != NUL)
4578 {
4579 LOG_TR("Sending cursor style request");
4580 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004581 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004582 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004583 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004584
4585 /* Only request the cursor blink mode if t_RC set. Not
4586 * for Gnome terminal, it can't handle t_RC, it
4587 * echoes the characters to the screen. */
4588 if (rbm_status == STATUS_GET
4589 && !is_not_xterm
4590 && *T_CRC != NUL)
4591 {
4592 LOG_TR("Sending cursor blink mode request");
4593 out_str(T_CRC);
4594 rbm_status = STATUS_SENT;
4595 need_flush = TRUE;
4596 }
4597
4598 if (need_flush)
4599 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004601 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004603 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604# endif
4605# ifdef FEAT_AUTOCMD
4606 apply_autocmds(EVENT_TERMRESPONSE,
4607 NULL, NULL, FALSE, curbuf);
4608# endif
4609 key_name[0] = (int)KS_EXTRA;
4610 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004612
Bram Moolenaar4db25542017-08-28 22:43:05 +02004613 /* Check blinking cursor from xterm:
4614 * {lead}?12;1$y set
4615 * {lead}?12;2$y not set
4616 *
4617 * {lead} can be <Esc>[ or CSI
4618 */
4619 else if (rbm_status == STATUS_SENT
4620 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4621 && i == j + 6
4622 && tp[j + 1] == '1'
4623 && tp[j + 2] == '2'
4624 && tp[j + 3] == ';'
4625 && tp[i - 1] == '$'
4626 && tp[i] == 'y')
4627 {
4628 initial_cursor_blink = (tp[j + 4] == '1');
4629 rbm_status = STATUS_GOT;
4630 LOG_TR("Received cursor blinking mode response");
4631 key_name[0] = (int)KS_EXTRA;
4632 key_name[1] = (int)KE_IGNORE;
4633 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004634# ifdef FEAT_EVAL
4635 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4636# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004637 }
4638
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004639 /*
4640 * Check for a window position response from the terminal:
4641 * {lead}3;{x}:{y}t
4642 */
4643 else if (waiting_for_winpos
4644 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4645 || (len >= 3 && tp[0] == CSI))
4646 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4647 && tp[j + 1] == ';')
4648 {
4649 j += 2;
4650 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4651 ;
4652 if (i < len && tp[i] == ';')
4653 {
4654 winpos_x = atoi((char *)tp + j);
4655 j = i + 1;
4656 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4657 ;
4658 if (i < len && tp[i] == 't')
4659 {
4660 winpos_y = atoi((char *)tp + j);
4661 /* got finished code: consume it */
4662 key_name[0] = (int)KS_EXTRA;
4663 key_name[1] = (int)KE_IGNORE;
4664 slen = i + 1;
4665 }
4666 }
4667 if (i == len)
4668 {
4669 LOG_TR("not enough characters for winpos");
4670 return -1;
4671 }
4672 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004673 }
4674
4675 /* Check for background color response from the terminal:
4676 *
4677 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4678 *
4679 * {lead} can be <Esc>] or OSC
4680 * {tail} can be '\007', <Esc>\ or STERM.
4681 *
4682 * Consume any code that starts with "{lead}11;", it's also
4683 * possible that "rgba" is following.
4684 */
4685 else if (*T_RBG != NUL
4686 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4687 || tp[0] == OSC))
4688 {
4689 j = 1 + (tp[0] == ESC);
4690 if (len >= j + 3 && (argp[0] != '1'
4691 || argp[1] != '1' || argp[2] != ';'))
4692 i = 0; /* no match */
4693 else
4694 for (i = j; i < len; ++i)
4695 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4696 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004697 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004698 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4699 && tp[j + 11] == '/' && tp[j + 16] == '/'
4700 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004701 {
4702 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4703 + tp[j+17]) ? "light" : "dark";
4704
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004705 LOG_TR("Received RBG response");
4706 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004707 if (STRCMP(p_bg, newval) != 0)
4708 {
4709 /* value differs, apply it */
4710 set_option_value((char_u *)"bg", 0L,
4711 (char_u *)newval, 0);
4712 reset_option_was_set((char_u *)"bg");
4713 redraw_asap(CLEAR);
4714 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004715 }
4716
4717 /* got finished code: consume it */
4718 key_name[0] = (int)KS_EXTRA;
4719 key_name[1] = (int)KE_IGNORE;
4720 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf6d9f962017-08-24 20:21:16 +02004721 if (tp[i] == 0x07 && i + 1 < len && tp[i + 1] == 0x18)
4722 /* Sometimes the 0x07 is followed by 0x18, unclear
4723 * when this happens. */
4724 ++slen;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004725# ifdef FEAT_EVAL
4726 set_vim_var_string(VV_TERMRGBRESP, tp, slen);
4727# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004728 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004729 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004730 if (i == len)
4731 {
4732 LOG_TR("not enough characters for RB");
4733 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004737 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004738 * {lead}{flag}+r<hex bytes><{tail}
4739 *
4740 * {lead} can be <Esc>P or DCS
4741 * {flag} can be '0' or '1'
4742 * {tail} can be Esc>\ or STERM
4743 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004744 * Check for cursor shape response from xterm:
4745 * {lead}1$r<number> q{tail}
4746 *
4747 * {lead} can be <Esc>P or DCS
4748 * {tail} can be Esc>\ or STERM
4749 *
4750 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004751 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004752 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004753 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 || tp[0] == DCS))
4755 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004756 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004757 if (len < j + 3)
4758 i = len; /* need more chars */
4759 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004760 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004761 else if (argp[1] == '+')
4762 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004763 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004764 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004765 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 || tp[i] == STERM)
4767 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004768 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 got_code_from_term(tp + j, i);
4770 key_name[0] = (int)KS_EXTRA;
4771 key_name[1] = (int)KE_IGNORE;
4772 slen = i + 1 + (tp[i] == ESC);
4773 break;
4774 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004775 }
4776 else if ((len >= j + 6 && isdigit(argp[3]))
4777 && argp[4] == ' '
4778 && argp[5] == 'q')
4779 {
4780 /* cursor shape response */
4781 i = j + 6;
4782 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4783 || tp[i] == STERM)
4784 {
4785 int number = argp[3] - '0';
4786
4787 /* 0, 1 = block blink, 2 = block
4788 * 3 = underline blink, 4 = underline
4789 * 5 = vertical bar blink, 6 = vertical bar */
4790 number = number == 0 ? 1 : number;
4791 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004792 /* The blink flag is actually inverted, compared to
4793 * the value set with T_SH. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004794 initial_cursor_shape_blink =
4795 (number & 1) ? FALSE : TRUE;
4796 rcs_status = STATUS_GOT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004797 LOG_TR("Received cursor shape response");
4798
4799 key_name[0] = (int)KS_EXTRA;
4800 key_name[1] = (int)KE_IGNORE;
4801 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004802# ifdef FEAT_EVAL
4803 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
4804# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004805 }
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004809 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004810 /* These codes arrive many together, each code can be
4811 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004812 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004813 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 }
4817#endif
4818
4819 if (key_name[0] == NUL)
4820 continue; /* No match at this position, try next one */
4821
4822 /* We only get here when we have a complete termcode match */
4823
4824#ifdef FEAT_MOUSE
4825# ifdef FEAT_GUI
4826 /*
4827 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4828 * so that we know which window to scroll later.
4829 */
4830 if (gui.in_use
4831 && key_name[0] == (int)KS_EXTRA
4832 && (key_name[1] == (int)KE_X1MOUSE
4833 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004834 || key_name[1] == (int)KE_MOUSELEFT
4835 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 || key_name[1] == (int)KE_MOUSEDOWN
4837 || key_name[1] == (int)KE_MOUSEUP))
4838 {
4839 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4840 if (num_bytes == -1) /* not enough coordinates */
4841 return -1;
4842 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4843 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4844 slen += num_bytes;
4845 }
4846 else
4847# endif
4848 /*
4849 * If it is a mouse click, get the coordinates.
4850 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004851 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004853 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854# endif
4855# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004856 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857# endif
4858# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004859 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860# endif
4861# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004862 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004864# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004865 || key_name[0] == KS_URXVT_MOUSE
4866# endif
4867# ifdef FEAT_MOUSE_SGR
4868 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004869 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 )
4872 {
4873 is_click = is_drag = FALSE;
4874
Bram Moolenaar864207d2008-06-24 22:14:38 +00004875# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4876 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (key_name[0] == (int)KS_MOUSE)
4878 {
4879 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004880 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 * s == encoded button state:
4882 * 0x20 = left button down
4883 * 0x21 = middle button down
4884 * 0x22 = right button down
4885 * 0x23 = any button release
4886 * 0x60 = button 4 down (scroll wheel down)
4887 * 0x61 = button 5 down (scroll wheel up)
4888 * add 0x04 for SHIFT
4889 * add 0x08 for ALT
4890 * add 0x10 for CTRL
4891 * add 0x20 for mouse drag (0x40 is drag with left button)
4892 * c == column + ' ' + 1 == column + 33
4893 * r == row + ' ' + 1 == row + 33
4894 *
4895 * The coordinates are passed on through global variables.
4896 * Ugly, but this avoids trouble with mouse clicks at an
4897 * unexpected moment and allows for mapping them.
4898 */
4899 for (;;)
4900 {
4901#ifdef FEAT_GUI
4902 if (gui.in_use)
4903 {
4904 /* GUI uses more bits for columns > 223 */
4905 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4906 if (num_bytes == -1) /* not enough coordinates */
4907 return -1;
4908 mouse_code = bytes[0];
4909 mouse_col = 128 * (bytes[1] - ' ' - 1)
4910 + bytes[2] - ' ' - 1;
4911 mouse_row = 128 * (bytes[3] - ' ' - 1)
4912 + bytes[4] - ' ' - 1;
4913 }
4914 else
4915#endif
4916 {
4917 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4918 if (num_bytes == -1) /* not enough coordinates */
4919 return -1;
4920 mouse_code = bytes[0];
4921 mouse_col = bytes[1] - ' ' - 1;
4922 mouse_row = bytes[2] - ' ' - 1;
4923 }
4924 slen += num_bytes;
4925
4926 /* If the following bytes is also a mouse code and it has
4927 * the same code, dump this one and get the next. This
4928 * makes dragging a whole lot faster. */
4929#ifdef FEAT_GUI
4930 if (gui.in_use)
4931 j = 3;
4932 else
4933#endif
4934 j = termcodes[idx].len;
4935 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4936 && tp[slen + j] == mouse_code
4937 && tp[slen + j + 1] != NUL
4938 && tp[slen + j + 2] != NUL
4939#ifdef FEAT_GUI
4940 && (!gui.in_use
4941 || (tp[slen + j + 3] != NUL
4942 && tp[slen + j + 4] != NUL))
4943#endif
4944 )
4945 slen += j;
4946 else
4947 break;
4948 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004951# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4952 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004953 || key_name[0] == KS_SGR_MOUSE
4954 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004955 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004956 /* URXVT 1015 mouse reporting mode:
4957 * Almost identical to xterm mouse mode, except the values
4958 * are decimal instead of bytes.
4959 *
4960 * \033[%d;%d;%dM
4961 * ^-- row
4962 * ^----- column
4963 * ^-------- code
4964 *
4965 * SGR 1006 mouse reporting mode:
4966 * Almost identical to xterm mouse mode, except the values
4967 * are decimal instead of bytes.
4968 *
4969 * \033[<%d;%d;%dM
4970 * ^-- row
4971 * ^----- column
4972 * ^-------- code
4973 *
4974 * \033[<%d;%d;%dm : mouse release event
4975 * ^-- row
4976 * ^----- column
4977 * ^-------- code
4978 */
4979 p = modifiers_start;
4980 if (p == NULL)
4981 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004982
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004983 mouse_code = getdigits(&p);
4984 if (*p++ != ';')
4985 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004986
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004987 /* when mouse reporting is SGR, add 32 to mouse code */
4988 if (key_name[0] == KS_SGR_MOUSE
4989 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4990 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004991
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004992 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4993 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004994
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004995 mouse_col = getdigits(&p) - 1;
4996 if (*p++ != ';')
4997 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004998
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004999 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005000
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005001 /* The modifiers were the mouse coordinates, not the
5002 * modifier keys (alt/shift/ctrl/meta) state. */
5003 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005004 }
5005# endif
5006
5007 if (key_name[0] == (int)KS_MOUSE
5008#ifdef FEAT_MOUSE_URXVT
5009 || key_name[0] == (int)KS_URXVT_MOUSE
5010#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005011#ifdef FEAT_MOUSE_SGR
5012 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005013 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005014#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005015 )
5016 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005017# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Handle mouse events.
5020 * Recognize the xterm mouse wheel, but not in the GUI, the
5021 * Linux console with GPM and the MS-DOS or Win32 console
5022 * (multi-clicks use >= 0x60).
5023 */
5024 if (mouse_code >= MOUSEWHEEL_LOW
5025# ifdef FEAT_GUI
5026 && !gui.in_use
5027# endif
5028# ifdef FEAT_MOUSE_GPM
5029 && gpm_flag == 0
5030# endif
5031 )
5032 {
5033 /* Keep the mouse_code before it's changed, so that we
5034 * remember that it was a mouse wheel click. */
5035 wheel_code = mouse_code;
5036 }
5037# ifdef FEAT_MOUSE_XTERM
5038 else if (held_button == MOUSE_RELEASE
5039# ifdef FEAT_GUI
5040 && !gui.in_use
5041# endif
5042 && (mouse_code == 0x23 || mouse_code == 0x24))
5043 {
5044 /* Apparently used by rxvt scroll wheel. */
5045 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5046 }
5047# endif
5048
5049# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5050 else if (use_xterm_mouse() > 1)
5051 {
5052 if (mouse_code & MOUSE_DRAG_XTERM)
5053 mouse_code |= MOUSE_DRAG;
5054 }
5055# endif
5056# ifdef FEAT_XCLIPBOARD
5057 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5058 {
5059 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5060 stop_xterm_trace();
5061 else
5062 start_xterm_trace(mouse_code);
5063 }
5064# endif
5065# endif
5066 }
5067# endif /* !UNIX || FEAT_MOUSE_XTERM */
5068# ifdef FEAT_MOUSE_NET
5069 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5070 {
5071 int mc, mr;
5072
5073 /* expect a rather limited sequence like: balancing {
5074 * \033}6,45\r
5075 * '6' is the row, 45 is the column
5076 */
5077 p = tp + slen;
5078 mr = getdigits(&p);
5079 if (*p++ != ',')
5080 return -1;
5081 mc = getdigits(&p);
5082 if (*p++ != '\r')
5083 return -1;
5084
5085 mouse_col = mc - 1;
5086 mouse_row = mr - 1;
5087 mouse_code = MOUSE_LEFT;
5088 slen += (int)(p - (tp + slen));
5089 }
5090# endif /* FEAT_MOUSE_NET */
5091# ifdef FEAT_MOUSE_JSB
5092 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5093 {
5094 int mult, val, iter, button, status;
5095
5096 /* JSBTERM Input Model
5097 * \033[0~zw uniq escape sequence
5098 * (L-x) Left button pressed - not pressed x not reporting
5099 * (M-x) Middle button pressed - not pressed x not reporting
5100 * (R-x) Right button pressed - not pressed x not reporting
5101 * (SDmdu) Single , Double click, m mouse move d button down
5102 * u button up
5103 * ### X cursor position padded to 3 digits
5104 * ### Y cursor position padded to 3 digits
5105 * (s-x) SHIFT key pressed - not pressed x not reporting
5106 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005107 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
5109
5110 p = tp + slen;
5111 button = mouse_code = 0;
5112 switch (*p++)
5113 {
5114 case 'L': button = 1; break;
5115 case '-': break;
5116 case 'x': break; /* ignore sequence */
5117 default: return -1; /* Unknown Result */
5118 }
5119 switch (*p++)
5120 {
5121 case 'M': button |= 2; break;
5122 case '-': break;
5123 case 'x': break; /* ignore sequence */
5124 default: return -1; /* Unknown Result */
5125 }
5126 switch (*p++)
5127 {
5128 case 'R': button |= 4; break;
5129 case '-': break;
5130 case 'x': break; /* ignore sequence */
5131 default: return -1; /* Unknown Result */
5132 }
5133 status = *p++;
5134 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5135 mult /= 10, p++)
5136 if (*p >= '0' && *p <= '9')
5137 val += (*p - '0') * mult;
5138 else
5139 return -1;
5140 mouse_col = val;
5141 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5142 mult /= 10, p++)
5143 if (*p >= '0' && *p <= '9')
5144 val += (*p - '0') * mult;
5145 else
5146 return -1;
5147 mouse_row = val;
5148 switch (*p++)
5149 {
5150 case 's': button |= 8; break; /* SHIFT key Pressed */
5151 case '-': break; /* Not Pressed */
5152 case 'x': break; /* Not Reporting */
5153 default: return -1; /* Unknown Result */
5154 }
5155 switch (*p++)
5156 {
5157 case 'c': button |= 16; break; /* CTRL key Pressed */
5158 case '-': break; /* Not Pressed */
5159 case 'x': break; /* Not Reporting */
5160 default: return -1; /* Unknown Result */
5161 }
5162 if (*p++ != '\033')
5163 return -1;
5164 if (*p++ != '\\')
5165 return -1;
5166 switch (status)
5167 {
5168 case 'D': /* Double Click */
5169 case 'S': /* Single Click */
5170 if (button & 1) mouse_code |= MOUSE_LEFT;
5171 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5172 if (button & 4) mouse_code |= MOUSE_RIGHT;
5173 if (button & 8) mouse_code |= MOUSE_SHIFT;
5174 if (button & 16) mouse_code |= MOUSE_CTRL;
5175 break;
5176 case 'm': /* Mouse move */
5177 if (button & 1) mouse_code |= MOUSE_LEFT;
5178 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5179 if (button & 4) mouse_code |= MOUSE_RIGHT;
5180 if (button & 8) mouse_code |= MOUSE_SHIFT;
5181 if (button & 16) mouse_code |= MOUSE_CTRL;
5182 if ((button & 7) != 0)
5183 {
5184 held_button = mouse_code;
5185 mouse_code |= MOUSE_DRAG;
5186 }
5187 is_drag = TRUE;
5188 showmode();
5189 break;
5190 case 'd': /* Button Down */
5191 if (button & 1) mouse_code |= MOUSE_LEFT;
5192 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5193 if (button & 4) mouse_code |= MOUSE_RIGHT;
5194 if (button & 8) mouse_code |= MOUSE_SHIFT;
5195 if (button & 16) mouse_code |= MOUSE_CTRL;
5196 break;
5197 case 'u': /* Button Up */
5198 if (button & 1)
5199 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5200 if (button & 2)
5201 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5202 if (button & 4)
5203 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5204 if (button & 8)
5205 mouse_code |= MOUSE_SHIFT;
5206 if (button & 16)
5207 mouse_code |= MOUSE_CTRL;
5208 break;
5209 default: return -1; /* Unknown Result */
5210 }
5211
5212 slen += (p - (tp + slen));
5213 }
5214# endif /* FEAT_MOUSE_JSB */
5215# ifdef FEAT_MOUSE_DEC
5216 if (key_name[0] == (int)KS_DEC_MOUSE)
5217 {
5218 /* The DEC Locator Input Model
5219 * Netterm delivers the code sequence:
5220 * \033[2;4;24;80&w (left button down)
5221 * \033[3;0;24;80&w (left button up)
5222 * \033[6;1;24;80&w (right button down)
5223 * \033[7;0;24;80&w (right button up)
5224 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5225 * Pe is the event code
5226 * Pb is the button code
5227 * Pr is the row coordinate
5228 * Pc is the column coordinate
5229 * Pp is the third coordinate (page number)
5230 * Pe, the event code indicates what event caused this report
5231 * The following event codes are defined:
5232 * 0 - request, the terminal received an explicit request
5233 * for a locator report, but the locator is unavailable
5234 * 1 - request, the terminal received an explicit request
5235 * for a locator report
5236 * 2 - left button down
5237 * 3 - left button up
5238 * 4 - middle button down
5239 * 5 - middle button up
5240 * 6 - right button down
5241 * 7 - right button up
5242 * 8 - fourth button down
5243 * 9 - fourth button up
5244 * 10 - locator outside filter rectangle
5245 * Pb, the button code, ASCII decimal 0-15 indicating which
5246 * buttons are down if any. The state of the four buttons
5247 * on the locator correspond to the low four bits of the
5248 * decimal value,
5249 * "1" means button depressed
5250 * 0 - no buttons down,
5251 * 1 - right,
5252 * 2 - middle,
5253 * 4 - left,
5254 * 8 - fourth
5255 * Pr is the row coordinate of the locator position in the page,
5256 * encoded as an ASCII decimal value.
5257 * If Pr is omitted, the locator position is undefined
5258 * (outside the terminal window for example).
5259 * Pc is the column coordinate of the locator position in the
5260 * page, encoded as an ASCII decimal value.
5261 * If Pc is omitted, the locator position is undefined
5262 * (outside the terminal window for example).
5263 * Pp is the page coordinate of the locator position
5264 * encoded as an ASCII decimal value.
5265 * The page coordinate may be omitted if the locator is on
5266 * page one (the default). We ignore it anyway.
5267 */
5268 int Pe, Pb, Pr, Pc;
5269
5270 p = tp + slen;
5271
5272 /* get event status */
5273 Pe = getdigits(&p);
5274 if (*p++ != ';')
5275 return -1;
5276
5277 /* get button status */
5278 Pb = getdigits(&p);
5279 if (*p++ != ';')
5280 return -1;
5281
5282 /* get row status */
5283 Pr = getdigits(&p);
5284 if (*p++ != ';')
5285 return -1;
5286
5287 /* get column status */
5288 Pc = getdigits(&p);
5289
5290 /* the page parameter is optional */
5291 if (*p == ';')
5292 {
5293 p++;
5294 (void)getdigits(&p);
5295 }
5296 if (*p++ != '&')
5297 return -1;
5298 if (*p++ != 'w')
5299 return -1;
5300
5301 mouse_code = 0;
5302 switch (Pe)
5303 {
5304 case 0: return -1; /* position request while unavailable */
5305 case 1: /* a response to a locator position request includes
5306 the status of all buttons */
5307 Pb &= 7; /* mask off and ignore fourth button */
5308 if (Pb & 4)
5309 mouse_code = MOUSE_LEFT;
5310 if (Pb & 2)
5311 mouse_code = MOUSE_MIDDLE;
5312 if (Pb & 1)
5313 mouse_code = MOUSE_RIGHT;
5314 if (Pb)
5315 {
5316 held_button = mouse_code;
5317 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005318 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320 is_drag = TRUE;
5321 showmode();
5322 break;
5323 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005324 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 break;
5326 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5327 break;
5328 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005329 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 break;
5331 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5332 break;
5333 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005334 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 break;
5336 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5337 break;
5338 case 8: return -1; /* fourth button down */
5339 case 9: return -1; /* fourth button up */
5340 case 10: return -1; /* mouse outside of filter rectangle */
5341 default: return -1; /* should never occur */
5342 }
5343
5344 mouse_col = Pc - 1;
5345 mouse_row = Pr - 1;
5346
5347 slen += (int)(p - (tp + slen));
5348 }
5349# endif /* FEAT_MOUSE_DEC */
5350# ifdef FEAT_MOUSE_PTERM
5351 if (key_name[0] == (int)KS_PTERM_MOUSE)
5352 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005353 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 p = tp + slen;
5356
5357 action = getdigits(&p);
5358 if (*p++ != ';')
5359 return -1;
5360
5361 mouse_row = getdigits(&p);
5362 if (*p++ != ';')
5363 return -1;
5364 mouse_col = getdigits(&p);
5365 if (*p++ != ';')
5366 return -1;
5367
5368 button = getdigits(&p);
5369 mouse_code = 0;
5370
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005371 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 case 4: mouse_code = MOUSE_LEFT; break;
5374 case 1: mouse_code = MOUSE_RIGHT; break;
5375 case 2: mouse_code = MOUSE_MIDDLE; break;
5376 default: return -1;
5377 }
5378
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005379 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 case 31: /* Initial press */
5382 if (*p++ != ';')
5383 return -1;
5384
5385 num_clicks = getdigits(&p); /* Not used */
5386 break;
5387
5388 case 32: /* Release */
5389 mouse_code |= MOUSE_RELEASE;
5390 break;
5391
5392 case 33: /* Drag */
5393 held_button = mouse_code;
5394 mouse_code |= MOUSE_DRAG;
5395 break;
5396
5397 default:
5398 return -1;
5399 }
5400
5401 if (*p++ != 't')
5402 return -1;
5403
5404 slen += (p - (tp + slen));
5405 }
5406# endif /* FEAT_MOUSE_PTERM */
5407
5408 /* Interpret the mouse code */
5409 current_button = (mouse_code & MOUSE_CLICK_MASK);
5410 if (current_button == MOUSE_RELEASE
5411# ifdef FEAT_MOUSE_XTERM
5412 && wheel_code == 0
5413# endif
5414 )
5415 {
5416 /*
5417 * If we get a mouse drag or release event when
5418 * there is no mouse button held down (held_button ==
5419 * MOUSE_RELEASE), produce a K_IGNORE below.
5420 * (can happen when you hold down two buttons
5421 * and then let them go, or click in the menu bar, but not
5422 * on a menu, and drag into the text).
5423 */
5424 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5425 is_drag = TRUE;
5426 current_button = held_button;
5427 }
5428 else if (wheel_code == 0)
5429 {
5430# ifdef CHECK_DOUBLE_CLICK
5431# ifdef FEAT_MOUSE_GPM
5432# ifdef FEAT_GUI
5433 /*
5434 * Only for Unix, when GUI or gpm is not active, we handle
5435 * multi-clicks here.
5436 */
5437 if (gpm_flag == 0 && !gui.in_use)
5438# else
5439 if (gpm_flag == 0)
5440# endif
5441# else
5442# ifdef FEAT_GUI
5443 if (!gui.in_use)
5444# endif
5445# endif
5446 {
5447 /*
5448 * Compute the time elapsed since the previous mouse click.
5449 */
5450 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005451 if (orig_mouse_time.tv_sec == 0)
5452 {
5453 /*
5454 * Avoid computing the difference between mouse_time
5455 * and orig_mouse_time for the first click, as the
5456 * difference would be huge and would cause multiplication
5457 * overflow.
5458 */
5459 timediff = p_mouset;
5460 }
5461 else
5462 {
5463 timediff = (mouse_time.tv_usec
5464 - orig_mouse_time.tv_usec) / 1000;
5465 if (timediff < 0)
5466 --orig_mouse_time.tv_sec;
5467 timediff += (mouse_time.tv_sec
5468 - orig_mouse_time.tv_sec) * 1000;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 orig_mouse_time = mouse_time;
5471 if (mouse_code == orig_mouse_code
5472 && timediff < p_mouset
5473 && orig_num_clicks != 4
5474 && orig_mouse_col == mouse_col
5475 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005476 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005478 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005480 )
5481#ifdef FEAT_WINDOWS
5482 /* Double click in tab pages line also works
5483 * when window contents changes. */
5484 || (mouse_row == 0 && firstwin->w_winrow > 0)
5485#endif
5486 )
5487 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 ++orig_num_clicks;
5489 else
5490 orig_num_clicks = 1;
5491 orig_mouse_col = mouse_col;
5492 orig_mouse_row = mouse_row;
5493 orig_topline = curwin->w_topline;
5494#ifdef FEAT_DIFF
5495 orig_topfill = curwin->w_topfill;
5496#endif
5497 }
5498# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5499 else
5500 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5501# endif
5502# else
5503 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5504# endif
5505 is_click = TRUE;
5506 orig_mouse_code = mouse_code;
5507 }
5508 if (!is_drag)
5509 held_button = mouse_code & MOUSE_CLICK_MASK;
5510
5511 /*
5512 * Translate the actual mouse event into a pseudo mouse event.
5513 * First work out what modifiers are to be used.
5514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (orig_mouse_code & MOUSE_SHIFT)
5516 modifiers |= MOD_MASK_SHIFT;
5517 if (orig_mouse_code & MOUSE_CTRL)
5518 modifiers |= MOD_MASK_CTRL;
5519 if (orig_mouse_code & MOUSE_ALT)
5520 modifiers |= MOD_MASK_ALT;
5521 if (orig_num_clicks == 2)
5522 modifiers |= MOD_MASK_2CLICK;
5523 else if (orig_num_clicks == 3)
5524 modifiers |= MOD_MASK_3CLICK;
5525 else if (orig_num_clicks == 4)
5526 modifiers |= MOD_MASK_4CLICK;
5527
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005528 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5529 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005531 if (wheel_code != 0
5532 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005533 {
5534 if (wheel_code & MOUSE_CTRL)
5535 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005536 if (wheel_code & MOUSE_ALT)
5537 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 key_name[1] = (wheel_code & 1)
5539 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 else
5542 key_name[1] = get_pseudo_mouse_code(current_button,
5543 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005544
5545 /* Make sure the mouse position is valid. Some terminals may
5546 * return weird values. */
5547 if (mouse_col >= Columns)
5548 mouse_col = Columns - 1;
5549 if (mouse_row >= Rows)
5550 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552#endif /* FEAT_MOUSE */
5553
5554#ifdef FEAT_GUI
5555 /*
5556 * If using the GUI, then we get menu and scrollbar events.
5557 *
5558 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5559 * four bytes which are to be taken as a pointer to the vimmenu_T
5560 * structure.
5561 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005562 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005563 * is one byte with the tab index.
5564 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5566 * by one byte representing the scrollbar number, and then four bytes
5567 * representing a long_u which is the new value of the scrollbar.
5568 *
5569 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5570 * KE_FILLER followed by four bytes representing a long_u which is the
5571 * new value of the scrollbar.
5572 */
5573# ifdef FEAT_MENU
5574 else if (key_name[0] == (int)KS_MENU)
5575 {
5576 long_u val;
5577
5578 num_bytes = get_long_from_buf(tp + slen, &val);
5579 if (num_bytes == -1)
5580 return -1;
5581 current_menu = (vimmenu_T *)val;
5582 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005583
5584 /* The menu may have been deleted right after it was used, check
5585 * for that. */
5586 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5587 {
5588 key_name[0] = KS_EXTRA;
5589 key_name[1] = (int)KE_IGNORE;
5590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005593# ifdef FEAT_GUI_TABLINE
5594 else if (key_name[0] == (int)KS_TABLINE)
5595 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005596 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005597 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5598 if (num_bytes == -1)
5599 return -1;
5600 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005601 if (current_tab == 255) /* -1 in a byte gives 255 */
5602 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005603 slen += num_bytes;
5604 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005605 else if (key_name[0] == (int)KS_TABMENU)
5606 {
5607 /* Selecting tabline tab or using its menu. */
5608 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5609 if (num_bytes == -1)
5610 return -1;
5611 current_tab = (int)bytes[0];
5612 current_tabmenu = (int)bytes[1];
5613 slen += num_bytes;
5614 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616# ifndef USE_ON_FLY_SCROLL
5617 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5618 {
5619 long_u val;
5620
5621 /* Get the last scrollbar event in the queue of the same type */
5622 j = 0;
5623 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5624 && tp[j + 2] != NUL; ++i)
5625 {
5626 j += 3;
5627 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5628 if (num_bytes == -1)
5629 break;
5630 if (i == 0)
5631 current_scrollbar = (int)bytes[0];
5632 else if (current_scrollbar != (int)bytes[0])
5633 break;
5634 j += num_bytes;
5635 num_bytes = get_long_from_buf(tp + j, &val);
5636 if (num_bytes == -1)
5637 break;
5638 scrollbar_value = val;
5639 j += num_bytes;
5640 slen = j;
5641 }
5642 if (i == 0) /* not enough characters to make one */
5643 return -1;
5644 }
5645 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5646 {
5647 long_u val;
5648
5649 /* Get the last horiz. scrollbar event in the queue */
5650 j = 0;
5651 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5652 && tp[j + 2] != NUL; ++i)
5653 {
5654 j += 3;
5655 num_bytes = get_long_from_buf(tp + j, &val);
5656 if (num_bytes == -1)
5657 break;
5658 scrollbar_value = val;
5659 j += num_bytes;
5660 slen = j;
5661 }
5662 if (i == 0) /* not enough characters to make one */
5663 return -1;
5664 }
5665# endif /* !USE_ON_FLY_SCROLL */
5666#endif /* FEAT_GUI */
5667
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005668 /*
5669 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5670 */
5671 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5672
5673 /*
5674 * Add any modifier codes to our string.
5675 */
5676 new_slen = 0; /* Length of what will replace the termcode */
5677 if (modifiers != 0)
5678 {
5679 /* Some keys have the modifier included. Need to handle that here
5680 * to make mappings work. */
5681 key = simplify_key(key, &modifiers);
5682 if (modifiers != 0)
5683 {
5684 string[new_slen++] = K_SPECIAL;
5685 string[new_slen++] = (int)KS_MODIFIER;
5686 string[new_slen++] = modifiers;
5687 }
5688 }
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005691 key_name[0] = KEY2TERMCAP0(key);
5692 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005694 {
5695 /* from ":set <M-b>=xx" */
5696#ifdef FEAT_MBYTE
5697 if (has_mbyte)
5698 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5699 else
5700#endif
5701 string[new_slen++] = key_name[1];
5702 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005703 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5704 && key_name[1] == KE_IGNORE)
5705 {
5706 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5707 * to indicate what happened. */
5708 retval = KEYLEN_REMOVED;
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else
5711 {
5712 string[new_slen++] = K_SPECIAL;
5713 string[new_slen++] = key_name[0];
5714 string[new_slen++] = key_name[1];
5715 }
5716 string[new_slen] = NUL;
5717 extra = new_slen - slen;
5718 if (buf == NULL)
5719 {
5720 if (extra < 0)
5721 /* remove matched chars, taking care of noremap */
5722 del_typebuf(-extra, offset);
5723 else if (extra > 0)
5724 /* insert the extra space we need */
5725 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5726
5727 /*
5728 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5729 * typebuf.tb_buf[]!
5730 */
5731 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5732 (size_t)new_slen);
5733 }
5734 else
5735 {
5736 if (extra < 0)
5737 /* remove matched characters */
5738 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005739 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005741 {
5742 /* Insert the extra space we need. If there is insufficient
5743 * space return -1. */
5744 if (*buflen + extra + new_slen >= bufsize)
5745 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005747 (size_t)(*buflen - offset));
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005750 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005752 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
Bram Moolenaar2951b772013-07-03 12:45:31 +02005755#ifdef FEAT_TERMRESPONSE
5756 LOG_TR("normal character");
5757#endif
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return 0; /* no match found */
5760}
5761
5762/*
5763 * Replace any terminal code strings in from[] with the equivalent internal
5764 * vim representation. This is used for the "from" and "to" part of a
5765 * mapping, and the "to" part of a menu command.
5766 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5767 * '<'.
5768 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5769 *
5770 * The replacement is done in result[] and finally copied into allocated
5771 * memory. If this all works well *bufp is set to the allocated memory and a
5772 * pointer to it is returned. If something fails *bufp is set to NULL and from
5773 * is returned.
5774 *
5775 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5776 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5777 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5778 * instead of a CTRL-V.
5779 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005780 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005781replace_termcodes(
5782 char_u *from,
5783 char_u **bufp,
5784 int from_part,
5785 int do_lt, /* also translate <lt> */
5786 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 int i;
5789 int slen;
5790 int key;
5791 int dlen = 0;
5792 char_u *src;
5793 int do_backslash; /* backslash is a special character */
5794 int do_special; /* recognize <> key codes */
5795 int do_key_code; /* recognize raw key codes */
5796 char_u *result; /* buffer for resulting string */
5797
5798 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005799 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5801
5802 /*
5803 * Allocate space for the translation. Worst case a single character is
5804 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5805 */
5806 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5807 if (result == NULL) /* out of memory */
5808 {
5809 *bufp = NULL;
5810 return from;
5811 }
5812
5813 src = from;
5814
5815 /*
5816 * Check for #n at start only: function key n
5817 */
5818 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5819 {
5820 result[dlen++] = K_SPECIAL;
5821 result[dlen++] = 'k';
5822 if (src[1] == '0')
5823 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5824 else
5825 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5826 src += 2;
5827 }
5828
5829 /*
5830 * Copy each byte from *from to result[dlen]
5831 */
5832 while (*src != NUL)
5833 {
5834 /*
5835 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005836 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 */
5838 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5839 {
5840#ifdef FEAT_EVAL
5841 /*
5842 * Replace <SID> by K_SNR <script-nr> _.
5843 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5844 */
5845 if (STRNICMP(src, "<SID>", 5) == 0)
5846 {
5847 if (current_SID <= 0)
5848 EMSG(_(e_usingsid));
5849 else
5850 {
5851 src += 5;
5852 result[dlen++] = K_SPECIAL;
5853 result[dlen++] = (int)KS_EXTRA;
5854 result[dlen++] = (int)KE_SNR;
5855 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5856 dlen += (int)STRLEN(result + dlen);
5857 result[dlen++] = '_';
5858 continue;
5859 }
5860 }
5861#endif
5862
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005863 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (slen)
5865 {
5866 dlen += slen;
5867 continue;
5868 }
5869 }
5870
5871 /*
5872 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5873 * Note that this is also checked after replacing the <> form.
5874 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5875 * it could be a character in the file.
5876 */
5877 if (do_key_code)
5878 {
5879 i = find_term_bykeys(src);
5880 if (i >= 0)
5881 {
5882 result[dlen++] = K_SPECIAL;
5883 result[dlen++] = termcodes[i].name[0];
5884 result[dlen++] = termcodes[i].name[1];
5885 src += termcodes[i].len;
5886 /* If terminal code matched, continue after it. */
5887 continue;
5888 }
5889 }
5890
5891#ifdef FEAT_EVAL
5892 if (do_special)
5893 {
5894 char_u *p, *s, len;
5895
5896 /*
5897 * Replace <Leader> by the value of "mapleader".
5898 * Replace <LocalLeader> by the value of "maplocalleader".
5899 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5900 */
5901 if (STRNICMP(src, "<Leader>", 8) == 0)
5902 {
5903 len = 8;
5904 p = get_var_value((char_u *)"g:mapleader");
5905 }
5906 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5907 {
5908 len = 13;
5909 p = get_var_value((char_u *)"g:maplocalleader");
5910 }
5911 else
5912 {
5913 len = 0;
5914 p = NULL;
5915 }
5916 if (len != 0)
5917 {
5918 /* Allow up to 8 * 6 characters for "mapleader". */
5919 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5920 s = (char_u *)"\\";
5921 else
5922 s = p;
5923 while (*s != NUL)
5924 result[dlen++] = *s++;
5925 src += len;
5926 continue;
5927 }
5928 }
5929#endif
5930
5931 /*
5932 * Remove CTRL-V and ignore the next character.
5933 * For "from" side the CTRL-V at the end is included, for the "to"
5934 * part it is removed.
5935 * If 'cpoptions' does not contain 'B', also accept a backslash.
5936 */
5937 key = *src;
5938 if (key == Ctrl_V || (do_backslash && key == '\\'))
5939 {
5940 ++src; /* skip CTRL-V or backslash */
5941 if (*src == NUL)
5942 {
5943 if (from_part)
5944 result[dlen++] = key;
5945 break;
5946 }
5947 }
5948
5949#ifdef FEAT_MBYTE
5950 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005951 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#endif
5953 {
5954 /*
5955 * If the character is K_SPECIAL, replace it with K_SPECIAL
5956 * KS_SPECIAL KE_FILLER.
5957 * If compiled with the GUI replace CSI with K_CSI.
5958 */
5959 if (*src == K_SPECIAL)
5960 {
5961 result[dlen++] = K_SPECIAL;
5962 result[dlen++] = KS_SPECIAL;
5963 result[dlen++] = KE_FILLER;
5964 }
5965# ifdef FEAT_GUI
5966 else if (*src == CSI)
5967 {
5968 result[dlen++] = K_SPECIAL;
5969 result[dlen++] = KS_EXTRA;
5970 result[dlen++] = (int)KE_CSI;
5971 }
5972# endif
5973 else
5974 result[dlen++] = *src;
5975 ++src;
5976 }
5977 }
5978 result[dlen] = NUL;
5979
5980 /*
5981 * Copy the new string to allocated memory.
5982 * If this fails, just return from.
5983 */
5984 if ((*bufp = vim_strsave(result)) != NULL)
5985 from = *bufp;
5986 vim_free(result);
5987 return from;
5988}
5989
5990/*
5991 * Find a termcode with keys 'src' (must be NUL terminated).
5992 * Return the index in termcodes[], or -1 if not found.
5993 */
5994 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005995find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005998 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000 for (i = 0; i < tc_len; ++i)
6001 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006002 if (slen == termcodes[i].len
6003 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 return i;
6005 }
6006 return -1;
6007}
6008
6009/*
6010 * Gather the first characters in the terminal key codes into a string.
6011 * Used to speed up check_termcode().
6012 */
6013 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006014gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 int i;
6017 int len = 0;
6018
6019#ifdef FEAT_GUI
6020 if (gui.in_use)
6021 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6022#endif
6023#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006024 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 termleader[len++] = DCS; /* the termcode response starts with DCS
6026 in 8-bit mode */
6027#endif
6028 termleader[len] = NUL;
6029
6030 for (i = 0; i < tc_len; ++i)
6031 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6032 {
6033 termleader[len++] = termcodes[i].code[0];
6034 termleader[len] = NUL;
6035 }
6036
6037 need_gather = FALSE;
6038}
6039
6040/*
6041 * Show all termcodes (for ":set termcap")
6042 * This code looks a lot like showoptions(), but is different.
6043 */
6044 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006045show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 int col;
6048 int *items;
6049 int item_count;
6050 int run;
6051 int row, rows;
6052 int cols;
6053 int i;
6054 int len;
6055
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006056#define INC3 27 /* try to make three columns */
6057#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#define GAP 2 /* spaces between columns */
6059
6060 if (tc_len == 0) /* no terminal codes (must be GUI) */
6061 return;
6062 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6063 if (items == NULL)
6064 return;
6065
6066 /* Highlight title */
6067 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6068
6069 /*
6070 * do the loop two times:
6071 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006072 * 2. display the medium items (medium length strings)
6073 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006075 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 /*
6078 * collect the items in items[]
6079 */
6080 item_count = 0;
6081 for (i = 0; i < tc_len; i++)
6082 {
6083 len = show_one_termcode(termcodes[i].name,
6084 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006085 if (len <= INC3 - GAP ? run == 1
6086 : len <= INC2 - GAP ? run == 2
6087 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 items[item_count++] = i;
6089 }
6090
6091 /*
6092 * display the items
6093 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006094 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006096 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (cols == 0)
6098 cols = 1;
6099 rows = (item_count + cols - 1) / cols;
6100 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 rows = item_count;
6103 for (row = 0; row < rows && !got_int; ++row)
6104 {
6105 msg_putchar('\n'); /* go to next line */
6106 if (got_int) /* 'q' typed in more */
6107 break;
6108 col = 0;
6109 for (i = row; i < item_count; i += rows)
6110 {
6111 msg_col = col; /* make columns */
6112 show_one_termcode(termcodes[items[i]].name,
6113 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006114 if (run == 2)
6115 col += INC2;
6116 else
6117 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119 out_flush();
6120 ui_breakcheck();
6121 }
6122 }
6123 vim_free(items);
6124}
6125
6126/*
6127 * Show one termcode entry.
6128 * Output goes into IObuff[]
6129 */
6130 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006131show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 char_u *p;
6134 int len;
6135
6136 if (name[0] > '~')
6137 {
6138 IObuff[0] = ' ';
6139 IObuff[1] = ' ';
6140 IObuff[2] = ' ';
6141 IObuff[3] = ' ';
6142 }
6143 else
6144 {
6145 IObuff[0] = 't';
6146 IObuff[1] = '_';
6147 IObuff[2] = name[0];
6148 IObuff[3] = name[1];
6149 }
6150 IObuff[4] = ' ';
6151
6152 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6153 if (p[1] != 't')
6154 STRCPY(IObuff + 5, p);
6155 else
6156 IObuff[5] = NUL;
6157 len = (int)STRLEN(IObuff);
6158 do
6159 IObuff[len++] = ' ';
6160 while (len < 17);
6161 IObuff[len] = NUL;
6162 if (code == NULL)
6163 len += 4;
6164 else
6165 len += vim_strsize(code);
6166
6167 if (printit)
6168 {
6169 msg_puts(IObuff);
6170 if (code == NULL)
6171 msg_puts((char_u *)"NULL");
6172 else
6173 msg_outtrans(code);
6174 }
6175 return len;
6176}
6177
6178#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6179/*
6180 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6181 * termcap codes from the terminal itself.
6182 * We get them one by one to avoid a very long response string.
6183 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006184static int xt_index_in = 0;
6185static int xt_index_out = 0;
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006188req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 xt_index_out = 0;
6191 xt_index_in = 0;
6192 req_more_codes_from_term();
6193}
6194
6195 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006196req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 char buf[11];
6199 int old_idx = xt_index_out;
6200
6201 /* Don't do anything when going to exit. */
6202 if (exiting)
6203 return;
6204
6205 /* Send up to 10 more requests out than we received. Avoid sending too
6206 * many, there can be a buffer overflow somewhere. */
6207 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6208 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006209# ifdef DEBUG_TERMRESPONSE
6210 char dbuf[100];
6211
6212 sprintf(dbuf, "Requesting XT %d: %s",
6213 xt_index_out, key_names[xt_index_out]);
6214 log_tr(dbuf);
6215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 sprintf(buf, "\033P+q%02x%02x\033\\",
6217 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6218 out_str_nf((char_u *)buf);
6219 ++xt_index_out;
6220 }
6221
6222 /* Send the codes out right away. */
6223 if (xt_index_out != old_idx)
6224 out_flush();
6225}
6226
6227/*
6228 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6229 * A "0" instead of the "1" indicates a code that isn't supported.
6230 * Both <name> and <string> are encoded in hex.
6231 * "code" points to the "0" or "1".
6232 */
6233 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006234got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236#define XT_LEN 100
6237 char_u name[3];
6238 char_u str[XT_LEN];
6239 int i;
6240 int j = 0;
6241 int c;
6242
6243 /* A '1' means the code is supported, a '0' means it isn't.
6244 * When half the length is > XT_LEN we can't use it.
6245 * Our names are currently all 2 characters. */
6246 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6247 {
6248 /* Get the name from the response and find it in the table. */
6249 name[0] = hexhex2nr(code + 3);
6250 name[1] = hexhex2nr(code + 5);
6251 name[2] = NUL;
6252 for (i = 0; key_names[i] != NULL; ++i)
6253 {
6254 if (STRCMP(key_names[i], name) == 0)
6255 {
6256 xt_index_in = i;
6257 break;
6258 }
6259 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006260# ifdef DEBUG_TERMRESPONSE
6261 {
6262 char buf[100];
6263
6264 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6265 log_tr(buf);
6266 }
6267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (key_names[i] != NULL)
6269 {
6270 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6271 str[j++] = c;
6272 str[j] = NUL;
6273 if (name[0] == 'C' && name[1] == 'o')
6274 {
6275 /* Color count is not a key code. */
6276 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006277 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 else
6280 {
6281 /* First delete any existing entry with the same code. */
6282 i = find_term_bykeys(str);
6283 if (i >= 0)
6284 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006285 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287 }
6288 }
6289
6290 /* May request more codes now that we received one. */
6291 ++xt_index_in;
6292 req_more_codes_from_term();
6293}
6294
6295/*
6296 * Check if there are any unanswered requests and deal with them.
6297 * This is called before starting an external program or getting direct
6298 * keyboard input. We don't want responses to be send to that program or
6299 * handled as typed text.
6300 */
6301 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006302check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 int c;
6305
6306 /* If no codes requested or all are answered, no need to wait. */
6307 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6308 return;
6309
6310 /* Vgetc() will check for and handle any response.
6311 * Keep calling vpeekc() until we don't get any responses. */
6312 ++no_mapping;
6313 ++allow_keys;
6314 for (;;)
6315 {
6316 c = vpeekc();
6317 if (c == NUL) /* nothing available */
6318 break;
6319
6320 /* If a response is recognized it's replaced with K_IGNORE, must read
6321 * it from the input stream. If there is no K_IGNORE we can't do
6322 * anything, break here (there might be some responses further on, but
6323 * we don't want to throw away any typed chars). */
6324 if (c != K_SPECIAL && c != K_IGNORE)
6325 break;
6326 c = vgetc();
6327 if (c != K_IGNORE)
6328 {
6329 vungetc(c);
6330 break;
6331 }
6332 }
6333 --no_mapping;
6334 --allow_keys;
6335}
6336#endif
6337
6338#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6339/*
6340 * Translate an internal mapping/abbreviation representation into the
6341 * corresponding external one recognized by :map/:abbrev commands;
6342 * respects the current B/k/< settings of 'cpoption'.
6343 *
6344 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006345 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 *
6347 * It uses a growarray to build the translation string since the
6348 * latter can be wider than the original description. The caller has to
6349 * free the string afterwards.
6350 *
6351 * Returns NULL when there is a problem.
6352 */
6353 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006354translate_mapping(
6355 char_u *str,
6356 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 garray_T ga;
6359 int c;
6360 int modifiers;
6361 int cpo_bslash;
6362 int cpo_special;
6363 int cpo_keycode;
6364
6365 ga_init(&ga);
6366 ga.ga_itemsize = 1;
6367 ga.ga_growsize = 40;
6368
6369 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6370 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6371 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6372
6373 for (; *str; ++str)
6374 {
6375 c = *str;
6376 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6377 {
6378 modifiers = 0;
6379 if (str[1] == KS_MODIFIER)
6380 {
6381 str++;
6382 modifiers = *++str;
6383 c = *++str;
6384 }
6385 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6386 {
6387 int i;
6388
6389 /* try to find special key in termcodes */
6390 for (i = 0; i < tc_len; ++i)
6391 if (termcodes[i].name[0] == str[1]
6392 && termcodes[i].name[1] == str[2])
6393 break;
6394 if (i < tc_len)
6395 {
6396 ga_concat(&ga, termcodes[i].code);
6397 str += 2;
6398 continue; /* for (str) */
6399 }
6400 }
6401 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6402 {
6403 if (expmap && cpo_special)
6404 {
6405 ga_clear(&ga);
6406 return NULL;
6407 }
6408 c = TO_SPECIAL(str[1], str[2]);
6409 if (c == K_ZERO) /* display <Nul> as ^@ */
6410 c = NUL;
6411 str += 2;
6412 }
6413 if (IS_SPECIAL(c) || modifiers) /* special key */
6414 {
6415 if (expmap && cpo_special)
6416 {
6417 ga_clear(&ga);
6418 return NULL;
6419 }
6420 ga_concat(&ga, get_special_key_name(c, modifiers));
6421 continue; /* for (str) */
6422 }
6423 }
6424 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6425 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6426 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6427 if (c)
6428 ga_append(&ga, c);
6429 }
6430 ga_append(&ga, NUL);
6431 return (char_u *)(ga.ga_data);
6432}
6433#endif
6434
6435#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6436static char ksme_str[20];
6437static char ksmr_str[20];
6438static char ksmd_str[20];
6439
6440/*
6441 * For Win32 console: update termcap codes for existing console attributes.
6442 */
6443 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006444update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 struct builtin_term *p;
6447
6448 p = find_builtin_term(DEFAULT_TERM);
6449 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6450 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6451 attr | 0x08); /* FOREGROUND_INTENSITY */
6452 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6453 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6454
6455 while (p->bt_string != NULL)
6456 {
6457 if (p->bt_entry == (int)KS_ME)
6458 p->bt_string = &ksme_str[0];
6459 else if (p->bt_entry == (int)KS_MR)
6460 p->bt_string = &ksmr_str[0];
6461 else if (p->bt_entry == (int)KS_MD)
6462 p->bt_string = &ksmd_str[0];
6463 ++p;
6464 }
6465}
6466#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006467
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006468#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006469 static int
6470hex_digit(int c)
6471{
6472 if (isdigit(c))
6473 return c - '0';
6474 c = TOLOWER_ASC(c);
6475 if (c >= 'a' && c <= 'f')
6476 return c - 'a' + 10;
6477 return 0x1ffffff;
6478}
6479
6480 guicolor_T
6481gui_get_color_cmn(char_u *name)
6482{
Bram Moolenaar28726652017-01-06 18:16:19 +01006483 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6484 * values as used by the MS-Windows GDI api. It should be used only for
6485 * MS-Windows GDI builds. */
6486# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6487# undef RGB
6488# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006489# ifndef RGB
6490# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6491# endif
6492# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006493 FILE *fd;
6494 char line[LINE_LEN];
6495 char_u *fname;
6496 int r, g, b, i;
6497 guicolor_T color;
6498
6499 struct rgbcolor_table_S {
6500 char_u *color_name;
6501 guicolor_T color;
6502 };
6503
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006504 /* Only non X11 colors (not present in rgb.txt) and colors in
6505 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006506 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006507 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6508 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6509 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6510 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6511 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6512 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6513 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6514 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6515 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6516 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6517 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6518 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6519 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006520 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6521 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006522 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006523 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006524 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6525 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6526 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6527 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6528 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6529 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6530 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6531 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6532 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006533 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006534 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006535 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6536 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006537 };
6538
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006539 static struct rgbcolor_table_S *colornames_table;
6540 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006541
6542 if (name[0] == '#' && STRLEN(name) == 7)
6543 {
6544 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006545 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006546 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6547 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6548 if (color > 0xffffff)
6549 return INVALCOLOR;
6550 return color;
6551 }
6552
6553 /* Check if the name is one of the colors we know */
6554 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6555 if (STRICMP(name, rgb_table[i].color_name) == 0)
6556 return rgb_table[i].color;
6557
6558 /*
6559 * Last attempt. Look in the file "$VIM/rgb.txt".
6560 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006561 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006562 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006563 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006564
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006565 /* colornames_table not yet initialized */
6566 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6567 if (fname == NULL)
6568 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006569
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006570 fd = fopen((char *)fname, "rt");
6571 vim_free(fname);
6572 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006573 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006574 if (p_verbose > 1)
6575 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6576 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006577 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006578
6579 for (counting = 1; counting >= 0; --counting)
6580 {
6581 if (!counting)
6582 {
6583 colornames_table = (struct rgbcolor_table_S *)alloc(
6584 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6585 if (colornames_table == NULL)
6586 {
6587 fclose(fd);
6588 return INVALCOLOR;
6589 }
6590 rewind(fd);
6591 }
6592 size = 0;
6593
6594 while (!feof(fd))
6595 {
6596 size_t len;
6597 int pos;
6598
6599 ignoredp = fgets(line, LINE_LEN, fd);
6600 len = strlen(line);
6601
6602 if (len <= 1 || line[len - 1] != '\n')
6603 continue;
6604
6605 line[len - 1] = '\0';
6606
6607 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6608 if (i != 3)
6609 continue;
6610
6611 if (!counting)
6612 {
6613 char_u *s = vim_strsave((char_u *)line + pos);
6614
6615 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006616 {
6617 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006618 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006619 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006620 colornames_table[size].color_name = s;
6621 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6622 }
6623 size++;
6624 }
6625 }
6626 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006627 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006628
6629 for (i = 0; i < size; i++)
6630 if (STRICMP(name, colornames_table[i].color_name) == 0)
6631 return colornames_table[i].color;
6632
Bram Moolenaarab302212016-04-26 20:59:29 +02006633 return INVALCOLOR;
6634}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006635
6636 guicolor_T
6637gui_get_rgb_color_cmn(int r, int g, int b)
6638{
6639 guicolor_T color = RGB(r, g, b);
6640
6641 if (color > 0xffffff)
6642 return INVALCOLOR;
6643 return color;
6644}
Bram Moolenaarab302212016-04-26 20:59:29 +02006645#endif