blob: 25aaa91ddc798f61649dd512fdd96f3d5753db15 [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 Moolenaar071d4272004-06-13 20:20:40 +0000199 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# else
201 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
202 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# endif
205 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
206 /* attributes switched on with 'h', off with * 'H' */
207 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
208 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
209 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
210 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
211 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
212 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
213 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000214 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
215 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200216 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
217 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
219 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
220 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
221 {(int)KS_MS, "y"},
222 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100223 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {(int)KS_LE, "\b"}, /* cursor-left = BS */
225 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
226# ifdef TERMINFO
227 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
228# else
229 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
230# endif
231 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000232 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
234
235#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
238/*
239 * Amiga console window, default for Amiga
240 */
241 {(int)KS_NAME, "amiga"},
242 {(int)KS_CE, "\033[K"},
243 {(int)KS_CD, "\033[J"},
244 {(int)KS_AL, "\033[L"},
245# ifdef TERMINFO
246 {(int)KS_CAL, "\033[%p1%dL"},
247# else
248 {(int)KS_CAL, "\033[%dL"},
249# endif
250 {(int)KS_DL, "\033[M"},
251# ifdef TERMINFO
252 {(int)KS_CDL, "\033[%p1%dM"},
253# else
254 {(int)KS_CDL, "\033[%dM"},
255# endif
256 {(int)KS_CL, "\014"},
257 {(int)KS_VI, "\033[0 p"},
258 {(int)KS_VE, "\033[1 p"},
259 {(int)KS_ME, "\033[0m"},
260 {(int)KS_MR, "\033[7m"},
261 {(int)KS_MD, "\033[1m"},
262 {(int)KS_SE, "\033[0m"},
263 {(int)KS_SO, "\033[33m"},
264 {(int)KS_US, "\033[4m"},
265 {(int)KS_UE, "\033[0m"},
266 {(int)KS_CZH, "\033[3m"},
267 {(int)KS_CZR, "\033[0m"},
268#if defined(__MORPHOS__) || defined(__AROS__)
269 {(int)KS_CCO, "8"}, /* allow 8 colors */
270# ifdef TERMINFO
271 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
272 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
273# else
274 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
275 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
276# endif
277 {(int)KS_OP, "\033[m"}, /* reset colors */
278#endif
279 {(int)KS_MS, "y"},
280 {(int)KS_UT, "y"}, /* guessed */
281 {(int)KS_LE, "\b"},
282# ifdef TERMINFO
283 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
284# else
285 {(int)KS_CM, "\033[%i%d;%dH"},
286# endif
287#if defined(__MORPHOS__)
288 {(int)KS_SR, "\033M"},
289#endif
290# ifdef TERMINFO
291 {(int)KS_CRI, "\033[%p1%dC"},
292# else
293 {(int)KS_CRI, "\033[%dC"},
294# endif
295 {K_UP, "\233A"},
296 {K_DOWN, "\233B"},
297 {K_LEFT, "\233D"},
298 {K_RIGHT, "\233C"},
299 {K_S_UP, "\233T"},
300 {K_S_DOWN, "\233S"},
301 {K_S_LEFT, "\233 A"},
302 {K_S_RIGHT, "\233 @"},
303 {K_S_TAB, "\233Z"},
304 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
305 {K_F2, "\233\061~"},
306 {K_F3, "\233\062~"},
307 {K_F4, "\233\063~"},
308 {K_F5, "\233\064~"},
309 {K_F6, "\233\065~"},
310 {K_F7, "\233\066~"},
311 {K_F8, "\233\067~"},
312 {K_F9, "\233\070~"},
313 {K_F10, "\233\071~"},
314 {K_S_F1, "\233\061\060~"},
315 {K_S_F2, "\233\061\061~"},
316 {K_S_F3, "\233\061\062~"},
317 {K_S_F4, "\233\061\063~"},
318 {K_S_F5, "\233\061\064~"},
319 {K_S_F6, "\233\061\065~"},
320 {K_S_F7, "\233\061\066~"},
321 {K_S_F8, "\233\061\067~"},
322 {K_S_F9, "\233\061\070~"},
323 {K_S_F10, "\233\061\071~"},
324 {K_HELP, "\233?~"},
325 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
326 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
327 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
328 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
329 {K_END, "\233\064\065~"}, /* 101 key keyboard */
330
331 {BT_EXTRA_KEYS, ""},
332 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
333 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
334 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
335# endif
336
337# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
338/*
339 * almost standard ANSI terminal, default for bebox
340 */
341 {(int)KS_NAME, "beos-ansi"},
342 {(int)KS_CE, "\033[K"},
343 {(int)KS_CD, "\033[J"},
344 {(int)KS_AL, "\033[L"},
345# ifdef TERMINFO
346 {(int)KS_CAL, "\033[%p1%dL"},
347# else
348 {(int)KS_CAL, "\033[%dL"},
349# endif
350 {(int)KS_DL, "\033[M"},
351# ifdef TERMINFO
352 {(int)KS_CDL, "\033[%p1%dM"},
353# else
354 {(int)KS_CDL, "\033[%dM"},
355# endif
356#ifdef BEOS_PR_OR_BETTER
357# ifdef TERMINFO
358 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
359# else
360 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
361# endif
362#endif
363 {(int)KS_CL, "\033[H\033[2J"},
364#ifdef notyet
365 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
366 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
367#endif
368 {(int)KS_ME, "\033[m"}, /* normal mode */
369 {(int)KS_MR, "\033[7m"}, /* reverse */
370 {(int)KS_MD, "\033[1m"}, /* bold */
371 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
372 {(int)KS_SE, "\033[m"}, /* standout end */
373 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
374 {(int)KS_CZR, "\033[m"}, /* italic end */
375 {(int)KS_US, "\033[4m"}, /* underscore mode */
376 {(int)KS_UE, "\033[m"}, /* underscore end */
377 {(int)KS_CCO, "8"}, /* allow 8 colors */
378# ifdef TERMINFO
379 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
380 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
381# else
382 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
383 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
384# endif
385 {(int)KS_OP, "\033[m"}, /* reset colors */
386 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
387 {(int)KS_UT, "y"}, /* guessed */
388 {(int)KS_LE, "\b"},
389# ifdef TERMINFO
390 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
391# else
392 {(int)KS_CM, "\033[%i%d;%dH"},
393# endif
394 {(int)KS_SR, "\033M"},
395# ifdef TERMINFO
396 {(int)KS_CRI, "\033[%p1%dC"},
397# else
398 {(int)KS_CRI, "\033[%dC"},
399# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200400# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404 {K_UP, "\033[A"},
405 {K_DOWN, "\033[B"},
406 {K_LEFT, "\033[D"},
407 {K_RIGHT, "\033[C"},
408# endif
409
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200410# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411/*
412 * standard ANSI terminal, default for unix
413 */
414 {(int)KS_NAME, "ansi"},
415 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
416 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
417# ifdef TERMINFO
418 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
419# else
420 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
421# endif
422 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
423# ifdef TERMINFO
424 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
425# else
426 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
427# endif
428 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
429 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
430 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
431 {(int)KS_MS, "y"},
432 {(int)KS_UT, "y"}, /* guessed */
433 {(int)KS_LE, "\b"},
434# ifdef TERMINFO
435 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
436# else
437 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
438# endif
439# ifdef TERMINFO
440 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
441# else
442 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
443# endif
444# endif
445
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200446# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447/*
448 * These codes are valid when nansi.sys or equivalent has been installed.
449 * Function keys on a PC are preceded with a NUL. These are converted into
450 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
451 * CTRL-arrow is used instead of SHIFT-arrow.
452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {(int)KS_NAME, "pcansi"},
454 {(int)KS_DL, "\033[M"},
455 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {(int)KS_CE, "\033[K"},
457 {(int)KS_CL, "\033[2J"},
458 {(int)KS_ME, "\033[0m"},
459 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
460 {(int)KS_MD, "\033[1m"}, /* bold: white text */
461 {(int)KS_SE, "\033[0m"}, /* standout end */
462 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
463 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
464 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
465 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
466 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
467 {(int)KS_CCO, "8"}, /* allow 8 colors */
468# ifdef TERMINFO
469 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
470 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
471# else
472 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
473 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
474# endif
475 {(int)KS_OP, "\033[0m"}, /* reset colors */
476 {(int)KS_MS, "y"},
477 {(int)KS_UT, "y"}, /* guessed */
478 {(int)KS_LE, "\b"},
479# ifdef TERMINFO
480 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
481# else
482 {(int)KS_CM, "\033[%i%d;%dH"},
483# endif
484# ifdef TERMINFO
485 {(int)KS_CRI, "\033[%p1%dC"},
486# else
487 {(int)KS_CRI, "\033[%dC"},
488# endif
489 {K_UP, "\316H"},
490 {K_DOWN, "\316P"},
491 {K_LEFT, "\316K"},
492 {K_RIGHT, "\316M"},
493 {K_S_LEFT, "\316s"},
494 {K_S_RIGHT, "\316t"},
495 {K_F1, "\316;"},
496 {K_F2, "\316<"},
497 {K_F3, "\316="},
498 {K_F4, "\316>"},
499 {K_F5, "\316?"},
500 {K_F6, "\316@"},
501 {K_F7, "\316A"},
502 {K_F8, "\316B"},
503 {K_F9, "\316C"},
504 {K_F10, "\316D"},
505 {K_F11, "\316\205"}, /* guessed */
506 {K_F12, "\316\206"}, /* guessed */
507 {K_S_F1, "\316T"},
508 {K_S_F2, "\316U"},
509 {K_S_F3, "\316V"},
510 {K_S_F4, "\316W"},
511 {K_S_F5, "\316X"},
512 {K_S_F6, "\316Y"},
513 {K_S_F7, "\316Z"},
514 {K_S_F8, "\316["},
515 {K_S_F9, "\316\\"},
516 {K_S_F10, "\316]"},
517 {K_S_F11, "\316\207"}, /* guessed */
518 {K_S_F12, "\316\210"}, /* guessed */
519 {K_INS, "\316R"},
520 {K_DEL, "\316S"},
521 {K_HOME, "\316G"},
522 {K_END, "\316O"},
523 {K_PAGEDOWN, "\316Q"},
524 {K_PAGEUP, "\316I"},
525# endif
526
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200527# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528/*
529 * These codes are valid for the Win32 Console . The entries that start with
530 * ESC | are translated into console calls in os_win32.c. The function keys
531 * are also translated in os_win32.c.
532 */
533 {(int)KS_NAME, "win32"},
534 {(int)KS_CE, "\033|K"}, /* clear to end of line */
535 {(int)KS_AL, "\033|L"}, /* add new blank line */
536# ifdef TERMINFO
537 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
538# else
539 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
540# endif
541 {(int)KS_DL, "\033|M"}, /* delete line */
542# ifdef TERMINFO
543 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
544# else
545 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
546# endif
547 {(int)KS_CL, "\033|J"}, /* clear screen */
548 {(int)KS_CD, "\033|j"}, /* clear to end of display */
549 {(int)KS_VI, "\033|v"}, /* cursor invisible */
550 {(int)KS_VE, "\033|V"}, /* cursor visible */
551
552 {(int)KS_ME, "\033|0m"}, /* normal */
553 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
554 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
555#if 1
556 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
557 {(int)KS_SE, "\033|0m"}, /* standout end */
558#else
559 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
560 {(int)KS_SE, "\033|f"}, /* standout end */
561#endif
562 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
563 {(int)KS_CZR, "\033|0m"}, /* italic end */
564 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
565 {(int)KS_UE, "\033|0m"}, /* underscore end */
566 {(int)KS_CCO, "16"}, /* allow 16 colors */
567# ifdef TERMINFO
568 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
569 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
570# else
571 {(int)KS_CAB, "\033|%db"}, /* set background color */
572 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
573# endif
574
575 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
576 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100577 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {(int)KS_LE, "\b"},
579# ifdef TERMINFO
580 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
581# else
582 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
583# endif
584 {(int)KS_VB, "\033|B"}, /* visual bell */
585 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
586 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
587# ifdef TERMINFO
588 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
589# else
590 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
591# endif
592
593 {K_UP, "\316H"},
594 {K_DOWN, "\316P"},
595 {K_LEFT, "\316K"},
596 {K_RIGHT, "\316M"},
597 {K_S_UP, "\316\304"},
598 {K_S_DOWN, "\316\317"},
599 {K_S_LEFT, "\316\311"},
600 {K_C_LEFT, "\316s"},
601 {K_S_RIGHT, "\316\313"},
602 {K_C_RIGHT, "\316t"},
603 {K_S_TAB, "\316\017"},
604 {K_F1, "\316;"},
605 {K_F2, "\316<"},
606 {K_F3, "\316="},
607 {K_F4, "\316>"},
608 {K_F5, "\316?"},
609 {K_F6, "\316@"},
610 {K_F7, "\316A"},
611 {K_F8, "\316B"},
612 {K_F9, "\316C"},
613 {K_F10, "\316D"},
614 {K_F11, "\316\205"},
615 {K_F12, "\316\206"},
616 {K_S_F1, "\316T"},
617 {K_S_F2, "\316U"},
618 {K_S_F3, "\316V"},
619 {K_S_F4, "\316W"},
620 {K_S_F5, "\316X"},
621 {K_S_F6, "\316Y"},
622 {K_S_F7, "\316Z"},
623 {K_S_F8, "\316["},
624 {K_S_F9, "\316\\"},
625 {K_S_F10, "\316]"},
626 {K_S_F11, "\316\207"},
627 {K_S_F12, "\316\210"},
628 {K_INS, "\316R"},
629 {K_DEL, "\316S"},
630 {K_HOME, "\316G"},
631 {K_S_HOME, "\316\302"},
632 {K_C_HOME, "\316w"},
633 {K_END, "\316O"},
634 {K_S_END, "\316\315"},
635 {K_C_END, "\316u"},
636 {K_PAGEDOWN, "\316Q"},
637 {K_PAGEUP, "\316I"},
638 {K_KPLUS, "\316N"},
639 {K_KMINUS, "\316J"},
640 {K_KMULTIPLY, "\316\067"},
641 {K_K0, "\316\332"},
642 {K_K1, "\316\336"},
643 {K_K2, "\316\342"},
644 {K_K3, "\316\346"},
645 {K_K4, "\316\352"},
646 {K_K5, "\316\356"},
647 {K_K6, "\316\362"},
648 {K_K7, "\316\366"},
649 {K_K8, "\316\372"},
650 {K_K9, "\316\376"},
651# endif
652
653# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
654/*
655 * VT320 is working as an ANSI terminal compatible DEC terminal.
656 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
657 * Note: K_F1...K_F5 are for internal use, should not be defined.
658 * TODO:- rewrite ESC[ codes to CSI
659 * - keyboard languages (CSI ? 26 n)
660 */
661 {(int)KS_NAME, "vt320"},
662 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
663 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
664# ifdef TERMINFO
665 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
666# else
667 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
668# endif
669 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
670# ifdef TERMINFO
671 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
672# else
673 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
674# endif
675 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000676 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
677 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
679 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000680 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
681 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
682 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
683 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
684 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
685 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
686 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
687 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
688 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
689 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {(int)KS_MS, "y"},
691 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100692 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {(int)KS_LE, "\b"},
694# ifdef TERMINFO
695 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
696 ESC_STR "[%i%p1%d;%p2%dH")},
697# else
698 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
699# endif
700# ifdef TERMINFO
701 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
702# else
703 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
704# endif
705 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
706 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
707 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
708 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000709 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
710 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
711 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
712 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
713 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
715 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
716 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
717 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
718 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
721 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
722 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
723 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
724 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
725 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
726 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
727 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
728 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
729 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
730 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
731 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
732 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
733 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
734 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
735 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
736 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
737 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
738 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
739 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
740 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
741# endif
742
743# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
744/*
745 * Ordinary vt52
746 */
747 {(int)KS_NAME, "vt52"},
748 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
749 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100750# ifdef TERMINFO
751 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
752 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
753# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100757 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
759 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {K_UP, IF_EB("\033A", ESC_STR "A")},
761 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
762 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
763 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100764 {K_F1, IF_EB("\033P", ESC_STR "P")},
765 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
766 {K_F3, IF_EB("\033R", ESC_STR "R")},
767# ifdef __MINT__
768 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
769 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
770 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
771 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
772 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
774 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
775 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
776 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_F4, IF_EB("\033S", ESC_STR "S")},
778 {K_F5, IF_EB("\033T", ESC_STR "T")},
779 {K_F6, IF_EB("\033U", ESC_STR "U")},
780 {K_F7, IF_EB("\033V", ESC_STR "V")},
781 {K_F8, IF_EB("\033W", ESC_STR "W")},
782 {K_F9, IF_EB("\033X", ESC_STR "X")},
783 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
784 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
785 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
786 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
787 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
788 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
789 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
790 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
791 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
792 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
793 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
794 {K_INS, IF_EB("\033I", ESC_STR "I")},
795 {K_HOME, IF_EB("\033E", ESC_STR "E")},
796 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
797 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
798# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {(int)KS_MS, "y"},
801# endif
802# endif
803
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200804# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200805 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
807 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
808# ifdef TERMINFO
809 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
810# else
811 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
812# endif
813 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
814# ifdef TERMINFO
815 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
816# else
817 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
818# endif
819# ifdef TERMINFO
820 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
821 ESC_STR "[%i%p1%d;%p2%dr")},
822# else
823 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
824# endif
825 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
826 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
827 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
828 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
829 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
830 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
831 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200832 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
833 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {(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 Moolenaar4033c552017-09-16 20:54:51 +02001120# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001122# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124# endif
1125# ifdef TERMINFO
1126 {(int)KS_CAB, "[CAB%p1%d]"},
1127 {(int)KS_CAF, "[CAF%p1%d]"},
1128 {(int)KS_CSB, "[CSB%p1%d]"},
1129 {(int)KS_CSF, "[CSF%p1%d]"},
1130# else
1131 {(int)KS_CAB, "[CAB%d]"},
1132 {(int)KS_CAF, "[CAF%d]"},
1133 {(int)KS_CSB, "[CSB%d]"},
1134 {(int)KS_CSF, "[CSF%d]"},
1135# endif
1136 {(int)KS_OP, "[OP]"},
1137 {(int)KS_LE, "[LE]"},
1138 {(int)KS_CL, "[CL]"},
1139 {(int)KS_VI, "[VI]"},
1140 {(int)KS_VE, "[VE]"},
1141 {(int)KS_VS, "[VS]"},
1142 {(int)KS_ME, "[ME]"},
1143 {(int)KS_MR, "[MR]"},
1144 {(int)KS_MB, "[MB]"},
1145 {(int)KS_MD, "[MD]"},
1146 {(int)KS_SE, "[SE]"},
1147 {(int)KS_SO, "[SO]"},
1148 {(int)KS_UE, "[UE]"},
1149 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001150 {(int)KS_UCE, "[UCE]"},
1151 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001152 {(int)KS_STE, "[STE]"},
1153 {(int)KS_STS, "[STS]"},
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"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001598 {KS_STE,"Te"}, {KS_STS,"Ts"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001599 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1601 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1602 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1603 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1604 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001605 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001607 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {KS_TS, "ts"}, {KS_FS, "fs"},
1609 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001610 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001611 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001612 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001613 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1614 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {(enum SpecialKey)0, NULL}
1616 };
1617
1618 /*
1619 * If the external termcap does not have a matching entry, try the
1620 * builtin ones.
1621 */
1622 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1623 {
1624 tp = tstrbuf;
1625 if (!termcap_cleared)
1626 {
1627 clear_termoptions(); /* clear old options */
1628 termcap_cleared = TRUE;
1629 }
1630
1631 /* get output strings */
1632 for (i = 0; string_names[i].name != NULL; ++i)
1633 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001634 if (TERM_STR(string_names[i].dest) == NULL
1635 || TERM_STR(string_names[i].dest) == empty_option)
1636 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 TGETSTR(string_names[i].name, &tp);
1638 }
1639
1640 /* tgetflag() returns 1 if the flag is present, 0 if not and
1641 * possibly -1 if the flag doesn't exist. */
1642 if ((T_MS == NULL || T_MS == empty_option)
1643 && tgetflag("ms") > 0)
1644 T_MS = (char_u *)"y";
1645 if ((T_XS == NULL || T_XS == empty_option)
1646 && tgetflag("xs") > 0)
1647 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001648 if ((T_XN == NULL || T_XN == empty_option)
1649 && tgetflag("xn") > 0)
1650 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if ((T_DB == NULL || T_DB == empty_option)
1652 && tgetflag("db") > 0)
1653 T_DB = (char_u *)"y";
1654 if ((T_DA == NULL || T_DA == empty_option)
1655 && tgetflag("da") > 0)
1656 T_DA = (char_u *)"y";
1657 if ((T_UT == NULL || T_UT == empty_option)
1658 && tgetflag("ut") > 0)
1659 T_UT = (char_u *)"y";
1660
1661
1662 /*
1663 * get key codes
1664 */
1665 for (i = 0; key_names[i] != NULL; ++i)
1666 {
1667 if (find_termcode((char_u *)key_names[i]) == NULL)
1668 {
1669 p = TGETSTR(key_names[i], &tp);
1670 /* if cursor-left == backspace, ignore it (televideo
1671 * 925) */
1672 if (p != NULL
1673 && (*p != Ctrl_H
1674 || key_names[i][0] != 'k'
1675 || key_names[i][1] != 'l'))
1676 add_termcode((char_u *)key_names[i], p, FALSE);
1677 }
1678 }
1679
1680 if (height == 0)
1681 height = tgetnum("li");
1682 if (width == 0)
1683 width = tgetnum("co");
1684
1685 /*
1686 * Get number of colors (if not done already).
1687 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001688 if (TERM_STR(KS_CCO) == NULL
1689 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 set_color_count(tgetnum("Co"));
1691
1692# ifndef hpux
1693 BC = (char *)TGETSTR("bc", &tp);
1694 UP = (char *)TGETSTR("up", &tp);
1695 p = TGETSTR("pc", &tp);
1696 if (p)
1697 PC = *p;
1698# endif /* hpux */
1699 }
1700 }
1701 else /* try == 0 || try == 2 */
1702#endif /* HAVE_TGETENT */
1703 /*
1704 * Use builtin termcap
1705 */
1706 {
1707#ifdef HAVE_TGETENT
1708 /*
1709 * If builtin termcap was already used, there is no need to search
1710 * for the builtin termcap again, quit now.
1711 */
1712 if (try == 2 && builtin_first && termcap_cleared)
1713 break;
1714#endif
1715 /*
1716 * search for 'term' in builtin_termcaps[]
1717 */
1718 termp = find_builtin_term(term);
1719 if (termp->bt_string == NULL) /* did not find it */
1720 {
1721#ifdef HAVE_TGETENT
1722 /*
1723 * If try == 0, first try the external termcap. If that is not
1724 * found we'll get back here with try == 2.
1725 * If termcap_cleared is set we used the external termcap,
1726 * don't complain about not finding the term in the builtin
1727 * termcap.
1728 */
1729 if (try == 0) /* try external one */
1730 continue;
1731 if (termcap_cleared) /* found in external termcap */
1732 break;
1733#endif
1734
1735 mch_errmsg("\r\n");
1736 if (error_msg != NULL)
1737 {
1738 mch_errmsg((char *)error_msg);
1739 mch_errmsg("\r\n");
1740 }
1741 mch_errmsg("'");
1742 mch_errmsg((char *)term);
1743 mch_errmsg(_("' not known. Available builtin terminals are:"));
1744 mch_errmsg("\r\n");
1745 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1746 ++termp)
1747 {
1748 if (termp->bt_entry == (int)KS_NAME)
1749 {
1750#ifdef HAVE_TGETENT
1751 mch_errmsg(" builtin_");
1752#else
1753 mch_errmsg(" ");
1754#endif
1755 mch_errmsg(termp->bt_string);
1756 mch_errmsg("\r\n");
1757 }
1758 }
1759 /* when user typed :set term=xxx, quit here */
1760 if (starting != NO_SCREEN)
1761 {
1762 screen_start(); /* don't know where cursor is now */
1763 wait_return(TRUE);
1764 return FAIL;
1765 }
1766 term = DEFAULT_TERM;
1767 mch_errmsg(_("defaulting to '"));
1768 mch_errmsg((char *)term);
1769 mch_errmsg("'\r\n");
1770 if (emsg_silent == 0)
1771 {
1772 screen_start(); /* don't know where cursor is now */
1773 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001774 if (!is_not_a_term())
1775 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001777 set_string_option_direct((char_u *)"term", -1, term,
1778 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 display_errors();
1780 }
1781 out_flush();
1782#ifdef HAVE_TGETENT
1783 if (!termcap_cleared)
1784 {
1785#endif
1786 clear_termoptions(); /* clear old options */
1787#ifdef HAVE_TGETENT
1788 termcap_cleared = TRUE;
1789 }
1790#endif
1791 parse_builtin_tcap(term);
1792#ifdef FEAT_GUI
1793 if (term_is_gui(term))
1794 {
1795 out_flush();
1796 gui_init();
1797 /* If starting the GUI failed, don't do any of the other
1798 * things for this terminal */
1799 if (!gui.in_use)
1800 return FAIL;
1801#ifdef HAVE_TGETENT
1802 break; /* don't try using external termcap */
1803#endif
1804 }
1805#endif /* FEAT_GUI */
1806 }
1807#ifdef HAVE_TGETENT
1808 }
1809#endif
1810
1811/*
1812 * special: There is no info in the termcap about whether the cursor
1813 * positioning is relative to the start of the screen or to the start of the
1814 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1815 * relative.
1816 */
1817 if (STRCMP(term, "pcterm") == 0)
1818 T_CCS = (char_u *)"yes";
1819 else
1820 T_CCS = empty_option;
1821
1822#ifdef UNIX
1823/*
1824 * Any "stty" settings override the default for t_kb from the termcap.
1825 * This is in os_unix.c, because it depends a lot on the version of unix that
1826 * is being used.
1827 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1828 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001829# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 get_stty();
1833#endif
1834
1835/*
1836 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1837 * didn't work, use the default CTRL-H
1838 * The default for t_kD is DEL, unless t_kb is DEL.
1839 * The vim_strsave'd strings are probably lost forever, well it's only two
1840 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1841 * directly.
1842 */
1843#ifdef FEAT_GUI
1844 if (!gui.in_use)
1845#endif
1846 {
1847 bs_p = find_termcode((char_u *)"kb");
1848 del_p = find_termcode((char_u *)"kD");
1849 if (bs_p == NULL || *bs_p == NUL)
1850 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1851 if ((del_p == NULL || *del_p == NUL) &&
1852 (bs_p == NULL || *bs_p != DEL))
1853 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1854 }
1855
1856#if defined(UNIX) || defined(VMS)
1857 term_is_xterm = vim_is_xterm(term);
1858#endif
1859
1860#ifdef FEAT_MOUSE
1861# if defined(UNIX) || defined(VMS)
1862# ifdef FEAT_MOUSE_TTY
1863 /*
1864 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1865 * The termcode for the mouse is added as a side effect in option.c.
1866 */
1867 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001868 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001871 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 if (use_xterm_mouse())
1874 p = NULL; /* keep existing value, might be "xterm2" */
1875 else
1876 p = (char_u *)"xterm";
1877 }
1878# endif
1879 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001882 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1883 * "xterm2" in check_termcode(). */
1884 reset_option_was_set((char_u *)"ttym");
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (p == NULL
1887# ifdef FEAT_GUI
1888 || gui.in_use
1889# endif
1890 )
1891 check_mouse_termcode(); /* set mouse termcode anyway */
1892 }
1893# endif
1894# else
1895 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1896# endif
1897#endif /* FEAT_MOUSE */
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#ifdef USE_TERM_CONSOLE
1900 /* DEFAULT_TERM indicates that it is the machine console. */
1901 if (STRCMP(term, DEFAULT_TERM) != 0)
1902 term_console = FALSE;
1903 else
1904 {
1905 term_console = TRUE;
1906# ifdef AMIGA
1907 win_resize_on(); /* enable window resizing reports */
1908# endif
1909 }
1910#endif
1911
1912#if defined(UNIX) || defined(VMS)
1913 /*
1914 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1915 */
1916 if (vim_is_fastterm(term))
1917 p_tf = TRUE;
1918#endif
1919#ifdef USE_TERM_CONSOLE
1920 /*
1921 * 'ttyfast' is default on consoles
1922 */
1923 if (term_console)
1924 p_tf = TRUE;
1925#endif
1926
1927 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1928
1929 full_screen = TRUE; /* we can use termcap codes from now on */
1930 set_term_defaults(); /* use current values as defaults */
1931#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001932 LOG_TR("setting crv_status to STATUS_GET");
1933 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#endif
1935
1936 /*
1937 * Initialize the terminal with the appropriate termcap codes.
1938 * Set the mouse and window title if possible.
1939 * Don't do this when starting, need to parse the .vimrc first, because it
1940 * may redefine t_TI etc.
1941 */
1942 if (starting != NO_SCREEN)
1943 {
1944 starttermcap(); /* may change terminal mode */
1945#ifdef FEAT_MOUSE
1946 setmouse(); /* may start using the mouse */
1947#endif
1948#ifdef FEAT_TITLE
1949 maketitle(); /* may display window title */
1950#endif
1951 }
1952
1953 /* display initial screen after ttest() checking. jw. */
1954 if (width <= 0 || height <= 0)
1955 {
1956 /* termcap failed to report size */
1957 /* set defaults, in case ui_get_shellsize() also fails */
1958 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001959#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 height = 25; /* console is often 25 lines */
1961#else
1962 height = 24; /* most terminals are 24 lines */
1963#endif
1964 }
1965 set_shellsize(width, height, FALSE); /* may change Rows */
1966 if (starting != NO_SCREEN)
1967 {
1968 if (scroll_region)
1969 scroll_region_reset(); /* In case Rows changed */
1970 check_map_keycodes(); /* check mappings for terminal codes used */
1971
1972#ifdef FEAT_AUTOCMD
1973 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001974 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /*
1977 * Execute the TermChanged autocommands for each buffer that is
1978 * loaded.
1979 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001980 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001981 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 if (curbuf->b_ml.ml_mfp != NULL)
1984 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1985 curbuf);
1986 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001987 if (bufref_valid(&old_curbuf))
1988 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990#endif
1991 }
1992
1993#ifdef FEAT_TERMRESPONSE
1994 may_req_termresponse();
1995#endif
1996
1997 return OK;
1998}
1999
2000#if defined(FEAT_MOUSE) || defined(PROTO)
2001
2002# ifdef FEAT_MOUSE_TTY
2003# define HMT_NORMAL 1
2004# define HMT_NETTERM 2
2005# define HMT_DEC 4
2006# define HMT_JSBTERM 8
2007# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002008# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002009# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002010# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011static int has_mouse_termcode = 0;
2012# endif
2013
Bram Moolenaar864207d2008-06-24 22:14:38 +00002014# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002016set_mouse_termcode(
2017 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2018 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 char_u name[2];
2021
2022 name[0] = n;
2023 name[1] = KE_FILLER;
2024 add_termcode(name, s, FALSE);
2025# ifdef FEAT_MOUSE_TTY
2026# ifdef FEAT_MOUSE_JSB
2027 if (n == KS_JSBTERM_MOUSE)
2028 has_mouse_termcode |= HMT_JSBTERM;
2029 else
2030# endif
2031# ifdef FEAT_MOUSE_NET
2032 if (n == KS_NETTERM_MOUSE)
2033 has_mouse_termcode |= HMT_NETTERM;
2034 else
2035# endif
2036# ifdef FEAT_MOUSE_DEC
2037 if (n == KS_DEC_MOUSE)
2038 has_mouse_termcode |= HMT_DEC;
2039 else
2040# endif
2041# ifdef FEAT_MOUSE_PTERM
2042 if (n == KS_PTERM_MOUSE)
2043 has_mouse_termcode |= HMT_PTERM;
2044 else
2045# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002046# ifdef FEAT_MOUSE_URXVT
2047 if (n == KS_URXVT_MOUSE)
2048 has_mouse_termcode |= HMT_URXVT;
2049 else
2050# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002051# ifdef FEAT_MOUSE_SGR
2052 if (n == KS_SGR_MOUSE)
2053 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002054 else if (n == KS_SGR_MOUSE_RELEASE)
2055 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002056 else
2057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 has_mouse_termcode |= HMT_NORMAL;
2059# endif
2060}
2061# endif
2062
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002063# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002064 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002066del_mouse_termcode(
2067 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 char_u name[2];
2070
2071 name[0] = n;
2072 name[1] = KE_FILLER;
2073 del_termcode(name);
2074# ifdef FEAT_MOUSE_TTY
2075# ifdef FEAT_MOUSE_JSB
2076 if (n == KS_JSBTERM_MOUSE)
2077 has_mouse_termcode &= ~HMT_JSBTERM;
2078 else
2079# endif
2080# ifdef FEAT_MOUSE_NET
2081 if (n == KS_NETTERM_MOUSE)
2082 has_mouse_termcode &= ~HMT_NETTERM;
2083 else
2084# endif
2085# ifdef FEAT_MOUSE_DEC
2086 if (n == KS_DEC_MOUSE)
2087 has_mouse_termcode &= ~HMT_DEC;
2088 else
2089# endif
2090# ifdef FEAT_MOUSE_PTERM
2091 if (n == KS_PTERM_MOUSE)
2092 has_mouse_termcode &= ~HMT_PTERM;
2093 else
2094# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002095# ifdef FEAT_MOUSE_URXVT
2096 if (n == KS_URXVT_MOUSE)
2097 has_mouse_termcode &= ~HMT_URXVT;
2098 else
2099# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002100# ifdef FEAT_MOUSE_SGR
2101 if (n == KS_SGR_MOUSE)
2102 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002103 else if (n == KS_SGR_MOUSE_RELEASE)
2104 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002105 else
2106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 has_mouse_termcode &= ~HMT_NORMAL;
2108# endif
2109}
2110# endif
2111#endif
2112
2113#ifdef HAVE_TGETENT
2114/*
2115 * Call tgetent()
2116 * Return error message if it fails, NULL if it's OK.
2117 */
2118 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002119tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 int i;
2122
2123 i = TGETENT(tbuf, term);
2124 if (i < 0 /* -1 is always an error */
2125# ifdef TGETENT_ZERO_ERR
2126 || i == 0 /* sometimes zero is also an error */
2127# endif
2128 )
2129 {
2130 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2131 * tgetent() with the always existing "dumb" entry to avoid a crash or
2132 * hang. */
2133 (void)TGETENT(tbuf, "dumb");
2134
2135 if (i < 0)
2136# ifdef TGETENT_ZERO_ERR
2137 return (char_u *)_("E557: Cannot open termcap file");
2138 if (i == 0)
2139# endif
2140#ifdef TERMINFO
2141 return (char_u *)_("E558: Terminal entry not found in terminfo");
2142#else
2143 return (char_u *)_("E559: Terminal entry not found in termcap");
2144#endif
2145 }
2146 return NULL;
2147}
2148
2149/*
2150 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2151 * Fix that here.
2152 */
2153 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002154vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 char *p;
2157
2158 p = tgetstr(s, (char **)pp);
2159 if (p == (char *)-1)
2160 p = NULL;
2161 return (char_u *)p;
2162}
2163#endif /* HAVE_TGETENT */
2164
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002165#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166/*
2167 * Get Columns and Rows from the termcap. Used after a window signal if the
2168 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2169 * and "li" entries never change. But on some systems this works.
2170 * Errors while getting the entries are ignored.
2171 */
2172 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002173getlinecol(
2174 long *cp, /* pointer to columns */
2175 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 char_u tbuf[TBUFSZ];
2178
2179 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2180 {
2181 if (*cp == 0)
2182 *cp = tgetnum("co");
2183 if (*rp == 0)
2184 *rp = tgetnum("li");
2185 }
2186}
2187#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2188
2189/*
2190 * Get a string entry from the termcap and add it to the list of termcodes.
2191 * Used for <t_xx> special keys.
2192 * Give an error message for failure when not sourcing.
2193 * If force given, replace an existing entry.
2194 * Return FAIL if the entry was not found, OK if the entry was added.
2195 */
2196 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002197add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 char_u *term;
2200 int key;
2201 struct builtin_term *termp;
2202#ifdef HAVE_TGETENT
2203 char_u *string;
2204 int i;
2205 int builtin_first;
2206 char_u tbuf[TBUFSZ];
2207 char_u tstrbuf[TBUFSZ];
2208 char_u *tp = tstrbuf;
2209 char_u *error_msg = NULL;
2210#endif
2211
2212/*
2213 * If the GUI is running or will start in a moment, we only support the keys
2214 * that the GUI can produce.
2215 */
2216#ifdef FEAT_GUI
2217 if (gui.in_use || gui.starting)
2218 return gui_mch_haskey(name);
2219#endif
2220
2221 if (!force && find_termcode(name) != NULL) /* it's already there */
2222 return OK;
2223
2224 term = T_NAME;
2225 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2226 return FAIL;
2227
2228 if (term_is_builtin(term)) /* name starts with "builtin_" */
2229 {
2230 term += 8;
2231#ifdef HAVE_TGETENT
2232 builtin_first = TRUE;
2233#endif
2234 }
2235#ifdef HAVE_TGETENT
2236 else
2237 builtin_first = p_tbi;
2238#endif
2239
2240#ifdef HAVE_TGETENT
2241/*
2242 * We can get the entry from the builtin termcap and from the external one.
2243 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2244 * builtin termcap first.
2245 * If 'ttybuiltin' is off, try external termcap first.
2246 */
2247 for (i = 0; i < 2; ++i)
2248 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002249 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#endif
2251 /*
2252 * Search in builtin termcap
2253 */
2254 {
2255 termp = find_builtin_term(term);
2256 if (termp->bt_string != NULL) /* found it */
2257 {
2258 key = TERMCAP2KEY(name[0], name[1]);
2259 while (termp->bt_entry != (int)KS_NAME)
2260 {
2261 if ((int)termp->bt_entry == key)
2262 {
2263 add_termcode(name, (char_u *)termp->bt_string,
2264 term_is_8bit(term));
2265 return OK;
2266 }
2267 ++termp;
2268 }
2269 }
2270 }
2271#ifdef HAVE_TGETENT
2272 else
2273 /*
2274 * Search in external termcap
2275 */
2276 {
2277 error_msg = tgetent_error(tbuf, term);
2278 if (error_msg == NULL)
2279 {
2280 string = TGETSTR((char *)name, &tp);
2281 if (string != NULL && *string != NUL)
2282 {
2283 add_termcode(name, string, FALSE);
2284 return OK;
2285 }
2286 }
2287 }
2288 }
2289#endif
2290
2291 if (sourcing_name == NULL)
2292 {
2293#ifdef HAVE_TGETENT
2294 if (error_msg != NULL)
2295 EMSG(error_msg);
2296 else
2297#endif
2298 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2299 }
2300 return FAIL;
2301}
2302
2303 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2307}
2308
2309/*
2310 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2311 * Assume that the terminal is using 8-bit controls when the name contains
2312 * "8bit", like in "xterm-8bit".
2313 */
2314 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002315term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2318}
2319
2320/*
2321 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002322 * <Esc>[ -> CSI <M_C_[>
2323 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * <Esc>O -> <M-C-O>
2325 */
2326 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002327term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 if (*p == ESC)
2330 {
2331 if (p[1] == '[')
2332 return CSI;
2333 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002334 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (p[1] == 'O')
2336 return 0x8f;
2337 }
2338 return 0;
2339}
2340
2341#ifdef FEAT_GUI
2342 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002343term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
2345 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2346}
2347#endif
2348
2349#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2350
2351 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002352tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354 static char_u buf[16];
2355 char_u *p;
2356
2357 p = buf + 15;
2358 *p = '\0';
2359 do
2360 {
2361 --p;
2362 *p = (char_u) (i % 10 + '0');
2363 i /= 10;
2364 }
2365 while (i > 0 && p > buf);
2366 return p;
2367}
2368#endif
2369
2370#ifndef HAVE_TGETENT
2371
2372/*
2373 * minimal tgoto() implementation.
2374 * no padding and we only parse for %i %d and %+char
2375 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002376static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002378 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002379tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 static char buf[30];
2382 char *p, *s, *e;
2383
2384 if (!cm)
2385 return "OOPS";
2386 e = buf + 29;
2387 for (s = buf; s < e && *cm; cm++)
2388 {
2389 if (*cm != '%')
2390 {
2391 *s++ = *cm;
2392 continue;
2393 }
2394 switch (*++cm)
2395 {
2396 case 'd':
2397 p = (char *)tltoa((unsigned long)y);
2398 y = x;
2399 while (*p)
2400 *s++ = *p++;
2401 break;
2402 case 'i':
2403 x++;
2404 y++;
2405 break;
2406 case '+':
2407 *s++ = (char)(*++cm + y);
2408 y = x;
2409 break;
2410 case '%':
2411 *s++ = *cm;
2412 break;
2413 default:
2414 return "OOPS";
2415 }
2416 }
2417 *s = '\0';
2418 return buf;
2419}
2420
2421#endif /* HAVE_TGETENT */
2422
2423/*
2424 * Set the terminal name and initialize the terminal options.
2425 * If "name" is NULL or empty, get the terminal name from the environment.
2426 * If that fails, use the default terminal name.
2427 */
2428 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002429termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 char_u *term;
2432
2433 if (name != NULL && *name == NUL)
2434 name = NULL; /* empty name is equal to no name */
2435 term = name;
2436
2437#ifdef __BEOS__
2438 /*
2439 * TERM environment variable is normally set to 'ansi' on the Bebox;
2440 * Since the BeBox doesn't quite support full ANSI yet, we use our
2441 * own custom 'ansi-beos' termcap instead, unless the -T option has
2442 * been given on the command line.
2443 */
2444 if (term == NULL
2445 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2446 term = DEFAULT_TERM;
2447#endif
2448#ifndef MSWIN
2449 if (term == NULL)
2450 term = mch_getenv((char_u *)"TERM");
2451#endif
2452 if (term == NULL || *term == NUL)
2453 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002454 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 /* Set the default terminal name. */
2457 set_string_default("term", term);
2458 set_string_default("ttytype", term);
2459
2460 /*
2461 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2462 */
2463 set_termname(T_NAME != NULL ? T_NAME : term);
2464}
2465
2466/*
2467 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2468 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002469#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2471static char_u out_buf[OUT_SIZE + 1];
2472static int out_pos = 0; /* number of chars in out_buf */
2473
2474/*
2475 * out_flush(): flush the output buffer
2476 */
2477 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002478out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480 int len;
2481
2482 if (out_pos != 0)
2483 {
2484 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2485 len = out_pos;
2486 out_pos = 0;
2487 ui_write(out_buf, len);
2488 }
2489}
2490
2491#if defined(FEAT_MBYTE) || defined(PROTO)
2492/*
2493 * Sometimes a byte out of a multi-byte character is written with out_char().
2494 * To avoid flushing half of the character, call this function first.
2495 */
2496 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002497out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2500 out_flush();
2501}
2502#endif
2503
2504#ifdef FEAT_GUI
2505/*
2506 * out_trash(): Throw away the contents of the output buffer
2507 */
2508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002509out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 out_pos = 0;
2512}
2513#endif
2514
2515/*
2516 * out_char(c): put a byte into the output buffer.
2517 * Flush it if it becomes full.
2518 * This should not be used for outputting text on the screen (use functions
2519 * like msg_puts() and screen_putchar() for that).
2520 */
2521 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2525 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2526 out_char('\r');
2527#endif
2528
2529 out_buf[out_pos++] = c;
2530
2531 /* For testing we flush each time. */
2532 if (out_pos >= OUT_SIZE || p_wd)
2533 out_flush();
2534}
2535
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002536static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
2538/*
2539 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2540 */
2541 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002542out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2545 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2546 out_char_nf('\r');
2547#endif
2548
2549 out_buf[out_pos++] = c;
2550
2551 if (out_pos >= OUT_SIZE)
2552 out_flush();
2553}
2554
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002555#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2556 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557/*
2558 * A never-padding out_str.
2559 * use this whenever you don't want to run the string through tputs.
2560 * tputs above is harmless, but tputs from the termcap library
2561 * is likely to strip off leading digits, that it mistakes for padding
2562 * information, and "%i", "%d", etc.
2563 * This should only be used for writing terminal codes, not for outputting
2564 * normal text (use functions like msg_puts() and screen_putchar() for that).
2565 */
2566 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002567out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2570 out_flush();
2571 while (*s)
2572 out_char_nf(*s++);
2573
2574 /* For testing we write one string at a time. */
2575 if (p_wd)
2576 out_flush();
2577}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002581 * A conditional-flushing out_str, mainly for visualbell.
2582 * Handles a delay internally, because termlib may not respect the delay or do
2583 * it at the wrong time.
2584 * Note: Only for terminal strings.
2585 */
2586 void
2587out_str_cf(char_u *s)
2588{
2589 if (s != NULL && *s)
2590 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002591#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002592 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002593#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002594
2595#ifdef FEAT_GUI
2596 /* Don't use tputs() when GUI is used, ncurses crashes. */
2597 if (gui.in_use)
2598 {
2599 out_str_nf(s);
2600 return;
2601 }
2602#endif
2603 if (out_pos > OUT_SIZE - 20)
2604 out_flush();
2605#ifdef HAVE_TGETENT
2606 for (p = s; *s; ++s)
2607 {
2608 /* flush just before delay command */
2609 if (*s == '$' && *(s + 1) == '<')
2610 {
2611 char_u save_c = *s;
2612 int duration = atoi((char *)s + 2);
2613
2614 *s = NUL;
2615 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2616 *s = save_c;
2617 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002618# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002619 /* Only sleep here if we can limit this happening in
2620 * vim_beep(). */
2621 p = vim_strchr(s, '>');
2622 if (p == NULL || duration <= 0)
2623 {
2624 /* can't parse the time, don't sleep here */
2625 p = s;
2626 }
2627 else
2628 {
2629 ++p;
2630 do_sleep(duration);
2631 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002632# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002633 /* Rely on the terminal library to sleep. */
2634 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002635# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002636 break;
2637 }
2638 }
2639 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2640#else
2641 while (*s)
2642 out_char_nf(*s++);
2643#endif
2644
2645 /* For testing we write one string at a time. */
2646 if (p_wd)
2647 out_flush();
2648 }
2649}
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * out_str(s): Put a character string a byte at a time into the output buffer.
2653 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2654 * This should only be used for writing terminal codes, not for outputting
2655 * normal text (use functions like msg_puts() and screen_putchar() for that).
2656 */
2657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 if (s != NULL && *s)
2661 {
2662#ifdef FEAT_GUI
2663 /* Don't use tputs() when GUI is used, ncurses crashes. */
2664 if (gui.in_use)
2665 {
2666 out_str_nf(s);
2667 return;
2668 }
2669#endif
2670 /* avoid terminal strings being split up */
2671 if (out_pos > OUT_SIZE - 20)
2672 out_flush();
2673#ifdef HAVE_TGETENT
2674 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2675#else
2676 while (*s)
2677 out_char_nf(*s++);
2678#endif
2679
2680 /* For testing we write one string at a time. */
2681 if (p_wd)
2682 out_flush();
2683 }
2684}
2685
2686/*
2687 * cursor positioning using termcap parser. (jw)
2688 */
2689 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002690term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 OUT_STR(tgoto((char *)T_CM, col, row));
2693}
2694
2695 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 OUT_STR(tgoto((char *)T_CRI, 0, i));
2699}
2700
2701 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002702term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2705}
2706
2707 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002708term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2711}
2712
2713#if defined(HAVE_TGETENT) || defined(PROTO)
2714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 /* Can't handle a negative value here */
2718 if (x < 0)
2719 x = 0;
2720 if (y < 0)
2721 y = 0;
2722 OUT_STR(tgoto((char *)T_CWP, y, x));
2723}
2724
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002725# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2726/*
2727 * Return TRUE if we can request the terminal for a response.
2728 */
2729 static int
2730can_get_termresponse()
2731{
2732 return cur_tmode == TMODE_RAW
2733 && termcap_active
2734# ifdef UNIX
2735 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2736# endif
2737 && p_ek;
2738}
2739
2740static int winpos_x;
2741static int winpos_y;
2742static int waiting_for_winpos = FALSE;
2743
2744/*
2745 * Try getting the Vim window position from the terminal.
2746 * Returns OK or FAIL.
2747 */
2748 int
2749term_get_winpos(int *x, int *y)
2750{
2751 int count = 0;
2752
2753 if (*T_CGP == NUL || !can_get_termresponse())
2754 return FAIL;
2755 winpos_x = -1;
2756 winpos_y = -1;
2757 waiting_for_winpos = TRUE;
2758 OUT_STR(T_CGP);
2759 out_flush();
2760
2761 /* Try reading the result for 100 msec. */
2762 while (count++ < 10)
2763 {
2764 (void)vpeekc_nomap();
2765 if (winpos_x >= 0 && winpos_y >= 0)
2766 {
2767 *x = winpos_x;
2768 *y = winpos_y;
2769 waiting_for_winpos = FALSE;
2770 return OK;
2771 }
2772 ui_delay(10, FALSE);
2773 }
2774 waiting_for_winpos = FALSE;
2775 return FALSE;
2776}
2777# endif
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002780term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002782 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784#endif
2785
2786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002787term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2790 if (*T_CAF)
2791 term_color(T_CAF, n);
2792 else if (*T_CSF)
2793 term_color(T_CSF, n);
2794}
2795
2796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002797term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2800 if (*T_CAB)
2801 term_color(T_CAB, n);
2802 else if (*T_CSB)
2803 term_color(T_CSB, n);
2804}
2805
2806 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002807term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 char buf[20];
2810 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2811
2812 /* Special handling of 16 colors, because termcap can't handle it */
2813 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2814 /* Also accept CSI instead of <Esc>[ */
2815 if (n >= 8 && t_colors >= 16
2816 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2817 && s[i] != NUL
2818 && (STRCMP(s + i + 1, "%p1%dm") == 0
2819 || STRCMP(s + i + 1, "%dm") == 0)
2820 && (s[i] == '3' || s[i] == '4'))
2821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002823 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002825 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002827 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2829 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2830 : (n >= 16 ? "48;5;" : "10"));
2831 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2832 }
2833 else
2834 OUT_STR(tgoto((char *)s, 0, n));
2835}
2836
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002837#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002838
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002839#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2840#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2841#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002842
2843 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002844term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002845{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002846#define MAX_COLOR_STR_LEN 100
2847 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002848
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002849 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002850 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002851 OUT_STR(buf);
2852}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002853
2854 void
2855term_fg_rgb_color(guicolor_T rgb)
2856{
2857 term_rgb_color(T_8F, rgb);
2858}
2859
2860 void
2861term_bg_rgb_color(guicolor_T rgb)
2862{
2863 term_rgb_color(T_8B, rgb);
2864}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002865#endif
2866
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002867#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2868 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869/*
2870 * Generic function to set window title, using t_ts and t_fs.
2871 */
2872 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002873term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 /* t_ts takes one argument: column in status line */
2876 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2877 out_str_nf(title);
2878 out_str(T_FS); /* set title end */
2879 out_flush();
2880}
2881#endif
2882
2883/*
2884 * Make sure we have a valid set or terminal options.
2885 * Replace all entries that are NULL by empty_option
2886 */
2887 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002888ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002890 char_u *env_colors;
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 check_options(); /* make sure no options are NULL */
2893
2894 /*
2895 * MUST have "cm": cursor motion.
2896 */
2897 if (*T_CM == NUL)
2898 EMSG(_("E437: terminal capability \"cm\" required"));
2899
2900 /*
2901 * if "cs" defined, use a scroll region, it's faster.
2902 */
2903 if (*T_CS != NUL)
2904 scroll_region = TRUE;
2905 else
2906 scroll_region = FALSE;
2907
2908 if (pairs)
2909 {
2910 /*
2911 * optional pairs
2912 */
2913 /* TP goes to normal mode for TI (invert) and TB (bold) */
2914 if (*T_ME == NUL)
2915 T_ME = T_MR = T_MD = T_MB = empty_option;
2916 if (*T_SO == NUL || *T_SE == NUL)
2917 T_SO = T_SE = empty_option;
2918 if (*T_US == NUL || *T_UE == NUL)
2919 T_US = T_UE = empty_option;
2920 if (*T_CZH == NUL || *T_CZR == NUL)
2921 T_CZH = T_CZR = empty_option;
2922
2923 /* T_VE is needed even though T_VI is not defined */
2924 if (*T_VE == NUL)
2925 T_VI = empty_option;
2926
2927 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2928 if (*T_ME == NUL)
2929 {
2930 T_ME = T_SE;
2931 T_MR = T_SO;
2932 T_MD = T_SO;
2933 }
2934
2935 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2936 if (*T_SO == NUL)
2937 {
2938 T_SE = T_ME;
2939 if (*T_MR == NUL)
2940 T_SO = T_MD;
2941 else
2942 T_SO = T_MR;
2943 }
2944
2945 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2946 if (*T_CZH == NUL)
2947 {
2948 T_CZR = T_ME;
2949 if (*T_MR == NUL)
2950 T_CZH = T_MD;
2951 else
2952 T_CZH = T_MR;
2953 }
2954
2955 /* "Sb" and "Sf" come in pairs */
2956 if (*T_CSB == NUL || *T_CSF == NUL)
2957 {
2958 T_CSB = empty_option;
2959 T_CSF = empty_option;
2960 }
2961
2962 /* "AB" and "AF" come in pairs */
2963 if (*T_CAB == NUL || *T_CAF == NUL)
2964 {
2965 T_CAB = empty_option;
2966 T_CAF = empty_option;
2967 }
2968
2969 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2970 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002971 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 /* Set 'weirdinvert' according to value of 't_xs' */
2974 p_wiv = (*T_XS != NUL);
2975 }
2976 need_gather = TRUE;
2977
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002978 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002980 env_colors = mch_getenv((char_u *)"COLORS");
2981 if (env_colors != NULL && isdigit(*env_colors))
2982 {
2983 int colors = atoi((char *)env_colors);
2984
2985 if (colors != t_colors)
2986 set_color_count(colors);
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988}
2989
2990#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2991 || defined(PROTO)
2992/*
2993 * Represent the given long_u as individual bytes, with the most significant
2994 * byte first, and store them in dst.
2995 */
2996 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002997add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 int i;
3000 int shift;
3001
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003002 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 {
3004 shift = 8 * (sizeof(long_u) - i);
3005 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3006 }
3007}
3008
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003009static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011/*
3012 * Interpret the next string of bytes in buf as a long integer, with the most
3013 * significant byte first. Note that it is assumed that buf has been through
3014 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3015 * Puts result in val, and returns the number of bytes read from buf
3016 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3017 * were present.
3018 */
3019 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003020get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 int len;
3023 char_u bytes[sizeof(long_u)];
3024 int i;
3025 int shift;
3026
3027 *val = 0;
3028 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3029 if (len != -1)
3030 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003031 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 {
3033 shift = 8 * (sizeof(long_u) - 1 - i);
3034 *val += (long_u)bytes[i] << shift;
3035 }
3036 }
3037 return len;
3038}
3039#endif
3040
3041#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003042 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3043 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3046 * that buf has been through inchar(). Returns the actual number of bytes used
3047 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3048 * available.
3049 */
3050 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003051get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 int len = 0;
3054 int i;
3055 char_u c;
3056
3057 for (i = 0; i < num_bytes; i++)
3058 {
3059 if ((c = buf[len++]) == NUL)
3060 return -1;
3061 if (c == K_SPECIAL)
3062 {
3063 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3064 return -1;
3065 if (buf[len++] == (int)KS_ZERO)
3066 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003067 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3068 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3069 if (buf[len++] == (int)KE_CSI)
3070 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003072 else if (c == CSI && buf[len] == KS_EXTRA
3073 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003074 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3075 * the start of a special key, see add_to_input_buf_csi(). */
3076 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 bytes[i] = c;
3078 }
3079 return len;
3080}
3081#endif
3082
3083/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003084 * Check if the new shell size is valid, correct it if it's too small or way
3085 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 */
3087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003088check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (Rows < min_rows()) /* need room for one window and command line */
3091 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003092 limit_screen_size();
3093}
3094
3095/*
3096 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3097 */
3098 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003099limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003100{
3101 if (Columns < MIN_COLUMNS)
3102 Columns = MIN_COLUMNS;
3103 else if (Columns > 10000)
3104 Columns = 10000;
3105 if (Rows > 1000)
3106 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107}
3108
Bram Moolenaar86b68352004-12-27 21:59:20 +00003109/*
3110 * Invoked just before the screen structures are going to be (re)allocated.
3111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003113win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 static int old_Rows = 0;
3116 static int old_Columns = 0;
3117
3118 if (old_Rows != Rows || old_Columns != Columns)
3119 ui_new_shellsize();
3120 if (old_Rows != Rows)
3121 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003122 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003123 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003124 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 old_Rows = Rows;
3126 shell_new_rows(); /* update window sizes */
3127 }
3128 if (old_Columns != Columns)
3129 {
3130 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
3133}
3134
3135/*
3136 * Call this function when the Vim shell has been resized in any way.
3137 * Will obtain the current size and redraw (also when size didn't change).
3138 */
3139 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003140shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 set_shellsize(0, 0, FALSE);
3143}
3144
3145/*
3146 * Check if the shell size changed. Handle a resize.
3147 * When the size didn't change, nothing happens.
3148 */
3149 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003150shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 int old_Rows = Rows;
3153 int old_Columns = Columns;
3154
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003155 if (!exiting
3156#ifdef FEAT_GUI
3157 /* Do not get the size when executing a shell command during
3158 * startup. */
3159 && !gui.starting
3160#endif
3161 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003162 {
3163 (void)ui_get_shellsize();
3164 check_shellsize();
3165 if (old_Rows != Rows || old_Columns != Columns)
3166 shell_resized();
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168}
3169
3170/*
3171 * Set size of the Vim shell.
3172 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3173 * window size (this is used for the :win command).
3174 * If 'mustset' is FALSE, we may try to get the real window size and if
3175 * it fails use 'width' and 'height'.
3176 */
3177 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003178set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179{
3180 static int busy = FALSE;
3181
3182 /*
3183 * Avoid recursiveness, can happen when setting the window size causes
3184 * another window-changed signal.
3185 */
3186 if (busy)
3187 return;
3188
3189 if (width < 0 || height < 0) /* just checking... */
3190 return;
3191
Bram Moolenaara971b822011-09-14 14:43:25 +02003192 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003194 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 State = SETWSIZE;
3196 return;
3197 }
3198
Bram Moolenaara971b822011-09-14 14:43:25 +02003199 /* curwin->w_buffer can be NULL when we are closing a window and the
3200 * buffer has already been closed and removing a scrollbar causes a resize
3201 * event. Don't resize then, it will happen after entering another buffer.
3202 */
3203 if (curwin->w_buffer == NULL)
3204 return;
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 ++busy;
3207
3208#ifdef AMIGA
3209 out_flush(); /* must do this before mch_get_shellsize() for
3210 some obscure reason */
3211#endif
3212
3213 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3214 {
3215 Rows = height;
3216 Columns = width;
3217 check_shellsize();
3218 ui_set_shellsize(mustset);
3219 }
3220 else
3221 check_shellsize();
3222
3223 /* The window layout used to be adjusted here, but it now happens in
3224 * screenalloc() (also invoked from screenclear()). That is because the
3225 * "busy" check above may skip this, but not screenalloc(). */
3226
3227 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3228 screenclear();
3229 else
3230 screen_start(); /* don't know where cursor is now */
3231
3232 if (starting != NO_SCREEN)
3233 {
3234#ifdef FEAT_TITLE
3235 maketitle();
3236#endif
3237 changed_line_abv_curs();
3238 invalidate_botline();
3239
3240 /*
3241 * We only redraw when it's needed:
3242 * - While at the more prompt or executing an external command, don't
3243 * redraw, but position the cursor.
3244 * - While editing the command line, only redraw that.
3245 * - in Ex mode, don't redraw anything.
3246 * - Otherwise, redraw right now, and position the cursor.
3247 * Always need to call update_screen() or screenalloc(), to make
3248 * sure Rows/Columns and the size of ScreenLines[] is correct!
3249 */
3250 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3251 || exmode_active)
3252 {
3253 screenalloc(FALSE);
3254 repeat_message();
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
3257 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003258#ifdef FEAT_SCROLLBIND
3259 if (curwin->w_p_scb)
3260 do_check_scrollbind(TRUE);
3261#endif
3262 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003263 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003264 update_screen(NOT_VALID);
3265 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003266 }
3267 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003268 {
3269 update_topline();
3270#if defined(FEAT_INS_EXPAND)
3271 if (pum_visible())
3272 {
3273 redraw_later(NOT_VALID);
3274 ins_compl_show_pum(); /* This includes the redraw. */
3275 }
3276 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003277#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003278 update_screen(NOT_VALID);
3279 if (redrawing())
3280 setcursor();
3281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283 cursor_on(); /* redrawing may have switched it off */
3284 }
3285 out_flush();
3286 --busy;
3287}
3288
3289/*
3290 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3291 * commands and Ex mode).
3292 */
3293 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003294settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296#ifdef FEAT_GUI
3297 /* don't set the term where gvim was started to any mode */
3298 if (gui.in_use)
3299 return;
3300#endif
3301
3302 if (full_screen)
3303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 /*
3305 * When returning after calling a shell we want to really set the
3306 * terminal to raw mode, even though we think it already is, because
3307 * the shell program may have reset the terminal mode.
3308 * When we think the terminal is normal, don't try to set it to
3309 * normal again, because that causes problems (logout!) on some
3310 * machines.
3311 */
3312 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3313 {
3314#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003315# ifdef FEAT_GUI
3316 if (!gui.in_use && !gui.starting)
3317# endif
3318 {
3319 /* May need to check for T_CRV response and termcodes, it
3320 * doesn't work in Cooked mode, an external program may get
3321 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003322 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3323 || u7_status == STATUS_SENT
3324 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003325 || rbm_status == STATUS_SENT
3326 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003327 (void)vpeekc_nomap();
3328 check_for_codes_from_term();
3329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331#ifdef FEAT_MOUSE_TTY
3332 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003333 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003335 if (tmode != TMODE_RAW)
3336 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003338 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 cur_tmode = tmode;
3340#ifdef FEAT_MOUSE
3341 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003342 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003344 if (tmode == TMODE_RAW)
3345 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 out_flush();
3347 }
3348#ifdef FEAT_TERMRESPONSE
3349 may_req_termresponse();
3350#endif
3351 }
3352}
3353
3354 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003355starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 if (full_screen && !termcap_active)
3358 {
3359 out_str(T_TI); /* start termcap mode */
3360 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003361 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 out_flush();
3363 termcap_active = TRUE;
3364 screen_start(); /* don't know where cursor is now */
3365#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003366# ifdef FEAT_GUI
3367 if (!gui.in_use && !gui.starting)
3368# endif
3369 {
3370 may_req_termresponse();
3371 /* Immediately check for a response. If t_Co changes, we don't
3372 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003373 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003374 check_for_codes_from_term();
3375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
3377 }
3378}
3379
3380 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003381stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 screen_stop_highlight();
3384 reset_cterm_colors();
3385 if (termcap_active)
3386 {
3387#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003388# ifdef FEAT_GUI
3389 if (!gui.in_use && !gui.starting)
3390# endif
3391 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003392 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003393 if (crv_status == STATUS_SENT
3394 || u7_status == STATUS_SENT
3395 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003396 || rbm_status == STATUS_SENT
3397 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003398 {
3399# ifdef UNIX
3400 /* Give the terminal a chance to respond. */
3401 mch_delay(100L, FALSE);
3402# endif
3403# ifdef TCIFLUSH
3404 /* Discard data received but not read. */
3405 if (exiting)
3406 tcflush(fileno(stdin), TCIFLUSH);
3407# endif
3408 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003409 /* Check for termcodes first, otherwise an external program may
3410 * get them. */
3411 check_for_codes_from_term();
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003414 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 out_str(T_KE); /* stop "keypad transmit" mode */
3416 out_flush();
3417 termcap_active = FALSE;
3418 cursor_on(); /* just in case it is still off */
3419 out_str(T_TE); /* stop termcap mode */
3420 screen_start(); /* don't know where cursor is now */
3421 out_flush();
3422 }
3423}
3424
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003425#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426/*
3427 * Request version string (for xterm) when needed.
3428 * Only do this after switching to raw mode, otherwise the result will be
3429 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003430 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003431 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 * Only do this after termcap mode has been started, otherwise the codes for
3433 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003434 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3435 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003436 * On Unix only do it when both output and input are a tty (avoid writing
3437 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 * The result is caught in check_termcode().
3439 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003441may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003443 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003444 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003445 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 && *T_CRV != NUL)
3447 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003448 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003450 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 /* check for the characters now, otherwise they might be eaten by
3452 * get_keystroke() */
3453 out_flush();
3454 (void)vpeekc_nomap();
3455 }
3456}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003457
3458# if defined(FEAT_MBYTE) || defined(PROTO)
3459/*
3460 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003461 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003462 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003463 * If the terminal treats \u25bd as single width, the position is (1, 1),
3464 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003465 * This function has the side effect that changes cursor position, so
3466 * it must be called immediately after entering termcap mode.
3467 */
3468 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003469may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003470{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003471 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003472 && can_get_termresponse()
3473 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003474 && *T_U7 != NUL
3475 && !option_was_set((char_u *)"ambiwidth"))
3476 {
3477 char_u buf[16];
3478
Bram Moolenaar2951b772013-07-03 12:45:31 +02003479 LOG_TR("Sending U7 request");
3480 /* Do this in the second row. In the first row the returned sequence
3481 * may be CSI 1;2R, which is the same as <S-F3>. */
3482 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003483 buf[mb_char2bytes(0x25bd, buf)] = 0;
3484 out_str(buf);
3485 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003486 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003487 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003488
3489 /* This overwrites a few characters on the screen, a redraw is needed
3490 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003491 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003492 out_str((char_u *)" ");
3493 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003494
Bram Moolenaar9584b312013-03-13 19:29:28 +01003495 /* check for the characters now, otherwise they might be eaten by
3496 * get_keystroke() */
3497 out_flush();
3498 (void)vpeekc_nomap();
3499 }
3500}
3501# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003502
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003503/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003504 * Similar to requesting the version string: Request the terminal background
3505 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003506 */
3507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003508may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003509{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003510 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003511 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003512 /* Only request background if t_RB is set and 'background' wasn't
3513 * changed. */
3514 if (rbg_status == STATUS_GET
3515 && *T_RBG != NUL
3516 && !option_was_set((char_u *)"bg"))
3517 {
3518 LOG_TR("Sending BG request");
3519 out_str(T_RBG);
3520 rbg_status = STATUS_SENT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003521
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003522 /* check for the characters now, otherwise they might be eaten by
3523 * get_keystroke() */
3524 out_flush();
3525 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003526 }
3527 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003528}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003529
Bram Moolenaar2951b772013-07-03 12:45:31 +02003530# ifdef DEBUG_TERMRESPONSE
3531 static void
3532log_tr(char *msg)
3533{
3534 static FILE *fd_tr = NULL;
3535 static proftime_T start;
3536 proftime_T now;
3537
3538 if (fd_tr == NULL)
3539 {
3540 fd_tr = fopen("termresponse.log", "w");
3541 profile_start(&start);
3542 }
3543 now = start;
3544 profile_end(&now);
3545 fprintf(fd_tr, "%s: %s %s\n",
3546 profile_msg(&now),
3547 must_redraw == NOT_VALID ? "NV"
3548 : must_redraw == CLEAR ? "CL" : " ",
3549 msg);
3550}
3551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
3553
3554/*
3555 * Return TRUE when saving and restoring the screen.
3556 */
3557 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003558swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 return (full_screen && *T_TI != NUL);
3561}
3562
3563#ifdef FEAT_MOUSE
3564/*
3565 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3566 */
3567 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003568setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570# ifdef FEAT_MOUSE_TTY
3571 int checkfor;
3572# endif
3573
3574# ifdef FEAT_MOUSESHAPE
3575 update_mouseshape(-1);
3576# endif
3577
3578# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3579# ifdef FEAT_GUI
3580 /* In the GUI the mouse is always enabled. */
3581 if (gui.in_use)
3582 return;
3583# endif
3584 /* be quick when mouse is off */
3585 if (*p_mouse == NUL || has_mouse_termcode == 0)
3586 return;
3587
3588 /* don't switch mouse on when not in raw mode (Ex mode) */
3589 if (cur_tmode != TMODE_RAW)
3590 {
3591 mch_setmouse(FALSE);
3592 return;
3593 }
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (VIsual_active)
3596 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003597 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 checkfor = MOUSE_RETURN;
3599 else if (State & INSERT)
3600 checkfor = MOUSE_INSERT;
3601 else if (State & CMDLINE)
3602 checkfor = MOUSE_COMMAND;
3603 else if (State == CONFIRM || State == EXTERNCMD)
3604 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3605 else
3606 checkfor = MOUSE_NORMAL; /* assume normal mode */
3607
3608 if (mouse_has(checkfor))
3609 mch_setmouse(TRUE);
3610 else
3611 mch_setmouse(FALSE);
3612# endif
3613}
3614
3615/*
3616 * Return TRUE if
3617 * - "c" is in 'mouse', or
3618 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3619 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3620 * normal editing mode (not at hit-return message).
3621 */
3622 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003623mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
3625 char_u *p;
3626
3627 for (p = p_mouse; *p; ++p)
3628 switch (*p)
3629 {
3630 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3631 return TRUE;
3632 break;
3633 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3634 return TRUE;
3635 break;
3636 default: if (c == *p) return TRUE; break;
3637 }
3638 return FALSE;
3639}
3640
3641/*
3642 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3643 */
3644 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003645mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 return (p_mousem[0] == 'p');
3648}
3649#endif
3650
3651/*
3652 * By outputting the 'cursor very visible' termcap code, for some windowed
3653 * terminals this makes the screen scrolled to the correct position.
3654 * Used when starting Vim or returning from a shell.
3655 */
3656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003657scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003659 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003662 out_str(T_CVS);
3663 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665}
3666
3667static int cursor_is_off = FALSE;
3668
3669/*
3670 * Enable the cursor.
3671 */
3672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003673cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 if (cursor_is_off)
3676 {
3677 out_str(T_VE);
3678 cursor_is_off = FALSE;
3679 }
3680}
3681
3682/*
3683 * Disable the cursor.
3684 */
3685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003686cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003688 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003690 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 cursor_is_off = TRUE;
3692 }
3693}
3694
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003695#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003697 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003698 */
3699 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003700term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003701{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003702 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003703 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003704
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003705 /* Only do something when redrawing the screen and we can restore the
3706 * mode. */
3707 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003708 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003709# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003710 if (forced && initial_cursor_shape > 0)
3711 /* Restore to initial values. */
3712 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003713# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003714 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003715 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003716
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003717 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003718 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003719 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003720 {
3721 if (*T_CSR != NUL)
3722 p = T_CSR; /* Replace mode cursor */
3723 else
3724 p = T_CSI; /* fall back to Insert mode cursor */
3725 if (*p != NUL)
3726 {
3727 out_str(p);
3728 showing_mode = REPLACE;
3729 }
3730 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003731 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003732 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003733 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003734 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003735 {
3736 out_str(T_CSI); /* Insert mode cursor */
3737 showing_mode = INSERT;
3738 }
3739 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003740 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003741 {
3742 out_str(T_CEI); /* non-Insert mode cursor */
3743 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003744 }
3745}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003746
3747# if defined(FEAT_TERMINAL) || defined(PROTO)
3748 void
3749term_cursor_color(char_u *color)
3750{
3751 if (*T_CSC != NUL)
3752 {
3753 out_str(T_CSC); /* set cursor color start */
3754 out_str_nf(color);
3755 out_str(T_CEC); /* set cursor color end */
3756 out_flush();
3757 }
3758}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003759# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003760
Bram Moolenaar4db25542017-08-28 22:43:05 +02003761 int
3762blink_state_is_inverted()
3763{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003764#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003765 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3766 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003767#else
3768 return FALSE;
3769#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003770}
3771
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003772/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003773 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003774 */
3775 void
3776term_cursor_shape(int shape, int blink)
3777{
3778 if (*T_CSH != NUL)
3779 {
3780 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3781 out_flush();
3782 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003783 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003784 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003785 int do_blink = blink;
3786
3787 /* t_SH is empty: try setting just the blink state.
3788 * The blink flags are XORed together, if the initial blinking from
3789 * style and shape differs, we need to invert the flag here. */
3790 if (blink_state_is_inverted())
3791 do_blink = !blink;
3792
3793 if (do_blink && *T_VS != NUL)
3794 {
3795 out_str(T_VS);
3796 out_flush();
3797 }
3798 else if (!do_blink && *T_CVS != NUL)
3799 {
3800 out_str(T_CVS);
3801 out_flush();
3802 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003803 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003804}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003805#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003806
3807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 * Set scrolling region for window 'wp'.
3809 * The region starts 'off' lines from the start of the window.
3810 * Also set the vertical scroll region for a vertically split window. Always
3811 * the full width of the window, excluding the vertical separator.
3812 */
3813 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003814scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3817 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (*T_CSV != NUL && wp->w_width != Columns)
3819 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3820 W_WINCOL(wp)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 screen_start(); /* don't know where cursor is now */
3822}
3823
3824/*
3825 * Reset scrolling region to the whole screen.
3826 */
3827 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003828scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (*T_CSV != NUL)
3832 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 screen_start(); /* don't know where cursor is now */
3834}
3835
3836
3837/*
3838 * List of terminal codes that are currently recognized.
3839 */
3840
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003841static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
3843 char_u name[2]; /* termcap name of entry */
3844 char_u *code; /* terminal code (in allocated memory) */
3845 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003846 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847} *termcodes = NULL;
3848
3849static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3850static int tc_len = 0; /* current number of entries in termcodes[] */
3851
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003852static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003855clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 while (tc_len > 0)
3858 vim_free(termcodes[--tc_len].code);
3859 vim_free(termcodes);
3860 termcodes = NULL;
3861 tc_max_len = 0;
3862
3863#ifdef HAVE_TGETENT
3864 BC = (char *)empty_option;
3865 UP = (char *)empty_option;
3866 PC = NUL; /* set pad character to NUL */
3867 ospeed = 0;
3868#endif
3869
3870 need_gather = TRUE; /* need to fill termleader[] */
3871}
3872
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003873#define ATC_FROM_TERM 55
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875/*
3876 * Add a new entry to the list of terminal codes.
3877 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003878 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3879 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
3881 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003882add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 struct termcode *new_tc;
3885 int i, j;
3886 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003887 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 if (string == NULL || *string == NUL)
3890 {
3891 del_termcode(name);
3892 return;
3893 }
3894
Bram Moolenaar45500912014-07-09 20:51:07 +02003895#if defined(WIN3264) && !defined(FEAT_GUI)
3896 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3897#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (s == NULL)
3901 return;
3902
3903 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003906 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 s[0] = term_7to8bit(string);
3908 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003909
3910#if defined(WIN3264) && !defined(FEAT_GUI)
3911 if (s[0] == K_NUL)
3912 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003913 STRMOVE(s + 1, s);
3914 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003915 }
3916#endif
3917
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003918 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 need_gather = TRUE; /* need to fill termleader[] */
3921
3922 /*
3923 * need to make space for more entries
3924 */
3925 if (tc_len == tc_max_len)
3926 {
3927 tc_max_len += 20;
3928 new_tc = (struct termcode *)alloc(
3929 (unsigned)(tc_max_len * sizeof(struct termcode)));
3930 if (new_tc == NULL)
3931 {
3932 tc_max_len -= 20;
3933 return;
3934 }
3935 for (i = 0; i < tc_len; ++i)
3936 new_tc[i] = termcodes[i];
3937 vim_free(termcodes);
3938 termcodes = new_tc;
3939 }
3940
3941 /*
3942 * Look for existing entry with the same name, it is replaced.
3943 * Look for an existing entry that is alphabetical higher, the new entry
3944 * is inserted in front of it.
3945 */
3946 for (i = 0; i < tc_len; ++i)
3947 {
3948 if (termcodes[i].name[0] < name[0])
3949 continue;
3950 if (termcodes[i].name[0] == name[0])
3951 {
3952 if (termcodes[i].name[1] < name[1])
3953 continue;
3954 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003955 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 if (termcodes[i].name[1] == name[1])
3958 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003959 if (flags == ATC_FROM_TERM && (j = termcode_star(
3960 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003961 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003962 /* Don't replace ESC[123;*X or ESC O*X with another when
3963 * invoked from got_code_from_term(). */
3964 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003965 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003966 && s[len - 1]
3967 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003968 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003969 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003970 vim_free(s);
3971 return;
3972 }
3973 }
3974 else
3975 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003976 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003977 vim_free(termcodes[i].code);
3978 --tc_len;
3979 break;
3980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
3983 /*
3984 * Found alphabetical larger entry, move rest to insert new entry
3985 */
3986 for (j = tc_len; j > i; --j)
3987 termcodes[j] = termcodes[j - 1];
3988 break;
3989 }
3990
3991 termcodes[i].name[0] = name[0];
3992 termcodes[i].name[1] = name[1];
3993 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003994 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995
3996 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3997 * accept modifiers. */
3998 termcodes[i].modlen = 0;
3999 j = termcode_star(s, len);
4000 if (j > 0)
4001 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 ++tc_len;
4003}
4004
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004005/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004006 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004008 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 */
4010 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004011termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004012{
4013 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4014 if (len >= 3 && code[len - 2] == '*')
4015 {
4016 if (len >= 5 && code[len - 3] == ';')
4017 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004018 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004019 return 1;
4020 }
4021 return 0;
4022}
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004025find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
4027 int i;
4028
4029 for (i = 0; i < tc_len; ++i)
4030 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4031 return termcodes[i].code;
4032 return NULL;
4033}
4034
4035#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4036 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004037get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 if (i >= tc_len)
4040 return NULL;
4041 return &termcodes[i].name[0];
4042}
4043#endif
4044
4045 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004046del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
4048 int i;
4049
4050 if (termcodes == NULL) /* nothing there yet */
4051 return;
4052
4053 need_gather = TRUE; /* need to fill termleader[] */
4054
4055 for (i = 0; i < tc_len; ++i)
4056 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4057 {
4058 del_termcode_idx(i);
4059 return;
4060 }
4061 /* not found. Give error message? */
4062}
4063
4064 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004065del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
4067 int i;
4068
4069 vim_free(termcodes[idx].code);
4070 --tc_len;
4071 for (i = idx; i < tc_len; ++i)
4072 termcodes[i] = termcodes[i + 1];
4073}
4074
4075#ifdef FEAT_TERMRESPONSE
4076/*
4077 * Called when detected that the terminal sends 8-bit codes.
4078 * Convert all 7-bit codes to their 8-bit equivalent.
4079 */
4080 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004081switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 int i;
4084 int c;
4085
4086 /* Only need to do something when not already using 8-bit codes. */
4087 if (!term_is_8bit(T_NAME))
4088 {
4089 for (i = 0; i < tc_len; ++i)
4090 {
4091 c = term_7to8bit(termcodes[i].code);
4092 if (c != 0)
4093 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004094 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 termcodes[i].code[0] = c;
4096 }
4097 }
4098 need_gather = TRUE; /* need to fill termleader[] */
4099 }
4100 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004101 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102}
4103#endif
4104
4105#ifdef CHECK_DOUBLE_CLICK
4106static linenr_T orig_topline = 0;
4107# ifdef FEAT_DIFF
4108static int orig_topfill = 0;
4109# endif
4110#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004111#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112/*
4113 * Checking for double clicks ourselves.
4114 * "orig_topline" is used to avoid detecting a double-click when the window
4115 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4116 */
4117/*
4118 * Set orig_topline. Used when jumping to another window, so that a double
4119 * click still works.
4120 */
4121 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004122set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 orig_topline = wp->w_topline;
4125# ifdef FEAT_DIFF
4126 orig_topfill = wp->w_topfill;
4127# endif
4128}
4129#endif
4130
4131/*
4132 * Check if typebuf.tb_buf[] contains a terminal key code.
4133 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4134 * + max_offset].
4135 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004136 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 * With a match, the match is removed, the replacement code is inserted in
4138 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4139 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004140 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4141 * "buflen" is then the length of the string in buf[] and is updated for
4142 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 */
4144 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004145check_termcode(
4146 int max_offset,
4147 char_u *buf,
4148 int bufsize,
4149 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 char_u *tp;
4152 char_u *p;
4153 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004156 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int offset;
4158 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004159 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004160 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004161 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int new_slen;
4163 int extra;
4164 char_u string[MAX_KEY_CODE_LEN + 1];
4165 int i, j;
4166 int idx = 0;
4167#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004168# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4169 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u bytes[6];
4171 int num_bytes;
4172# endif
4173 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 int is_click, is_drag;
4175 int wheel_code = 0;
4176 int current_button;
4177 static int held_button = MOUSE_RELEASE;
4178 static int orig_num_clicks = 1;
4179 static int orig_mouse_code = 0x0;
4180# ifdef CHECK_DOUBLE_CLICK
4181 static int orig_mouse_col = 0;
4182 static int orig_mouse_row = 0;
4183 static struct timeval orig_mouse_time = {0, 0};
4184 /* time of previous mouse click */
4185 struct timeval mouse_time; /* time of current mouse click */
4186 long timediff; /* elapsed time in msec */
4187# endif
4188#endif
4189 int cpo_koffset;
4190#ifdef FEAT_MOUSE_GPM
4191 extern int gpm_flag; /* gpm library variable */
4192#endif
4193
4194 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4195
4196 /*
4197 * Speed up the checks for terminal codes by gathering all first bytes
4198 * used in termleader[]. Often this is just a single <Esc>.
4199 */
4200 if (need_gather)
4201 gather_termleader();
4202
4203 /*
4204 * Check at several positions in typebuf.tb_buf[], to catch something like
4205 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4206 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004207 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 * This is used often, KEEP IT FAST!
4209 */
4210 for (offset = 0; offset < max_offset; ++offset)
4211 {
4212 if (buf == NULL)
4213 {
4214 if (offset >= typebuf.tb_len)
4215 break;
4216 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4217 len = typebuf.tb_len - offset; /* length of the input */
4218 }
4219 else
4220 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004221 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
4223 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004224 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226
4227 /*
4228 * Don't check characters after K_SPECIAL, those are already
4229 * translated terminal chars (avoid translating ~@^Hx).
4230 */
4231 if (*tp == K_SPECIAL)
4232 {
4233 offset += 2; /* there are always 2 extra characters */
4234 continue;
4235 }
4236
4237 /*
4238 * Skip this position if the character does not appear as the first
4239 * character in term_strings. This speeds up a lot, since most
4240 * termcodes start with the same character (ESC or CSI).
4241 */
4242 i = *tp;
4243 for (p = termleader; *p && *p != i; ++p)
4244 ;
4245 if (*p == NUL)
4246 continue;
4247
4248 /*
4249 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4250 * are in Insert mode.
4251 */
4252 if (*tp == ESC && !p_ek && (State & INSERT))
4253 continue;
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004256 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004257 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259#ifdef FEAT_GUI
4260 if (gui.in_use)
4261 {
4262 /*
4263 * GUI special key codes are all of the form [CSI xx].
4264 */
4265 if (*tp == CSI) /* Special key from GUI */
4266 {
4267 if (len < 3)
4268 return -1; /* Shouldn't happen */
4269 slen = 3;
4270 key_name[0] = tp[1];
4271 key_name[1] = tp[2];
4272 }
4273 }
4274 else
4275#endif /* FEAT_GUI */
4276 {
4277 for (idx = 0; idx < tc_len; ++idx)
4278 {
4279 /*
4280 * Ignore the entry if we are not at the start of
4281 * typebuf.tb_buf[]
4282 * and there are not enough characters to make a match.
4283 * But only when the 'K' flag is in 'cpoptions'.
4284 */
4285 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004286 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (cpo_koffset && offset && len < slen)
4288 continue;
4289 if (STRNCMP(termcodes[idx].code, tp,
4290 (size_t)(slen > len ? len : slen)) == 0)
4291 {
4292 if (len < slen) /* got a partial sequence */
4293 return -1; /* need to get more chars */
4294
4295 /*
4296 * When found a keypad key, check if there is another key
4297 * that matches and use that one. This makes <Home> to be
4298 * found instead of <kHome> when they produce the same
4299 * key code.
4300 */
4301 if (termcodes[idx].name[0] == 'K'
4302 && VIM_ISDIGIT(termcodes[idx].name[1]))
4303 {
4304 for (j = idx + 1; j < tc_len; ++j)
4305 if (termcodes[j].len == slen &&
4306 STRNCMP(termcodes[idx].code,
4307 termcodes[j].code, slen) == 0)
4308 {
4309 idx = j;
4310 break;
4311 }
4312 }
4313
4314 key_name[0] = termcodes[idx].name[0];
4315 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 break;
4317 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004318
4319 /*
4320 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004321 * <Esc>[123;*X (modslen == slen - 3)
4322 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4323 * When there is a modifier the * matches a number.
4324 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004325 */
4326 if (termcodes[idx].modlen > 0)
4327 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004328 modslen = termcodes[idx].modlen;
4329 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004330 continue;
4331 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004332 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004333 {
4334 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004335
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004336 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004337 return -1; /* need to get more chars */
4338
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004339 if (tp[modslen] == termcodes[idx].code[slen - 1])
4340 slen = modslen + 1; /* no modifiers */
4341 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342 continue; /* no match */
4343 else
4344 {
4345 /* Skip over the digits, the final char must
4346 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004347 for (j = slen - 2; j < len && (isdigit(tp[j])
4348 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 ;
4350 ++j;
4351 if (len < j) /* got a partial sequence */
4352 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4354 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004355
Bram Moolenaara529ce02017-06-22 22:37:57 +02004356 modifiers_start = tp + slen - 2;
4357
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004358 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004359 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004360 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004361 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004362 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004363 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004364 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004365 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004366 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004367 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004368
4369 slen = j;
4370 }
4371 key_name[0] = termcodes[idx].name[0];
4372 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004373 break;
4374 }
4375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 }
4378
4379#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004380 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004381 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004382 * detecting the start of these mouse codes they might as well be
4383 * another key code or terminal response. */
4384# ifdef FEAT_MOUSE_DEC
4385 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004386# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004387# ifdef FEAT_MOUSE_PTERM
4388 || key_name[0] == KS_PTERM_MOUSE
4389# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004390 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004392 /* Check for some responses from the terminal starting with
4393 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004394 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004395 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004396 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004397 * Also eat other possible responses to t_RV, rxvt returns
4398 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4399 * mrxvt has been reported to have "+" in the version. Assume
4400 * the escape sequence ends with a letter or one of "{|}~".
4401 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004402 * - Cursor position report: <Esc>[{row};{col}R
4403 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004404 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004405 *
4406 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004407 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004408 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004409
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004410 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004411 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004412 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004413 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004415 int col = 0;
4416 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004417#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004418 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004419#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004422 for (i = 2 + (tp[0] != CSI); i < len
4423 && !(tp[i] >= '{' && tp[i] <= '~')
4424 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004425 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004426 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004427 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004428#ifdef FEAT_MBYTE
4429 row_char = tp[i - 1];
4430#endif
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004433 {
4434 LOG_TR("Not enough characters for CRV");
4435 return -1;
4436 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004437 if (extra > 0)
4438 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004439
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004440#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004441 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4442 * u7_status is not "sent", it may be from a previous Vim that
4443 * just exited. But not for <S-F3>, it sends something
4444 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004445 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004446 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004447 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004448 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004449 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004450
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004451 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004452 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004453# ifdef FEAT_AUTOCMD
4454 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004455# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004456 if (col == 2)
4457 aw = "single";
4458 else if (col == 3)
4459 aw = "double";
4460 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4461 {
4462 /* Setting the option causes a screen redraw. Do
4463 * that right away if possible, keeping any
4464 * messages. */
4465 set_option_value((char_u *)"ambw", 0L,
4466 (char_u *)aw, 0);
4467# ifdef DEBUG_TERMRESPONSE
4468 {
4469 char buf[100];
4470 int r = redraw_asap(CLEAR);
4471
4472 sprintf(buf,
4473 "set 'ambiwidth', redraw_asap(): %d",
4474 r);
4475 log_tr(buf);
4476 }
4477# else
4478 redraw_asap(CLEAR);
4479# endif
4480 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004481 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004482 key_name[0] = (int)KS_EXTRA;
4483 key_name[1] = (int)KE_IGNORE;
4484 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004485# ifdef FEAT_EVAL
4486 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4487# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004488 }
4489 else
4490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004492 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004494 int version = col;
4495
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004496 LOG_TR("Received CRV response");
4497 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004498# ifdef FEAT_AUTOCMD
4499 did_cursorhold = TRUE;
4500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /* If this code starts with CSI, you can bet that the
4503 * terminal uses 8-bit codes. */
4504 if (tp[0] == CSI)
4505 switch_to_8bit();
4506
4507 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004508 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 * Ignore it for when the user has set 'term' to xterm,
4510 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004511 if (version > 20000)
4512 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004514 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004516 int need_flush = FALSE;
4517
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004518 /* Only set 'ttymouse' automatically if it was not set
4519 * by the user already. */
4520 if (!option_was_set((char_u *)"ttym"))
4521 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004522# ifdef TTYM_SGR
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004523 if (version >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004524 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004525 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004526 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004527# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004528 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004529 if (version >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004530 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004532 }
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004535 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004537 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 check_for_codes = TRUE;
4539 need_gather = TRUE;
4540 req_codes_from_term();
4541 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004542
4543 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004544 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004545 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004546 {
4547 /* If run from Vim $COLORS is set to the number of
4548 * colors the terminal supports. Otherwise assume
4549 * 256, libvterm supports even more. */
4550 if (mch_getenv((char_u *)"COLORS") == NULL)
4551 may_adjust_color_count(256);
4552 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004553
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004554 /* Detect terminals that set $TERM to something like
4555 * "xterm-256colors" but are not fully xterm
4556 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004557
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004558 /* Mac Terminal.app sends 1;95;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004559 if (version == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004560 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004561 is_not_xterm = TRUE;
Bram Moolenaarc2127982017-09-11 20:34:13 +02004562
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004563 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004564 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004565 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004566 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004567 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004568 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004569 is_not_xterm = TRUE;
4570
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004571 /* PuTTY sends 0;136;0
4572 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004573 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004574 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004575 is_not_xterm = TRUE;
4576
4577 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004578 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004579 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004580 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004581
4582 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004583 * set. Only supported properly by xterm since version
4584 * 279 (otherwise it returns 0x18).
4585 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004586 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004587 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004588 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004589 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004590 && *T_CSH != NUL
4591 && *T_CRS != NUL)
4592 {
4593 LOG_TR("Sending cursor style request");
4594 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004595 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004596 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004597 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004598
4599 /* Only request the cursor blink mode if t_RC set. Not
4600 * for Gnome terminal, it can't handle t_RC, it
4601 * echoes the characters to the screen. */
4602 if (rbm_status == STATUS_GET
4603 && !is_not_xterm
4604 && *T_CRC != NUL)
4605 {
4606 LOG_TR("Sending cursor blink mode request");
4607 out_str(T_CRC);
4608 rbm_status = STATUS_SENT;
4609 need_flush = TRUE;
4610 }
4611
4612 if (need_flush)
4613 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004615 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004617 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618# endif
4619# ifdef FEAT_AUTOCMD
4620 apply_autocmds(EVENT_TERMRESPONSE,
4621 NULL, NULL, FALSE, curbuf);
4622# endif
4623 key_name[0] = (int)KS_EXTRA;
4624 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004626
Bram Moolenaar4db25542017-08-28 22:43:05 +02004627 /* Check blinking cursor from xterm:
4628 * {lead}?12;1$y set
4629 * {lead}?12;2$y not set
4630 *
4631 * {lead} can be <Esc>[ or CSI
4632 */
4633 else if (rbm_status == STATUS_SENT
4634 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4635 && i == j + 6
4636 && tp[j + 1] == '1'
4637 && tp[j + 2] == '2'
4638 && tp[j + 3] == ';'
4639 && tp[i - 1] == '$'
4640 && tp[i] == 'y')
4641 {
4642 initial_cursor_blink = (tp[j + 4] == '1');
4643 rbm_status = STATUS_GOT;
4644 LOG_TR("Received cursor blinking mode response");
4645 key_name[0] = (int)KS_EXTRA;
4646 key_name[1] = (int)KE_IGNORE;
4647 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004648# ifdef FEAT_EVAL
4649 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4650# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004651 }
4652
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004653 /*
4654 * Check for a window position response from the terminal:
4655 * {lead}3;{x}:{y}t
4656 */
4657 else if (waiting_for_winpos
4658 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4659 || (len >= 3 && tp[0] == CSI))
4660 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4661 && tp[j + 1] == ';')
4662 {
4663 j += 2;
4664 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4665 ;
4666 if (i < len && tp[i] == ';')
4667 {
4668 winpos_x = atoi((char *)tp + j);
4669 j = i + 1;
4670 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4671 ;
4672 if (i < len && tp[i] == 't')
4673 {
4674 winpos_y = atoi((char *)tp + j);
4675 /* got finished code: consume it */
4676 key_name[0] = (int)KS_EXTRA;
4677 key_name[1] = (int)KE_IGNORE;
4678 slen = i + 1;
4679 }
4680 }
4681 if (i == len)
4682 {
4683 LOG_TR("not enough characters for winpos");
4684 return -1;
4685 }
4686 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004687 }
4688
4689 /* Check for background color response from the terminal:
4690 *
4691 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4692 *
4693 * {lead} can be <Esc>] or OSC
4694 * {tail} can be '\007', <Esc>\ or STERM.
4695 *
4696 * Consume any code that starts with "{lead}11;", it's also
4697 * possible that "rgba" is following.
4698 */
4699 else if (*T_RBG != NUL
4700 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4701 || tp[0] == OSC))
4702 {
4703 j = 1 + (tp[0] == ESC);
4704 if (len >= j + 3 && (argp[0] != '1'
4705 || argp[1] != '1' || argp[2] != ';'))
4706 i = 0; /* no match */
4707 else
4708 for (i = j; i < len; ++i)
4709 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4710 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004711 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004712 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4713 && tp[j + 11] == '/' && tp[j + 16] == '/'
4714 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004715 {
4716 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4717 + tp[j+17]) ? "light" : "dark";
4718
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004719 LOG_TR("Received RBG response");
4720 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004721 if (STRCMP(p_bg, newval) != 0)
4722 {
4723 /* value differs, apply it */
4724 set_option_value((char_u *)"bg", 0L,
4725 (char_u *)newval, 0);
4726 reset_option_was_set((char_u *)"bg");
4727 redraw_asap(CLEAR);
4728 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004729 }
4730
4731 /* got finished code: consume it */
4732 key_name[0] = (int)KS_EXTRA;
4733 key_name[1] = (int)KE_IGNORE;
4734 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004735# ifdef FEAT_EVAL
4736 set_vim_var_string(VV_TERMRGBRESP, tp, slen);
4737# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004738 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004739 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004740 if (i == len)
4741 {
4742 LOG_TR("not enough characters for RB");
4743 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004747 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004748 * {lead}{flag}+r<hex bytes><{tail}
4749 *
4750 * {lead} can be <Esc>P or DCS
4751 * {flag} can be '0' or '1'
4752 * {tail} can be Esc>\ or STERM
4753 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004754 * Check for cursor shape response from xterm:
4755 * {lead}1$r<number> q{tail}
4756 *
4757 * {lead} can be <Esc>P or DCS
4758 * {tail} can be Esc>\ or STERM
4759 *
4760 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004761 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004762 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004763 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 || tp[0] == DCS))
4765 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004766 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004767 if (len < j + 3)
4768 i = len; /* need more chars */
4769 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004770 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004771 else if (argp[1] == '+')
4772 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004773 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004774 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004775 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 || tp[i] == STERM)
4777 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004778 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 got_code_from_term(tp + j, i);
4780 key_name[0] = (int)KS_EXTRA;
4781 key_name[1] = (int)KE_IGNORE;
4782 slen = i + 1 + (tp[i] == ESC);
4783 break;
4784 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004785 }
4786 else if ((len >= j + 6 && isdigit(argp[3]))
4787 && argp[4] == ' '
4788 && argp[5] == 'q')
4789 {
4790 /* cursor shape response */
4791 i = j + 6;
4792 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4793 || tp[i] == STERM)
4794 {
4795 int number = argp[3] - '0';
4796
4797 /* 0, 1 = block blink, 2 = block
4798 * 3 = underline blink, 4 = underline
4799 * 5 = vertical bar blink, 6 = vertical bar */
4800 number = number == 0 ? 1 : number;
4801 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004802 /* The blink flag is actually inverted, compared to
4803 * the value set with T_SH. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004804 initial_cursor_shape_blink =
4805 (number & 1) ? FALSE : TRUE;
4806 rcs_status = STATUS_GOT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004807 LOG_TR("Received cursor shape response");
4808
4809 key_name[0] = (int)KS_EXTRA;
4810 key_name[1] = (int)KE_IGNORE;
4811 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004812# ifdef FEAT_EVAL
4813 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
4814# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004815 }
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
4818 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004819 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004820 /* These codes arrive many together, each code can be
4821 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004822 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004823 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 }
4827#endif
4828
4829 if (key_name[0] == NUL)
4830 continue; /* No match at this position, try next one */
4831
4832 /* We only get here when we have a complete termcode match */
4833
4834#ifdef FEAT_MOUSE
4835# ifdef FEAT_GUI
4836 /*
4837 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4838 * so that we know which window to scroll later.
4839 */
4840 if (gui.in_use
4841 && key_name[0] == (int)KS_EXTRA
4842 && (key_name[1] == (int)KE_X1MOUSE
4843 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004844 || key_name[1] == (int)KE_MOUSELEFT
4845 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 || key_name[1] == (int)KE_MOUSEDOWN
4847 || key_name[1] == (int)KE_MOUSEUP))
4848 {
4849 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4850 if (num_bytes == -1) /* not enough coordinates */
4851 return -1;
4852 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4853 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4854 slen += num_bytes;
4855 }
4856 else
4857# endif
4858 /*
4859 * If it is a mouse click, get the coordinates.
4860 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004861 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004863 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864# endif
4865# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004866 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867# endif
4868# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004869 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870# endif
4871# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004872 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004874# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004875 || key_name[0] == KS_URXVT_MOUSE
4876# endif
4877# ifdef FEAT_MOUSE_SGR
4878 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004879 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 )
4882 {
4883 is_click = is_drag = FALSE;
4884
Bram Moolenaar864207d2008-06-24 22:14:38 +00004885# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4886 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (key_name[0] == (int)KS_MOUSE)
4888 {
4889 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004890 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * s == encoded button state:
4892 * 0x20 = left button down
4893 * 0x21 = middle button down
4894 * 0x22 = right button down
4895 * 0x23 = any button release
4896 * 0x60 = button 4 down (scroll wheel down)
4897 * 0x61 = button 5 down (scroll wheel up)
4898 * add 0x04 for SHIFT
4899 * add 0x08 for ALT
4900 * add 0x10 for CTRL
4901 * add 0x20 for mouse drag (0x40 is drag with left button)
4902 * c == column + ' ' + 1 == column + 33
4903 * r == row + ' ' + 1 == row + 33
4904 *
4905 * The coordinates are passed on through global variables.
4906 * Ugly, but this avoids trouble with mouse clicks at an
4907 * unexpected moment and allows for mapping them.
4908 */
4909 for (;;)
4910 {
4911#ifdef FEAT_GUI
4912 if (gui.in_use)
4913 {
4914 /* GUI uses more bits for columns > 223 */
4915 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4916 if (num_bytes == -1) /* not enough coordinates */
4917 return -1;
4918 mouse_code = bytes[0];
4919 mouse_col = 128 * (bytes[1] - ' ' - 1)
4920 + bytes[2] - ' ' - 1;
4921 mouse_row = 128 * (bytes[3] - ' ' - 1)
4922 + bytes[4] - ' ' - 1;
4923 }
4924 else
4925#endif
4926 {
4927 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4928 if (num_bytes == -1) /* not enough coordinates */
4929 return -1;
4930 mouse_code = bytes[0];
4931 mouse_col = bytes[1] - ' ' - 1;
4932 mouse_row = bytes[2] - ' ' - 1;
4933 }
4934 slen += num_bytes;
4935
4936 /* If the following bytes is also a mouse code and it has
4937 * the same code, dump this one and get the next. This
4938 * makes dragging a whole lot faster. */
4939#ifdef FEAT_GUI
4940 if (gui.in_use)
4941 j = 3;
4942 else
4943#endif
4944 j = termcodes[idx].len;
4945 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4946 && tp[slen + j] == mouse_code
4947 && tp[slen + j + 1] != NUL
4948 && tp[slen + j + 2] != NUL
4949#ifdef FEAT_GUI
4950 && (!gui.in_use
4951 || (tp[slen + j + 3] != NUL
4952 && tp[slen + j + 4] != NUL))
4953#endif
4954 )
4955 slen += j;
4956 else
4957 break;
4958 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004961# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4962 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004963 || key_name[0] == KS_SGR_MOUSE
4964 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004965 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004966 /* URXVT 1015 mouse reporting mode:
4967 * Almost identical to xterm mouse mode, except the values
4968 * are decimal instead of bytes.
4969 *
4970 * \033[%d;%d;%dM
4971 * ^-- row
4972 * ^----- column
4973 * ^-------- code
4974 *
4975 * SGR 1006 mouse reporting mode:
4976 * Almost identical to xterm mouse mode, except the values
4977 * are decimal instead of bytes.
4978 *
4979 * \033[<%d;%d;%dM
4980 * ^-- row
4981 * ^----- column
4982 * ^-------- code
4983 *
4984 * \033[<%d;%d;%dm : mouse release event
4985 * ^-- row
4986 * ^----- column
4987 * ^-------- code
4988 */
4989 p = modifiers_start;
4990 if (p == NULL)
4991 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004992
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004993 mouse_code = getdigits(&p);
4994 if (*p++ != ';')
4995 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004996
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004997 /* when mouse reporting is SGR, add 32 to mouse code */
4998 if (key_name[0] == KS_SGR_MOUSE
4999 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5000 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005001
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005002 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5003 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005004
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005005 mouse_col = getdigits(&p) - 1;
5006 if (*p++ != ';')
5007 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005008
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005009 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005010
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005011 /* The modifiers were the mouse coordinates, not the
5012 * modifier keys (alt/shift/ctrl/meta) state. */
5013 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005014 }
5015# endif
5016
5017 if (key_name[0] == (int)KS_MOUSE
5018#ifdef FEAT_MOUSE_URXVT
5019 || key_name[0] == (int)KS_URXVT_MOUSE
5020#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005021#ifdef FEAT_MOUSE_SGR
5022 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005023 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005024#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005025 )
5026 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005027# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /*
5029 * Handle mouse events.
5030 * Recognize the xterm mouse wheel, but not in the GUI, the
5031 * Linux console with GPM and the MS-DOS or Win32 console
5032 * (multi-clicks use >= 0x60).
5033 */
5034 if (mouse_code >= MOUSEWHEEL_LOW
5035# ifdef FEAT_GUI
5036 && !gui.in_use
5037# endif
5038# ifdef FEAT_MOUSE_GPM
5039 && gpm_flag == 0
5040# endif
5041 )
5042 {
5043 /* Keep the mouse_code before it's changed, so that we
5044 * remember that it was a mouse wheel click. */
5045 wheel_code = mouse_code;
5046 }
5047# ifdef FEAT_MOUSE_XTERM
5048 else if (held_button == MOUSE_RELEASE
5049# ifdef FEAT_GUI
5050 && !gui.in_use
5051# endif
5052 && (mouse_code == 0x23 || mouse_code == 0x24))
5053 {
5054 /* Apparently used by rxvt scroll wheel. */
5055 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5056 }
5057# endif
5058
5059# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5060 else if (use_xterm_mouse() > 1)
5061 {
5062 if (mouse_code & MOUSE_DRAG_XTERM)
5063 mouse_code |= MOUSE_DRAG;
5064 }
5065# endif
5066# ifdef FEAT_XCLIPBOARD
5067 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5068 {
5069 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5070 stop_xterm_trace();
5071 else
5072 start_xterm_trace(mouse_code);
5073 }
5074# endif
5075# endif
5076 }
5077# endif /* !UNIX || FEAT_MOUSE_XTERM */
5078# ifdef FEAT_MOUSE_NET
5079 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5080 {
5081 int mc, mr;
5082
5083 /* expect a rather limited sequence like: balancing {
5084 * \033}6,45\r
5085 * '6' is the row, 45 is the column
5086 */
5087 p = tp + slen;
5088 mr = getdigits(&p);
5089 if (*p++ != ',')
5090 return -1;
5091 mc = getdigits(&p);
5092 if (*p++ != '\r')
5093 return -1;
5094
5095 mouse_col = mc - 1;
5096 mouse_row = mr - 1;
5097 mouse_code = MOUSE_LEFT;
5098 slen += (int)(p - (tp + slen));
5099 }
5100# endif /* FEAT_MOUSE_NET */
5101# ifdef FEAT_MOUSE_JSB
5102 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5103 {
5104 int mult, val, iter, button, status;
5105
5106 /* JSBTERM Input Model
5107 * \033[0~zw uniq escape sequence
5108 * (L-x) Left button pressed - not pressed x not reporting
5109 * (M-x) Middle button pressed - not pressed x not reporting
5110 * (R-x) Right button pressed - not pressed x not reporting
5111 * (SDmdu) Single , Double click, m mouse move d button down
5112 * u button up
5113 * ### X cursor position padded to 3 digits
5114 * ### Y cursor position padded to 3 digits
5115 * (s-x) SHIFT key pressed - not pressed x not reporting
5116 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005117 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 */
5119
5120 p = tp + slen;
5121 button = mouse_code = 0;
5122 switch (*p++)
5123 {
5124 case 'L': button = 1; break;
5125 case '-': break;
5126 case 'x': break; /* ignore sequence */
5127 default: return -1; /* Unknown Result */
5128 }
5129 switch (*p++)
5130 {
5131 case 'M': button |= 2; break;
5132 case '-': break;
5133 case 'x': break; /* ignore sequence */
5134 default: return -1; /* Unknown Result */
5135 }
5136 switch (*p++)
5137 {
5138 case 'R': button |= 4; break;
5139 case '-': break;
5140 case 'x': break; /* ignore sequence */
5141 default: return -1; /* Unknown Result */
5142 }
5143 status = *p++;
5144 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5145 mult /= 10, p++)
5146 if (*p >= '0' && *p <= '9')
5147 val += (*p - '0') * mult;
5148 else
5149 return -1;
5150 mouse_col = val;
5151 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5152 mult /= 10, p++)
5153 if (*p >= '0' && *p <= '9')
5154 val += (*p - '0') * mult;
5155 else
5156 return -1;
5157 mouse_row = val;
5158 switch (*p++)
5159 {
5160 case 's': button |= 8; break; /* SHIFT key Pressed */
5161 case '-': break; /* Not Pressed */
5162 case 'x': break; /* Not Reporting */
5163 default: return -1; /* Unknown Result */
5164 }
5165 switch (*p++)
5166 {
5167 case 'c': button |= 16; break; /* CTRL key Pressed */
5168 case '-': break; /* Not Pressed */
5169 case 'x': break; /* Not Reporting */
5170 default: return -1; /* Unknown Result */
5171 }
5172 if (*p++ != '\033')
5173 return -1;
5174 if (*p++ != '\\')
5175 return -1;
5176 switch (status)
5177 {
5178 case 'D': /* Double Click */
5179 case 'S': /* Single Click */
5180 if (button & 1) mouse_code |= MOUSE_LEFT;
5181 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5182 if (button & 4) mouse_code |= MOUSE_RIGHT;
5183 if (button & 8) mouse_code |= MOUSE_SHIFT;
5184 if (button & 16) mouse_code |= MOUSE_CTRL;
5185 break;
5186 case 'm': /* Mouse move */
5187 if (button & 1) mouse_code |= MOUSE_LEFT;
5188 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5189 if (button & 4) mouse_code |= MOUSE_RIGHT;
5190 if (button & 8) mouse_code |= MOUSE_SHIFT;
5191 if (button & 16) mouse_code |= MOUSE_CTRL;
5192 if ((button & 7) != 0)
5193 {
5194 held_button = mouse_code;
5195 mouse_code |= MOUSE_DRAG;
5196 }
5197 is_drag = TRUE;
5198 showmode();
5199 break;
5200 case 'd': /* Button Down */
5201 if (button & 1) mouse_code |= MOUSE_LEFT;
5202 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5203 if (button & 4) mouse_code |= MOUSE_RIGHT;
5204 if (button & 8) mouse_code |= MOUSE_SHIFT;
5205 if (button & 16) mouse_code |= MOUSE_CTRL;
5206 break;
5207 case 'u': /* Button Up */
5208 if (button & 1)
5209 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5210 if (button & 2)
5211 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5212 if (button & 4)
5213 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5214 if (button & 8)
5215 mouse_code |= MOUSE_SHIFT;
5216 if (button & 16)
5217 mouse_code |= MOUSE_CTRL;
5218 break;
5219 default: return -1; /* Unknown Result */
5220 }
5221
5222 slen += (p - (tp + slen));
5223 }
5224# endif /* FEAT_MOUSE_JSB */
5225# ifdef FEAT_MOUSE_DEC
5226 if (key_name[0] == (int)KS_DEC_MOUSE)
5227 {
5228 /* The DEC Locator Input Model
5229 * Netterm delivers the code sequence:
5230 * \033[2;4;24;80&w (left button down)
5231 * \033[3;0;24;80&w (left button up)
5232 * \033[6;1;24;80&w (right button down)
5233 * \033[7;0;24;80&w (right button up)
5234 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5235 * Pe is the event code
5236 * Pb is the button code
5237 * Pr is the row coordinate
5238 * Pc is the column coordinate
5239 * Pp is the third coordinate (page number)
5240 * Pe, the event code indicates what event caused this report
5241 * The following event codes are defined:
5242 * 0 - request, the terminal received an explicit request
5243 * for a locator report, but the locator is unavailable
5244 * 1 - request, the terminal received an explicit request
5245 * for a locator report
5246 * 2 - left button down
5247 * 3 - left button up
5248 * 4 - middle button down
5249 * 5 - middle button up
5250 * 6 - right button down
5251 * 7 - right button up
5252 * 8 - fourth button down
5253 * 9 - fourth button up
5254 * 10 - locator outside filter rectangle
5255 * Pb, the button code, ASCII decimal 0-15 indicating which
5256 * buttons are down if any. The state of the four buttons
5257 * on the locator correspond to the low four bits of the
5258 * decimal value,
5259 * "1" means button depressed
5260 * 0 - no buttons down,
5261 * 1 - right,
5262 * 2 - middle,
5263 * 4 - left,
5264 * 8 - fourth
5265 * Pr is the row coordinate of the locator position in the page,
5266 * encoded as an ASCII decimal value.
5267 * If Pr is omitted, the locator position is undefined
5268 * (outside the terminal window for example).
5269 * Pc is the column coordinate of the locator position in the
5270 * page, encoded as an ASCII decimal value.
5271 * If Pc is omitted, the locator position is undefined
5272 * (outside the terminal window for example).
5273 * Pp is the page coordinate of the locator position
5274 * encoded as an ASCII decimal value.
5275 * The page coordinate may be omitted if the locator is on
5276 * page one (the default). We ignore it anyway.
5277 */
5278 int Pe, Pb, Pr, Pc;
5279
5280 p = tp + slen;
5281
5282 /* get event status */
5283 Pe = getdigits(&p);
5284 if (*p++ != ';')
5285 return -1;
5286
5287 /* get button status */
5288 Pb = getdigits(&p);
5289 if (*p++ != ';')
5290 return -1;
5291
5292 /* get row status */
5293 Pr = getdigits(&p);
5294 if (*p++ != ';')
5295 return -1;
5296
5297 /* get column status */
5298 Pc = getdigits(&p);
5299
5300 /* the page parameter is optional */
5301 if (*p == ';')
5302 {
5303 p++;
5304 (void)getdigits(&p);
5305 }
5306 if (*p++ != '&')
5307 return -1;
5308 if (*p++ != 'w')
5309 return -1;
5310
5311 mouse_code = 0;
5312 switch (Pe)
5313 {
5314 case 0: return -1; /* position request while unavailable */
5315 case 1: /* a response to a locator position request includes
5316 the status of all buttons */
5317 Pb &= 7; /* mask off and ignore fourth button */
5318 if (Pb & 4)
5319 mouse_code = MOUSE_LEFT;
5320 if (Pb & 2)
5321 mouse_code = MOUSE_MIDDLE;
5322 if (Pb & 1)
5323 mouse_code = MOUSE_RIGHT;
5324 if (Pb)
5325 {
5326 held_button = mouse_code;
5327 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005328 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 is_drag = TRUE;
5331 showmode();
5332 break;
5333 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005334 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 break;
5336 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5337 break;
5338 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005339 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 break;
5341 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5342 break;
5343 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005344 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 break;
5346 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5347 break;
5348 case 8: return -1; /* fourth button down */
5349 case 9: return -1; /* fourth button up */
5350 case 10: return -1; /* mouse outside of filter rectangle */
5351 default: return -1; /* should never occur */
5352 }
5353
5354 mouse_col = Pc - 1;
5355 mouse_row = Pr - 1;
5356
5357 slen += (int)(p - (tp + slen));
5358 }
5359# endif /* FEAT_MOUSE_DEC */
5360# ifdef FEAT_MOUSE_PTERM
5361 if (key_name[0] == (int)KS_PTERM_MOUSE)
5362 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005363 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 p = tp + slen;
5366
5367 action = getdigits(&p);
5368 if (*p++ != ';')
5369 return -1;
5370
5371 mouse_row = getdigits(&p);
5372 if (*p++ != ';')
5373 return -1;
5374 mouse_col = getdigits(&p);
5375 if (*p++ != ';')
5376 return -1;
5377
5378 button = getdigits(&p);
5379 mouse_code = 0;
5380
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005381 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 case 4: mouse_code = MOUSE_LEFT; break;
5384 case 1: mouse_code = MOUSE_RIGHT; break;
5385 case 2: mouse_code = MOUSE_MIDDLE; break;
5386 default: return -1;
5387 }
5388
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005389 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 case 31: /* Initial press */
5392 if (*p++ != ';')
5393 return -1;
5394
5395 num_clicks = getdigits(&p); /* Not used */
5396 break;
5397
5398 case 32: /* Release */
5399 mouse_code |= MOUSE_RELEASE;
5400 break;
5401
5402 case 33: /* Drag */
5403 held_button = mouse_code;
5404 mouse_code |= MOUSE_DRAG;
5405 break;
5406
5407 default:
5408 return -1;
5409 }
5410
5411 if (*p++ != 't')
5412 return -1;
5413
5414 slen += (p - (tp + slen));
5415 }
5416# endif /* FEAT_MOUSE_PTERM */
5417
5418 /* Interpret the mouse code */
5419 current_button = (mouse_code & MOUSE_CLICK_MASK);
5420 if (current_button == MOUSE_RELEASE
5421# ifdef FEAT_MOUSE_XTERM
5422 && wheel_code == 0
5423# endif
5424 )
5425 {
5426 /*
5427 * If we get a mouse drag or release event when
5428 * there is no mouse button held down (held_button ==
5429 * MOUSE_RELEASE), produce a K_IGNORE below.
5430 * (can happen when you hold down two buttons
5431 * and then let them go, or click in the menu bar, but not
5432 * on a menu, and drag into the text).
5433 */
5434 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5435 is_drag = TRUE;
5436 current_button = held_button;
5437 }
5438 else if (wheel_code == 0)
5439 {
5440# ifdef CHECK_DOUBLE_CLICK
5441# ifdef FEAT_MOUSE_GPM
5442# ifdef FEAT_GUI
5443 /*
5444 * Only for Unix, when GUI or gpm is not active, we handle
5445 * multi-clicks here.
5446 */
5447 if (gpm_flag == 0 && !gui.in_use)
5448# else
5449 if (gpm_flag == 0)
5450# endif
5451# else
5452# ifdef FEAT_GUI
5453 if (!gui.in_use)
5454# endif
5455# endif
5456 {
5457 /*
5458 * Compute the time elapsed since the previous mouse click.
5459 */
5460 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005461 if (orig_mouse_time.tv_sec == 0)
5462 {
5463 /*
5464 * Avoid computing the difference between mouse_time
5465 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005466 * difference would be huge and would cause
5467 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005468 */
5469 timediff = p_mouset;
5470 }
5471 else
5472 {
5473 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005474 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005475 if (timediff < 0)
5476 --orig_mouse_time.tv_sec;
5477 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005478 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 orig_mouse_time = mouse_time;
5481 if (mouse_code == orig_mouse_code
5482 && timediff < p_mouset
5483 && orig_num_clicks != 4
5484 && orig_mouse_col == mouse_col
5485 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005486 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005488 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005490 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005491 /* Double click in tab pages line also works
5492 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005493 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005494 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 ++orig_num_clicks;
5496 else
5497 orig_num_clicks = 1;
5498 orig_mouse_col = mouse_col;
5499 orig_mouse_row = mouse_row;
5500 orig_topline = curwin->w_topline;
5501#ifdef FEAT_DIFF
5502 orig_topfill = curwin->w_topfill;
5503#endif
5504 }
5505# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5506 else
5507 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5508# endif
5509# else
5510 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5511# endif
5512 is_click = TRUE;
5513 orig_mouse_code = mouse_code;
5514 }
5515 if (!is_drag)
5516 held_button = mouse_code & MOUSE_CLICK_MASK;
5517
5518 /*
5519 * Translate the actual mouse event into a pseudo mouse event.
5520 * First work out what modifiers are to be used.
5521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (orig_mouse_code & MOUSE_SHIFT)
5523 modifiers |= MOD_MASK_SHIFT;
5524 if (orig_mouse_code & MOUSE_CTRL)
5525 modifiers |= MOD_MASK_CTRL;
5526 if (orig_mouse_code & MOUSE_ALT)
5527 modifiers |= MOD_MASK_ALT;
5528 if (orig_num_clicks == 2)
5529 modifiers |= MOD_MASK_2CLICK;
5530 else if (orig_num_clicks == 3)
5531 modifiers |= MOD_MASK_3CLICK;
5532 else if (orig_num_clicks == 4)
5533 modifiers |= MOD_MASK_4CLICK;
5534
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005535 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5536 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005538 if (wheel_code != 0
5539 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005540 {
5541 if (wheel_code & MOUSE_CTRL)
5542 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005543 if (wheel_code & MOUSE_ALT)
5544 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 key_name[1] = (wheel_code & 1)
5546 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else
5549 key_name[1] = get_pseudo_mouse_code(current_button,
5550 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005551
5552 /* Make sure the mouse position is valid. Some terminals may
5553 * return weird values. */
5554 if (mouse_col >= Columns)
5555 mouse_col = Columns - 1;
5556 if (mouse_row >= Rows)
5557 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559#endif /* FEAT_MOUSE */
5560
5561#ifdef FEAT_GUI
5562 /*
5563 * If using the GUI, then we get menu and scrollbar events.
5564 *
5565 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5566 * four bytes which are to be taken as a pointer to the vimmenu_T
5567 * structure.
5568 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005569 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005570 * is one byte with the tab index.
5571 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5573 * by one byte representing the scrollbar number, and then four bytes
5574 * representing a long_u which is the new value of the scrollbar.
5575 *
5576 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5577 * KE_FILLER followed by four bytes representing a long_u which is the
5578 * new value of the scrollbar.
5579 */
5580# ifdef FEAT_MENU
5581 else if (key_name[0] == (int)KS_MENU)
5582 {
5583 long_u val;
5584
5585 num_bytes = get_long_from_buf(tp + slen, &val);
5586 if (num_bytes == -1)
5587 return -1;
5588 current_menu = (vimmenu_T *)val;
5589 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005590
5591 /* The menu may have been deleted right after it was used, check
5592 * for that. */
5593 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5594 {
5595 key_name[0] = KS_EXTRA;
5596 key_name[1] = (int)KE_IGNORE;
5597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005600# ifdef FEAT_GUI_TABLINE
5601 else if (key_name[0] == (int)KS_TABLINE)
5602 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005603 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005604 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5605 if (num_bytes == -1)
5606 return -1;
5607 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005608 if (current_tab == 255) /* -1 in a byte gives 255 */
5609 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005610 slen += num_bytes;
5611 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005612 else if (key_name[0] == (int)KS_TABMENU)
5613 {
5614 /* Selecting tabline tab or using its menu. */
5615 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5616 if (num_bytes == -1)
5617 return -1;
5618 current_tab = (int)bytes[0];
5619 current_tabmenu = (int)bytes[1];
5620 slen += num_bytes;
5621 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623# ifndef USE_ON_FLY_SCROLL
5624 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5625 {
5626 long_u val;
5627
5628 /* Get the last scrollbar event in the queue of the same type */
5629 j = 0;
5630 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5631 && tp[j + 2] != NUL; ++i)
5632 {
5633 j += 3;
5634 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5635 if (num_bytes == -1)
5636 break;
5637 if (i == 0)
5638 current_scrollbar = (int)bytes[0];
5639 else if (current_scrollbar != (int)bytes[0])
5640 break;
5641 j += num_bytes;
5642 num_bytes = get_long_from_buf(tp + j, &val);
5643 if (num_bytes == -1)
5644 break;
5645 scrollbar_value = val;
5646 j += num_bytes;
5647 slen = j;
5648 }
5649 if (i == 0) /* not enough characters to make one */
5650 return -1;
5651 }
5652 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5653 {
5654 long_u val;
5655
5656 /* Get the last horiz. scrollbar event in the queue */
5657 j = 0;
5658 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5659 && tp[j + 2] != NUL; ++i)
5660 {
5661 j += 3;
5662 num_bytes = get_long_from_buf(tp + j, &val);
5663 if (num_bytes == -1)
5664 break;
5665 scrollbar_value = val;
5666 j += num_bytes;
5667 slen = j;
5668 }
5669 if (i == 0) /* not enough characters to make one */
5670 return -1;
5671 }
5672# endif /* !USE_ON_FLY_SCROLL */
5673#endif /* FEAT_GUI */
5674
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005675 /*
5676 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5677 */
5678 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5679
5680 /*
5681 * Add any modifier codes to our string.
5682 */
5683 new_slen = 0; /* Length of what will replace the termcode */
5684 if (modifiers != 0)
5685 {
5686 /* Some keys have the modifier included. Need to handle that here
5687 * to make mappings work. */
5688 key = simplify_key(key, &modifiers);
5689 if (modifiers != 0)
5690 {
5691 string[new_slen++] = K_SPECIAL;
5692 string[new_slen++] = (int)KS_MODIFIER;
5693 string[new_slen++] = modifiers;
5694 }
5695 }
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005698 key_name[0] = KEY2TERMCAP0(key);
5699 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005701 {
5702 /* from ":set <M-b>=xx" */
5703#ifdef FEAT_MBYTE
5704 if (has_mbyte)
5705 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5706 else
5707#endif
5708 string[new_slen++] = key_name[1];
5709 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005710 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5711 && key_name[1] == KE_IGNORE)
5712 {
5713 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5714 * to indicate what happened. */
5715 retval = KEYLEN_REMOVED;
5716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 else
5718 {
5719 string[new_slen++] = K_SPECIAL;
5720 string[new_slen++] = key_name[0];
5721 string[new_slen++] = key_name[1];
5722 }
5723 string[new_slen] = NUL;
5724 extra = new_slen - slen;
5725 if (buf == NULL)
5726 {
5727 if (extra < 0)
5728 /* remove matched chars, taking care of noremap */
5729 del_typebuf(-extra, offset);
5730 else if (extra > 0)
5731 /* insert the extra space we need */
5732 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5733
5734 /*
5735 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5736 * typebuf.tb_buf[]!
5737 */
5738 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5739 (size_t)new_slen);
5740 }
5741 else
5742 {
5743 if (extra < 0)
5744 /* remove matched characters */
5745 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005746 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005748 {
5749 /* Insert the extra space we need. If there is insufficient
5750 * space return -1. */
5751 if (*buflen + extra + new_slen >= bufsize)
5752 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005754 (size_t)(*buflen - offset));
5755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005757 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005759 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761
Bram Moolenaar2951b772013-07-03 12:45:31 +02005762#ifdef FEAT_TERMRESPONSE
5763 LOG_TR("normal character");
5764#endif
5765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 return 0; /* no match found */
5767}
5768
5769/*
5770 * Replace any terminal code strings in from[] with the equivalent internal
5771 * vim representation. This is used for the "from" and "to" part of a
5772 * mapping, and the "to" part of a menu command.
5773 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5774 * '<'.
5775 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5776 *
5777 * The replacement is done in result[] and finally copied into allocated
5778 * memory. If this all works well *bufp is set to the allocated memory and a
5779 * pointer to it is returned. If something fails *bufp is set to NULL and from
5780 * is returned.
5781 *
5782 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5783 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5784 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5785 * instead of a CTRL-V.
5786 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005787 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005788replace_termcodes(
5789 char_u *from,
5790 char_u **bufp,
5791 int from_part,
5792 int do_lt, /* also translate <lt> */
5793 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 int i;
5796 int slen;
5797 int key;
5798 int dlen = 0;
5799 char_u *src;
5800 int do_backslash; /* backslash is a special character */
5801 int do_special; /* recognize <> key codes */
5802 int do_key_code; /* recognize raw key codes */
5803 char_u *result; /* buffer for resulting string */
5804
5805 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005806 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5808
5809 /*
5810 * Allocate space for the translation. Worst case a single character is
5811 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5812 */
5813 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5814 if (result == NULL) /* out of memory */
5815 {
5816 *bufp = NULL;
5817 return from;
5818 }
5819
5820 src = from;
5821
5822 /*
5823 * Check for #n at start only: function key n
5824 */
5825 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5826 {
5827 result[dlen++] = K_SPECIAL;
5828 result[dlen++] = 'k';
5829 if (src[1] == '0')
5830 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5831 else
5832 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5833 src += 2;
5834 }
5835
5836 /*
5837 * Copy each byte from *from to result[dlen]
5838 */
5839 while (*src != NUL)
5840 {
5841 /*
5842 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005843 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
5845 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5846 {
5847#ifdef FEAT_EVAL
5848 /*
5849 * Replace <SID> by K_SNR <script-nr> _.
5850 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5851 */
5852 if (STRNICMP(src, "<SID>", 5) == 0)
5853 {
5854 if (current_SID <= 0)
5855 EMSG(_(e_usingsid));
5856 else
5857 {
5858 src += 5;
5859 result[dlen++] = K_SPECIAL;
5860 result[dlen++] = (int)KS_EXTRA;
5861 result[dlen++] = (int)KE_SNR;
5862 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5863 dlen += (int)STRLEN(result + dlen);
5864 result[dlen++] = '_';
5865 continue;
5866 }
5867 }
5868#endif
5869
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005870 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 if (slen)
5872 {
5873 dlen += slen;
5874 continue;
5875 }
5876 }
5877
5878 /*
5879 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5880 * Note that this is also checked after replacing the <> form.
5881 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5882 * it could be a character in the file.
5883 */
5884 if (do_key_code)
5885 {
5886 i = find_term_bykeys(src);
5887 if (i >= 0)
5888 {
5889 result[dlen++] = K_SPECIAL;
5890 result[dlen++] = termcodes[i].name[0];
5891 result[dlen++] = termcodes[i].name[1];
5892 src += termcodes[i].len;
5893 /* If terminal code matched, continue after it. */
5894 continue;
5895 }
5896 }
5897
5898#ifdef FEAT_EVAL
5899 if (do_special)
5900 {
5901 char_u *p, *s, len;
5902
5903 /*
5904 * Replace <Leader> by the value of "mapleader".
5905 * Replace <LocalLeader> by the value of "maplocalleader".
5906 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5907 */
5908 if (STRNICMP(src, "<Leader>", 8) == 0)
5909 {
5910 len = 8;
5911 p = get_var_value((char_u *)"g:mapleader");
5912 }
5913 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5914 {
5915 len = 13;
5916 p = get_var_value((char_u *)"g:maplocalleader");
5917 }
5918 else
5919 {
5920 len = 0;
5921 p = NULL;
5922 }
5923 if (len != 0)
5924 {
5925 /* Allow up to 8 * 6 characters for "mapleader". */
5926 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5927 s = (char_u *)"\\";
5928 else
5929 s = p;
5930 while (*s != NUL)
5931 result[dlen++] = *s++;
5932 src += len;
5933 continue;
5934 }
5935 }
5936#endif
5937
5938 /*
5939 * Remove CTRL-V and ignore the next character.
5940 * For "from" side the CTRL-V at the end is included, for the "to"
5941 * part it is removed.
5942 * If 'cpoptions' does not contain 'B', also accept a backslash.
5943 */
5944 key = *src;
5945 if (key == Ctrl_V || (do_backslash && key == '\\'))
5946 {
5947 ++src; /* skip CTRL-V or backslash */
5948 if (*src == NUL)
5949 {
5950 if (from_part)
5951 result[dlen++] = key;
5952 break;
5953 }
5954 }
5955
5956#ifdef FEAT_MBYTE
5957 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005958 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959#endif
5960 {
5961 /*
5962 * If the character is K_SPECIAL, replace it with K_SPECIAL
5963 * KS_SPECIAL KE_FILLER.
5964 * If compiled with the GUI replace CSI with K_CSI.
5965 */
5966 if (*src == K_SPECIAL)
5967 {
5968 result[dlen++] = K_SPECIAL;
5969 result[dlen++] = KS_SPECIAL;
5970 result[dlen++] = KE_FILLER;
5971 }
5972# ifdef FEAT_GUI
5973 else if (*src == CSI)
5974 {
5975 result[dlen++] = K_SPECIAL;
5976 result[dlen++] = KS_EXTRA;
5977 result[dlen++] = (int)KE_CSI;
5978 }
5979# endif
5980 else
5981 result[dlen++] = *src;
5982 ++src;
5983 }
5984 }
5985 result[dlen] = NUL;
5986
5987 /*
5988 * Copy the new string to allocated memory.
5989 * If this fails, just return from.
5990 */
5991 if ((*bufp = vim_strsave(result)) != NULL)
5992 from = *bufp;
5993 vim_free(result);
5994 return from;
5995}
5996
5997/*
5998 * Find a termcode with keys 'src' (must be NUL terminated).
5999 * Return the index in termcodes[], or -1 if not found.
6000 */
6001 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006002find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006005 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 for (i = 0; i < tc_len; ++i)
6008 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006009 if (slen == termcodes[i].len
6010 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return i;
6012 }
6013 return -1;
6014}
6015
6016/*
6017 * Gather the first characters in the terminal key codes into a string.
6018 * Used to speed up check_termcode().
6019 */
6020 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006021gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 int i;
6024 int len = 0;
6025
6026#ifdef FEAT_GUI
6027 if (gui.in_use)
6028 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6029#endif
6030#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006031 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 termleader[len++] = DCS; /* the termcode response starts with DCS
6033 in 8-bit mode */
6034#endif
6035 termleader[len] = NUL;
6036
6037 for (i = 0; i < tc_len; ++i)
6038 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6039 {
6040 termleader[len++] = termcodes[i].code[0];
6041 termleader[len] = NUL;
6042 }
6043
6044 need_gather = FALSE;
6045}
6046
6047/*
6048 * Show all termcodes (for ":set termcap")
6049 * This code looks a lot like showoptions(), but is different.
6050 */
6051 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006052show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 int col;
6055 int *items;
6056 int item_count;
6057 int run;
6058 int row, rows;
6059 int cols;
6060 int i;
6061 int len;
6062
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006063#define INC3 27 /* try to make three columns */
6064#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065#define GAP 2 /* spaces between columns */
6066
6067 if (tc_len == 0) /* no terminal codes (must be GUI) */
6068 return;
6069 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6070 if (items == NULL)
6071 return;
6072
6073 /* Highlight title */
6074 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6075
6076 /*
6077 * do the loop two times:
6078 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006079 * 2. display the medium items (medium length strings)
6080 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006082 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
6084 /*
6085 * collect the items in items[]
6086 */
6087 item_count = 0;
6088 for (i = 0; i < tc_len; i++)
6089 {
6090 len = show_one_termcode(termcodes[i].name,
6091 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006092 if (len <= INC3 - GAP ? run == 1
6093 : len <= INC2 - GAP ? run == 2
6094 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 items[item_count++] = i;
6096 }
6097
6098 /*
6099 * display the items
6100 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006103 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (cols == 0)
6105 cols = 1;
6106 rows = (item_count + cols - 1) / cols;
6107 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006108 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 rows = item_count;
6110 for (row = 0; row < rows && !got_int; ++row)
6111 {
6112 msg_putchar('\n'); /* go to next line */
6113 if (got_int) /* 'q' typed in more */
6114 break;
6115 col = 0;
6116 for (i = row; i < item_count; i += rows)
6117 {
6118 msg_col = col; /* make columns */
6119 show_one_termcode(termcodes[items[i]].name,
6120 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006121 if (run == 2)
6122 col += INC2;
6123 else
6124 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 }
6126 out_flush();
6127 ui_breakcheck();
6128 }
6129 }
6130 vim_free(items);
6131}
6132
6133/*
6134 * Show one termcode entry.
6135 * Output goes into IObuff[]
6136 */
6137 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006138show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *p;
6141 int len;
6142
6143 if (name[0] > '~')
6144 {
6145 IObuff[0] = ' ';
6146 IObuff[1] = ' ';
6147 IObuff[2] = ' ';
6148 IObuff[3] = ' ';
6149 }
6150 else
6151 {
6152 IObuff[0] = 't';
6153 IObuff[1] = '_';
6154 IObuff[2] = name[0];
6155 IObuff[3] = name[1];
6156 }
6157 IObuff[4] = ' ';
6158
6159 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6160 if (p[1] != 't')
6161 STRCPY(IObuff + 5, p);
6162 else
6163 IObuff[5] = NUL;
6164 len = (int)STRLEN(IObuff);
6165 do
6166 IObuff[len++] = ' ';
6167 while (len < 17);
6168 IObuff[len] = NUL;
6169 if (code == NULL)
6170 len += 4;
6171 else
6172 len += vim_strsize(code);
6173
6174 if (printit)
6175 {
6176 msg_puts(IObuff);
6177 if (code == NULL)
6178 msg_puts((char_u *)"NULL");
6179 else
6180 msg_outtrans(code);
6181 }
6182 return len;
6183}
6184
6185#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6186/*
6187 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6188 * termcap codes from the terminal itself.
6189 * We get them one by one to avoid a very long response string.
6190 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006191static int xt_index_in = 0;
6192static int xt_index_out = 0;
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006195req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 xt_index_out = 0;
6198 xt_index_in = 0;
6199 req_more_codes_from_term();
6200}
6201
6202 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006203req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 char buf[11];
6206 int old_idx = xt_index_out;
6207
6208 /* Don't do anything when going to exit. */
6209 if (exiting)
6210 return;
6211
6212 /* Send up to 10 more requests out than we received. Avoid sending too
6213 * many, there can be a buffer overflow somewhere. */
6214 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6215 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006216# ifdef DEBUG_TERMRESPONSE
6217 char dbuf[100];
6218
6219 sprintf(dbuf, "Requesting XT %d: %s",
6220 xt_index_out, key_names[xt_index_out]);
6221 log_tr(dbuf);
6222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 sprintf(buf, "\033P+q%02x%02x\033\\",
6224 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6225 out_str_nf((char_u *)buf);
6226 ++xt_index_out;
6227 }
6228
6229 /* Send the codes out right away. */
6230 if (xt_index_out != old_idx)
6231 out_flush();
6232}
6233
6234/*
6235 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6236 * A "0" instead of the "1" indicates a code that isn't supported.
6237 * Both <name> and <string> are encoded in hex.
6238 * "code" points to the "0" or "1".
6239 */
6240 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006241got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243#define XT_LEN 100
6244 char_u name[3];
6245 char_u str[XT_LEN];
6246 int i;
6247 int j = 0;
6248 int c;
6249
6250 /* A '1' means the code is supported, a '0' means it isn't.
6251 * When half the length is > XT_LEN we can't use it.
6252 * Our names are currently all 2 characters. */
6253 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6254 {
6255 /* Get the name from the response and find it in the table. */
6256 name[0] = hexhex2nr(code + 3);
6257 name[1] = hexhex2nr(code + 5);
6258 name[2] = NUL;
6259 for (i = 0; key_names[i] != NULL; ++i)
6260 {
6261 if (STRCMP(key_names[i], name) == 0)
6262 {
6263 xt_index_in = i;
6264 break;
6265 }
6266 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006267# ifdef DEBUG_TERMRESPONSE
6268 {
6269 char buf[100];
6270
6271 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6272 log_tr(buf);
6273 }
6274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (key_names[i] != NULL)
6276 {
6277 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6278 str[j++] = c;
6279 str[j] = NUL;
6280 if (name[0] == 'C' && name[1] == 'o')
6281 {
6282 /* Color count is not a key code. */
6283 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006284 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 }
6286 else
6287 {
6288 /* First delete any existing entry with the same code. */
6289 i = find_term_bykeys(str);
6290 if (i >= 0)
6291 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006292 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294 }
6295 }
6296
6297 /* May request more codes now that we received one. */
6298 ++xt_index_in;
6299 req_more_codes_from_term();
6300}
6301
6302/*
6303 * Check if there are any unanswered requests and deal with them.
6304 * This is called before starting an external program or getting direct
6305 * keyboard input. We don't want responses to be send to that program or
6306 * handled as typed text.
6307 */
6308 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006309check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 int c;
6312
6313 /* If no codes requested or all are answered, no need to wait. */
6314 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6315 return;
6316
6317 /* Vgetc() will check for and handle any response.
6318 * Keep calling vpeekc() until we don't get any responses. */
6319 ++no_mapping;
6320 ++allow_keys;
6321 for (;;)
6322 {
6323 c = vpeekc();
6324 if (c == NUL) /* nothing available */
6325 break;
6326
6327 /* If a response is recognized it's replaced with K_IGNORE, must read
6328 * it from the input stream. If there is no K_IGNORE we can't do
6329 * anything, break here (there might be some responses further on, but
6330 * we don't want to throw away any typed chars). */
6331 if (c != K_SPECIAL && c != K_IGNORE)
6332 break;
6333 c = vgetc();
6334 if (c != K_IGNORE)
6335 {
6336 vungetc(c);
6337 break;
6338 }
6339 }
6340 --no_mapping;
6341 --allow_keys;
6342}
6343#endif
6344
6345#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6346/*
6347 * Translate an internal mapping/abbreviation representation into the
6348 * corresponding external one recognized by :map/:abbrev commands;
6349 * respects the current B/k/< settings of 'cpoption'.
6350 *
6351 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006352 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 *
6354 * It uses a growarray to build the translation string since the
6355 * latter can be wider than the original description. The caller has to
6356 * free the string afterwards.
6357 *
6358 * Returns NULL when there is a problem.
6359 */
6360 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006361translate_mapping(
6362 char_u *str,
6363 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 garray_T ga;
6366 int c;
6367 int modifiers;
6368 int cpo_bslash;
6369 int cpo_special;
6370 int cpo_keycode;
6371
6372 ga_init(&ga);
6373 ga.ga_itemsize = 1;
6374 ga.ga_growsize = 40;
6375
6376 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6377 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6378 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6379
6380 for (; *str; ++str)
6381 {
6382 c = *str;
6383 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6384 {
6385 modifiers = 0;
6386 if (str[1] == KS_MODIFIER)
6387 {
6388 str++;
6389 modifiers = *++str;
6390 c = *++str;
6391 }
6392 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6393 {
6394 int i;
6395
6396 /* try to find special key in termcodes */
6397 for (i = 0; i < tc_len; ++i)
6398 if (termcodes[i].name[0] == str[1]
6399 && termcodes[i].name[1] == str[2])
6400 break;
6401 if (i < tc_len)
6402 {
6403 ga_concat(&ga, termcodes[i].code);
6404 str += 2;
6405 continue; /* for (str) */
6406 }
6407 }
6408 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6409 {
6410 if (expmap && cpo_special)
6411 {
6412 ga_clear(&ga);
6413 return NULL;
6414 }
6415 c = TO_SPECIAL(str[1], str[2]);
6416 if (c == K_ZERO) /* display <Nul> as ^@ */
6417 c = NUL;
6418 str += 2;
6419 }
6420 if (IS_SPECIAL(c) || modifiers) /* special key */
6421 {
6422 if (expmap && cpo_special)
6423 {
6424 ga_clear(&ga);
6425 return NULL;
6426 }
6427 ga_concat(&ga, get_special_key_name(c, modifiers));
6428 continue; /* for (str) */
6429 }
6430 }
6431 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6432 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6433 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6434 if (c)
6435 ga_append(&ga, c);
6436 }
6437 ga_append(&ga, NUL);
6438 return (char_u *)(ga.ga_data);
6439}
6440#endif
6441
6442#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6443static char ksme_str[20];
6444static char ksmr_str[20];
6445static char ksmd_str[20];
6446
6447/*
6448 * For Win32 console: update termcap codes for existing console attributes.
6449 */
6450 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006451update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 struct builtin_term *p;
6454
6455 p = find_builtin_term(DEFAULT_TERM);
6456 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6457 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6458 attr | 0x08); /* FOREGROUND_INTENSITY */
6459 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6460 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6461
6462 while (p->bt_string != NULL)
6463 {
6464 if (p->bt_entry == (int)KS_ME)
6465 p->bt_string = &ksme_str[0];
6466 else if (p->bt_entry == (int)KS_MR)
6467 p->bt_string = &ksmr_str[0];
6468 else if (p->bt_entry == (int)KS_MD)
6469 p->bt_string = &ksmd_str[0];
6470 ++p;
6471 }
6472}
6473#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006474
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006475#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006476 static int
6477hex_digit(int c)
6478{
6479 if (isdigit(c))
6480 return c - '0';
6481 c = TOLOWER_ASC(c);
6482 if (c >= 'a' && c <= 'f')
6483 return c - 'a' + 10;
6484 return 0x1ffffff;
6485}
6486
6487 guicolor_T
6488gui_get_color_cmn(char_u *name)
6489{
Bram Moolenaar28726652017-01-06 18:16:19 +01006490 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6491 * values as used by the MS-Windows GDI api. It should be used only for
6492 * MS-Windows GDI builds. */
6493# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6494# undef RGB
6495# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006496# ifndef RGB
6497# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6498# endif
6499# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006500 FILE *fd;
6501 char line[LINE_LEN];
6502 char_u *fname;
6503 int r, g, b, i;
6504 guicolor_T color;
6505
6506 struct rgbcolor_table_S {
6507 char_u *color_name;
6508 guicolor_T color;
6509 };
6510
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006511 /* Only non X11 colors (not present in rgb.txt) and colors in
6512 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006513 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006514 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6515 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6516 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6517 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6518 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6519 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6520 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6521 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6522 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6523 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6524 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6525 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6526 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006527 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6528 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006529 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006530 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006531 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6532 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6533 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6534 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6535 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6536 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6537 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6538 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6539 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006540 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006541 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006542 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6543 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006544 };
6545
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006546 static struct rgbcolor_table_S *colornames_table;
6547 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006548
6549 if (name[0] == '#' && STRLEN(name) == 7)
6550 {
6551 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006552 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006553 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6554 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6555 if (color > 0xffffff)
6556 return INVALCOLOR;
6557 return color;
6558 }
6559
6560 /* Check if the name is one of the colors we know */
6561 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6562 if (STRICMP(name, rgb_table[i].color_name) == 0)
6563 return rgb_table[i].color;
6564
6565 /*
6566 * Last attempt. Look in the file "$VIM/rgb.txt".
6567 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006568 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006569 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006570 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006571
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006572 /* colornames_table not yet initialized */
6573 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6574 if (fname == NULL)
6575 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006576
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006577 fd = fopen((char *)fname, "rt");
6578 vim_free(fname);
6579 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006580 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006581 if (p_verbose > 1)
6582 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6583 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006584 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006585
6586 for (counting = 1; counting >= 0; --counting)
6587 {
6588 if (!counting)
6589 {
6590 colornames_table = (struct rgbcolor_table_S *)alloc(
6591 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6592 if (colornames_table == NULL)
6593 {
6594 fclose(fd);
6595 return INVALCOLOR;
6596 }
6597 rewind(fd);
6598 }
6599 size = 0;
6600
6601 while (!feof(fd))
6602 {
6603 size_t len;
6604 int pos;
6605
6606 ignoredp = fgets(line, LINE_LEN, fd);
6607 len = strlen(line);
6608
6609 if (len <= 1 || line[len - 1] != '\n')
6610 continue;
6611
6612 line[len - 1] = '\0';
6613
6614 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6615 if (i != 3)
6616 continue;
6617
6618 if (!counting)
6619 {
6620 char_u *s = vim_strsave((char_u *)line + pos);
6621
6622 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006623 {
6624 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006625 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006626 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006627 colornames_table[size].color_name = s;
6628 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6629 }
6630 size++;
6631 }
6632 }
6633 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006634 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006635
6636 for (i = 0; i < size; i++)
6637 if (STRICMP(name, colornames_table[i].color_name) == 0)
6638 return colornames_table[i].color;
6639
Bram Moolenaarab302212016-04-26 20:59:29 +02006640 return INVALCOLOR;
6641}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006642
6643 guicolor_T
6644gui_get_rgb_color_cmn(int r, int g, int b)
6645{
6646 guicolor_T color = RGB(r, g, b);
6647
6648 if (color > 0xffffff)
6649 return INVALCOLOR;
6650 return color;
6651}
Bram Moolenaarab302212016-04-26 20:59:29 +02006652#endif