blob: 07292bca2e635fb4d7d293ae3fe6055fc3655d83 [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);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003274 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003275 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003276#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +02003277 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003278 if (redrawing())
3279 setcursor();
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 cursor_on(); /* redrawing may have switched it off */
3283 }
3284 out_flush();
3285 --busy;
3286}
3287
3288/*
3289 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3290 * commands and Ex mode).
3291 */
3292 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003293settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295#ifdef FEAT_GUI
3296 /* don't set the term where gvim was started to any mode */
3297 if (gui.in_use)
3298 return;
3299#endif
3300
3301 if (full_screen)
3302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 /*
3304 * When returning after calling a shell we want to really set the
3305 * terminal to raw mode, even though we think it already is, because
3306 * the shell program may have reset the terminal mode.
3307 * When we think the terminal is normal, don't try to set it to
3308 * normal again, because that causes problems (logout!) on some
3309 * machines.
3310 */
3311 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3312 {
3313#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003314# ifdef FEAT_GUI
3315 if (!gui.in_use && !gui.starting)
3316# endif
3317 {
3318 /* May need to check for T_CRV response and termcodes, it
3319 * doesn't work in Cooked mode, an external program may get
3320 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003321 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3322 || u7_status == STATUS_SENT
3323 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003324 || rbm_status == STATUS_SENT
3325 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003326 (void)vpeekc_nomap();
3327 check_for_codes_from_term();
3328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
3330#ifdef FEAT_MOUSE_TTY
3331 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003332 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003334 if (tmode != TMODE_RAW)
3335 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003337 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 cur_tmode = tmode;
3339#ifdef FEAT_MOUSE
3340 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003341 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003343 if (tmode == TMODE_RAW)
3344 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 out_flush();
3346 }
3347#ifdef FEAT_TERMRESPONSE
3348 may_req_termresponse();
3349#endif
3350 }
3351}
3352
3353 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003354starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 if (full_screen && !termcap_active)
3357 {
3358 out_str(T_TI); /* start termcap mode */
3359 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003360 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 out_flush();
3362 termcap_active = TRUE;
3363 screen_start(); /* don't know where cursor is now */
3364#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003365# ifdef FEAT_GUI
3366 if (!gui.in_use && !gui.starting)
3367# endif
3368 {
3369 may_req_termresponse();
3370 /* Immediately check for a response. If t_Co changes, we don't
3371 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003372 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003373 check_for_codes_from_term();
3374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
3376 }
3377}
3378
3379 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003380stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 screen_stop_highlight();
3383 reset_cterm_colors();
3384 if (termcap_active)
3385 {
3386#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003387# ifdef FEAT_GUI
3388 if (!gui.in_use && !gui.starting)
3389# endif
3390 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003391 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003392 if (crv_status == STATUS_SENT
3393 || u7_status == STATUS_SENT
3394 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003395 || rbm_status == STATUS_SENT
3396 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003397 {
3398# ifdef UNIX
3399 /* Give the terminal a chance to respond. */
3400 mch_delay(100L, FALSE);
3401# endif
3402# ifdef TCIFLUSH
3403 /* Discard data received but not read. */
3404 if (exiting)
3405 tcflush(fileno(stdin), TCIFLUSH);
3406# endif
3407 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003408 /* Check for termcodes first, otherwise an external program may
3409 * get them. */
3410 check_for_codes_from_term();
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003413 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 out_str(T_KE); /* stop "keypad transmit" mode */
3415 out_flush();
3416 termcap_active = FALSE;
3417 cursor_on(); /* just in case it is still off */
3418 out_str(T_TE); /* stop termcap mode */
3419 screen_start(); /* don't know where cursor is now */
3420 out_flush();
3421 }
3422}
3423
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003424#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425/*
3426 * Request version string (for xterm) when needed.
3427 * Only do this after switching to raw mode, otherwise the result will be
3428 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003429 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003430 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 * Only do this after termcap mode has been started, otherwise the codes for
3432 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003433 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3434 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003435 * On Unix only do it when both output and input are a tty (avoid writing
3436 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * The result is caught in check_termcode().
3438 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003439 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003440may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003442 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003443 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003444 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 && *T_CRV != NUL)
3446 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003447 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003449 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 /* check for the characters now, otherwise they might be eaten by
3451 * get_keystroke() */
3452 out_flush();
3453 (void)vpeekc_nomap();
3454 }
3455}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003456
3457# if defined(FEAT_MBYTE) || defined(PROTO)
3458/*
3459 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003460 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003461 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003462 * If the terminal treats \u25bd as single width, the position is (1, 1),
3463 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003464 * This function has the side effect that changes cursor position, so
3465 * it must be called immediately after entering termcap mode.
3466 */
3467 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003468may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003469{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003470 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003471 && can_get_termresponse()
3472 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003473 && *T_U7 != NUL
3474 && !option_was_set((char_u *)"ambiwidth"))
3475 {
3476 char_u buf[16];
3477
Bram Moolenaar2951b772013-07-03 12:45:31 +02003478 LOG_TR("Sending U7 request");
3479 /* Do this in the second row. In the first row the returned sequence
3480 * may be CSI 1;2R, which is the same as <S-F3>. */
3481 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003482 buf[mb_char2bytes(0x25bd, buf)] = 0;
3483 out_str(buf);
3484 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003485 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003486 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003487
3488 /* This overwrites a few characters on the screen, a redraw is needed
3489 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003490 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003491 out_str((char_u *)" ");
3492 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003493
Bram Moolenaar9584b312013-03-13 19:29:28 +01003494 /* check for the characters now, otherwise they might be eaten by
3495 * get_keystroke() */
3496 out_flush();
3497 (void)vpeekc_nomap();
3498 }
3499}
3500# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003501
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003502/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003503 * Similar to requesting the version string: Request the terminal background
3504 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003505 */
3506 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003507may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003508{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003509 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003510 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003511 /* Only request background if t_RB is set and 'background' wasn't
3512 * changed. */
3513 if (rbg_status == STATUS_GET
3514 && *T_RBG != NUL
3515 && !option_was_set((char_u *)"bg"))
3516 {
3517 LOG_TR("Sending BG request");
3518 out_str(T_RBG);
3519 rbg_status = STATUS_SENT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003520
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003521 /* check for the characters now, otherwise they might be eaten by
3522 * get_keystroke() */
3523 out_flush();
3524 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003525 }
3526 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003527}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003528
Bram Moolenaar2951b772013-07-03 12:45:31 +02003529# ifdef DEBUG_TERMRESPONSE
3530 static void
3531log_tr(char *msg)
3532{
3533 static FILE *fd_tr = NULL;
3534 static proftime_T start;
3535 proftime_T now;
3536
3537 if (fd_tr == NULL)
3538 {
3539 fd_tr = fopen("termresponse.log", "w");
3540 profile_start(&start);
3541 }
3542 now = start;
3543 profile_end(&now);
3544 fprintf(fd_tr, "%s: %s %s\n",
3545 profile_msg(&now),
3546 must_redraw == NOT_VALID ? "NV"
3547 : must_redraw == CLEAR ? "CL" : " ",
3548 msg);
3549}
3550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552
3553/*
3554 * Return TRUE when saving and restoring the screen.
3555 */
3556 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003557swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 return (full_screen && *T_TI != NUL);
3560}
3561
3562#ifdef FEAT_MOUSE
3563/*
3564 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3565 */
3566 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003567setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569# ifdef FEAT_MOUSE_TTY
3570 int checkfor;
3571# endif
3572
3573# ifdef FEAT_MOUSESHAPE
3574 update_mouseshape(-1);
3575# endif
3576
3577# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3578# ifdef FEAT_GUI
3579 /* In the GUI the mouse is always enabled. */
3580 if (gui.in_use)
3581 return;
3582# endif
3583 /* be quick when mouse is off */
3584 if (*p_mouse == NUL || has_mouse_termcode == 0)
3585 return;
3586
3587 /* don't switch mouse on when not in raw mode (Ex mode) */
3588 if (cur_tmode != TMODE_RAW)
3589 {
3590 mch_setmouse(FALSE);
3591 return;
3592 }
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (VIsual_active)
3595 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003596 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 checkfor = MOUSE_RETURN;
3598 else if (State & INSERT)
3599 checkfor = MOUSE_INSERT;
3600 else if (State & CMDLINE)
3601 checkfor = MOUSE_COMMAND;
3602 else if (State == CONFIRM || State == EXTERNCMD)
3603 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3604 else
3605 checkfor = MOUSE_NORMAL; /* assume normal mode */
3606
3607 if (mouse_has(checkfor))
3608 mch_setmouse(TRUE);
3609 else
3610 mch_setmouse(FALSE);
3611# endif
3612}
3613
3614/*
3615 * Return TRUE if
3616 * - "c" is in 'mouse', or
3617 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3618 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3619 * normal editing mode (not at hit-return message).
3620 */
3621 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003622mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 char_u *p;
3625
3626 for (p = p_mouse; *p; ++p)
3627 switch (*p)
3628 {
3629 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3630 return TRUE;
3631 break;
3632 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3633 return TRUE;
3634 break;
3635 default: if (c == *p) return TRUE; break;
3636 }
3637 return FALSE;
3638}
3639
3640/*
3641 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3642 */
3643 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003644mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 return (p_mousem[0] == 'p');
3647}
3648#endif
3649
3650/*
3651 * By outputting the 'cursor very visible' termcap code, for some windowed
3652 * terminals this makes the screen scrolled to the correct position.
3653 * Used when starting Vim or returning from a shell.
3654 */
3655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003656scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003658 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003661 out_str(T_CVS);
3662 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664}
3665
3666static int cursor_is_off = FALSE;
3667
3668/*
3669 * Enable the cursor.
3670 */
3671 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003672cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (cursor_is_off)
3675 {
3676 out_str(T_VE);
3677 cursor_is_off = FALSE;
3678 }
3679}
3680
3681/*
3682 * Disable the cursor.
3683 */
3684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003685cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003687 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003689 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 cursor_is_off = TRUE;
3691 }
3692}
3693
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003694#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003696 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003697 */
3698 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003699term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003700{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003701 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003702 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003703
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003704 /* Only do something when redrawing the screen and we can restore the
3705 * mode. */
3706 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003707 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003708# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003709 if (forced && initial_cursor_shape > 0)
3710 /* Restore to initial values. */
3711 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003712# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003713 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003714 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003715
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003716 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003717 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003718 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003719 {
3720 if (*T_CSR != NUL)
3721 p = T_CSR; /* Replace mode cursor */
3722 else
3723 p = T_CSI; /* fall back to Insert mode cursor */
3724 if (*p != NUL)
3725 {
3726 out_str(p);
3727 showing_mode = REPLACE;
3728 }
3729 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003730 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003731 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003732 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003733 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003734 {
3735 out_str(T_CSI); /* Insert mode cursor */
3736 showing_mode = INSERT;
3737 }
3738 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003739 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003740 {
3741 out_str(T_CEI); /* non-Insert mode cursor */
3742 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003743 }
3744}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003745
3746# if defined(FEAT_TERMINAL) || defined(PROTO)
3747 void
3748term_cursor_color(char_u *color)
3749{
3750 if (*T_CSC != NUL)
3751 {
3752 out_str(T_CSC); /* set cursor color start */
3753 out_str_nf(color);
3754 out_str(T_CEC); /* set cursor color end */
3755 out_flush();
3756 }
3757}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003758# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003759
Bram Moolenaar4db25542017-08-28 22:43:05 +02003760 int
3761blink_state_is_inverted()
3762{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003763#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003764 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3765 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003766#else
3767 return FALSE;
3768#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003769}
3770
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003771/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003772 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003773 */
3774 void
3775term_cursor_shape(int shape, int blink)
3776{
3777 if (*T_CSH != NUL)
3778 {
3779 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3780 out_flush();
3781 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003782 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003783 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003784 int do_blink = blink;
3785
3786 /* t_SH is empty: try setting just the blink state.
3787 * The blink flags are XORed together, if the initial blinking from
3788 * style and shape differs, we need to invert the flag here. */
3789 if (blink_state_is_inverted())
3790 do_blink = !blink;
3791
3792 if (do_blink && *T_VS != NUL)
3793 {
3794 out_str(T_VS);
3795 out_flush();
3796 }
3797 else if (!do_blink && *T_CVS != NUL)
3798 {
3799 out_str(T_CVS);
3800 out_flush();
3801 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003802 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003803}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003804#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003805
3806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 * Set scrolling region for window 'wp'.
3808 * The region starts 'off' lines from the start of the window.
3809 * Also set the vertical scroll region for a vertically split window. Always
3810 * the full width of the window, excluding the vertical separator.
3811 */
3812 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003813scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3816 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003818 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3819 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 screen_start(); /* don't know where cursor is now */
3821}
3822
3823/*
3824 * Reset scrolling region to the whole screen.
3825 */
3826 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003827scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (*T_CSV != NUL)
3831 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 screen_start(); /* don't know where cursor is now */
3833}
3834
3835
3836/*
3837 * List of terminal codes that are currently recognized.
3838 */
3839
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003840static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
3842 char_u name[2]; /* termcap name of entry */
3843 char_u *code; /* terminal code (in allocated memory) */
3844 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003845 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846} *termcodes = NULL;
3847
3848static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3849static int tc_len = 0; /* current number of entries in termcodes[] */
3850
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003851static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003854clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 while (tc_len > 0)
3857 vim_free(termcodes[--tc_len].code);
3858 vim_free(termcodes);
3859 termcodes = NULL;
3860 tc_max_len = 0;
3861
3862#ifdef HAVE_TGETENT
3863 BC = (char *)empty_option;
3864 UP = (char *)empty_option;
3865 PC = NUL; /* set pad character to NUL */
3866 ospeed = 0;
3867#endif
3868
3869 need_gather = TRUE; /* need to fill termleader[] */
3870}
3871
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003872#define ATC_FROM_TERM 55
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874/*
3875 * Add a new entry to the list of terminal codes.
3876 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003877 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3878 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
3880 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003881add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 struct termcode *new_tc;
3884 int i, j;
3885 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003886 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 if (string == NULL || *string == NUL)
3889 {
3890 del_termcode(name);
3891 return;
3892 }
3893
Bram Moolenaar45500912014-07-09 20:51:07 +02003894#if defined(WIN3264) && !defined(FEAT_GUI)
3895 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3896#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (s == NULL)
3900 return;
3901
3902 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003905 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 s[0] = term_7to8bit(string);
3907 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003908
3909#if defined(WIN3264) && !defined(FEAT_GUI)
3910 if (s[0] == K_NUL)
3911 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003912 STRMOVE(s + 1, s);
3913 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003914 }
3915#endif
3916
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003917 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 need_gather = TRUE; /* need to fill termleader[] */
3920
3921 /*
3922 * need to make space for more entries
3923 */
3924 if (tc_len == tc_max_len)
3925 {
3926 tc_max_len += 20;
3927 new_tc = (struct termcode *)alloc(
3928 (unsigned)(tc_max_len * sizeof(struct termcode)));
3929 if (new_tc == NULL)
3930 {
3931 tc_max_len -= 20;
3932 return;
3933 }
3934 for (i = 0; i < tc_len; ++i)
3935 new_tc[i] = termcodes[i];
3936 vim_free(termcodes);
3937 termcodes = new_tc;
3938 }
3939
3940 /*
3941 * Look for existing entry with the same name, it is replaced.
3942 * Look for an existing entry that is alphabetical higher, the new entry
3943 * is inserted in front of it.
3944 */
3945 for (i = 0; i < tc_len; ++i)
3946 {
3947 if (termcodes[i].name[0] < name[0])
3948 continue;
3949 if (termcodes[i].name[0] == name[0])
3950 {
3951 if (termcodes[i].name[1] < name[1])
3952 continue;
3953 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 */
3956 if (termcodes[i].name[1] == name[1])
3957 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 if (flags == ATC_FROM_TERM && (j = termcode_star(
3959 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003960 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003961 /* Don't replace ESC[123;*X or ESC O*X with another when
3962 * invoked from got_code_from_term(). */
3963 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003964 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 && s[len - 1]
3966 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003967 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003969 vim_free(s);
3970 return;
3971 }
3972 }
3973 else
3974 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003976 vim_free(termcodes[i].code);
3977 --tc_len;
3978 break;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982 /*
3983 * Found alphabetical larger entry, move rest to insert new entry
3984 */
3985 for (j = tc_len; j > i; --j)
3986 termcodes[j] = termcodes[j - 1];
3987 break;
3988 }
3989
3990 termcodes[i].name[0] = name[0];
3991 termcodes[i].name[1] = name[1];
3992 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003993 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994
3995 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3996 * accept modifiers. */
3997 termcodes[i].modlen = 0;
3998 j = termcode_star(s, len);
3999 if (j > 0)
4000 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 ++tc_len;
4002}
4003
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004005 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004007 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004008 */
4009 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004010termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011{
4012 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4013 if (len >= 3 && code[len - 2] == '*')
4014 {
4015 if (len >= 5 && code[len - 3] == ';')
4016 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004017 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 return 1;
4019 }
4020 return 0;
4021}
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004024find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 int i;
4027
4028 for (i = 0; i < tc_len; ++i)
4029 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4030 return termcodes[i].code;
4031 return NULL;
4032}
4033
4034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4035 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004036get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038 if (i >= tc_len)
4039 return NULL;
4040 return &termcodes[i].name[0];
4041}
4042#endif
4043
4044 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004045del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 int i;
4048
4049 if (termcodes == NULL) /* nothing there yet */
4050 return;
4051
4052 need_gather = TRUE; /* need to fill termleader[] */
4053
4054 for (i = 0; i < tc_len; ++i)
4055 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4056 {
4057 del_termcode_idx(i);
4058 return;
4059 }
4060 /* not found. Give error message? */
4061}
4062
4063 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004064del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 int i;
4067
4068 vim_free(termcodes[idx].code);
4069 --tc_len;
4070 for (i = idx; i < tc_len; ++i)
4071 termcodes[i] = termcodes[i + 1];
4072}
4073
4074#ifdef FEAT_TERMRESPONSE
4075/*
4076 * Called when detected that the terminal sends 8-bit codes.
4077 * Convert all 7-bit codes to their 8-bit equivalent.
4078 */
4079 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004080switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 int i;
4083 int c;
4084
4085 /* Only need to do something when not already using 8-bit codes. */
4086 if (!term_is_8bit(T_NAME))
4087 {
4088 for (i = 0; i < tc_len; ++i)
4089 {
4090 c = term_7to8bit(termcodes[i].code);
4091 if (c != 0)
4092 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004093 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 termcodes[i].code[0] = c;
4095 }
4096 }
4097 need_gather = TRUE; /* need to fill termleader[] */
4098 }
4099 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004100 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101}
4102#endif
4103
4104#ifdef CHECK_DOUBLE_CLICK
4105static linenr_T orig_topline = 0;
4106# ifdef FEAT_DIFF
4107static int orig_topfill = 0;
4108# endif
4109#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004110#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111/*
4112 * Checking for double clicks ourselves.
4113 * "orig_topline" is used to avoid detecting a double-click when the window
4114 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4115 */
4116/*
4117 * Set orig_topline. Used when jumping to another window, so that a double
4118 * click still works.
4119 */
4120 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004121set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 orig_topline = wp->w_topline;
4124# ifdef FEAT_DIFF
4125 orig_topfill = wp->w_topfill;
4126# endif
4127}
4128#endif
4129
4130/*
4131 * Check if typebuf.tb_buf[] contains a terminal key code.
4132 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4133 * + max_offset].
4134 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004135 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * With a match, the match is removed, the replacement code is inserted in
4137 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4138 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004139 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4140 * "buflen" is then the length of the string in buf[] and is updated for
4141 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
4143 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004144check_termcode(
4145 int max_offset,
4146 char_u *buf,
4147 int bufsize,
4148 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 char_u *tp;
4151 char_u *p;
4152 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004155 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int offset;
4157 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004158 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004159 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int new_slen;
4162 int extra;
4163 char_u string[MAX_KEY_CODE_LEN + 1];
4164 int i, j;
4165 int idx = 0;
4166#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004167# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4168 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 char_u bytes[6];
4170 int num_bytes;
4171# endif
4172 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int is_click, is_drag;
4174 int wheel_code = 0;
4175 int current_button;
4176 static int held_button = MOUSE_RELEASE;
4177 static int orig_num_clicks = 1;
4178 static int orig_mouse_code = 0x0;
4179# ifdef CHECK_DOUBLE_CLICK
4180 static int orig_mouse_col = 0;
4181 static int orig_mouse_row = 0;
4182 static struct timeval orig_mouse_time = {0, 0};
4183 /* time of previous mouse click */
4184 struct timeval mouse_time; /* time of current mouse click */
4185 long timediff; /* elapsed time in msec */
4186# endif
4187#endif
4188 int cpo_koffset;
4189#ifdef FEAT_MOUSE_GPM
4190 extern int gpm_flag; /* gpm library variable */
4191#endif
4192
4193 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4194
4195 /*
4196 * Speed up the checks for terminal codes by gathering all first bytes
4197 * used in termleader[]. Often this is just a single <Esc>.
4198 */
4199 if (need_gather)
4200 gather_termleader();
4201
4202 /*
4203 * Check at several positions in typebuf.tb_buf[], to catch something like
4204 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4205 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004206 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 * This is used often, KEEP IT FAST!
4208 */
4209 for (offset = 0; offset < max_offset; ++offset)
4210 {
4211 if (buf == NULL)
4212 {
4213 if (offset >= typebuf.tb_len)
4214 break;
4215 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4216 len = typebuf.tb_len - offset; /* length of the input */
4217 }
4218 else
4219 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004220 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 break;
4222 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004223 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225
4226 /*
4227 * Don't check characters after K_SPECIAL, those are already
4228 * translated terminal chars (avoid translating ~@^Hx).
4229 */
4230 if (*tp == K_SPECIAL)
4231 {
4232 offset += 2; /* there are always 2 extra characters */
4233 continue;
4234 }
4235
4236 /*
4237 * Skip this position if the character does not appear as the first
4238 * character in term_strings. This speeds up a lot, since most
4239 * termcodes start with the same character (ESC or CSI).
4240 */
4241 i = *tp;
4242 for (p = termleader; *p && *p != i; ++p)
4243 ;
4244 if (*p == NUL)
4245 continue;
4246
4247 /*
4248 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4249 * are in Insert mode.
4250 */
4251 if (*tp == ESC && !p_ek && (State & INSERT))
4252 continue;
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004255 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258#ifdef FEAT_GUI
4259 if (gui.in_use)
4260 {
4261 /*
4262 * GUI special key codes are all of the form [CSI xx].
4263 */
4264 if (*tp == CSI) /* Special key from GUI */
4265 {
4266 if (len < 3)
4267 return -1; /* Shouldn't happen */
4268 slen = 3;
4269 key_name[0] = tp[1];
4270 key_name[1] = tp[2];
4271 }
4272 }
4273 else
4274#endif /* FEAT_GUI */
4275 {
4276 for (idx = 0; idx < tc_len; ++idx)
4277 {
4278 /*
4279 * Ignore the entry if we are not at the start of
4280 * typebuf.tb_buf[]
4281 * and there are not enough characters to make a match.
4282 * But only when the 'K' flag is in 'cpoptions'.
4283 */
4284 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004285 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (cpo_koffset && offset && len < slen)
4287 continue;
4288 if (STRNCMP(termcodes[idx].code, tp,
4289 (size_t)(slen > len ? len : slen)) == 0)
4290 {
4291 if (len < slen) /* got a partial sequence */
4292 return -1; /* need to get more chars */
4293
4294 /*
4295 * When found a keypad key, check if there is another key
4296 * that matches and use that one. This makes <Home> to be
4297 * found instead of <kHome> when they produce the same
4298 * key code.
4299 */
4300 if (termcodes[idx].name[0] == 'K'
4301 && VIM_ISDIGIT(termcodes[idx].name[1]))
4302 {
4303 for (j = idx + 1; j < tc_len; ++j)
4304 if (termcodes[j].len == slen &&
4305 STRNCMP(termcodes[idx].code,
4306 termcodes[j].code, slen) == 0)
4307 {
4308 idx = j;
4309 break;
4310 }
4311 }
4312
4313 key_name[0] = termcodes[idx].name[0];
4314 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 break;
4316 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004317
4318 /*
4319 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 * <Esc>[123;*X (modslen == slen - 3)
4321 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4322 * When there is a modifier the * matches a number.
4323 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004324 */
4325 if (termcodes[idx].modlen > 0)
4326 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004327 modslen = termcodes[idx].modlen;
4328 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004329 continue;
4330 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004332 {
4333 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004334
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004336 return -1; /* need to get more chars */
4337
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004338 if (tp[modslen] == termcodes[idx].code[slen - 1])
4339 slen = modslen + 1; /* no modifiers */
4340 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004341 continue; /* no match */
4342 else
4343 {
4344 /* Skip over the digits, the final char must
4345 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004346 for (j = slen - 2; j < len && (isdigit(tp[j])
4347 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004348 ;
4349 ++j;
4350 if (len < j) /* got a partial sequence */
4351 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4353 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354
Bram Moolenaara529ce02017-06-22 22:37:57 +02004355 modifiers_start = tp + slen - 2;
4356
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004357 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004358 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004359 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004360 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004361 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004362 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004363 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004364 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004365 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004367
4368 slen = j;
4369 }
4370 key_name[0] = termcodes[idx].name[0];
4371 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004372 break;
4373 }
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377
4378#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004379 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004380 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004381 * detecting the start of these mouse codes they might as well be
4382 * another key code or terminal response. */
4383# ifdef FEAT_MOUSE_DEC
4384 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004385# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004386# ifdef FEAT_MOUSE_PTERM
4387 || key_name[0] == KS_PTERM_MOUSE
4388# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004389 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004391 /* Check for some responses from the terminal starting with
4392 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004393 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004394 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004395 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004396 * Also eat other possible responses to t_RV, rxvt returns
4397 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4398 * mrxvt has been reported to have "+" in the version. Assume
4399 * the escape sequence ends with a letter or one of "{|}~".
4400 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004401 * - Cursor position report: <Esc>[{row};{col}R
4402 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004403 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004404 *
4405 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004406 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004407 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004408
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004409 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004410 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004411 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004412 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004414 int col = 0;
4415 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004416#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004417 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004418#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004421 for (i = 2 + (tp[0] != CSI); i < len
4422 && !(tp[i] >= '{' && tp[i] <= '~')
4423 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004424 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004425 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004426 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004427#ifdef FEAT_MBYTE
4428 row_char = tp[i - 1];
4429#endif
4430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004432 {
4433 LOG_TR("Not enough characters for CRV");
4434 return -1;
4435 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004436 if (extra > 0)
4437 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004438
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004439#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004440 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4441 * u7_status is not "sent", it may be from a previous Vim that
4442 * just exited. But not for <S-F3>, it sends something
4443 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004444 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004445 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004446 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004447 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004448 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004449
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004450 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004451 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004452# ifdef FEAT_AUTOCMD
4453 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004454# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004455 if (col == 2)
4456 aw = "single";
4457 else if (col == 3)
4458 aw = "double";
4459 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4460 {
4461 /* Setting the option causes a screen redraw. Do
4462 * that right away if possible, keeping any
4463 * messages. */
4464 set_option_value((char_u *)"ambw", 0L,
4465 (char_u *)aw, 0);
4466# ifdef DEBUG_TERMRESPONSE
4467 {
4468 char buf[100];
4469 int r = redraw_asap(CLEAR);
4470
4471 sprintf(buf,
4472 "set 'ambiwidth', redraw_asap(): %d",
4473 r);
4474 log_tr(buf);
4475 }
4476# else
4477 redraw_asap(CLEAR);
4478# endif
4479 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004480 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004481 key_name[0] = (int)KS_EXTRA;
4482 key_name[1] = (int)KE_IGNORE;
4483 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004484# ifdef FEAT_EVAL
4485 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4486# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004487 }
4488 else
4489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004491 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004493 int version = col;
4494
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004495 LOG_TR("Received CRV response");
4496 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004497# ifdef FEAT_AUTOCMD
4498 did_cursorhold = TRUE;
4499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 /* If this code starts with CSI, you can bet that the
4502 * terminal uses 8-bit codes. */
4503 if (tp[0] == CSI)
4504 switch_to_8bit();
4505
4506 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004507 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 * Ignore it for when the user has set 'term' to xterm,
4509 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004510 if (version > 20000)
4511 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004513 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004515 int need_flush = FALSE;
4516
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004517 /* Only set 'ttymouse' automatically if it was not set
4518 * by the user already. */
4519 if (!option_was_set((char_u *)"ttym"))
4520 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004521# ifdef TTYM_SGR
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004522 if (version >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004523 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004524 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004525 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004526# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004527 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004528 if (version >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004529 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004531 }
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004534 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004536 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 check_for_codes = TRUE;
4538 need_gather = TRUE;
4539 req_codes_from_term();
4540 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004541
4542 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004543 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004544 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004545 {
4546 /* If run from Vim $COLORS is set to the number of
4547 * colors the terminal supports. Otherwise assume
4548 * 256, libvterm supports even more. */
4549 if (mch_getenv((char_u *)"COLORS") == NULL)
4550 may_adjust_color_count(256);
4551 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004552
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004553 /* Detect terminals that set $TERM to something like
4554 * "xterm-256colors" but are not fully xterm
4555 * compatible. */
Bram Moolenaarc2127982017-09-11 20:34:13 +02004556
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004557 /* Mac Terminal.app sends 1;95;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004558 if (version == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004559 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004560 is_not_xterm = TRUE;
Bram Moolenaarc2127982017-09-11 20:34:13 +02004561
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004562 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004563 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004564 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004565 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004566 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004567 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004568 is_not_xterm = TRUE;
4569
Bram Moolenaar2e49b6b2017-09-06 22:08:16 +02004570 /* PuTTY sends 0;136;0
4571 * vandyke SecureCRT sends 1;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004572 if (version == 136
Bram Moolenaar618d6d22017-09-07 12:59:25 +02004573 && STRNCMP(tp + extra - 1, ";136;0c", 7) == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004574 is_not_xterm = TRUE;
4575
4576 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004577 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004578 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004579 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004580
4581 /* Only request the cursor style if t_SH and t_RS are
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004582 * set. Only supported properly by xterm since version
4583 * 279 (otherwise it returns 0x18).
4584 * Not for Terminal.app, it can't handle t_RS, it
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004585 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004586 if (rcs_status == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004587 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004588 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004589 && *T_CSH != NUL
4590 && *T_CRS != NUL)
4591 {
4592 LOG_TR("Sending cursor style request");
4593 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004594 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004595 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004596 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004597
4598 /* Only request the cursor blink mode if t_RC set. Not
4599 * for Gnome terminal, it can't handle t_RC, it
4600 * echoes the characters to the screen. */
4601 if (rbm_status == STATUS_GET
4602 && !is_not_xterm
4603 && *T_CRC != NUL)
4604 {
4605 LOG_TR("Sending cursor blink mode request");
4606 out_str(T_CRC);
4607 rbm_status = STATUS_SENT;
4608 need_flush = TRUE;
4609 }
4610
4611 if (need_flush)
4612 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004614 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004616 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617# endif
4618# ifdef FEAT_AUTOCMD
4619 apply_autocmds(EVENT_TERMRESPONSE,
4620 NULL, NULL, FALSE, curbuf);
4621# endif
4622 key_name[0] = (int)KS_EXTRA;
4623 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004625
Bram Moolenaar4db25542017-08-28 22:43:05 +02004626 /* Check blinking cursor from xterm:
4627 * {lead}?12;1$y set
4628 * {lead}?12;2$y not set
4629 *
4630 * {lead} can be <Esc>[ or CSI
4631 */
4632 else if (rbm_status == STATUS_SENT
4633 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4634 && i == j + 6
4635 && tp[j + 1] == '1'
4636 && tp[j + 2] == '2'
4637 && tp[j + 3] == ';'
4638 && tp[i - 1] == '$'
4639 && tp[i] == 'y')
4640 {
4641 initial_cursor_blink = (tp[j + 4] == '1');
4642 rbm_status = STATUS_GOT;
4643 LOG_TR("Received cursor blinking mode response");
4644 key_name[0] = (int)KS_EXTRA;
4645 key_name[1] = (int)KE_IGNORE;
4646 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004647# ifdef FEAT_EVAL
4648 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4649# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004650 }
4651
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004652 /*
4653 * Check for a window position response from the terminal:
4654 * {lead}3;{x}:{y}t
4655 */
4656 else if (waiting_for_winpos
4657 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4658 || (len >= 3 && tp[0] == CSI))
4659 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4660 && tp[j + 1] == ';')
4661 {
4662 j += 2;
4663 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4664 ;
4665 if (i < len && tp[i] == ';')
4666 {
4667 winpos_x = atoi((char *)tp + j);
4668 j = i + 1;
4669 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4670 ;
4671 if (i < len && tp[i] == 't')
4672 {
4673 winpos_y = atoi((char *)tp + j);
4674 /* got finished code: consume it */
4675 key_name[0] = (int)KS_EXTRA;
4676 key_name[1] = (int)KE_IGNORE;
4677 slen = i + 1;
4678 }
4679 }
4680 if (i == len)
4681 {
4682 LOG_TR("not enough characters for winpos");
4683 return -1;
4684 }
4685 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004686 }
4687
4688 /* Check for background color response from the terminal:
4689 *
4690 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4691 *
4692 * {lead} can be <Esc>] or OSC
4693 * {tail} can be '\007', <Esc>\ or STERM.
4694 *
4695 * Consume any code that starts with "{lead}11;", it's also
4696 * possible that "rgba" is following.
4697 */
4698 else if (*T_RBG != NUL
4699 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4700 || tp[0] == OSC))
4701 {
4702 j = 1 + (tp[0] == ESC);
4703 if (len >= j + 3 && (argp[0] != '1'
4704 || argp[1] != '1' || argp[2] != ';'))
4705 i = 0; /* no match */
4706 else
4707 for (i = j; i < len; ++i)
4708 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4709 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004710 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004711 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4712 && tp[j + 11] == '/' && tp[j + 16] == '/'
4713 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004714 {
4715 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4716 + tp[j+17]) ? "light" : "dark";
4717
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004718 LOG_TR("Received RBG response");
4719 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004720 if (STRCMP(p_bg, newval) != 0)
4721 {
4722 /* value differs, apply it */
4723 set_option_value((char_u *)"bg", 0L,
4724 (char_u *)newval, 0);
4725 reset_option_was_set((char_u *)"bg");
4726 redraw_asap(CLEAR);
4727 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004728 }
4729
4730 /* got finished code: consume it */
4731 key_name[0] = (int)KS_EXTRA;
4732 key_name[1] = (int)KE_IGNORE;
4733 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004734# ifdef FEAT_EVAL
4735 set_vim_var_string(VV_TERMRGBRESP, tp, slen);
4736# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004737 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004738 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004739 if (i == len)
4740 {
4741 LOG_TR("not enough characters for RB");
4742 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004746 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004747 * {lead}{flag}+r<hex bytes><{tail}
4748 *
4749 * {lead} can be <Esc>P or DCS
4750 * {flag} can be '0' or '1'
4751 * {tail} can be Esc>\ or STERM
4752 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004753 * Check for cursor shape response from xterm:
4754 * {lead}1$r<number> q{tail}
4755 *
4756 * {lead} can be <Esc>P or DCS
4757 * {tail} can be Esc>\ or STERM
4758 *
4759 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004760 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004761 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004762 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 || tp[0] == DCS))
4764 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004765 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004766 if (len < j + 3)
4767 i = len; /* need more chars */
4768 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004769 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004770 else if (argp[1] == '+')
4771 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004772 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004773 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004774 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 || tp[i] == STERM)
4776 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004777 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 got_code_from_term(tp + j, i);
4779 key_name[0] = (int)KS_EXTRA;
4780 key_name[1] = (int)KE_IGNORE;
4781 slen = i + 1 + (tp[i] == ESC);
4782 break;
4783 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004784 }
4785 else if ((len >= j + 6 && isdigit(argp[3]))
4786 && argp[4] == ' '
4787 && argp[5] == 'q')
4788 {
4789 /* cursor shape response */
4790 i = j + 6;
4791 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4792 || tp[i] == STERM)
4793 {
4794 int number = argp[3] - '0';
4795
4796 /* 0, 1 = block blink, 2 = block
4797 * 3 = underline blink, 4 = underline
4798 * 5 = vertical bar blink, 6 = vertical bar */
4799 number = number == 0 ? 1 : number;
4800 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004801 /* The blink flag is actually inverted, compared to
4802 * the value set with T_SH. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004803 initial_cursor_shape_blink =
4804 (number & 1) ? FALSE : TRUE;
4805 rcs_status = STATUS_GOT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004806 LOG_TR("Received cursor shape response");
4807
4808 key_name[0] = (int)KS_EXTRA;
4809 key_name[1] = (int)KE_IGNORE;
4810 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004811# ifdef FEAT_EVAL
4812 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
4813# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004814 }
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
4817 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004818 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004819 /* These codes arrive many together, each code can be
4820 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004821 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004822 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 }
4826#endif
4827
4828 if (key_name[0] == NUL)
4829 continue; /* No match at this position, try next one */
4830
4831 /* We only get here when we have a complete termcode match */
4832
4833#ifdef FEAT_MOUSE
4834# ifdef FEAT_GUI
4835 /*
4836 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4837 * so that we know which window to scroll later.
4838 */
4839 if (gui.in_use
4840 && key_name[0] == (int)KS_EXTRA
4841 && (key_name[1] == (int)KE_X1MOUSE
4842 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004843 || key_name[1] == (int)KE_MOUSELEFT
4844 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 || key_name[1] == (int)KE_MOUSEDOWN
4846 || key_name[1] == (int)KE_MOUSEUP))
4847 {
4848 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4849 if (num_bytes == -1) /* not enough coordinates */
4850 return -1;
4851 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4852 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4853 slen += num_bytes;
4854 }
4855 else
4856# endif
4857 /*
4858 * If it is a mouse click, get the coordinates.
4859 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004860 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004862 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863# endif
4864# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004865 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866# endif
4867# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004868 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869# endif
4870# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004871 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004873# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004874 || key_name[0] == KS_URXVT_MOUSE
4875# endif
4876# ifdef FEAT_MOUSE_SGR
4877 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004878 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 )
4881 {
4882 is_click = is_drag = FALSE;
4883
Bram Moolenaar864207d2008-06-24 22:14:38 +00004884# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4885 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (key_name[0] == (int)KS_MOUSE)
4887 {
4888 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004889 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 * s == encoded button state:
4891 * 0x20 = left button down
4892 * 0x21 = middle button down
4893 * 0x22 = right button down
4894 * 0x23 = any button release
4895 * 0x60 = button 4 down (scroll wheel down)
4896 * 0x61 = button 5 down (scroll wheel up)
4897 * add 0x04 for SHIFT
4898 * add 0x08 for ALT
4899 * add 0x10 for CTRL
4900 * add 0x20 for mouse drag (0x40 is drag with left button)
4901 * c == column + ' ' + 1 == column + 33
4902 * r == row + ' ' + 1 == row + 33
4903 *
4904 * The coordinates are passed on through global variables.
4905 * Ugly, but this avoids trouble with mouse clicks at an
4906 * unexpected moment and allows for mapping them.
4907 */
4908 for (;;)
4909 {
4910#ifdef FEAT_GUI
4911 if (gui.in_use)
4912 {
4913 /* GUI uses more bits for columns > 223 */
4914 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4915 if (num_bytes == -1) /* not enough coordinates */
4916 return -1;
4917 mouse_code = bytes[0];
4918 mouse_col = 128 * (bytes[1] - ' ' - 1)
4919 + bytes[2] - ' ' - 1;
4920 mouse_row = 128 * (bytes[3] - ' ' - 1)
4921 + bytes[4] - ' ' - 1;
4922 }
4923 else
4924#endif
4925 {
4926 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4927 if (num_bytes == -1) /* not enough coordinates */
4928 return -1;
4929 mouse_code = bytes[0];
4930 mouse_col = bytes[1] - ' ' - 1;
4931 mouse_row = bytes[2] - ' ' - 1;
4932 }
4933 slen += num_bytes;
4934
4935 /* If the following bytes is also a mouse code and it has
4936 * the same code, dump this one and get the next. This
4937 * makes dragging a whole lot faster. */
4938#ifdef FEAT_GUI
4939 if (gui.in_use)
4940 j = 3;
4941 else
4942#endif
4943 j = termcodes[idx].len;
4944 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4945 && tp[slen + j] == mouse_code
4946 && tp[slen + j + 1] != NUL
4947 && tp[slen + j + 2] != NUL
4948#ifdef FEAT_GUI
4949 && (!gui.in_use
4950 || (tp[slen + j + 3] != NUL
4951 && tp[slen + j + 4] != NUL))
4952#endif
4953 )
4954 slen += j;
4955 else
4956 break;
4957 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004960# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4961 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004962 || key_name[0] == KS_SGR_MOUSE
4963 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004964 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004965 /* URXVT 1015 mouse reporting mode:
4966 * Almost identical to xterm mouse mode, except the values
4967 * are decimal instead of bytes.
4968 *
4969 * \033[%d;%d;%dM
4970 * ^-- row
4971 * ^----- column
4972 * ^-------- code
4973 *
4974 * SGR 1006 mouse reporting mode:
4975 * Almost identical to xterm mouse mode, except the values
4976 * are decimal instead of bytes.
4977 *
4978 * \033[<%d;%d;%dM
4979 * ^-- row
4980 * ^----- column
4981 * ^-------- code
4982 *
4983 * \033[<%d;%d;%dm : mouse release event
4984 * ^-- row
4985 * ^----- column
4986 * ^-------- code
4987 */
4988 p = modifiers_start;
4989 if (p == NULL)
4990 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004991
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004992 mouse_code = getdigits(&p);
4993 if (*p++ != ';')
4994 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004995
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004996 /* when mouse reporting is SGR, add 32 to mouse code */
4997 if (key_name[0] == KS_SGR_MOUSE
4998 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4999 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005000
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005001 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5002 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005003
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005004 mouse_col = getdigits(&p) - 1;
5005 if (*p++ != ';')
5006 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005007
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005008 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005009
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005010 /* The modifiers were the mouse coordinates, not the
5011 * modifier keys (alt/shift/ctrl/meta) state. */
5012 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005013 }
5014# endif
5015
5016 if (key_name[0] == (int)KS_MOUSE
5017#ifdef FEAT_MOUSE_URXVT
5018 || key_name[0] == (int)KS_URXVT_MOUSE
5019#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005020#ifdef FEAT_MOUSE_SGR
5021 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005022 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005023#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005024 )
5025 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005026# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /*
5028 * Handle mouse events.
5029 * Recognize the xterm mouse wheel, but not in the GUI, the
5030 * Linux console with GPM and the MS-DOS or Win32 console
5031 * (multi-clicks use >= 0x60).
5032 */
5033 if (mouse_code >= MOUSEWHEEL_LOW
5034# ifdef FEAT_GUI
5035 && !gui.in_use
5036# endif
5037# ifdef FEAT_MOUSE_GPM
5038 && gpm_flag == 0
5039# endif
5040 )
5041 {
5042 /* Keep the mouse_code before it's changed, so that we
5043 * remember that it was a mouse wheel click. */
5044 wheel_code = mouse_code;
5045 }
5046# ifdef FEAT_MOUSE_XTERM
5047 else if (held_button == MOUSE_RELEASE
5048# ifdef FEAT_GUI
5049 && !gui.in_use
5050# endif
5051 && (mouse_code == 0x23 || mouse_code == 0x24))
5052 {
5053 /* Apparently used by rxvt scroll wheel. */
5054 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5055 }
5056# endif
5057
5058# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5059 else if (use_xterm_mouse() > 1)
5060 {
5061 if (mouse_code & MOUSE_DRAG_XTERM)
5062 mouse_code |= MOUSE_DRAG;
5063 }
5064# endif
5065# ifdef FEAT_XCLIPBOARD
5066 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5067 {
5068 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5069 stop_xterm_trace();
5070 else
5071 start_xterm_trace(mouse_code);
5072 }
5073# endif
5074# endif
5075 }
5076# endif /* !UNIX || FEAT_MOUSE_XTERM */
5077# ifdef FEAT_MOUSE_NET
5078 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5079 {
5080 int mc, mr;
5081
5082 /* expect a rather limited sequence like: balancing {
5083 * \033}6,45\r
5084 * '6' is the row, 45 is the column
5085 */
5086 p = tp + slen;
5087 mr = getdigits(&p);
5088 if (*p++ != ',')
5089 return -1;
5090 mc = getdigits(&p);
5091 if (*p++ != '\r')
5092 return -1;
5093
5094 mouse_col = mc - 1;
5095 mouse_row = mr - 1;
5096 mouse_code = MOUSE_LEFT;
5097 slen += (int)(p - (tp + slen));
5098 }
5099# endif /* FEAT_MOUSE_NET */
5100# ifdef FEAT_MOUSE_JSB
5101 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5102 {
5103 int mult, val, iter, button, status;
5104
5105 /* JSBTERM Input Model
5106 * \033[0~zw uniq escape sequence
5107 * (L-x) Left button pressed - not pressed x not reporting
5108 * (M-x) Middle button pressed - not pressed x not reporting
5109 * (R-x) Right button pressed - not pressed x not reporting
5110 * (SDmdu) Single , Double click, m mouse move d button down
5111 * u button up
5112 * ### X cursor position padded to 3 digits
5113 * ### Y cursor position padded to 3 digits
5114 * (s-x) SHIFT key pressed - not pressed x not reporting
5115 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005116 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
5118
5119 p = tp + slen;
5120 button = mouse_code = 0;
5121 switch (*p++)
5122 {
5123 case 'L': button = 1; break;
5124 case '-': break;
5125 case 'x': break; /* ignore sequence */
5126 default: return -1; /* Unknown Result */
5127 }
5128 switch (*p++)
5129 {
5130 case 'M': button |= 2; break;
5131 case '-': break;
5132 case 'x': break; /* ignore sequence */
5133 default: return -1; /* Unknown Result */
5134 }
5135 switch (*p++)
5136 {
5137 case 'R': button |= 4; break;
5138 case '-': break;
5139 case 'x': break; /* ignore sequence */
5140 default: return -1; /* Unknown Result */
5141 }
5142 status = *p++;
5143 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5144 mult /= 10, p++)
5145 if (*p >= '0' && *p <= '9')
5146 val += (*p - '0') * mult;
5147 else
5148 return -1;
5149 mouse_col = val;
5150 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5151 mult /= 10, p++)
5152 if (*p >= '0' && *p <= '9')
5153 val += (*p - '0') * mult;
5154 else
5155 return -1;
5156 mouse_row = val;
5157 switch (*p++)
5158 {
5159 case 's': button |= 8; break; /* SHIFT key Pressed */
5160 case '-': break; /* Not Pressed */
5161 case 'x': break; /* Not Reporting */
5162 default: return -1; /* Unknown Result */
5163 }
5164 switch (*p++)
5165 {
5166 case 'c': button |= 16; break; /* CTRL key Pressed */
5167 case '-': break; /* Not Pressed */
5168 case 'x': break; /* Not Reporting */
5169 default: return -1; /* Unknown Result */
5170 }
5171 if (*p++ != '\033')
5172 return -1;
5173 if (*p++ != '\\')
5174 return -1;
5175 switch (status)
5176 {
5177 case 'D': /* Double Click */
5178 case 'S': /* Single Click */
5179 if (button & 1) mouse_code |= MOUSE_LEFT;
5180 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5181 if (button & 4) mouse_code |= MOUSE_RIGHT;
5182 if (button & 8) mouse_code |= MOUSE_SHIFT;
5183 if (button & 16) mouse_code |= MOUSE_CTRL;
5184 break;
5185 case 'm': /* Mouse move */
5186 if (button & 1) mouse_code |= MOUSE_LEFT;
5187 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5188 if (button & 4) mouse_code |= MOUSE_RIGHT;
5189 if (button & 8) mouse_code |= MOUSE_SHIFT;
5190 if (button & 16) mouse_code |= MOUSE_CTRL;
5191 if ((button & 7) != 0)
5192 {
5193 held_button = mouse_code;
5194 mouse_code |= MOUSE_DRAG;
5195 }
5196 is_drag = TRUE;
5197 showmode();
5198 break;
5199 case 'd': /* Button Down */
5200 if (button & 1) mouse_code |= MOUSE_LEFT;
5201 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5202 if (button & 4) mouse_code |= MOUSE_RIGHT;
5203 if (button & 8) mouse_code |= MOUSE_SHIFT;
5204 if (button & 16) mouse_code |= MOUSE_CTRL;
5205 break;
5206 case 'u': /* Button Up */
5207 if (button & 1)
5208 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5209 if (button & 2)
5210 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5211 if (button & 4)
5212 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5213 if (button & 8)
5214 mouse_code |= MOUSE_SHIFT;
5215 if (button & 16)
5216 mouse_code |= MOUSE_CTRL;
5217 break;
5218 default: return -1; /* Unknown Result */
5219 }
5220
5221 slen += (p - (tp + slen));
5222 }
5223# endif /* FEAT_MOUSE_JSB */
5224# ifdef FEAT_MOUSE_DEC
5225 if (key_name[0] == (int)KS_DEC_MOUSE)
5226 {
5227 /* The DEC Locator Input Model
5228 * Netterm delivers the code sequence:
5229 * \033[2;4;24;80&w (left button down)
5230 * \033[3;0;24;80&w (left button up)
5231 * \033[6;1;24;80&w (right button down)
5232 * \033[7;0;24;80&w (right button up)
5233 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5234 * Pe is the event code
5235 * Pb is the button code
5236 * Pr is the row coordinate
5237 * Pc is the column coordinate
5238 * Pp is the third coordinate (page number)
5239 * Pe, the event code indicates what event caused this report
5240 * The following event codes are defined:
5241 * 0 - request, the terminal received an explicit request
5242 * for a locator report, but the locator is unavailable
5243 * 1 - request, the terminal received an explicit request
5244 * for a locator report
5245 * 2 - left button down
5246 * 3 - left button up
5247 * 4 - middle button down
5248 * 5 - middle button up
5249 * 6 - right button down
5250 * 7 - right button up
5251 * 8 - fourth button down
5252 * 9 - fourth button up
5253 * 10 - locator outside filter rectangle
5254 * Pb, the button code, ASCII decimal 0-15 indicating which
5255 * buttons are down if any. The state of the four buttons
5256 * on the locator correspond to the low four bits of the
5257 * decimal value,
5258 * "1" means button depressed
5259 * 0 - no buttons down,
5260 * 1 - right,
5261 * 2 - middle,
5262 * 4 - left,
5263 * 8 - fourth
5264 * Pr is the row coordinate of the locator position in the page,
5265 * encoded as an ASCII decimal value.
5266 * If Pr is omitted, the locator position is undefined
5267 * (outside the terminal window for example).
5268 * Pc is the column coordinate of the locator position in the
5269 * page, encoded as an ASCII decimal value.
5270 * If Pc is omitted, the locator position is undefined
5271 * (outside the terminal window for example).
5272 * Pp is the page coordinate of the locator position
5273 * encoded as an ASCII decimal value.
5274 * The page coordinate may be omitted if the locator is on
5275 * page one (the default). We ignore it anyway.
5276 */
5277 int Pe, Pb, Pr, Pc;
5278
5279 p = tp + slen;
5280
5281 /* get event status */
5282 Pe = getdigits(&p);
5283 if (*p++ != ';')
5284 return -1;
5285
5286 /* get button status */
5287 Pb = getdigits(&p);
5288 if (*p++ != ';')
5289 return -1;
5290
5291 /* get row status */
5292 Pr = getdigits(&p);
5293 if (*p++ != ';')
5294 return -1;
5295
5296 /* get column status */
5297 Pc = getdigits(&p);
5298
5299 /* the page parameter is optional */
5300 if (*p == ';')
5301 {
5302 p++;
5303 (void)getdigits(&p);
5304 }
5305 if (*p++ != '&')
5306 return -1;
5307 if (*p++ != 'w')
5308 return -1;
5309
5310 mouse_code = 0;
5311 switch (Pe)
5312 {
5313 case 0: return -1; /* position request while unavailable */
5314 case 1: /* a response to a locator position request includes
5315 the status of all buttons */
5316 Pb &= 7; /* mask off and ignore fourth button */
5317 if (Pb & 4)
5318 mouse_code = MOUSE_LEFT;
5319 if (Pb & 2)
5320 mouse_code = MOUSE_MIDDLE;
5321 if (Pb & 1)
5322 mouse_code = MOUSE_RIGHT;
5323 if (Pb)
5324 {
5325 held_button = mouse_code;
5326 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005327 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 is_drag = TRUE;
5330 showmode();
5331 break;
5332 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005333 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 break;
5335 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5336 break;
5337 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005338 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 break;
5340 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5341 break;
5342 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005343 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 break;
5345 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5346 break;
5347 case 8: return -1; /* fourth button down */
5348 case 9: return -1; /* fourth button up */
5349 case 10: return -1; /* mouse outside of filter rectangle */
5350 default: return -1; /* should never occur */
5351 }
5352
5353 mouse_col = Pc - 1;
5354 mouse_row = Pr - 1;
5355
5356 slen += (int)(p - (tp + slen));
5357 }
5358# endif /* FEAT_MOUSE_DEC */
5359# ifdef FEAT_MOUSE_PTERM
5360 if (key_name[0] == (int)KS_PTERM_MOUSE)
5361 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005362 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364 p = tp + slen;
5365
5366 action = getdigits(&p);
5367 if (*p++ != ';')
5368 return -1;
5369
5370 mouse_row = getdigits(&p);
5371 if (*p++ != ';')
5372 return -1;
5373 mouse_col = getdigits(&p);
5374 if (*p++ != ';')
5375 return -1;
5376
5377 button = getdigits(&p);
5378 mouse_code = 0;
5379
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005380 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 case 4: mouse_code = MOUSE_LEFT; break;
5383 case 1: mouse_code = MOUSE_RIGHT; break;
5384 case 2: mouse_code = MOUSE_MIDDLE; break;
5385 default: return -1;
5386 }
5387
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005388 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 case 31: /* Initial press */
5391 if (*p++ != ';')
5392 return -1;
5393
5394 num_clicks = getdigits(&p); /* Not used */
5395 break;
5396
5397 case 32: /* Release */
5398 mouse_code |= MOUSE_RELEASE;
5399 break;
5400
5401 case 33: /* Drag */
5402 held_button = mouse_code;
5403 mouse_code |= MOUSE_DRAG;
5404 break;
5405
5406 default:
5407 return -1;
5408 }
5409
5410 if (*p++ != 't')
5411 return -1;
5412
5413 slen += (p - (tp + slen));
5414 }
5415# endif /* FEAT_MOUSE_PTERM */
5416
5417 /* Interpret the mouse code */
5418 current_button = (mouse_code & MOUSE_CLICK_MASK);
5419 if (current_button == MOUSE_RELEASE
5420# ifdef FEAT_MOUSE_XTERM
5421 && wheel_code == 0
5422# endif
5423 )
5424 {
5425 /*
5426 * If we get a mouse drag or release event when
5427 * there is no mouse button held down (held_button ==
5428 * MOUSE_RELEASE), produce a K_IGNORE below.
5429 * (can happen when you hold down two buttons
5430 * and then let them go, or click in the menu bar, but not
5431 * on a menu, and drag into the text).
5432 */
5433 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5434 is_drag = TRUE;
5435 current_button = held_button;
5436 }
5437 else if (wheel_code == 0)
5438 {
5439# ifdef CHECK_DOUBLE_CLICK
5440# ifdef FEAT_MOUSE_GPM
5441# ifdef FEAT_GUI
5442 /*
5443 * Only for Unix, when GUI or gpm is not active, we handle
5444 * multi-clicks here.
5445 */
5446 if (gpm_flag == 0 && !gui.in_use)
5447# else
5448 if (gpm_flag == 0)
5449# endif
5450# else
5451# ifdef FEAT_GUI
5452 if (!gui.in_use)
5453# endif
5454# endif
5455 {
5456 /*
5457 * Compute the time elapsed since the previous mouse click.
5458 */
5459 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005460 if (orig_mouse_time.tv_sec == 0)
5461 {
5462 /*
5463 * Avoid computing the difference between mouse_time
5464 * and orig_mouse_time for the first click, as the
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005465 * difference would be huge and would cause
5466 * multiplication overflow.
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005467 */
5468 timediff = p_mouset;
5469 }
5470 else
5471 {
5472 timediff = (mouse_time.tv_usec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005473 - orig_mouse_time.tv_usec) / 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005474 if (timediff < 0)
5475 --orig_mouse_time.tv_sec;
5476 timediff += (mouse_time.tv_sec
Bram Moolenaare22bbf62017-09-19 20:47:16 +02005477 - orig_mouse_time.tv_sec) * 1000;
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 orig_mouse_time = mouse_time;
5480 if (mouse_code == orig_mouse_code
5481 && timediff < p_mouset
5482 && orig_num_clicks != 4
5483 && orig_mouse_col == mouse_col
5484 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005485 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005487 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005489 )
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005490 /* Double click in tab pages line also works
5491 * when window contents changes. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005492 || (mouse_row == 0 && firstwin->w_winrow > 0))
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005493 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 ++orig_num_clicks;
5495 else
5496 orig_num_clicks = 1;
5497 orig_mouse_col = mouse_col;
5498 orig_mouse_row = mouse_row;
5499 orig_topline = curwin->w_topline;
5500#ifdef FEAT_DIFF
5501 orig_topfill = curwin->w_topfill;
5502#endif
5503 }
5504# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5505 else
5506 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5507# endif
5508# else
5509 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5510# endif
5511 is_click = TRUE;
5512 orig_mouse_code = mouse_code;
5513 }
5514 if (!is_drag)
5515 held_button = mouse_code & MOUSE_CLICK_MASK;
5516
5517 /*
5518 * Translate the actual mouse event into a pseudo mouse event.
5519 * First work out what modifiers are to be used.
5520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (orig_mouse_code & MOUSE_SHIFT)
5522 modifiers |= MOD_MASK_SHIFT;
5523 if (orig_mouse_code & MOUSE_CTRL)
5524 modifiers |= MOD_MASK_CTRL;
5525 if (orig_mouse_code & MOUSE_ALT)
5526 modifiers |= MOD_MASK_ALT;
5527 if (orig_num_clicks == 2)
5528 modifiers |= MOD_MASK_2CLICK;
5529 else if (orig_num_clicks == 3)
5530 modifiers |= MOD_MASK_3CLICK;
5531 else if (orig_num_clicks == 4)
5532 modifiers |= MOD_MASK_4CLICK;
5533
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005534 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5535 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005537 if (wheel_code != 0
5538 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005539 {
5540 if (wheel_code & MOUSE_CTRL)
5541 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005542 if (wheel_code & MOUSE_ALT)
5543 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 key_name[1] = (wheel_code & 1)
5545 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 else
5548 key_name[1] = get_pseudo_mouse_code(current_button,
5549 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005550
5551 /* Make sure the mouse position is valid. Some terminals may
5552 * return weird values. */
5553 if (mouse_col >= Columns)
5554 mouse_col = Columns - 1;
5555 if (mouse_row >= Rows)
5556 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558#endif /* FEAT_MOUSE */
5559
5560#ifdef FEAT_GUI
5561 /*
5562 * If using the GUI, then we get menu and scrollbar events.
5563 *
5564 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5565 * four bytes which are to be taken as a pointer to the vimmenu_T
5566 * structure.
5567 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005568 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005569 * is one byte with the tab index.
5570 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5572 * by one byte representing the scrollbar number, and then four bytes
5573 * representing a long_u which is the new value of the scrollbar.
5574 *
5575 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5576 * KE_FILLER followed by four bytes representing a long_u which is the
5577 * new value of the scrollbar.
5578 */
5579# ifdef FEAT_MENU
5580 else if (key_name[0] == (int)KS_MENU)
5581 {
5582 long_u val;
5583
5584 num_bytes = get_long_from_buf(tp + slen, &val);
5585 if (num_bytes == -1)
5586 return -1;
5587 current_menu = (vimmenu_T *)val;
5588 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005589
5590 /* The menu may have been deleted right after it was used, check
5591 * for that. */
5592 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5593 {
5594 key_name[0] = KS_EXTRA;
5595 key_name[1] = (int)KE_IGNORE;
5596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005599# ifdef FEAT_GUI_TABLINE
5600 else if (key_name[0] == (int)KS_TABLINE)
5601 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005602 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005603 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5604 if (num_bytes == -1)
5605 return -1;
5606 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005607 if (current_tab == 255) /* -1 in a byte gives 255 */
5608 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005609 slen += num_bytes;
5610 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005611 else if (key_name[0] == (int)KS_TABMENU)
5612 {
5613 /* Selecting tabline tab or using its menu. */
5614 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5615 if (num_bytes == -1)
5616 return -1;
5617 current_tab = (int)bytes[0];
5618 current_tabmenu = (int)bytes[1];
5619 slen += num_bytes;
5620 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622# ifndef USE_ON_FLY_SCROLL
5623 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5624 {
5625 long_u val;
5626
5627 /* Get the last scrollbar event in the queue of the same type */
5628 j = 0;
5629 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5630 && tp[j + 2] != NUL; ++i)
5631 {
5632 j += 3;
5633 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5634 if (num_bytes == -1)
5635 break;
5636 if (i == 0)
5637 current_scrollbar = (int)bytes[0];
5638 else if (current_scrollbar != (int)bytes[0])
5639 break;
5640 j += num_bytes;
5641 num_bytes = get_long_from_buf(tp + j, &val);
5642 if (num_bytes == -1)
5643 break;
5644 scrollbar_value = val;
5645 j += num_bytes;
5646 slen = j;
5647 }
5648 if (i == 0) /* not enough characters to make one */
5649 return -1;
5650 }
5651 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5652 {
5653 long_u val;
5654
5655 /* Get the last horiz. scrollbar event in the queue */
5656 j = 0;
5657 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5658 && tp[j + 2] != NUL; ++i)
5659 {
5660 j += 3;
5661 num_bytes = get_long_from_buf(tp + j, &val);
5662 if (num_bytes == -1)
5663 break;
5664 scrollbar_value = val;
5665 j += num_bytes;
5666 slen = j;
5667 }
5668 if (i == 0) /* not enough characters to make one */
5669 return -1;
5670 }
5671# endif /* !USE_ON_FLY_SCROLL */
5672#endif /* FEAT_GUI */
5673
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005674 /*
5675 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5676 */
5677 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5678
5679 /*
5680 * Add any modifier codes to our string.
5681 */
5682 new_slen = 0; /* Length of what will replace the termcode */
5683 if (modifiers != 0)
5684 {
5685 /* Some keys have the modifier included. Need to handle that here
5686 * to make mappings work. */
5687 key = simplify_key(key, &modifiers);
5688 if (modifiers != 0)
5689 {
5690 string[new_slen++] = K_SPECIAL;
5691 string[new_slen++] = (int)KS_MODIFIER;
5692 string[new_slen++] = modifiers;
5693 }
5694 }
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005697 key_name[0] = KEY2TERMCAP0(key);
5698 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005700 {
5701 /* from ":set <M-b>=xx" */
5702#ifdef FEAT_MBYTE
5703 if (has_mbyte)
5704 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5705 else
5706#endif
5707 string[new_slen++] = key_name[1];
5708 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005709 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5710 && key_name[1] == KE_IGNORE)
5711 {
5712 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5713 * to indicate what happened. */
5714 retval = KEYLEN_REMOVED;
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 else
5717 {
5718 string[new_slen++] = K_SPECIAL;
5719 string[new_slen++] = key_name[0];
5720 string[new_slen++] = key_name[1];
5721 }
5722 string[new_slen] = NUL;
5723 extra = new_slen - slen;
5724 if (buf == NULL)
5725 {
5726 if (extra < 0)
5727 /* remove matched chars, taking care of noremap */
5728 del_typebuf(-extra, offset);
5729 else if (extra > 0)
5730 /* insert the extra space we need */
5731 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5732
5733 /*
5734 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5735 * typebuf.tb_buf[]!
5736 */
5737 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5738 (size_t)new_slen);
5739 }
5740 else
5741 {
5742 if (extra < 0)
5743 /* remove matched characters */
5744 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005745 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005747 {
5748 /* Insert the extra space we need. If there is insufficient
5749 * space return -1. */
5750 if (*buflen + extra + new_slen >= bufsize)
5751 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005753 (size_t)(*buflen - offset));
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005756 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005758 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760
Bram Moolenaar2951b772013-07-03 12:45:31 +02005761#ifdef FEAT_TERMRESPONSE
5762 LOG_TR("normal character");
5763#endif
5764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 return 0; /* no match found */
5766}
5767
5768/*
5769 * Replace any terminal code strings in from[] with the equivalent internal
5770 * vim representation. This is used for the "from" and "to" part of a
5771 * mapping, and the "to" part of a menu command.
5772 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5773 * '<'.
5774 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5775 *
5776 * The replacement is done in result[] and finally copied into allocated
5777 * memory. If this all works well *bufp is set to the allocated memory and a
5778 * pointer to it is returned. If something fails *bufp is set to NULL and from
5779 * is returned.
5780 *
5781 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5782 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5783 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5784 * instead of a CTRL-V.
5785 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005786 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005787replace_termcodes(
5788 char_u *from,
5789 char_u **bufp,
5790 int from_part,
5791 int do_lt, /* also translate <lt> */
5792 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 int i;
5795 int slen;
5796 int key;
5797 int dlen = 0;
5798 char_u *src;
5799 int do_backslash; /* backslash is a special character */
5800 int do_special; /* recognize <> key codes */
5801 int do_key_code; /* recognize raw key codes */
5802 char_u *result; /* buffer for resulting string */
5803
5804 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005805 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5807
5808 /*
5809 * Allocate space for the translation. Worst case a single character is
5810 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5811 */
5812 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5813 if (result == NULL) /* out of memory */
5814 {
5815 *bufp = NULL;
5816 return from;
5817 }
5818
5819 src = from;
5820
5821 /*
5822 * Check for #n at start only: function key n
5823 */
5824 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5825 {
5826 result[dlen++] = K_SPECIAL;
5827 result[dlen++] = 'k';
5828 if (src[1] == '0')
5829 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5830 else
5831 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5832 src += 2;
5833 }
5834
5835 /*
5836 * Copy each byte from *from to result[dlen]
5837 */
5838 while (*src != NUL)
5839 {
5840 /*
5841 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005842 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5845 {
5846#ifdef FEAT_EVAL
5847 /*
5848 * Replace <SID> by K_SNR <script-nr> _.
5849 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5850 */
5851 if (STRNICMP(src, "<SID>", 5) == 0)
5852 {
5853 if (current_SID <= 0)
5854 EMSG(_(e_usingsid));
5855 else
5856 {
5857 src += 5;
5858 result[dlen++] = K_SPECIAL;
5859 result[dlen++] = (int)KS_EXTRA;
5860 result[dlen++] = (int)KE_SNR;
5861 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5862 dlen += (int)STRLEN(result + dlen);
5863 result[dlen++] = '_';
5864 continue;
5865 }
5866 }
5867#endif
5868
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005869 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (slen)
5871 {
5872 dlen += slen;
5873 continue;
5874 }
5875 }
5876
5877 /*
5878 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5879 * Note that this is also checked after replacing the <> form.
5880 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5881 * it could be a character in the file.
5882 */
5883 if (do_key_code)
5884 {
5885 i = find_term_bykeys(src);
5886 if (i >= 0)
5887 {
5888 result[dlen++] = K_SPECIAL;
5889 result[dlen++] = termcodes[i].name[0];
5890 result[dlen++] = termcodes[i].name[1];
5891 src += termcodes[i].len;
5892 /* If terminal code matched, continue after it. */
5893 continue;
5894 }
5895 }
5896
5897#ifdef FEAT_EVAL
5898 if (do_special)
5899 {
5900 char_u *p, *s, len;
5901
5902 /*
5903 * Replace <Leader> by the value of "mapleader".
5904 * Replace <LocalLeader> by the value of "maplocalleader".
5905 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5906 */
5907 if (STRNICMP(src, "<Leader>", 8) == 0)
5908 {
5909 len = 8;
5910 p = get_var_value((char_u *)"g:mapleader");
5911 }
5912 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5913 {
5914 len = 13;
5915 p = get_var_value((char_u *)"g:maplocalleader");
5916 }
5917 else
5918 {
5919 len = 0;
5920 p = NULL;
5921 }
5922 if (len != 0)
5923 {
5924 /* Allow up to 8 * 6 characters for "mapleader". */
5925 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5926 s = (char_u *)"\\";
5927 else
5928 s = p;
5929 while (*s != NUL)
5930 result[dlen++] = *s++;
5931 src += len;
5932 continue;
5933 }
5934 }
5935#endif
5936
5937 /*
5938 * Remove CTRL-V and ignore the next character.
5939 * For "from" side the CTRL-V at the end is included, for the "to"
5940 * part it is removed.
5941 * If 'cpoptions' does not contain 'B', also accept a backslash.
5942 */
5943 key = *src;
5944 if (key == Ctrl_V || (do_backslash && key == '\\'))
5945 {
5946 ++src; /* skip CTRL-V or backslash */
5947 if (*src == NUL)
5948 {
5949 if (from_part)
5950 result[dlen++] = key;
5951 break;
5952 }
5953 }
5954
5955#ifdef FEAT_MBYTE
5956 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005957 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#endif
5959 {
5960 /*
5961 * If the character is K_SPECIAL, replace it with K_SPECIAL
5962 * KS_SPECIAL KE_FILLER.
5963 * If compiled with the GUI replace CSI with K_CSI.
5964 */
5965 if (*src == K_SPECIAL)
5966 {
5967 result[dlen++] = K_SPECIAL;
5968 result[dlen++] = KS_SPECIAL;
5969 result[dlen++] = KE_FILLER;
5970 }
5971# ifdef FEAT_GUI
5972 else if (*src == CSI)
5973 {
5974 result[dlen++] = K_SPECIAL;
5975 result[dlen++] = KS_EXTRA;
5976 result[dlen++] = (int)KE_CSI;
5977 }
5978# endif
5979 else
5980 result[dlen++] = *src;
5981 ++src;
5982 }
5983 }
5984 result[dlen] = NUL;
5985
5986 /*
5987 * Copy the new string to allocated memory.
5988 * If this fails, just return from.
5989 */
5990 if ((*bufp = vim_strsave(result)) != NULL)
5991 from = *bufp;
5992 vim_free(result);
5993 return from;
5994}
5995
5996/*
5997 * Find a termcode with keys 'src' (must be NUL terminated).
5998 * Return the index in termcodes[], or -1 if not found.
5999 */
6000 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006001find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006004 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 for (i = 0; i < tc_len; ++i)
6007 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006008 if (slen == termcodes[i].len
6009 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return i;
6011 }
6012 return -1;
6013}
6014
6015/*
6016 * Gather the first characters in the terminal key codes into a string.
6017 * Used to speed up check_termcode().
6018 */
6019 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006020gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 int i;
6023 int len = 0;
6024
6025#ifdef FEAT_GUI
6026 if (gui.in_use)
6027 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6028#endif
6029#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006030 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 termleader[len++] = DCS; /* the termcode response starts with DCS
6032 in 8-bit mode */
6033#endif
6034 termleader[len] = NUL;
6035
6036 for (i = 0; i < tc_len; ++i)
6037 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6038 {
6039 termleader[len++] = termcodes[i].code[0];
6040 termleader[len] = NUL;
6041 }
6042
6043 need_gather = FALSE;
6044}
6045
6046/*
6047 * Show all termcodes (for ":set termcap")
6048 * This code looks a lot like showoptions(), but is different.
6049 */
6050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006051show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 int col;
6054 int *items;
6055 int item_count;
6056 int run;
6057 int row, rows;
6058 int cols;
6059 int i;
6060 int len;
6061
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006062#define INC3 27 /* try to make three columns */
6063#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#define GAP 2 /* spaces between columns */
6065
6066 if (tc_len == 0) /* no terminal codes (must be GUI) */
6067 return;
6068 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6069 if (items == NULL)
6070 return;
6071
6072 /* Highlight title */
6073 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6074
6075 /*
6076 * do the loop two times:
6077 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006078 * 2. display the medium items (medium length strings)
6079 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006081 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 {
6083 /*
6084 * collect the items in items[]
6085 */
6086 item_count = 0;
6087 for (i = 0; i < tc_len; i++)
6088 {
6089 len = show_one_termcode(termcodes[i].name,
6090 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006091 if (len <= INC3 - GAP ? run == 1
6092 : len <= INC2 - GAP ? run == 2
6093 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 items[item_count++] = i;
6095 }
6096
6097 /*
6098 * display the items
6099 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006100 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006102 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (cols == 0)
6104 cols = 1;
6105 rows = (item_count + cols - 1) / cols;
6106 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006107 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 rows = item_count;
6109 for (row = 0; row < rows && !got_int; ++row)
6110 {
6111 msg_putchar('\n'); /* go to next line */
6112 if (got_int) /* 'q' typed in more */
6113 break;
6114 col = 0;
6115 for (i = row; i < item_count; i += rows)
6116 {
6117 msg_col = col; /* make columns */
6118 show_one_termcode(termcodes[items[i]].name,
6119 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006120 if (run == 2)
6121 col += INC2;
6122 else
6123 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
6125 out_flush();
6126 ui_breakcheck();
6127 }
6128 }
6129 vim_free(items);
6130}
6131
6132/*
6133 * Show one termcode entry.
6134 * Output goes into IObuff[]
6135 */
6136 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006137show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 char_u *p;
6140 int len;
6141
6142 if (name[0] > '~')
6143 {
6144 IObuff[0] = ' ';
6145 IObuff[1] = ' ';
6146 IObuff[2] = ' ';
6147 IObuff[3] = ' ';
6148 }
6149 else
6150 {
6151 IObuff[0] = 't';
6152 IObuff[1] = '_';
6153 IObuff[2] = name[0];
6154 IObuff[3] = name[1];
6155 }
6156 IObuff[4] = ' ';
6157
6158 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6159 if (p[1] != 't')
6160 STRCPY(IObuff + 5, p);
6161 else
6162 IObuff[5] = NUL;
6163 len = (int)STRLEN(IObuff);
6164 do
6165 IObuff[len++] = ' ';
6166 while (len < 17);
6167 IObuff[len] = NUL;
6168 if (code == NULL)
6169 len += 4;
6170 else
6171 len += vim_strsize(code);
6172
6173 if (printit)
6174 {
6175 msg_puts(IObuff);
6176 if (code == NULL)
6177 msg_puts((char_u *)"NULL");
6178 else
6179 msg_outtrans(code);
6180 }
6181 return len;
6182}
6183
6184#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6185/*
6186 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6187 * termcap codes from the terminal itself.
6188 * We get them one by one to avoid a very long response string.
6189 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006190static int xt_index_in = 0;
6191static int xt_index_out = 0;
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006194req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 xt_index_out = 0;
6197 xt_index_in = 0;
6198 req_more_codes_from_term();
6199}
6200
6201 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006202req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 char buf[11];
6205 int old_idx = xt_index_out;
6206
6207 /* Don't do anything when going to exit. */
6208 if (exiting)
6209 return;
6210
6211 /* Send up to 10 more requests out than we received. Avoid sending too
6212 * many, there can be a buffer overflow somewhere. */
6213 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6214 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006215# ifdef DEBUG_TERMRESPONSE
6216 char dbuf[100];
6217
6218 sprintf(dbuf, "Requesting XT %d: %s",
6219 xt_index_out, key_names[xt_index_out]);
6220 log_tr(dbuf);
6221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 sprintf(buf, "\033P+q%02x%02x\033\\",
6223 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6224 out_str_nf((char_u *)buf);
6225 ++xt_index_out;
6226 }
6227
6228 /* Send the codes out right away. */
6229 if (xt_index_out != old_idx)
6230 out_flush();
6231}
6232
6233/*
6234 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6235 * A "0" instead of the "1" indicates a code that isn't supported.
6236 * Both <name> and <string> are encoded in hex.
6237 * "code" points to the "0" or "1".
6238 */
6239 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006240got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242#define XT_LEN 100
6243 char_u name[3];
6244 char_u str[XT_LEN];
6245 int i;
6246 int j = 0;
6247 int c;
6248
6249 /* A '1' means the code is supported, a '0' means it isn't.
6250 * When half the length is > XT_LEN we can't use it.
6251 * Our names are currently all 2 characters. */
6252 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6253 {
6254 /* Get the name from the response and find it in the table. */
6255 name[0] = hexhex2nr(code + 3);
6256 name[1] = hexhex2nr(code + 5);
6257 name[2] = NUL;
6258 for (i = 0; key_names[i] != NULL; ++i)
6259 {
6260 if (STRCMP(key_names[i], name) == 0)
6261 {
6262 xt_index_in = i;
6263 break;
6264 }
6265 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006266# ifdef DEBUG_TERMRESPONSE
6267 {
6268 char buf[100];
6269
6270 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6271 log_tr(buf);
6272 }
6273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (key_names[i] != NULL)
6275 {
6276 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6277 str[j++] = c;
6278 str[j] = NUL;
6279 if (name[0] == 'C' && name[1] == 'o')
6280 {
6281 /* Color count is not a key code. */
6282 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006283 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285 else
6286 {
6287 /* First delete any existing entry with the same code. */
6288 i = find_term_bykeys(str);
6289 if (i >= 0)
6290 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006291 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
6293 }
6294 }
6295
6296 /* May request more codes now that we received one. */
6297 ++xt_index_in;
6298 req_more_codes_from_term();
6299}
6300
6301/*
6302 * Check if there are any unanswered requests and deal with them.
6303 * This is called before starting an external program or getting direct
6304 * keyboard input. We don't want responses to be send to that program or
6305 * handled as typed text.
6306 */
6307 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006308check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 int c;
6311
6312 /* If no codes requested or all are answered, no need to wait. */
6313 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6314 return;
6315
6316 /* Vgetc() will check for and handle any response.
6317 * Keep calling vpeekc() until we don't get any responses. */
6318 ++no_mapping;
6319 ++allow_keys;
6320 for (;;)
6321 {
6322 c = vpeekc();
6323 if (c == NUL) /* nothing available */
6324 break;
6325
6326 /* If a response is recognized it's replaced with K_IGNORE, must read
6327 * it from the input stream. If there is no K_IGNORE we can't do
6328 * anything, break here (there might be some responses further on, but
6329 * we don't want to throw away any typed chars). */
6330 if (c != K_SPECIAL && c != K_IGNORE)
6331 break;
6332 c = vgetc();
6333 if (c != K_IGNORE)
6334 {
6335 vungetc(c);
6336 break;
6337 }
6338 }
6339 --no_mapping;
6340 --allow_keys;
6341}
6342#endif
6343
6344#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6345/*
6346 * Translate an internal mapping/abbreviation representation into the
6347 * corresponding external one recognized by :map/:abbrev commands;
6348 * respects the current B/k/< settings of 'cpoption'.
6349 *
6350 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006351 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 *
6353 * It uses a growarray to build the translation string since the
6354 * latter can be wider than the original description. The caller has to
6355 * free the string afterwards.
6356 *
6357 * Returns NULL when there is a problem.
6358 */
6359 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006360translate_mapping(
6361 char_u *str,
6362 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 garray_T ga;
6365 int c;
6366 int modifiers;
6367 int cpo_bslash;
6368 int cpo_special;
6369 int cpo_keycode;
6370
6371 ga_init(&ga);
6372 ga.ga_itemsize = 1;
6373 ga.ga_growsize = 40;
6374
6375 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6376 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6377 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6378
6379 for (; *str; ++str)
6380 {
6381 c = *str;
6382 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6383 {
6384 modifiers = 0;
6385 if (str[1] == KS_MODIFIER)
6386 {
6387 str++;
6388 modifiers = *++str;
6389 c = *++str;
6390 }
6391 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6392 {
6393 int i;
6394
6395 /* try to find special key in termcodes */
6396 for (i = 0; i < tc_len; ++i)
6397 if (termcodes[i].name[0] == str[1]
6398 && termcodes[i].name[1] == str[2])
6399 break;
6400 if (i < tc_len)
6401 {
6402 ga_concat(&ga, termcodes[i].code);
6403 str += 2;
6404 continue; /* for (str) */
6405 }
6406 }
6407 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6408 {
6409 if (expmap && cpo_special)
6410 {
6411 ga_clear(&ga);
6412 return NULL;
6413 }
6414 c = TO_SPECIAL(str[1], str[2]);
6415 if (c == K_ZERO) /* display <Nul> as ^@ */
6416 c = NUL;
6417 str += 2;
6418 }
6419 if (IS_SPECIAL(c) || modifiers) /* special key */
6420 {
6421 if (expmap && cpo_special)
6422 {
6423 ga_clear(&ga);
6424 return NULL;
6425 }
6426 ga_concat(&ga, get_special_key_name(c, modifiers));
6427 continue; /* for (str) */
6428 }
6429 }
6430 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6431 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6432 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6433 if (c)
6434 ga_append(&ga, c);
6435 }
6436 ga_append(&ga, NUL);
6437 return (char_u *)(ga.ga_data);
6438}
6439#endif
6440
6441#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6442static char ksme_str[20];
6443static char ksmr_str[20];
6444static char ksmd_str[20];
6445
6446/*
6447 * For Win32 console: update termcap codes for existing console attributes.
6448 */
6449 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006450update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 struct builtin_term *p;
6453
6454 p = find_builtin_term(DEFAULT_TERM);
6455 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6456 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6457 attr | 0x08); /* FOREGROUND_INTENSITY */
6458 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6459 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6460
6461 while (p->bt_string != NULL)
6462 {
6463 if (p->bt_entry == (int)KS_ME)
6464 p->bt_string = &ksme_str[0];
6465 else if (p->bt_entry == (int)KS_MR)
6466 p->bt_string = &ksmr_str[0];
6467 else if (p->bt_entry == (int)KS_MD)
6468 p->bt_string = &ksmd_str[0];
6469 ++p;
6470 }
6471}
6472#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006473
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006474#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006475 static int
6476hex_digit(int c)
6477{
6478 if (isdigit(c))
6479 return c - '0';
6480 c = TOLOWER_ASC(c);
6481 if (c >= 'a' && c <= 'f')
6482 return c - 'a' + 10;
6483 return 0x1ffffff;
6484}
6485
6486 guicolor_T
6487gui_get_color_cmn(char_u *name)
6488{
Bram Moolenaar28726652017-01-06 18:16:19 +01006489 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6490 * values as used by the MS-Windows GDI api. It should be used only for
6491 * MS-Windows GDI builds. */
6492# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6493# undef RGB
6494# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006495# ifndef RGB
6496# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6497# endif
6498# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006499 FILE *fd;
6500 char line[LINE_LEN];
6501 char_u *fname;
6502 int r, g, b, i;
6503 guicolor_T color;
6504
6505 struct rgbcolor_table_S {
6506 char_u *color_name;
6507 guicolor_T color;
6508 };
6509
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006510 /* Only non X11 colors (not present in rgb.txt) and colors in
6511 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006512 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006513 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6514 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6515 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6516 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6517 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6518 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6519 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6520 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6521 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6522 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6523 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6524 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6525 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006526 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6527 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006528 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006529 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006530 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6531 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6532 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6533 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6534 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6535 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6536 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6537 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6538 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006539 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006540 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006541 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6542 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006543 };
6544
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006545 static struct rgbcolor_table_S *colornames_table;
6546 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006547
6548 if (name[0] == '#' && STRLEN(name) == 7)
6549 {
6550 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006551 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006552 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6553 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6554 if (color > 0xffffff)
6555 return INVALCOLOR;
6556 return color;
6557 }
6558
6559 /* Check if the name is one of the colors we know */
6560 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6561 if (STRICMP(name, rgb_table[i].color_name) == 0)
6562 return rgb_table[i].color;
6563
6564 /*
6565 * Last attempt. Look in the file "$VIM/rgb.txt".
6566 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006567 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006568 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006569 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006570
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006571 /* colornames_table not yet initialized */
6572 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6573 if (fname == NULL)
6574 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006575
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006576 fd = fopen((char *)fname, "rt");
6577 vim_free(fname);
6578 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006579 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006580 if (p_verbose > 1)
6581 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6582 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006583 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006584
6585 for (counting = 1; counting >= 0; --counting)
6586 {
6587 if (!counting)
6588 {
6589 colornames_table = (struct rgbcolor_table_S *)alloc(
6590 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6591 if (colornames_table == NULL)
6592 {
6593 fclose(fd);
6594 return INVALCOLOR;
6595 }
6596 rewind(fd);
6597 }
6598 size = 0;
6599
6600 while (!feof(fd))
6601 {
6602 size_t len;
6603 int pos;
6604
6605 ignoredp = fgets(line, LINE_LEN, fd);
6606 len = strlen(line);
6607
6608 if (len <= 1 || line[len - 1] != '\n')
6609 continue;
6610
6611 line[len - 1] = '\0';
6612
6613 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6614 if (i != 3)
6615 continue;
6616
6617 if (!counting)
6618 {
6619 char_u *s = vim_strsave((char_u *)line + pos);
6620
6621 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006622 {
6623 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006624 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006625 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006626 colornames_table[size].color_name = s;
6627 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6628 }
6629 size++;
6630 }
6631 }
6632 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006633 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006634
6635 for (i = 0; i < size; i++)
6636 if (STRICMP(name, colornames_table[i].color_name) == 0)
6637 return colornames_table[i].color;
6638
Bram Moolenaarab302212016-04-26 20:59:29 +02006639 return INVALCOLOR;
6640}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006641
6642 guicolor_T
6643gui_get_rgb_color_cmn(int r, int g, int b)
6644{
6645 guicolor_T color = RGB(r, g, b);
6646
6647 if (color > 0xffffff)
6648 return INVALCOLOR;
6649 return color;
6650}
Bram Moolenaarab302212016-04-26 20:59:29 +02006651#endif