blob: 7fa08c3dbd6ac728edd255b635c6737ba799f2a9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
79static void term_color(char_u *s, int n);
80static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static void req_codes_from_term(void);
83static void req_more_codes_from_term(void);
84static void got_code_from_term(char_u *code, int len);
85static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void del_termcode_idx(int idx);
93static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
98
99#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200117
118# define STATUS_GET 1 /* send request when switching to RAW mode */
119# define STATUS_SENT 2 /* did send request, waiting for response */
120# define STATUS_GOT 3 /* received response */
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200123static int crv_status = STATUS_GET;
124
Bram Moolenaar9584b312013-03-13 19:29:28 +0100125/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126static int u7_status = STATUS_GET;
127
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200128/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200129static int rbg_status = STATUS_GET;
130
Bram Moolenaar4db25542017-08-28 22:43:05 +0200131/* Request cursor blinking mode report: */
132static int rbm_status = STATUS_GET;
133
134/* Request cursor style report: */
135static int rcs_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136# endif
137
138/*
139 * Don't declare these variables if termcap.h contains them.
140 * Autoconf checks if these variables should be declared extern (not all
141 * systems have them).
142 * Some versions define ospeed to be speed_t, but that is incompatible with
143 * BSD, where ospeed is short and speed_t is long.
144 */
145# ifndef HAVE_OSPEED
146# ifdef OSPEED_EXTERN
147extern short ospeed;
148# else
149short ospeed;
150# endif
151# endif
152# ifndef HAVE_UP_BC_PC
153# ifdef UP_BC_PC_EXTERN
154extern char *UP, *BC, PC;
155# else
156char *UP, *BC, PC;
157# endif
158# endif
159
160# define TGETSTR(s, p) vim_tgetstr((s), (p))
161# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif /* HAVE_TGETENT */
164
165static int detected_8bit = FALSE; /* detected 8-bit terminal */
166
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200167#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200168/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200169 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200170static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200171
172/* The blink flag from the style response may be inverted from the actual
173 * blinking state, xterm XORs the flags. */
174static int initial_cursor_shape_blink = FALSE;
175
176/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200177static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200178#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200179
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000180static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181{
182
183#if defined(FEAT_GUI)
184/*
185 * GUI pseudo term-cap.
186 */
187 {(int)KS_NAME, "gui"},
188 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
189 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
190# ifdef TERMINFO
191 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
192# else
193 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
194# endif
195 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
196# ifdef TERMINFO
197 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
198 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100199# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
201# endif
202# else
203 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
204 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100205# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
207# endif
208# endif
209 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
210 /* attributes switched on with 'h', off with * 'H' */
211 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
212 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
213 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
214 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
215 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
216 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
217 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000218 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
219 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200220 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
221 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
223 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
224 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
225 {(int)KS_MS, "y"},
226 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100227 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {(int)KS_LE, "\b"}, /* cursor-left = BS */
229 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
230# ifdef TERMINFO
231 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
232# else
233 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
234# endif
235 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000236 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#endif
238
239#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
242/*
243 * Amiga console window, default for Amiga
244 */
245 {(int)KS_NAME, "amiga"},
246 {(int)KS_CE, "\033[K"},
247 {(int)KS_CD, "\033[J"},
248 {(int)KS_AL, "\033[L"},
249# ifdef TERMINFO
250 {(int)KS_CAL, "\033[%p1%dL"},
251# else
252 {(int)KS_CAL, "\033[%dL"},
253# endif
254 {(int)KS_DL, "\033[M"},
255# ifdef TERMINFO
256 {(int)KS_CDL, "\033[%p1%dM"},
257# else
258 {(int)KS_CDL, "\033[%dM"},
259# endif
260 {(int)KS_CL, "\014"},
261 {(int)KS_VI, "\033[0 p"},
262 {(int)KS_VE, "\033[1 p"},
263 {(int)KS_ME, "\033[0m"},
264 {(int)KS_MR, "\033[7m"},
265 {(int)KS_MD, "\033[1m"},
266 {(int)KS_SE, "\033[0m"},
267 {(int)KS_SO, "\033[33m"},
268 {(int)KS_US, "\033[4m"},
269 {(int)KS_UE, "\033[0m"},
270 {(int)KS_CZH, "\033[3m"},
271 {(int)KS_CZR, "\033[0m"},
272#if defined(__MORPHOS__) || defined(__AROS__)
273 {(int)KS_CCO, "8"}, /* allow 8 colors */
274# ifdef TERMINFO
275 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
276 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
277# else
278 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
279 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
280# endif
281 {(int)KS_OP, "\033[m"}, /* reset colors */
282#endif
283 {(int)KS_MS, "y"},
284 {(int)KS_UT, "y"}, /* guessed */
285 {(int)KS_LE, "\b"},
286# ifdef TERMINFO
287 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
288# else
289 {(int)KS_CM, "\033[%i%d;%dH"},
290# endif
291#if defined(__MORPHOS__)
292 {(int)KS_SR, "\033M"},
293#endif
294# ifdef TERMINFO
295 {(int)KS_CRI, "\033[%p1%dC"},
296# else
297 {(int)KS_CRI, "\033[%dC"},
298# endif
299 {K_UP, "\233A"},
300 {K_DOWN, "\233B"},
301 {K_LEFT, "\233D"},
302 {K_RIGHT, "\233C"},
303 {K_S_UP, "\233T"},
304 {K_S_DOWN, "\233S"},
305 {K_S_LEFT, "\233 A"},
306 {K_S_RIGHT, "\233 @"},
307 {K_S_TAB, "\233Z"},
308 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
309 {K_F2, "\233\061~"},
310 {K_F3, "\233\062~"},
311 {K_F4, "\233\063~"},
312 {K_F5, "\233\064~"},
313 {K_F6, "\233\065~"},
314 {K_F7, "\233\066~"},
315 {K_F8, "\233\067~"},
316 {K_F9, "\233\070~"},
317 {K_F10, "\233\071~"},
318 {K_S_F1, "\233\061\060~"},
319 {K_S_F2, "\233\061\061~"},
320 {K_S_F3, "\233\061\062~"},
321 {K_S_F4, "\233\061\063~"},
322 {K_S_F5, "\233\061\064~"},
323 {K_S_F6, "\233\061\065~"},
324 {K_S_F7, "\233\061\066~"},
325 {K_S_F8, "\233\061\067~"},
326 {K_S_F9, "\233\061\070~"},
327 {K_S_F10, "\233\061\071~"},
328 {K_HELP, "\233?~"},
329 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
330 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
331 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
332 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
333 {K_END, "\233\064\065~"}, /* 101 key keyboard */
334
335 {BT_EXTRA_KEYS, ""},
336 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
337 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
338 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
339# endif
340
341# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
342/*
343 * almost standard ANSI terminal, default for bebox
344 */
345 {(int)KS_NAME, "beos-ansi"},
346 {(int)KS_CE, "\033[K"},
347 {(int)KS_CD, "\033[J"},
348 {(int)KS_AL, "\033[L"},
349# ifdef TERMINFO
350 {(int)KS_CAL, "\033[%p1%dL"},
351# else
352 {(int)KS_CAL, "\033[%dL"},
353# endif
354 {(int)KS_DL, "\033[M"},
355# ifdef TERMINFO
356 {(int)KS_CDL, "\033[%p1%dM"},
357# else
358 {(int)KS_CDL, "\033[%dM"},
359# endif
360#ifdef BEOS_PR_OR_BETTER
361# ifdef TERMINFO
362 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
363# else
364 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
365# endif
366#endif
367 {(int)KS_CL, "\033[H\033[2J"},
368#ifdef notyet
369 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
370 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
371#endif
372 {(int)KS_ME, "\033[m"}, /* normal mode */
373 {(int)KS_MR, "\033[7m"}, /* reverse */
374 {(int)KS_MD, "\033[1m"}, /* bold */
375 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
376 {(int)KS_SE, "\033[m"}, /* standout end */
377 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
378 {(int)KS_CZR, "\033[m"}, /* italic end */
379 {(int)KS_US, "\033[4m"}, /* underscore mode */
380 {(int)KS_UE, "\033[m"}, /* underscore end */
381 {(int)KS_CCO, "8"}, /* allow 8 colors */
382# ifdef TERMINFO
383 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
384 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
385# else
386 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
387 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
388# endif
389 {(int)KS_OP, "\033[m"}, /* reset colors */
390 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
391 {(int)KS_UT, "y"}, /* guessed */
392 {(int)KS_LE, "\b"},
393# ifdef TERMINFO
394 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
395# else
396 {(int)KS_CM, "\033[%i%d;%dH"},
397# endif
398 {(int)KS_SR, "\033M"},
399# ifdef TERMINFO
400 {(int)KS_CRI, "\033[%p1%dC"},
401# else
402 {(int)KS_CRI, "\033[%dC"},
403# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200404# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408 {K_UP, "\033[A"},
409 {K_DOWN, "\033[B"},
410 {K_LEFT, "\033[D"},
411 {K_RIGHT, "\033[C"},
412# endif
413
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200414# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415/*
416 * standard ANSI terminal, default for unix
417 */
418 {(int)KS_NAME, "ansi"},
419 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
420 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
421# ifdef TERMINFO
422 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
423# else
424 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
425# endif
426 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
427# ifdef TERMINFO
428 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
429# else
430 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
431# endif
432 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
433 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
434 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
435 {(int)KS_MS, "y"},
436 {(int)KS_UT, "y"}, /* guessed */
437 {(int)KS_LE, "\b"},
438# ifdef TERMINFO
439 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
440# else
441 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
442# endif
443# ifdef TERMINFO
444 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
445# else
446 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
447# endif
448# endif
449
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200450# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451/*
452 * These codes are valid when nansi.sys or equivalent has been installed.
453 * Function keys on a PC are preceded with a NUL. These are converted into
454 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
455 * CTRL-arrow is used instead of SHIFT-arrow.
456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {(int)KS_NAME, "pcansi"},
458 {(int)KS_DL, "\033[M"},
459 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {(int)KS_CE, "\033[K"},
461 {(int)KS_CL, "\033[2J"},
462 {(int)KS_ME, "\033[0m"},
463 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
464 {(int)KS_MD, "\033[1m"}, /* bold: white text */
465 {(int)KS_SE, "\033[0m"}, /* standout end */
466 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
467 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
468 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
469 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
470 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
471 {(int)KS_CCO, "8"}, /* allow 8 colors */
472# ifdef TERMINFO
473 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
474 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
475# else
476 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
477 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
478# endif
479 {(int)KS_OP, "\033[0m"}, /* reset colors */
480 {(int)KS_MS, "y"},
481 {(int)KS_UT, "y"}, /* guessed */
482 {(int)KS_LE, "\b"},
483# ifdef TERMINFO
484 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
485# else
486 {(int)KS_CM, "\033[%i%d;%dH"},
487# endif
488# ifdef TERMINFO
489 {(int)KS_CRI, "\033[%p1%dC"},
490# else
491 {(int)KS_CRI, "\033[%dC"},
492# endif
493 {K_UP, "\316H"},
494 {K_DOWN, "\316P"},
495 {K_LEFT, "\316K"},
496 {K_RIGHT, "\316M"},
497 {K_S_LEFT, "\316s"},
498 {K_S_RIGHT, "\316t"},
499 {K_F1, "\316;"},
500 {K_F2, "\316<"},
501 {K_F3, "\316="},
502 {K_F4, "\316>"},
503 {K_F5, "\316?"},
504 {K_F6, "\316@"},
505 {K_F7, "\316A"},
506 {K_F8, "\316B"},
507 {K_F9, "\316C"},
508 {K_F10, "\316D"},
509 {K_F11, "\316\205"}, /* guessed */
510 {K_F12, "\316\206"}, /* guessed */
511 {K_S_F1, "\316T"},
512 {K_S_F2, "\316U"},
513 {K_S_F3, "\316V"},
514 {K_S_F4, "\316W"},
515 {K_S_F5, "\316X"},
516 {K_S_F6, "\316Y"},
517 {K_S_F7, "\316Z"},
518 {K_S_F8, "\316["},
519 {K_S_F9, "\316\\"},
520 {K_S_F10, "\316]"},
521 {K_S_F11, "\316\207"}, /* guessed */
522 {K_S_F12, "\316\210"}, /* guessed */
523 {K_INS, "\316R"},
524 {K_DEL, "\316S"},
525 {K_HOME, "\316G"},
526 {K_END, "\316O"},
527 {K_PAGEDOWN, "\316Q"},
528 {K_PAGEUP, "\316I"},
529# endif
530
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200531# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
533 * These codes are valid for the Win32 Console . The entries that start with
534 * ESC | are translated into console calls in os_win32.c. The function keys
535 * are also translated in os_win32.c.
536 */
537 {(int)KS_NAME, "win32"},
538 {(int)KS_CE, "\033|K"}, /* clear to end of line */
539 {(int)KS_AL, "\033|L"}, /* add new blank line */
540# ifdef TERMINFO
541 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
542# else
543 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
544# endif
545 {(int)KS_DL, "\033|M"}, /* delete line */
546# ifdef TERMINFO
547 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
548# else
549 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
550# endif
551 {(int)KS_CL, "\033|J"}, /* clear screen */
552 {(int)KS_CD, "\033|j"}, /* clear to end of display */
553 {(int)KS_VI, "\033|v"}, /* cursor invisible */
554 {(int)KS_VE, "\033|V"}, /* cursor visible */
555
556 {(int)KS_ME, "\033|0m"}, /* normal */
557 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
558 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
559#if 1
560 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
561 {(int)KS_SE, "\033|0m"}, /* standout end */
562#else
563 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
564 {(int)KS_SE, "\033|f"}, /* standout end */
565#endif
566 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
567 {(int)KS_CZR, "\033|0m"}, /* italic end */
568 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
569 {(int)KS_UE, "\033|0m"}, /* underscore end */
570 {(int)KS_CCO, "16"}, /* allow 16 colors */
571# ifdef TERMINFO
572 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
573 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
574# else
575 {(int)KS_CAB, "\033|%db"}, /* set background color */
576 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
577# endif
578
579 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
580 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100581 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {(int)KS_LE, "\b"},
583# ifdef TERMINFO
584 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
585# else
586 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
587# endif
588 {(int)KS_VB, "\033|B"}, /* visual bell */
589 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
590 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
591# ifdef TERMINFO
592 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
593# else
594 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
595# endif
596
597 {K_UP, "\316H"},
598 {K_DOWN, "\316P"},
599 {K_LEFT, "\316K"},
600 {K_RIGHT, "\316M"},
601 {K_S_UP, "\316\304"},
602 {K_S_DOWN, "\316\317"},
603 {K_S_LEFT, "\316\311"},
604 {K_C_LEFT, "\316s"},
605 {K_S_RIGHT, "\316\313"},
606 {K_C_RIGHT, "\316t"},
607 {K_S_TAB, "\316\017"},
608 {K_F1, "\316;"},
609 {K_F2, "\316<"},
610 {K_F3, "\316="},
611 {K_F4, "\316>"},
612 {K_F5, "\316?"},
613 {K_F6, "\316@"},
614 {K_F7, "\316A"},
615 {K_F8, "\316B"},
616 {K_F9, "\316C"},
617 {K_F10, "\316D"},
618 {K_F11, "\316\205"},
619 {K_F12, "\316\206"},
620 {K_S_F1, "\316T"},
621 {K_S_F2, "\316U"},
622 {K_S_F3, "\316V"},
623 {K_S_F4, "\316W"},
624 {K_S_F5, "\316X"},
625 {K_S_F6, "\316Y"},
626 {K_S_F7, "\316Z"},
627 {K_S_F8, "\316["},
628 {K_S_F9, "\316\\"},
629 {K_S_F10, "\316]"},
630 {K_S_F11, "\316\207"},
631 {K_S_F12, "\316\210"},
632 {K_INS, "\316R"},
633 {K_DEL, "\316S"},
634 {K_HOME, "\316G"},
635 {K_S_HOME, "\316\302"},
636 {K_C_HOME, "\316w"},
637 {K_END, "\316O"},
638 {K_S_END, "\316\315"},
639 {K_C_END, "\316u"},
640 {K_PAGEDOWN, "\316Q"},
641 {K_PAGEUP, "\316I"},
642 {K_KPLUS, "\316N"},
643 {K_KMINUS, "\316J"},
644 {K_KMULTIPLY, "\316\067"},
645 {K_K0, "\316\332"},
646 {K_K1, "\316\336"},
647 {K_K2, "\316\342"},
648 {K_K3, "\316\346"},
649 {K_K4, "\316\352"},
650 {K_K5, "\316\356"},
651 {K_K6, "\316\362"},
652 {K_K7, "\316\366"},
653 {K_K8, "\316\372"},
654 {K_K9, "\316\376"},
655# endif
656
657# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
658/*
659 * VT320 is working as an ANSI terminal compatible DEC terminal.
660 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
661 * Note: K_F1...K_F5 are for internal use, should not be defined.
662 * TODO:- rewrite ESC[ codes to CSI
663 * - keyboard languages (CSI ? 26 n)
664 */
665 {(int)KS_NAME, "vt320"},
666 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
667 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
668# ifdef TERMINFO
669 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
670# else
671 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
672# endif
673 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
674# ifdef TERMINFO
675 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
676# else
677 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
678# endif
679 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000680 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
681 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
683 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000684 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
685 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
686 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
687 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
688 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
689 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
690 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
691 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
692 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
693 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {(int)KS_MS, "y"},
695 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100696 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {(int)KS_LE, "\b"},
698# ifdef TERMINFO
699 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
700 ESC_STR "[%i%p1%d;%p2%dH")},
701# else
702 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
703# endif
704# ifdef TERMINFO
705 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
706# else
707 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
708# endif
709 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
710 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
711 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
712 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000713 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
714 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
715 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
716 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
717 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
719 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
720 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
721 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
722 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000723 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
725 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
726 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
727 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
728 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
729 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
730 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
731 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
732 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
733 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
734 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
735 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
736 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
737 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
738 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
739 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
740 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
741 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
742 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
743 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
744 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
745# endif
746
747# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
748/*
749 * Ordinary vt52
750 */
751 {(int)KS_NAME, "vt52"},
752 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
753 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100754# ifdef TERMINFO
755 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
756 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
757# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100761 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
763 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {K_UP, IF_EB("\033A", ESC_STR "A")},
765 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
766 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
767 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100768 {K_F1, IF_EB("\033P", ESC_STR "P")},
769 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
770 {K_F3, IF_EB("\033R", ESC_STR "R")},
771# ifdef __MINT__
772 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
773 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
774 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
775 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
776 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
778 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
779 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
780 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {K_F4, IF_EB("\033S", ESC_STR "S")},
782 {K_F5, IF_EB("\033T", ESC_STR "T")},
783 {K_F6, IF_EB("\033U", ESC_STR "U")},
784 {K_F7, IF_EB("\033V", ESC_STR "V")},
785 {K_F8, IF_EB("\033W", ESC_STR "W")},
786 {K_F9, IF_EB("\033X", ESC_STR "X")},
787 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
788 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
789 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
790 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
791 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
792 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
793 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
794 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
795 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
796 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
797 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
798 {K_INS, IF_EB("\033I", ESC_STR "I")},
799 {K_HOME, IF_EB("\033E", ESC_STR "E")},
800 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
801 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
802# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {(int)KS_MS, "y"},
805# endif
806# endif
807
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200808# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200809 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
811 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
812# ifdef TERMINFO
813 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
814# else
815 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
816# endif
817 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
818# ifdef TERMINFO
819 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
820# else
821 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
822# endif
823# ifdef TERMINFO
824 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
825 ESC_STR "[%i%p1%d;%p2%dr")},
826# else
827 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
828# endif
829 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
830 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
831 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
832 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
833 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
834 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
835 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200836 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
837 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {(int)KS_MS, "y"},
839 {(int)KS_UT, "y"},
840 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200841 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
842 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200843 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200844 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200845# ifdef TERMINFO
846 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
847# else
848 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
849# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200850 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200851 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852# ifdef TERMINFO
853 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
854 ESC_STR "[%i%p1%d;%p2%dH")},
855# else
856 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
857# endif
858 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
859# ifdef TERMINFO
860 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
861# else
862 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
863# endif
864 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
865 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
866# ifdef FEAT_XTERM_SAVE
867 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
868 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
869 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
870# endif
871 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
872 {(int)KS_CIE, "\007"},
873 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
874 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200875 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
876 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877# ifdef TERMINFO
878 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
879 ESC_STR "[8;%p1%d;%p2%dt")},
880 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
881 ESC_STR "[3;%p1%d;%p2%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# else
884 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
885 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200886 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887# endif
888 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200889 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100890 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200891# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200892 /* These are printf strings, not terminal codes. */
893 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
894 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
895# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100896 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
897 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000898
899 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
900 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
901 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
902 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
903 /* An extra set of cursor keys for vt100 mode */
904 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
905 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
906 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
907 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000909 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
910 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
911 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
912 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000913 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
914 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
915 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
916 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
917 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
918 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
919 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
920 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
921 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
922 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
923 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
924 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000926 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
927 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
928 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
929 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000930 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
931 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000932 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000933 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000934 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000935 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000936 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
937 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000938 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000939 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000940 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000941 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
942 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100943 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
944 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
945 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
946 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
947 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
948 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
949 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
950 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
951 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000954 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
955 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
956 /* F14 and F15 are missing, because they send the same codes as the undo
957 * and help key, although they don't work on all keyboards. */
958 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
959 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
960 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
961 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
962 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
963
964 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
965 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
966 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
967 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
968 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
969 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
970 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
971 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
972 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
973 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
974
975 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
976 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
977 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
978 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
979 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
980 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
981 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# endif
983
984# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
985/*
986 * iris-ansi for Silicon Graphics machines.
987 */
988 {(int)KS_NAME, "iris-ansi"},
989 {(int)KS_CE, "\033[K"},
990 {(int)KS_CD, "\033[J"},
991 {(int)KS_AL, "\033[L"},
992# ifdef TERMINFO
993 {(int)KS_CAL, "\033[%p1%dL"},
994# else
995 {(int)KS_CAL, "\033[%dL"},
996# endif
997 {(int)KS_DL, "\033[M"},
998# ifdef TERMINFO
999 {(int)KS_CDL, "\033[%p1%dM"},
1000# else
1001 {(int)KS_CDL, "\033[%dM"},
1002# endif
1003#if 0 /* The scroll region is not working as Vim expects. */
1004# ifdef TERMINFO
1005 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1006# else
1007 {(int)KS_CS, "\033[%i%d;%dr"},
1008# endif
1009#endif
1010 {(int)KS_CL, "\033[H\033[2J"},
1011 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1012 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1013 {(int)KS_TI, "\033[=6h"},
1014 {(int)KS_TE, "\033[=6l"},
1015 {(int)KS_SE, "\033[21;27m"},
1016 {(int)KS_SO, "\033[1;7m"},
1017 {(int)KS_ME, "\033[m"},
1018 {(int)KS_MR, "\033[7m"},
1019 {(int)KS_MD, "\033[1m"},
1020 {(int)KS_CCO, "8"}, /* allow 8 colors */
1021 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1022 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1023 {(int)KS_US, "\033[4m"}, /* underline on */
1024 {(int)KS_UE, "\033[24m"}, /* underline off */
1025# ifdef TERMINFO
1026 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1027 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1028 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1029 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1030# else
1031 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1032 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1033 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1034 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1035# endif
1036 {(int)KS_MS, "y"}, /* guessed */
1037 {(int)KS_UT, "y"}, /* guessed */
1038 {(int)KS_LE, "\b"},
1039# ifdef TERMINFO
1040 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1041# else
1042 {(int)KS_CM, "\033[%i%d;%dH"},
1043# endif
1044 {(int)KS_SR, "\033M"},
1045# ifdef TERMINFO
1046 {(int)KS_CRI, "\033[%p1%dC"},
1047# else
1048 {(int)KS_CRI, "\033[%dC"},
1049# endif
1050 {(int)KS_CIS, "\033P3.y"},
1051 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1052 {(int)KS_TS, "\033P1.y"},
1053 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1054# ifdef TERMINFO
1055 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1056 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1057# else
1058 {(int)KS_CWS, "\033[203;%d;%d/y"},
1059 {(int)KS_CWP, "\033[205;%d;%d/y"},
1060# endif
1061 {K_UP, "\033[A"},
1062 {K_DOWN, "\033[B"},
1063 {K_LEFT, "\033[D"},
1064 {K_RIGHT, "\033[C"},
1065 {K_S_UP, "\033[161q"},
1066 {K_S_DOWN, "\033[164q"},
1067 {K_S_LEFT, "\033[158q"},
1068 {K_S_RIGHT, "\033[167q"},
1069 {K_F1, "\033[001q"},
1070 {K_F2, "\033[002q"},
1071 {K_F3, "\033[003q"},
1072 {K_F4, "\033[004q"},
1073 {K_F5, "\033[005q"},
1074 {K_F6, "\033[006q"},
1075 {K_F7, "\033[007q"},
1076 {K_F8, "\033[008q"},
1077 {K_F9, "\033[009q"},
1078 {K_F10, "\033[010q"},
1079 {K_F11, "\033[011q"},
1080 {K_F12, "\033[012q"},
1081 {K_S_F1, "\033[013q"},
1082 {K_S_F2, "\033[014q"},
1083 {K_S_F3, "\033[015q"},
1084 {K_S_F4, "\033[016q"},
1085 {K_S_F5, "\033[017q"},
1086 {K_S_F6, "\033[018q"},
1087 {K_S_F7, "\033[019q"},
1088 {K_S_F8, "\033[020q"},
1089 {K_S_F9, "\033[021q"},
1090 {K_S_F10, "\033[022q"},
1091 {K_S_F11, "\033[023q"},
1092 {K_S_F12, "\033[024q"},
1093 {K_INS, "\033[139q"},
1094 {K_HOME, "\033[H"},
1095 {K_END, "\033[146q"},
1096 {K_PAGEUP, "\033[150q"},
1097 {K_PAGEDOWN, "\033[154q"},
1098# endif
1099
1100# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1101/*
1102 * for debugging
1103 */
1104 {(int)KS_NAME, "debug"},
1105 {(int)KS_CE, "[CE]"},
1106 {(int)KS_CD, "[CD]"},
1107 {(int)KS_AL, "[AL]"},
1108# ifdef TERMINFO
1109 {(int)KS_CAL, "[CAL%p1%d]"},
1110# else
1111 {(int)KS_CAL, "[CAL%d]"},
1112# endif
1113 {(int)KS_DL, "[DL]"},
1114# ifdef TERMINFO
1115 {(int)KS_CDL, "[CDL%p1%d]"},
1116# else
1117 {(int)KS_CDL, "[CDL%d]"},
1118# endif
1119# ifdef TERMINFO
1120 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1121# else
1122 {(int)KS_CS, "[%dCS%d]"},
1123# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001124# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125# ifdef TERMINFO
1126 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1127# else
1128 {(int)KS_CSV, "[%dCSV%d]"},
1129# endif
1130# endif
1131# ifdef TERMINFO
1132 {(int)KS_CAB, "[CAB%p1%d]"},
1133 {(int)KS_CAF, "[CAF%p1%d]"},
1134 {(int)KS_CSB, "[CSB%p1%d]"},
1135 {(int)KS_CSF, "[CSF%p1%d]"},
1136# else
1137 {(int)KS_CAB, "[CAB%d]"},
1138 {(int)KS_CAF, "[CAF%d]"},
1139 {(int)KS_CSB, "[CSB%d]"},
1140 {(int)KS_CSF, "[CSF%d]"},
1141# endif
1142 {(int)KS_OP, "[OP]"},
1143 {(int)KS_LE, "[LE]"},
1144 {(int)KS_CL, "[CL]"},
1145 {(int)KS_VI, "[VI]"},
1146 {(int)KS_VE, "[VE]"},
1147 {(int)KS_VS, "[VS]"},
1148 {(int)KS_ME, "[ME]"},
1149 {(int)KS_MR, "[MR]"},
1150 {(int)KS_MB, "[MB]"},
1151 {(int)KS_MD, "[MD]"},
1152 {(int)KS_SE, "[SE]"},
1153 {(int)KS_SO, "[SO]"},
1154 {(int)KS_UE, "[UE]"},
1155 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001156 {(int)KS_UCE, "[UCE]"},
1157 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001158 {(int)KS_STE, "[STE]"},
1159 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {(int)KS_MS, "[MS]"},
1161 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001162 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# ifdef TERMINFO
1164 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1165# else
1166 {(int)KS_CM, "[%dCM%d]"},
1167# endif
1168 {(int)KS_SR, "[SR]"},
1169# ifdef TERMINFO
1170 {(int)KS_CRI, "[CRI%p1%d]"},
1171# else
1172 {(int)KS_CRI, "[CRI%d]"},
1173# endif
1174 {(int)KS_VB, "[VB]"},
1175 {(int)KS_KS, "[KS]"},
1176 {(int)KS_KE, "[KE]"},
1177 {(int)KS_TI, "[TI]"},
1178 {(int)KS_TE, "[TE]"},
1179 {(int)KS_CIS, "[CIS]"},
1180 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001181 {(int)KS_CSC, "[CSC]"},
1182 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {(int)KS_TS, "[TS]"},
1184 {(int)KS_FS, "[FS]"},
1185# ifdef TERMINFO
1186 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1187 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1188# else
1189 {(int)KS_CWS, "[%dCWS%d]"},
1190 {(int)KS_CWP, "[%dCWP%d]"},
1191# endif
1192 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001193 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001194 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {K_UP, "[KU]"},
1196 {K_DOWN, "[KD]"},
1197 {K_LEFT, "[KL]"},
1198 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001199 {K_XUP, "[xKU]"},
1200 {K_XDOWN, "[xKD]"},
1201 {K_XLEFT, "[xKL]"},
1202 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {K_S_UP, "[S-KU]"},
1204 {K_S_DOWN, "[S-KD]"},
1205 {K_S_LEFT, "[S-KL]"},
1206 {K_C_LEFT, "[C-KL]"},
1207 {K_S_RIGHT, "[S-KR]"},
1208 {K_C_RIGHT, "[C-KR]"},
1209 {K_F1, "[F1]"},
1210 {K_XF1, "[xF1]"},
1211 {K_F2, "[F2]"},
1212 {K_XF2, "[xF2]"},
1213 {K_F3, "[F3]"},
1214 {K_XF3, "[xF3]"},
1215 {K_F4, "[F4]"},
1216 {K_XF4, "[xF4]"},
1217 {K_F5, "[F5]"},
1218 {K_F6, "[F6]"},
1219 {K_F7, "[F7]"},
1220 {K_F8, "[F8]"},
1221 {K_F9, "[F9]"},
1222 {K_F10, "[F10]"},
1223 {K_F11, "[F11]"},
1224 {K_F12, "[F12]"},
1225 {K_S_F1, "[S-F1]"},
1226 {K_S_XF1, "[S-xF1]"},
1227 {K_S_F2, "[S-F2]"},
1228 {K_S_XF2, "[S-xF2]"},
1229 {K_S_F3, "[S-F3]"},
1230 {K_S_XF3, "[S-xF3]"},
1231 {K_S_F4, "[S-F4]"},
1232 {K_S_XF4, "[S-xF4]"},
1233 {K_S_F5, "[S-F5]"},
1234 {K_S_F6, "[S-F6]"},
1235 {K_S_F7, "[S-F7]"},
1236 {K_S_F8, "[S-F8]"},
1237 {K_S_F9, "[S-F9]"},
1238 {K_S_F10, "[S-F10]"},
1239 {K_S_F11, "[S-F11]"},
1240 {K_S_F12, "[S-F12]"},
1241 {K_HELP, "[HELP]"},
1242 {K_UNDO, "[UNDO]"},
1243 {K_BS, "[BS]"},
1244 {K_INS, "[INS]"},
1245 {K_KINS, "[KINS]"},
1246 {K_DEL, "[DEL]"},
1247 {K_KDEL, "[KDEL]"},
1248 {K_HOME, "[HOME]"},
1249 {K_S_HOME, "[C-HOME]"},
1250 {K_C_HOME, "[C-HOME]"},
1251 {K_KHOME, "[KHOME]"},
1252 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001253 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {K_END, "[END]"},
1255 {K_S_END, "[C-END]"},
1256 {K_C_END, "[C-END]"},
1257 {K_KEND, "[KEND]"},
1258 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001259 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {K_PAGEUP, "[PAGEUP]"},
1261 {K_PAGEDOWN, "[PAGEDOWN]"},
1262 {K_KPAGEUP, "[KPAGEUP]"},
1263 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1264 {K_MOUSE, "[MOUSE]"},
1265 {K_KPLUS, "[KPLUS]"},
1266 {K_KMINUS, "[KMINUS]"},
1267 {K_KDIVIDE, "[KDIVIDE]"},
1268 {K_KMULTIPLY, "[KMULTIPLY]"},
1269 {K_KENTER, "[KENTER]"},
1270 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001271 {K_PS, "[PASTE-START]"},
1272 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {K_K0, "[K0]"},
1274 {K_K1, "[K1]"},
1275 {K_K2, "[K2]"},
1276 {K_K3, "[K3]"},
1277 {K_K4, "[K4]"},
1278 {K_K5, "[K5]"},
1279 {K_K6, "[K6]"},
1280 {K_K7, "[K7]"},
1281 {K_K8, "[K8]"},
1282 {K_K9, "[K9]"},
1283# endif
1284
1285#endif /* NO_BUILTIN_TCAPS */
1286
1287/*
1288 * The most minimal terminal: only clear screen and cursor positioning
1289 * Always included.
1290 */
1291 {(int)KS_NAME, "dumb"},
1292 {(int)KS_CL, "\014"},
1293#ifdef TERMINFO
1294 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1295 ESC_STR "[%i%p1%d;%p2%dH")},
1296#else
1297 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1298#endif
1299
1300/*
1301 * end marker
1302 */
1303 {(int)KS_NAME, NULL}
1304
1305}; /* end of builtin_termcaps */
1306
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001307#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001308 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001309termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001310{
Bram Moolenaarab302212016-04-26 20:59:29 +02001311 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001312}
1313
1314 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001315termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001316{
1317 guicolor_T t;
1318
1319 if (*name == NUL)
1320 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001321 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001322
1323 if (t == INVALCOLOR)
1324 EMSG2(_("E254: Cannot allocate color %s"), name);
1325 return t;
1326}
1327
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001328 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001329termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001330{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001331 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001332}
1333#endif
1334
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335/*
1336 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#ifdef AMIGA
1339# define DEFAULT_TERM (char_u *)"amiga"
1340#endif
1341
1342#ifdef MSWIN
1343# define DEFAULT_TERM (char_u *)"win32"
1344#endif
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#if defined(UNIX) && !defined(__MINT__)
1347# define DEFAULT_TERM (char_u *)"ansi"
1348#endif
1349
1350#ifdef __MINT__
1351# define DEFAULT_TERM (char_u *)"vt52"
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#ifdef VMS
1355# define DEFAULT_TERM (char_u *)"vt320"
1356#endif
1357
1358#ifdef __BEOS__
1359# undef DEFAULT_TERM
1360# define DEFAULT_TERM (char_u *)"beos-ansi"
1361#endif
1362
1363#ifndef DEFAULT_TERM
1364# define DEFAULT_TERM (char_u *)"dumb"
1365#endif
1366
1367/*
1368 * Term_strings contains currently used terminal output strings.
1369 * It is initialized with the default values by parse_builtin_tcap().
1370 * The values can be changed by setting the option with the same name.
1371 */
1372char_u *(term_strings[(int)KS_LAST + 1]);
1373
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001374static int need_gather = FALSE; /* need to fill termleader[] */
1375static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001377static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001378static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#endif
1380
1381 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001382find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 struct builtin_term *p;
1385
1386 p = builtin_termcaps;
1387 while (p->bt_string != NULL)
1388 {
1389 if (p->bt_entry == (int)KS_NAME)
1390 {
1391#ifdef UNIX
1392 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1393 return p;
1394 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1395 return p;
1396 else
1397#endif
1398#ifdef VMS
1399 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1400 return p;
1401 else
1402#endif
1403 if (STRCMP(term, p->bt_string) == 0)
1404 return p;
1405 }
1406 ++p;
1407 }
1408 return p;
1409}
1410
1411/*
1412 * Parsing of the builtin termcap entries.
1413 * Caller should check if 'name' is a valid builtin term.
1414 * The terminal's name is not set, as this is already done in termcapinit().
1415 */
1416 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001417parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 struct builtin_term *p;
1420 char_u name[2];
1421 int term_8bit;
1422
1423 p = find_builtin_term(term);
1424 term_8bit = term_is_8bit(term);
1425
1426 /* Do not parse if builtin term not found */
1427 if (p->bt_string == NULL)
1428 return;
1429
1430 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1431 {
1432 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1433 {
1434 /* Only set the value if it wasn't set yet. */
1435 if (term_strings[p->bt_entry] == NULL
1436 || term_strings[p->bt_entry] == empty_option)
1437 {
1438 /* 8bit terminal: use CSI instead of <Esc>[ */
1439 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1440 {
1441 char_u *s, *t;
1442
1443 s = vim_strsave((char_u *)p->bt_string);
1444 if (s != NULL)
1445 {
1446 for (t = s; *t; ++t)
1447 if (term_7to8bit(t))
1448 {
1449 *t = term_7to8bit(t);
1450 STRCPY(t + 1, t + 2);
1451 }
1452 term_strings[p->bt_entry] = s;
1453 set_term_option_alloced(&term_strings[p->bt_entry]);
1454 }
1455 }
1456 else
1457 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1458 }
1459 }
1460 else
1461 {
1462 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1463 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1464 if (find_termcode(name) == NULL)
1465 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1466 }
1467 }
1468}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
1471 * Set number of colors.
1472 * Store it as a number in t_colors.
1473 * Store it as a string in T_CCO (using nr_colors[]).
1474 */
1475 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001476set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 char_u nr_colors[20]; /* string for number of colors */
1479
1480 t_colors = nr;
1481 if (t_colors > 1)
1482 sprintf((char *)nr_colors, "%d", t_colors);
1483 else
1484 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001485 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001487
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001488#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001489/*
1490 * Set the color count to "val" and redraw if it changed.
1491 */
1492 static void
1493may_adjust_color_count(int val)
1494{
1495 if (val != t_colors)
1496 {
1497 /* Nr of colors changed, initialize highlighting and
1498 * redraw everything. This causes a redraw, which usually
1499 * clears the message. Try keeping the message if it
1500 * might work. */
1501 set_keep_msg_from_hist();
1502 set_color_count(val);
1503 init_highlight(TRUE, FALSE);
1504# ifdef DEBUG_TERMRESPONSE
1505 {
1506 char buf[100];
1507 int r = redraw_asap(CLEAR);
1508
1509 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1510 log_tr(buf);
1511 }
1512# else
1513 redraw_asap(CLEAR);
1514# endif
1515 }
1516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#endif
1518
1519#ifdef HAVE_TGETENT
1520static char *(key_names[]) =
1521{
1522#ifdef FEAT_TERMRESPONSE
1523 /* Do this one first, it may cause a screen redraw. */
1524 "Co",
1525#endif
1526 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 "#2", "#4", "%i", "*7",
1528 "k1", "k2", "k3", "k4", "k5", "k6",
1529 "k7", "k8", "k9", "k;", "F1", "F2",
1530 "%1", "&8", "kb", "kI", "kD", "kh",
1531 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1532 NULL
1533};
1534#endif
1535
1536/*
1537 * Set terminal options for terminal "term".
1538 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1539 *
1540 * While doing this, until ttest(), some options may be NULL, be careful.
1541 */
1542 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001543set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 struct builtin_term *termp;
1546#ifdef HAVE_TGETENT
1547 int builtin_first = p_tbi;
1548 int try;
1549 int termcap_cleared = FALSE;
1550#endif
1551 int width = 0, height = 0;
1552 char_u *error_msg = NULL;
1553 char_u *bs_p, *del_p;
1554
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001555 /* In silect mode (ex -s) we don't use the 'term' option. */
1556 if (silent_mode)
1557 return OK;
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 detected_8bit = FALSE; /* reset 8-bit detection */
1560
1561 if (term_is_builtin(term))
1562 {
1563 term += 8;
1564#ifdef HAVE_TGETENT
1565 builtin_first = 1;
1566#endif
1567 }
1568
1569/*
1570 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1571 * If builtin_first is TRUE:
1572 * 0. try builtin termcap
1573 * 1. try external termcap
1574 * 2. if both fail default to a builtin terminal
1575 * If builtin_first is FALSE:
1576 * 1. try external termcap
1577 * 2. try builtin termcap, if both fail default to a builtin terminal
1578 */
1579#ifdef HAVE_TGETENT
1580 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1581 {
1582 /*
1583 * Use external termcap
1584 */
1585 if (try == 1)
1586 {
1587 char_u *p;
1588 static char_u tstrbuf[TBUFSZ];
1589 int i;
1590 char_u tbuf[TBUFSZ];
1591 char_u *tp;
1592 static struct {
1593 enum SpecialKey dest; /* index in term_strings[] */
1594 char *name; /* termcap name for string */
1595 } string_names[] =
1596 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1597 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1598 {KS_CL, "cl"}, {KS_CD, "cd"},
1599 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001600 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1602 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001603 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001604 {KS_STE,"Te"}, {KS_STS,"Ts"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001605 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1607 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1608 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1609 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1610 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001611 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001613 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {KS_TS, "ts"}, {KS_FS, "fs"},
1615 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001616 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001617 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001618 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001619 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1620 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {(enum SpecialKey)0, NULL}
1622 };
1623
1624 /*
1625 * If the external termcap does not have a matching entry, try the
1626 * builtin ones.
1627 */
1628 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1629 {
1630 tp = tstrbuf;
1631 if (!termcap_cleared)
1632 {
1633 clear_termoptions(); /* clear old options */
1634 termcap_cleared = TRUE;
1635 }
1636
1637 /* get output strings */
1638 for (i = 0; string_names[i].name != NULL; ++i)
1639 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001640 if (TERM_STR(string_names[i].dest) == NULL
1641 || TERM_STR(string_names[i].dest) == empty_option)
1642 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 TGETSTR(string_names[i].name, &tp);
1644 }
1645
1646 /* tgetflag() returns 1 if the flag is present, 0 if not and
1647 * possibly -1 if the flag doesn't exist. */
1648 if ((T_MS == NULL || T_MS == empty_option)
1649 && tgetflag("ms") > 0)
1650 T_MS = (char_u *)"y";
1651 if ((T_XS == NULL || T_XS == empty_option)
1652 && tgetflag("xs") > 0)
1653 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001654 if ((T_XN == NULL || T_XN == empty_option)
1655 && tgetflag("xn") > 0)
1656 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if ((T_DB == NULL || T_DB == empty_option)
1658 && tgetflag("db") > 0)
1659 T_DB = (char_u *)"y";
1660 if ((T_DA == NULL || T_DA == empty_option)
1661 && tgetflag("da") > 0)
1662 T_DA = (char_u *)"y";
1663 if ((T_UT == NULL || T_UT == empty_option)
1664 && tgetflag("ut") > 0)
1665 T_UT = (char_u *)"y";
1666
1667
1668 /*
1669 * get key codes
1670 */
1671 for (i = 0; key_names[i] != NULL; ++i)
1672 {
1673 if (find_termcode((char_u *)key_names[i]) == NULL)
1674 {
1675 p = TGETSTR(key_names[i], &tp);
1676 /* if cursor-left == backspace, ignore it (televideo
1677 * 925) */
1678 if (p != NULL
1679 && (*p != Ctrl_H
1680 || key_names[i][0] != 'k'
1681 || key_names[i][1] != 'l'))
1682 add_termcode((char_u *)key_names[i], p, FALSE);
1683 }
1684 }
1685
1686 if (height == 0)
1687 height = tgetnum("li");
1688 if (width == 0)
1689 width = tgetnum("co");
1690
1691 /*
1692 * Get number of colors (if not done already).
1693 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001694 if (TERM_STR(KS_CCO) == NULL
1695 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 set_color_count(tgetnum("Co"));
1697
1698# ifndef hpux
1699 BC = (char *)TGETSTR("bc", &tp);
1700 UP = (char *)TGETSTR("up", &tp);
1701 p = TGETSTR("pc", &tp);
1702 if (p)
1703 PC = *p;
1704# endif /* hpux */
1705 }
1706 }
1707 else /* try == 0 || try == 2 */
1708#endif /* HAVE_TGETENT */
1709 /*
1710 * Use builtin termcap
1711 */
1712 {
1713#ifdef HAVE_TGETENT
1714 /*
1715 * If builtin termcap was already used, there is no need to search
1716 * for the builtin termcap again, quit now.
1717 */
1718 if (try == 2 && builtin_first && termcap_cleared)
1719 break;
1720#endif
1721 /*
1722 * search for 'term' in builtin_termcaps[]
1723 */
1724 termp = find_builtin_term(term);
1725 if (termp->bt_string == NULL) /* did not find it */
1726 {
1727#ifdef HAVE_TGETENT
1728 /*
1729 * If try == 0, first try the external termcap. If that is not
1730 * found we'll get back here with try == 2.
1731 * If termcap_cleared is set we used the external termcap,
1732 * don't complain about not finding the term in the builtin
1733 * termcap.
1734 */
1735 if (try == 0) /* try external one */
1736 continue;
1737 if (termcap_cleared) /* found in external termcap */
1738 break;
1739#endif
1740
1741 mch_errmsg("\r\n");
1742 if (error_msg != NULL)
1743 {
1744 mch_errmsg((char *)error_msg);
1745 mch_errmsg("\r\n");
1746 }
1747 mch_errmsg("'");
1748 mch_errmsg((char *)term);
1749 mch_errmsg(_("' not known. Available builtin terminals are:"));
1750 mch_errmsg("\r\n");
1751 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1752 ++termp)
1753 {
1754 if (termp->bt_entry == (int)KS_NAME)
1755 {
1756#ifdef HAVE_TGETENT
1757 mch_errmsg(" builtin_");
1758#else
1759 mch_errmsg(" ");
1760#endif
1761 mch_errmsg(termp->bt_string);
1762 mch_errmsg("\r\n");
1763 }
1764 }
1765 /* when user typed :set term=xxx, quit here */
1766 if (starting != NO_SCREEN)
1767 {
1768 screen_start(); /* don't know where cursor is now */
1769 wait_return(TRUE);
1770 return FAIL;
1771 }
1772 term = DEFAULT_TERM;
1773 mch_errmsg(_("defaulting to '"));
1774 mch_errmsg((char *)term);
1775 mch_errmsg("'\r\n");
1776 if (emsg_silent == 0)
1777 {
1778 screen_start(); /* don't know where cursor is now */
1779 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001780 if (!is_not_a_term())
1781 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001783 set_string_option_direct((char_u *)"term", -1, term,
1784 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 display_errors();
1786 }
1787 out_flush();
1788#ifdef HAVE_TGETENT
1789 if (!termcap_cleared)
1790 {
1791#endif
1792 clear_termoptions(); /* clear old options */
1793#ifdef HAVE_TGETENT
1794 termcap_cleared = TRUE;
1795 }
1796#endif
1797 parse_builtin_tcap(term);
1798#ifdef FEAT_GUI
1799 if (term_is_gui(term))
1800 {
1801 out_flush();
1802 gui_init();
1803 /* If starting the GUI failed, don't do any of the other
1804 * things for this terminal */
1805 if (!gui.in_use)
1806 return FAIL;
1807#ifdef HAVE_TGETENT
1808 break; /* don't try using external termcap */
1809#endif
1810 }
1811#endif /* FEAT_GUI */
1812 }
1813#ifdef HAVE_TGETENT
1814 }
1815#endif
1816
1817/*
1818 * special: There is no info in the termcap about whether the cursor
1819 * positioning is relative to the start of the screen or to the start of the
1820 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1821 * relative.
1822 */
1823 if (STRCMP(term, "pcterm") == 0)
1824 T_CCS = (char_u *)"yes";
1825 else
1826 T_CCS = empty_option;
1827
1828#ifdef UNIX
1829/*
1830 * Any "stty" settings override the default for t_kb from the termcap.
1831 * This is in os_unix.c, because it depends a lot on the version of unix that
1832 * is being used.
1833 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1834 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001835# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 get_stty();
1839#endif
1840
1841/*
1842 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1843 * didn't work, use the default CTRL-H
1844 * The default for t_kD is DEL, unless t_kb is DEL.
1845 * The vim_strsave'd strings are probably lost forever, well it's only two
1846 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1847 * directly.
1848 */
1849#ifdef FEAT_GUI
1850 if (!gui.in_use)
1851#endif
1852 {
1853 bs_p = find_termcode((char_u *)"kb");
1854 del_p = find_termcode((char_u *)"kD");
1855 if (bs_p == NULL || *bs_p == NUL)
1856 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1857 if ((del_p == NULL || *del_p == NUL) &&
1858 (bs_p == NULL || *bs_p != DEL))
1859 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1860 }
1861
1862#if defined(UNIX) || defined(VMS)
1863 term_is_xterm = vim_is_xterm(term);
1864#endif
1865
1866#ifdef FEAT_MOUSE
1867# if defined(UNIX) || defined(VMS)
1868# ifdef FEAT_MOUSE_TTY
1869 /*
1870 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1871 * The termcode for the mouse is added as a side effect in option.c.
1872 */
1873 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001874 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001877 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 if (use_xterm_mouse())
1880 p = NULL; /* keep existing value, might be "xterm2" */
1881 else
1882 p = (char_u *)"xterm";
1883 }
1884# endif
1885 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001888 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1889 * "xterm2" in check_termcode(). */
1890 reset_option_was_set((char_u *)"ttym");
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (p == NULL
1893# ifdef FEAT_GUI
1894 || gui.in_use
1895# endif
1896 )
1897 check_mouse_termcode(); /* set mouse termcode anyway */
1898 }
1899# endif
1900# else
1901 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1902# endif
1903#endif /* FEAT_MOUSE */
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#ifdef USE_TERM_CONSOLE
1906 /* DEFAULT_TERM indicates that it is the machine console. */
1907 if (STRCMP(term, DEFAULT_TERM) != 0)
1908 term_console = FALSE;
1909 else
1910 {
1911 term_console = TRUE;
1912# ifdef AMIGA
1913 win_resize_on(); /* enable window resizing reports */
1914# endif
1915 }
1916#endif
1917
1918#if defined(UNIX) || defined(VMS)
1919 /*
1920 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1921 */
1922 if (vim_is_fastterm(term))
1923 p_tf = TRUE;
1924#endif
1925#ifdef USE_TERM_CONSOLE
1926 /*
1927 * 'ttyfast' is default on consoles
1928 */
1929 if (term_console)
1930 p_tf = TRUE;
1931#endif
1932
1933 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1934
1935 full_screen = TRUE; /* we can use termcap codes from now on */
1936 set_term_defaults(); /* use current values as defaults */
1937#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001938 LOG_TR("setting crv_status to STATUS_GET");
1939 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#endif
1941
1942 /*
1943 * Initialize the terminal with the appropriate termcap codes.
1944 * Set the mouse and window title if possible.
1945 * Don't do this when starting, need to parse the .vimrc first, because it
1946 * may redefine t_TI etc.
1947 */
1948 if (starting != NO_SCREEN)
1949 {
1950 starttermcap(); /* may change terminal mode */
1951#ifdef FEAT_MOUSE
1952 setmouse(); /* may start using the mouse */
1953#endif
1954#ifdef FEAT_TITLE
1955 maketitle(); /* may display window title */
1956#endif
1957 }
1958
1959 /* display initial screen after ttest() checking. jw. */
1960 if (width <= 0 || height <= 0)
1961 {
1962 /* termcap failed to report size */
1963 /* set defaults, in case ui_get_shellsize() also fails */
1964 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001965#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 height = 25; /* console is often 25 lines */
1967#else
1968 height = 24; /* most terminals are 24 lines */
1969#endif
1970 }
1971 set_shellsize(width, height, FALSE); /* may change Rows */
1972 if (starting != NO_SCREEN)
1973 {
1974 if (scroll_region)
1975 scroll_region_reset(); /* In case Rows changed */
1976 check_map_keycodes(); /* check mappings for terminal codes used */
1977
1978#ifdef FEAT_AUTOCMD
1979 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001980 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 /*
1983 * Execute the TermChanged autocommands for each buffer that is
1984 * loaded.
1985 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001986 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001987 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 if (curbuf->b_ml.ml_mfp != NULL)
1990 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1991 curbuf);
1992 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001993 if (bufref_valid(&old_curbuf))
1994 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996#endif
1997 }
1998
1999#ifdef FEAT_TERMRESPONSE
2000 may_req_termresponse();
2001#endif
2002
2003 return OK;
2004}
2005
2006#if defined(FEAT_MOUSE) || defined(PROTO)
2007
2008# ifdef FEAT_MOUSE_TTY
2009# define HMT_NORMAL 1
2010# define HMT_NETTERM 2
2011# define HMT_DEC 4
2012# define HMT_JSBTERM 8
2013# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002014# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002015# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002016# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017static int has_mouse_termcode = 0;
2018# endif
2019
Bram Moolenaar864207d2008-06-24 22:14:38 +00002020# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002022set_mouse_termcode(
2023 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2024 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 char_u name[2];
2027
2028 name[0] = n;
2029 name[1] = KE_FILLER;
2030 add_termcode(name, s, FALSE);
2031# ifdef FEAT_MOUSE_TTY
2032# ifdef FEAT_MOUSE_JSB
2033 if (n == KS_JSBTERM_MOUSE)
2034 has_mouse_termcode |= HMT_JSBTERM;
2035 else
2036# endif
2037# ifdef FEAT_MOUSE_NET
2038 if (n == KS_NETTERM_MOUSE)
2039 has_mouse_termcode |= HMT_NETTERM;
2040 else
2041# endif
2042# ifdef FEAT_MOUSE_DEC
2043 if (n == KS_DEC_MOUSE)
2044 has_mouse_termcode |= HMT_DEC;
2045 else
2046# endif
2047# ifdef FEAT_MOUSE_PTERM
2048 if (n == KS_PTERM_MOUSE)
2049 has_mouse_termcode |= HMT_PTERM;
2050 else
2051# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002052# ifdef FEAT_MOUSE_URXVT
2053 if (n == KS_URXVT_MOUSE)
2054 has_mouse_termcode |= HMT_URXVT;
2055 else
2056# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002057# ifdef FEAT_MOUSE_SGR
2058 if (n == KS_SGR_MOUSE)
2059 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002060 else if (n == KS_SGR_MOUSE_RELEASE)
2061 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002062 else
2063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 has_mouse_termcode |= HMT_NORMAL;
2065# endif
2066}
2067# endif
2068
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002069# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002070 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002072del_mouse_termcode(
2073 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 char_u name[2];
2076
2077 name[0] = n;
2078 name[1] = KE_FILLER;
2079 del_termcode(name);
2080# ifdef FEAT_MOUSE_TTY
2081# ifdef FEAT_MOUSE_JSB
2082 if (n == KS_JSBTERM_MOUSE)
2083 has_mouse_termcode &= ~HMT_JSBTERM;
2084 else
2085# endif
2086# ifdef FEAT_MOUSE_NET
2087 if (n == KS_NETTERM_MOUSE)
2088 has_mouse_termcode &= ~HMT_NETTERM;
2089 else
2090# endif
2091# ifdef FEAT_MOUSE_DEC
2092 if (n == KS_DEC_MOUSE)
2093 has_mouse_termcode &= ~HMT_DEC;
2094 else
2095# endif
2096# ifdef FEAT_MOUSE_PTERM
2097 if (n == KS_PTERM_MOUSE)
2098 has_mouse_termcode &= ~HMT_PTERM;
2099 else
2100# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002101# ifdef FEAT_MOUSE_URXVT
2102 if (n == KS_URXVT_MOUSE)
2103 has_mouse_termcode &= ~HMT_URXVT;
2104 else
2105# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002106# ifdef FEAT_MOUSE_SGR
2107 if (n == KS_SGR_MOUSE)
2108 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002109 else if (n == KS_SGR_MOUSE_RELEASE)
2110 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002111 else
2112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 has_mouse_termcode &= ~HMT_NORMAL;
2114# endif
2115}
2116# endif
2117#endif
2118
2119#ifdef HAVE_TGETENT
2120/*
2121 * Call tgetent()
2122 * Return error message if it fails, NULL if it's OK.
2123 */
2124 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002125tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int i;
2128
2129 i = TGETENT(tbuf, term);
2130 if (i < 0 /* -1 is always an error */
2131# ifdef TGETENT_ZERO_ERR
2132 || i == 0 /* sometimes zero is also an error */
2133# endif
2134 )
2135 {
2136 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2137 * tgetent() with the always existing "dumb" entry to avoid a crash or
2138 * hang. */
2139 (void)TGETENT(tbuf, "dumb");
2140
2141 if (i < 0)
2142# ifdef TGETENT_ZERO_ERR
2143 return (char_u *)_("E557: Cannot open termcap file");
2144 if (i == 0)
2145# endif
2146#ifdef TERMINFO
2147 return (char_u *)_("E558: Terminal entry not found in terminfo");
2148#else
2149 return (char_u *)_("E559: Terminal entry not found in termcap");
2150#endif
2151 }
2152 return NULL;
2153}
2154
2155/*
2156 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2157 * Fix that here.
2158 */
2159 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002160vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 char *p;
2163
2164 p = tgetstr(s, (char **)pp);
2165 if (p == (char *)-1)
2166 p = NULL;
2167 return (char_u *)p;
2168}
2169#endif /* HAVE_TGETENT */
2170
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002171#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172/*
2173 * Get Columns and Rows from the termcap. Used after a window signal if the
2174 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2175 * and "li" entries never change. But on some systems this works.
2176 * Errors while getting the entries are ignored.
2177 */
2178 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002179getlinecol(
2180 long *cp, /* pointer to columns */
2181 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 char_u tbuf[TBUFSZ];
2184
2185 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2186 {
2187 if (*cp == 0)
2188 *cp = tgetnum("co");
2189 if (*rp == 0)
2190 *rp = tgetnum("li");
2191 }
2192}
2193#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2194
2195/*
2196 * Get a string entry from the termcap and add it to the list of termcodes.
2197 * Used for <t_xx> special keys.
2198 * Give an error message for failure when not sourcing.
2199 * If force given, replace an existing entry.
2200 * Return FAIL if the entry was not found, OK if the entry was added.
2201 */
2202 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002203add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 char_u *term;
2206 int key;
2207 struct builtin_term *termp;
2208#ifdef HAVE_TGETENT
2209 char_u *string;
2210 int i;
2211 int builtin_first;
2212 char_u tbuf[TBUFSZ];
2213 char_u tstrbuf[TBUFSZ];
2214 char_u *tp = tstrbuf;
2215 char_u *error_msg = NULL;
2216#endif
2217
2218/*
2219 * If the GUI is running or will start in a moment, we only support the keys
2220 * that the GUI can produce.
2221 */
2222#ifdef FEAT_GUI
2223 if (gui.in_use || gui.starting)
2224 return gui_mch_haskey(name);
2225#endif
2226
2227 if (!force && find_termcode(name) != NULL) /* it's already there */
2228 return OK;
2229
2230 term = T_NAME;
2231 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2232 return FAIL;
2233
2234 if (term_is_builtin(term)) /* name starts with "builtin_" */
2235 {
2236 term += 8;
2237#ifdef HAVE_TGETENT
2238 builtin_first = TRUE;
2239#endif
2240 }
2241#ifdef HAVE_TGETENT
2242 else
2243 builtin_first = p_tbi;
2244#endif
2245
2246#ifdef HAVE_TGETENT
2247/*
2248 * We can get the entry from the builtin termcap and from the external one.
2249 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2250 * builtin termcap first.
2251 * If 'ttybuiltin' is off, try external termcap first.
2252 */
2253 for (i = 0; i < 2; ++i)
2254 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002255 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256#endif
2257 /*
2258 * Search in builtin termcap
2259 */
2260 {
2261 termp = find_builtin_term(term);
2262 if (termp->bt_string != NULL) /* found it */
2263 {
2264 key = TERMCAP2KEY(name[0], name[1]);
2265 while (termp->bt_entry != (int)KS_NAME)
2266 {
2267 if ((int)termp->bt_entry == key)
2268 {
2269 add_termcode(name, (char_u *)termp->bt_string,
2270 term_is_8bit(term));
2271 return OK;
2272 }
2273 ++termp;
2274 }
2275 }
2276 }
2277#ifdef HAVE_TGETENT
2278 else
2279 /*
2280 * Search in external termcap
2281 */
2282 {
2283 error_msg = tgetent_error(tbuf, term);
2284 if (error_msg == NULL)
2285 {
2286 string = TGETSTR((char *)name, &tp);
2287 if (string != NULL && *string != NUL)
2288 {
2289 add_termcode(name, string, FALSE);
2290 return OK;
2291 }
2292 }
2293 }
2294 }
2295#endif
2296
2297 if (sourcing_name == NULL)
2298 {
2299#ifdef HAVE_TGETENT
2300 if (error_msg != NULL)
2301 EMSG(error_msg);
2302 else
2303#endif
2304 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2305 }
2306 return FAIL;
2307}
2308
2309 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002310term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2313}
2314
2315/*
2316 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2317 * Assume that the terminal is using 8-bit controls when the name contains
2318 * "8bit", like in "xterm-8bit".
2319 */
2320 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002321term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2324}
2325
2326/*
2327 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002328 * <Esc>[ -> CSI <M_C_[>
2329 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 * <Esc>O -> <M-C-O>
2331 */
2332 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002333term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 if (*p == ESC)
2336 {
2337 if (p[1] == '[')
2338 return CSI;
2339 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002340 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (p[1] == 'O')
2342 return 0x8f;
2343 }
2344 return 0;
2345}
2346
2347#ifdef FEAT_GUI
2348 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002349term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2352}
2353#endif
2354
2355#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2356
2357 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002358tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 static char_u buf[16];
2361 char_u *p;
2362
2363 p = buf + 15;
2364 *p = '\0';
2365 do
2366 {
2367 --p;
2368 *p = (char_u) (i % 10 + '0');
2369 i /= 10;
2370 }
2371 while (i > 0 && p > buf);
2372 return p;
2373}
2374#endif
2375
2376#ifndef HAVE_TGETENT
2377
2378/*
2379 * minimal tgoto() implementation.
2380 * no padding and we only parse for %i %d and %+char
2381 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002382static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002384 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002385tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387 static char buf[30];
2388 char *p, *s, *e;
2389
2390 if (!cm)
2391 return "OOPS";
2392 e = buf + 29;
2393 for (s = buf; s < e && *cm; cm++)
2394 {
2395 if (*cm != '%')
2396 {
2397 *s++ = *cm;
2398 continue;
2399 }
2400 switch (*++cm)
2401 {
2402 case 'd':
2403 p = (char *)tltoa((unsigned long)y);
2404 y = x;
2405 while (*p)
2406 *s++ = *p++;
2407 break;
2408 case 'i':
2409 x++;
2410 y++;
2411 break;
2412 case '+':
2413 *s++ = (char)(*++cm + y);
2414 y = x;
2415 break;
2416 case '%':
2417 *s++ = *cm;
2418 break;
2419 default:
2420 return "OOPS";
2421 }
2422 }
2423 *s = '\0';
2424 return buf;
2425}
2426
2427#endif /* HAVE_TGETENT */
2428
2429/*
2430 * Set the terminal name and initialize the terminal options.
2431 * If "name" is NULL or empty, get the terminal name from the environment.
2432 * If that fails, use the default terminal name.
2433 */
2434 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002435termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 char_u *term;
2438
2439 if (name != NULL && *name == NUL)
2440 name = NULL; /* empty name is equal to no name */
2441 term = name;
2442
2443#ifdef __BEOS__
2444 /*
2445 * TERM environment variable is normally set to 'ansi' on the Bebox;
2446 * Since the BeBox doesn't quite support full ANSI yet, we use our
2447 * own custom 'ansi-beos' termcap instead, unless the -T option has
2448 * been given on the command line.
2449 */
2450 if (term == NULL
2451 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2452 term = DEFAULT_TERM;
2453#endif
2454#ifndef MSWIN
2455 if (term == NULL)
2456 term = mch_getenv((char_u *)"TERM");
2457#endif
2458 if (term == NULL || *term == NUL)
2459 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002460 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /* Set the default terminal name. */
2463 set_string_default("term", term);
2464 set_string_default("ttytype", term);
2465
2466 /*
2467 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2468 */
2469 set_termname(T_NAME != NULL ? T_NAME : term);
2470}
2471
2472/*
2473 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2474 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002475#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2477static char_u out_buf[OUT_SIZE + 1];
2478static int out_pos = 0; /* number of chars in out_buf */
2479
2480/*
2481 * out_flush(): flush the output buffer
2482 */
2483 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002484out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 int len;
2487
2488 if (out_pos != 0)
2489 {
2490 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2491 len = out_pos;
2492 out_pos = 0;
2493 ui_write(out_buf, len);
2494 }
2495}
2496
2497#if defined(FEAT_MBYTE) || defined(PROTO)
2498/*
2499 * Sometimes a byte out of a multi-byte character is written with out_char().
2500 * To avoid flushing half of the character, call this function first.
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2506 out_flush();
2507}
2508#endif
2509
2510#ifdef FEAT_GUI
2511/*
2512 * out_trash(): Throw away the contents of the output buffer
2513 */
2514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002515out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 out_pos = 0;
2518}
2519#endif
2520
2521/*
2522 * out_char(c): put a byte into the output buffer.
2523 * Flush it if it becomes full.
2524 * This should not be used for outputting text on the screen (use functions
2525 * like msg_puts() and screen_putchar() for that).
2526 */
2527 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002528out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2531 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2532 out_char('\r');
2533#endif
2534
2535 out_buf[out_pos++] = c;
2536
2537 /* For testing we flush each time. */
2538 if (out_pos >= OUT_SIZE || p_wd)
2539 out_flush();
2540}
2541
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002542static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
2544/*
2545 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2546 */
2547 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002548out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2551 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2552 out_char_nf('\r');
2553#endif
2554
2555 out_buf[out_pos++] = c;
2556
2557 if (out_pos >= OUT_SIZE)
2558 out_flush();
2559}
2560
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002561#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2562 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563/*
2564 * A never-padding out_str.
2565 * use this whenever you don't want to run the string through tputs.
2566 * tputs above is harmless, but tputs from the termcap library
2567 * is likely to strip off leading digits, that it mistakes for padding
2568 * information, and "%i", "%d", etc.
2569 * This should only be used for writing terminal codes, not for outputting
2570 * normal text (use functions like msg_puts() and screen_putchar() for that).
2571 */
2572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002573out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2576 out_flush();
2577 while (*s)
2578 out_char_nf(*s++);
2579
2580 /* For testing we write one string at a time. */
2581 if (p_wd)
2582 out_flush();
2583}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002587 * A conditional-flushing out_str, mainly for visualbell.
2588 * Handles a delay internally, because termlib may not respect the delay or do
2589 * it at the wrong time.
2590 * Note: Only for terminal strings.
2591 */
2592 void
2593out_str_cf(char_u *s)
2594{
2595 if (s != NULL && *s)
2596 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002597#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002598 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002599#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002600
2601#ifdef FEAT_GUI
2602 /* Don't use tputs() when GUI is used, ncurses crashes. */
2603 if (gui.in_use)
2604 {
2605 out_str_nf(s);
2606 return;
2607 }
2608#endif
2609 if (out_pos > OUT_SIZE - 20)
2610 out_flush();
2611#ifdef HAVE_TGETENT
2612 for (p = s; *s; ++s)
2613 {
2614 /* flush just before delay command */
2615 if (*s == '$' && *(s + 1) == '<')
2616 {
2617 char_u save_c = *s;
2618 int duration = atoi((char *)s + 2);
2619
2620 *s = NUL;
2621 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2622 *s = save_c;
2623 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002624# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002625 /* Only sleep here if we can limit this happening in
2626 * vim_beep(). */
2627 p = vim_strchr(s, '>');
2628 if (p == NULL || duration <= 0)
2629 {
2630 /* can't parse the time, don't sleep here */
2631 p = s;
2632 }
2633 else
2634 {
2635 ++p;
2636 do_sleep(duration);
2637 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002638# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002639 /* Rely on the terminal library to sleep. */
2640 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002641# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002642 break;
2643 }
2644 }
2645 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2646#else
2647 while (*s)
2648 out_char_nf(*s++);
2649#endif
2650
2651 /* For testing we write one string at a time. */
2652 if (p_wd)
2653 out_flush();
2654 }
2655}
2656
2657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * out_str(s): Put a character string a byte at a time into the output buffer.
2659 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2660 * This should only be used for writing terminal codes, not for outputting
2661 * normal text (use functions like msg_puts() and screen_putchar() for that).
2662 */
2663 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002664out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 if (s != NULL && *s)
2667 {
2668#ifdef FEAT_GUI
2669 /* Don't use tputs() when GUI is used, ncurses crashes. */
2670 if (gui.in_use)
2671 {
2672 out_str_nf(s);
2673 return;
2674 }
2675#endif
2676 /* avoid terminal strings being split up */
2677 if (out_pos > OUT_SIZE - 20)
2678 out_flush();
2679#ifdef HAVE_TGETENT
2680 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2681#else
2682 while (*s)
2683 out_char_nf(*s++);
2684#endif
2685
2686 /* For testing we write one string at a time. */
2687 if (p_wd)
2688 out_flush();
2689 }
2690}
2691
2692/*
2693 * cursor positioning using termcap parser. (jw)
2694 */
2695 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 OUT_STR(tgoto((char *)T_CM, col, row));
2699}
2700
2701 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002702term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 OUT_STR(tgoto((char *)T_CRI, 0, i));
2705}
2706
2707 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002708term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2711}
2712
2713 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2717}
2718
2719#if defined(HAVE_TGETENT) || defined(PROTO)
2720 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002721term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 /* Can't handle a negative value here */
2724 if (x < 0)
2725 x = 0;
2726 if (y < 0)
2727 y = 0;
2728 OUT_STR(tgoto((char *)T_CWP, y, x));
2729}
2730
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002731# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2732/*
2733 * Return TRUE if we can request the terminal for a response.
2734 */
2735 static int
2736can_get_termresponse()
2737{
2738 return cur_tmode == TMODE_RAW
2739 && termcap_active
2740# ifdef UNIX
2741 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2742# endif
2743 && p_ek;
2744}
2745
2746static int winpos_x;
2747static int winpos_y;
2748static int waiting_for_winpos = FALSE;
2749
2750/*
2751 * Try getting the Vim window position from the terminal.
2752 * Returns OK or FAIL.
2753 */
2754 int
2755term_get_winpos(int *x, int *y)
2756{
2757 int count = 0;
2758
2759 if (*T_CGP == NUL || !can_get_termresponse())
2760 return FAIL;
2761 winpos_x = -1;
2762 winpos_y = -1;
2763 waiting_for_winpos = TRUE;
2764 OUT_STR(T_CGP);
2765 out_flush();
2766
2767 /* Try reading the result for 100 msec. */
2768 while (count++ < 10)
2769 {
2770 (void)vpeekc_nomap();
2771 if (winpos_x >= 0 && winpos_y >= 0)
2772 {
2773 *x = winpos_x;
2774 *y = winpos_y;
2775 waiting_for_winpos = FALSE;
2776 return OK;
2777 }
2778 ui_delay(10, FALSE);
2779 }
2780 waiting_for_winpos = FALSE;
2781 return FALSE;
2782}
2783# endif
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002786term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002788 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790#endif
2791
2792 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002793term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2796 if (*T_CAF)
2797 term_color(T_CAF, n);
2798 else if (*T_CSF)
2799 term_color(T_CSF, n);
2800}
2801
2802 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002803term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2806 if (*T_CAB)
2807 term_color(T_CAB, n);
2808 else if (*T_CSB)
2809 term_color(T_CSB, n);
2810}
2811
2812 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002813term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 char buf[20];
2816 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2817
2818 /* Special handling of 16 colors, because termcap can't handle it */
2819 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2820 /* Also accept CSI instead of <Esc>[ */
2821 if (n >= 8 && t_colors >= 16
2822 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2823 && s[i] != NUL
2824 && (STRCMP(s + i + 1, "%p1%dm") == 0
2825 || STRCMP(s + i + 1, "%dm") == 0)
2826 && (s[i] == '3' || s[i] == '4'))
2827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002829 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002831 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002833 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2835 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2836 : (n >= 16 ? "48;5;" : "10"));
2837 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2838 }
2839 else
2840 OUT_STR(tgoto((char *)s, 0, n));
2841}
2842
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002843#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002844
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002845#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2846#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2847#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002848
2849 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002850term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002851{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002852#define MAX_COLOR_STR_LEN 100
2853 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002854
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002855 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002856 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002857 OUT_STR(buf);
2858}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002859
2860 void
2861term_fg_rgb_color(guicolor_T rgb)
2862{
2863 term_rgb_color(T_8F, rgb);
2864}
2865
2866 void
2867term_bg_rgb_color(guicolor_T rgb)
2868{
2869 term_rgb_color(T_8B, rgb);
2870}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002871#endif
2872
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002873#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2874 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875/*
2876 * Generic function to set window title, using t_ts and t_fs.
2877 */
2878 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002879term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 /* t_ts takes one argument: column in status line */
2882 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2883 out_str_nf(title);
2884 out_str(T_FS); /* set title end */
2885 out_flush();
2886}
2887#endif
2888
2889/*
2890 * Make sure we have a valid set or terminal options.
2891 * Replace all entries that are NULL by empty_option
2892 */
2893 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002894ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002896 char_u *env_colors;
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 check_options(); /* make sure no options are NULL */
2899
2900 /*
2901 * MUST have "cm": cursor motion.
2902 */
2903 if (*T_CM == NUL)
2904 EMSG(_("E437: terminal capability \"cm\" required"));
2905
2906 /*
2907 * if "cs" defined, use a scroll region, it's faster.
2908 */
2909 if (*T_CS != NUL)
2910 scroll_region = TRUE;
2911 else
2912 scroll_region = FALSE;
2913
2914 if (pairs)
2915 {
2916 /*
2917 * optional pairs
2918 */
2919 /* TP goes to normal mode for TI (invert) and TB (bold) */
2920 if (*T_ME == NUL)
2921 T_ME = T_MR = T_MD = T_MB = empty_option;
2922 if (*T_SO == NUL || *T_SE == NUL)
2923 T_SO = T_SE = empty_option;
2924 if (*T_US == NUL || *T_UE == NUL)
2925 T_US = T_UE = empty_option;
2926 if (*T_CZH == NUL || *T_CZR == NUL)
2927 T_CZH = T_CZR = empty_option;
2928
2929 /* T_VE is needed even though T_VI is not defined */
2930 if (*T_VE == NUL)
2931 T_VI = empty_option;
2932
2933 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2934 if (*T_ME == NUL)
2935 {
2936 T_ME = T_SE;
2937 T_MR = T_SO;
2938 T_MD = T_SO;
2939 }
2940
2941 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2942 if (*T_SO == NUL)
2943 {
2944 T_SE = T_ME;
2945 if (*T_MR == NUL)
2946 T_SO = T_MD;
2947 else
2948 T_SO = T_MR;
2949 }
2950
2951 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2952 if (*T_CZH == NUL)
2953 {
2954 T_CZR = T_ME;
2955 if (*T_MR == NUL)
2956 T_CZH = T_MD;
2957 else
2958 T_CZH = T_MR;
2959 }
2960
2961 /* "Sb" and "Sf" come in pairs */
2962 if (*T_CSB == NUL || *T_CSF == NUL)
2963 {
2964 T_CSB = empty_option;
2965 T_CSF = empty_option;
2966 }
2967
2968 /* "AB" and "AF" come in pairs */
2969 if (*T_CAB == NUL || *T_CAF == NUL)
2970 {
2971 T_CAB = empty_option;
2972 T_CAF = empty_option;
2973 }
2974
2975 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2976 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002977 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 /* Set 'weirdinvert' according to value of 't_xs' */
2980 p_wiv = (*T_XS != NUL);
2981 }
2982 need_gather = TRUE;
2983
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002984 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002986 env_colors = mch_getenv((char_u *)"COLORS");
2987 if (env_colors != NULL && isdigit(*env_colors))
2988 {
2989 int colors = atoi((char *)env_colors);
2990
2991 if (colors != t_colors)
2992 set_color_count(colors);
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2997 || defined(PROTO)
2998/*
2999 * Represent the given long_u as individual bytes, with the most significant
3000 * byte first, and store them in dst.
3001 */
3002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003003add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 int i;
3006 int shift;
3007
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003008 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
3010 shift = 8 * (sizeof(long_u) - i);
3011 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3012 }
3013}
3014
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003015static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017/*
3018 * Interpret the next string of bytes in buf as a long integer, with the most
3019 * significant byte first. Note that it is assumed that buf has been through
3020 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3021 * Puts result in val, and returns the number of bytes read from buf
3022 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3023 * were present.
3024 */
3025 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003026get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 int len;
3029 char_u bytes[sizeof(long_u)];
3030 int i;
3031 int shift;
3032
3033 *val = 0;
3034 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3035 if (len != -1)
3036 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003037 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 shift = 8 * (sizeof(long_u) - 1 - i);
3040 *val += (long_u)bytes[i] << shift;
3041 }
3042 }
3043 return len;
3044}
3045#endif
3046
3047#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003048 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3049 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
3051 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3052 * that buf has been through inchar(). Returns the actual number of bytes used
3053 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3054 * available.
3055 */
3056 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003057get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 int len = 0;
3060 int i;
3061 char_u c;
3062
3063 for (i = 0; i < num_bytes; i++)
3064 {
3065 if ((c = buf[len++]) == NUL)
3066 return -1;
3067 if (c == K_SPECIAL)
3068 {
3069 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3070 return -1;
3071 if (buf[len++] == (int)KS_ZERO)
3072 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003073 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3074 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3075 if (buf[len++] == (int)KE_CSI)
3076 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003078 else if (c == CSI && buf[len] == KS_EXTRA
3079 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003080 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3081 * the start of a special key, see add_to_input_buf_csi(). */
3082 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 bytes[i] = c;
3084 }
3085 return len;
3086}
3087#endif
3088
3089/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003090 * Check if the new shell size is valid, correct it if it's too small or way
3091 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 */
3093 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003094check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (Rows < min_rows()) /* need room for one window and command line */
3097 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003098 limit_screen_size();
3099}
3100
3101/*
3102 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3103 */
3104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003105limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003106{
3107 if (Columns < MIN_COLUMNS)
3108 Columns = MIN_COLUMNS;
3109 else if (Columns > 10000)
3110 Columns = 10000;
3111 if (Rows > 1000)
3112 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113}
3114
Bram Moolenaar86b68352004-12-27 21:59:20 +00003115/*
3116 * Invoked just before the screen structures are going to be (re)allocated.
3117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003119win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 static int old_Rows = 0;
3122 static int old_Columns = 0;
3123
3124 if (old_Rows != Rows || old_Columns != Columns)
3125 ui_new_shellsize();
3126 if (old_Rows != Rows)
3127 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003128 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003129 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003130 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 old_Rows = Rows;
3132 shell_new_rows(); /* update window sizes */
3133 }
3134 if (old_Columns != Columns)
3135 {
3136 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003137#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 shell_new_columns(); /* update window sizes */
3139#endif
3140 }
3141}
3142
3143/*
3144 * Call this function when the Vim shell has been resized in any way.
3145 * Will obtain the current size and redraw (also when size didn't change).
3146 */
3147 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003148shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 set_shellsize(0, 0, FALSE);
3151}
3152
3153/*
3154 * Check if the shell size changed. Handle a resize.
3155 * When the size didn't change, nothing happens.
3156 */
3157 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003158shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 int old_Rows = Rows;
3161 int old_Columns = Columns;
3162
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003163 if (!exiting
3164#ifdef FEAT_GUI
3165 /* Do not get the size when executing a shell command during
3166 * startup. */
3167 && !gui.starting
3168#endif
3169 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003170 {
3171 (void)ui_get_shellsize();
3172 check_shellsize();
3173 if (old_Rows != Rows || old_Columns != Columns)
3174 shell_resized();
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * Set size of the Vim shell.
3180 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3181 * window size (this is used for the :win command).
3182 * If 'mustset' is FALSE, we may try to get the real window size and if
3183 * it fails use 'width' and 'height'.
3184 */
3185 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003186set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 static int busy = FALSE;
3189
3190 /*
3191 * Avoid recursiveness, can happen when setting the window size causes
3192 * another window-changed signal.
3193 */
3194 if (busy)
3195 return;
3196
3197 if (width < 0 || height < 0) /* just checking... */
3198 return;
3199
Bram Moolenaara971b822011-09-14 14:43:25 +02003200 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003202 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 State = SETWSIZE;
3204 return;
3205 }
3206
Bram Moolenaara971b822011-09-14 14:43:25 +02003207 /* curwin->w_buffer can be NULL when we are closing a window and the
3208 * buffer has already been closed and removing a scrollbar causes a resize
3209 * event. Don't resize then, it will happen after entering another buffer.
3210 */
3211 if (curwin->w_buffer == NULL)
3212 return;
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 ++busy;
3215
3216#ifdef AMIGA
3217 out_flush(); /* must do this before mch_get_shellsize() for
3218 some obscure reason */
3219#endif
3220
3221 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3222 {
3223 Rows = height;
3224 Columns = width;
3225 check_shellsize();
3226 ui_set_shellsize(mustset);
3227 }
3228 else
3229 check_shellsize();
3230
3231 /* The window layout used to be adjusted here, but it now happens in
3232 * screenalloc() (also invoked from screenclear()). That is because the
3233 * "busy" check above may skip this, but not screenalloc(). */
3234
3235 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3236 screenclear();
3237 else
3238 screen_start(); /* don't know where cursor is now */
3239
3240 if (starting != NO_SCREEN)
3241 {
3242#ifdef FEAT_TITLE
3243 maketitle();
3244#endif
3245 changed_line_abv_curs();
3246 invalidate_botline();
3247
3248 /*
3249 * We only redraw when it's needed:
3250 * - While at the more prompt or executing an external command, don't
3251 * redraw, but position the cursor.
3252 * - While editing the command line, only redraw that.
3253 * - in Ex mode, don't redraw anything.
3254 * - Otherwise, redraw right now, and position the cursor.
3255 * Always need to call update_screen() or screenalloc(), to make
3256 * sure Rows/Columns and the size of ScreenLines[] is correct!
3257 */
3258 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3259 || exmode_active)
3260 {
3261 screenalloc(FALSE);
3262 repeat_message();
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 else
3265 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003266#ifdef FEAT_SCROLLBIND
3267 if (curwin->w_p_scb)
3268 do_check_scrollbind(TRUE);
3269#endif
3270 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003271 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003272 update_screen(NOT_VALID);
3273 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003274 }
3275 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003276 {
3277 update_topline();
3278#if defined(FEAT_INS_EXPAND)
3279 if (pum_visible())
3280 {
3281 redraw_later(NOT_VALID);
3282 ins_compl_show_pum(); /* This includes the redraw. */
3283 }
3284 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003285#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003286 update_screen(NOT_VALID);
3287 if (redrawing())
3288 setcursor();
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 cursor_on(); /* redrawing may have switched it off */
3292 }
3293 out_flush();
3294 --busy;
3295}
3296
3297/*
3298 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3299 * commands and Ex mode).
3300 */
3301 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003302settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304#ifdef FEAT_GUI
3305 /* don't set the term where gvim was started to any mode */
3306 if (gui.in_use)
3307 return;
3308#endif
3309
3310 if (full_screen)
3311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 /*
3313 * When returning after calling a shell we want to really set the
3314 * terminal to raw mode, even though we think it already is, because
3315 * the shell program may have reset the terminal mode.
3316 * When we think the terminal is normal, don't try to set it to
3317 * normal again, because that causes problems (logout!) on some
3318 * machines.
3319 */
3320 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3321 {
3322#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003323# ifdef FEAT_GUI
3324 if (!gui.in_use && !gui.starting)
3325# endif
3326 {
3327 /* May need to check for T_CRV response and termcodes, it
3328 * doesn't work in Cooked mode, an external program may get
3329 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003330 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3331 || u7_status == STATUS_SENT
3332 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003333 || rbm_status == STATUS_SENT
3334 || rcs_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003335 (void)vpeekc_nomap();
3336 check_for_codes_from_term();
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
3339#ifdef FEAT_MOUSE_TTY
3340 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003341 mch_setmouse(FALSE); /* switch mouse off */
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_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003346 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 cur_tmode = tmode;
3348#ifdef FEAT_MOUSE
3349 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003350 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003352 if (tmode == TMODE_RAW)
3353 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 out_flush();
3355 }
3356#ifdef FEAT_TERMRESPONSE
3357 may_req_termresponse();
3358#endif
3359 }
3360}
3361
3362 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003363starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 if (full_screen && !termcap_active)
3366 {
3367 out_str(T_TI); /* start termcap mode */
3368 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003369 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 out_flush();
3371 termcap_active = TRUE;
3372 screen_start(); /* don't know where cursor is now */
3373#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003374# ifdef FEAT_GUI
3375 if (!gui.in_use && !gui.starting)
3376# endif
3377 {
3378 may_req_termresponse();
3379 /* Immediately check for a response. If t_Co changes, we don't
3380 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003381 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003382 check_for_codes_from_term();
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#endif
3385 }
3386}
3387
3388 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003389stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 screen_stop_highlight();
3392 reset_cterm_colors();
3393 if (termcap_active)
3394 {
3395#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003396# ifdef FEAT_GUI
3397 if (!gui.in_use && !gui.starting)
3398# endif
3399 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003400 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003401 if (crv_status == STATUS_SENT
3402 || u7_status == STATUS_SENT
3403 || rbg_status == STATUS_SENT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003404 || rbm_status == STATUS_SENT
3405 || rcs_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003406 {
3407# ifdef UNIX
3408 /* Give the terminal a chance to respond. */
3409 mch_delay(100L, FALSE);
3410# endif
3411# ifdef TCIFLUSH
3412 /* Discard data received but not read. */
3413 if (exiting)
3414 tcflush(fileno(stdin), TCIFLUSH);
3415# endif
3416 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003417 /* Check for termcodes first, otherwise an external program may
3418 * get them. */
3419 check_for_codes_from_term();
3420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003422 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 out_str(T_KE); /* stop "keypad transmit" mode */
3424 out_flush();
3425 termcap_active = FALSE;
3426 cursor_on(); /* just in case it is still off */
3427 out_str(T_TE); /* stop termcap mode */
3428 screen_start(); /* don't know where cursor is now */
3429 out_flush();
3430 }
3431}
3432
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003433#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434/*
3435 * Request version string (for xterm) when needed.
3436 * Only do this after switching to raw mode, otherwise the result will be
3437 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003438 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003439 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Only do this after termcap mode has been started, otherwise the codes for
3441 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003442 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3443 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003444 * On Unix only do it when both output and input are a tty (avoid writing
3445 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 * The result is caught in check_termcode().
3447 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003448 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003449may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003451 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003452 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003453 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 && *T_CRV != NUL)
3455 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003456 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003458 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 /* check for the characters now, otherwise they might be eaten by
3460 * get_keystroke() */
3461 out_flush();
3462 (void)vpeekc_nomap();
3463 }
3464}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003465
3466# if defined(FEAT_MBYTE) || defined(PROTO)
3467/*
3468 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003469 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003470 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003471 * If the terminal treats \u25bd as single width, the position is (1, 1),
3472 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003473 * This function has the side effect that changes cursor position, so
3474 * it must be called immediately after entering termcap mode.
3475 */
3476 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003477may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003478{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003479 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003480 && can_get_termresponse()
3481 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003482 && *T_U7 != NUL
3483 && !option_was_set((char_u *)"ambiwidth"))
3484 {
3485 char_u buf[16];
3486
Bram Moolenaar2951b772013-07-03 12:45:31 +02003487 LOG_TR("Sending U7 request");
3488 /* Do this in the second row. In the first row the returned sequence
3489 * may be CSI 1;2R, which is the same as <S-F3>. */
3490 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003491 buf[mb_char2bytes(0x25bd, buf)] = 0;
3492 out_str(buf);
3493 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003494 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003495 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003496
3497 /* This overwrites a few characters on the screen, a redraw is needed
3498 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003499 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003500 out_str((char_u *)" ");
3501 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003502
Bram Moolenaar9584b312013-03-13 19:29:28 +01003503 /* check for the characters now, otherwise they might be eaten by
3504 * get_keystroke() */
3505 out_flush();
3506 (void)vpeekc_nomap();
3507 }
3508}
3509# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003510
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003511/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003512 * Similar to requesting the version string: Request the terminal background
3513 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003514 */
3515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003516may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003517{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003518 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003519 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003520 /* Only request background if t_RB is set and 'background' wasn't
3521 * changed. */
3522 if (rbg_status == STATUS_GET
3523 && *T_RBG != NUL
3524 && !option_was_set((char_u *)"bg"))
3525 {
3526 LOG_TR("Sending BG request");
3527 out_str(T_RBG);
3528 rbg_status = STATUS_SENT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003529
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003530 /* check for the characters now, otherwise they might be eaten by
3531 * get_keystroke() */
3532 out_flush();
3533 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003534 }
3535 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003536}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003537
Bram Moolenaar2951b772013-07-03 12:45:31 +02003538# ifdef DEBUG_TERMRESPONSE
3539 static void
3540log_tr(char *msg)
3541{
3542 static FILE *fd_tr = NULL;
3543 static proftime_T start;
3544 proftime_T now;
3545
3546 if (fd_tr == NULL)
3547 {
3548 fd_tr = fopen("termresponse.log", "w");
3549 profile_start(&start);
3550 }
3551 now = start;
3552 profile_end(&now);
3553 fprintf(fd_tr, "%s: %s %s\n",
3554 profile_msg(&now),
3555 must_redraw == NOT_VALID ? "NV"
3556 : must_redraw == CLEAR ? "CL" : " ",
3557 msg);
3558}
3559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#endif
3561
3562/*
3563 * Return TRUE when saving and restoring the screen.
3564 */
3565 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003566swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
3568 return (full_screen && *T_TI != NUL);
3569}
3570
3571#ifdef FEAT_MOUSE
3572/*
3573 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3574 */
3575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003576setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
3578# ifdef FEAT_MOUSE_TTY
3579 int checkfor;
3580# endif
3581
3582# ifdef FEAT_MOUSESHAPE
3583 update_mouseshape(-1);
3584# endif
3585
3586# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3587# ifdef FEAT_GUI
3588 /* In the GUI the mouse is always enabled. */
3589 if (gui.in_use)
3590 return;
3591# endif
3592 /* be quick when mouse is off */
3593 if (*p_mouse == NUL || has_mouse_termcode == 0)
3594 return;
3595
3596 /* don't switch mouse on when not in raw mode (Ex mode) */
3597 if (cur_tmode != TMODE_RAW)
3598 {
3599 mch_setmouse(FALSE);
3600 return;
3601 }
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (VIsual_active)
3604 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003605 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 checkfor = MOUSE_RETURN;
3607 else if (State & INSERT)
3608 checkfor = MOUSE_INSERT;
3609 else if (State & CMDLINE)
3610 checkfor = MOUSE_COMMAND;
3611 else if (State == CONFIRM || State == EXTERNCMD)
3612 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3613 else
3614 checkfor = MOUSE_NORMAL; /* assume normal mode */
3615
3616 if (mouse_has(checkfor))
3617 mch_setmouse(TRUE);
3618 else
3619 mch_setmouse(FALSE);
3620# endif
3621}
3622
3623/*
3624 * Return TRUE if
3625 * - "c" is in 'mouse', or
3626 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3627 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3628 * normal editing mode (not at hit-return message).
3629 */
3630 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003631mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 char_u *p;
3634
3635 for (p = p_mouse; *p; ++p)
3636 switch (*p)
3637 {
3638 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3639 return TRUE;
3640 break;
3641 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3642 return TRUE;
3643 break;
3644 default: if (c == *p) return TRUE; break;
3645 }
3646 return FALSE;
3647}
3648
3649/*
3650 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3651 */
3652 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003653mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
3655 return (p_mousem[0] == 'p');
3656}
3657#endif
3658
3659/*
3660 * By outputting the 'cursor very visible' termcap code, for some windowed
3661 * terminals this makes the screen scrolled to the correct position.
3662 * Used when starting Vim or returning from a shell.
3663 */
3664 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003665scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003667 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003670 out_str(T_CVS);
3671 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673}
3674
3675static int cursor_is_off = FALSE;
3676
3677/*
3678 * Enable the cursor.
3679 */
3680 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003681cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 if (cursor_is_off)
3684 {
3685 out_str(T_VE);
3686 cursor_is_off = FALSE;
3687 }
3688}
3689
3690/*
3691 * Disable the cursor.
3692 */
3693 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003694cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003696 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003698 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 cursor_is_off = TRUE;
3700 }
3701}
3702
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003703#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003705 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003706 */
3707 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003708term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003709{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003710 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003711 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003712
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003713 /* Only do something when redrawing the screen and we can restore the
3714 * mode. */
3715 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003716 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003717# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003718 if (forced && initial_cursor_shape > 0)
3719 /* Restore to initial values. */
3720 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003721# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003722 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003723 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003724
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003725 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003726 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003727 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003728 {
3729 if (*T_CSR != NUL)
3730 p = T_CSR; /* Replace mode cursor */
3731 else
3732 p = T_CSI; /* fall back to Insert mode cursor */
3733 if (*p != NUL)
3734 {
3735 out_str(p);
3736 showing_mode = REPLACE;
3737 }
3738 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003739 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003740 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003741 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003742 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003743 {
3744 out_str(T_CSI); /* Insert mode cursor */
3745 showing_mode = INSERT;
3746 }
3747 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003748 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003749 {
3750 out_str(T_CEI); /* non-Insert mode cursor */
3751 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003752 }
3753}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003754
3755# if defined(FEAT_TERMINAL) || defined(PROTO)
3756 void
3757term_cursor_color(char_u *color)
3758{
3759 if (*T_CSC != NUL)
3760 {
3761 out_str(T_CSC); /* set cursor color start */
3762 out_str_nf(color);
3763 out_str(T_CEC); /* set cursor color end */
3764 out_flush();
3765 }
3766}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003767# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003768
Bram Moolenaar4db25542017-08-28 22:43:05 +02003769 int
3770blink_state_is_inverted()
3771{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003772#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4db25542017-08-28 22:43:05 +02003773 return rbm_status == STATUS_GOT && rcs_status == STATUS_GOT
3774 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003775#else
3776 return FALSE;
3777#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003778}
3779
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003780/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003781 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003782 */
3783 void
3784term_cursor_shape(int shape, int blink)
3785{
3786 if (*T_CSH != NUL)
3787 {
3788 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3789 out_flush();
3790 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003791 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003792 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003793 int do_blink = blink;
3794
3795 /* t_SH is empty: try setting just the blink state.
3796 * The blink flags are XORed together, if the initial blinking from
3797 * style and shape differs, we need to invert the flag here. */
3798 if (blink_state_is_inverted())
3799 do_blink = !blink;
3800
3801 if (do_blink && *T_VS != NUL)
3802 {
3803 out_str(T_VS);
3804 out_flush();
3805 }
3806 else if (!do_blink && *T_CVS != NUL)
3807 {
3808 out_str(T_CVS);
3809 out_flush();
3810 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003811 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003812}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003813#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003814
3815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 * Set scrolling region for window 'wp'.
3817 * The region starts 'off' lines from the start of the window.
3818 * Also set the vertical scroll region for a vertically split window. Always
3819 * the full width of the window, excluding the vertical separator.
3820 */
3821 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003822scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3825 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003826#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (*T_CSV != NUL && wp->w_width != Columns)
3828 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3829 W_WINCOL(wp)));
3830#endif
3831 screen_start(); /* don't know where cursor is now */
3832}
3833
3834/*
3835 * Reset scrolling region to the whole screen.
3836 */
3837 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003838scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003841#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (*T_CSV != NUL)
3843 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3844#endif
3845 screen_start(); /* don't know where cursor is now */
3846}
3847
3848
3849/*
3850 * List of terminal codes that are currently recognized.
3851 */
3852
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003853static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 char_u name[2]; /* termcap name of entry */
3856 char_u *code; /* terminal code (in allocated memory) */
3857 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003858 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859} *termcodes = NULL;
3860
3861static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3862static int tc_len = 0; /* current number of entries in termcodes[] */
3863
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003864static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003867clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 while (tc_len > 0)
3870 vim_free(termcodes[--tc_len].code);
3871 vim_free(termcodes);
3872 termcodes = NULL;
3873 tc_max_len = 0;
3874
3875#ifdef HAVE_TGETENT
3876 BC = (char *)empty_option;
3877 UP = (char *)empty_option;
3878 PC = NUL; /* set pad character to NUL */
3879 ospeed = 0;
3880#endif
3881
3882 need_gather = TRUE; /* need to fill termleader[] */
3883}
3884
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003885#define ATC_FROM_TERM 55
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887/*
3888 * Add a new entry to the list of terminal codes.
3889 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003890 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3891 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003894add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 struct termcode *new_tc;
3897 int i, j;
3898 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003899 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 if (string == NULL || *string == NUL)
3902 {
3903 del_termcode(name);
3904 return;
3905 }
3906
Bram Moolenaar45500912014-07-09 20:51:07 +02003907#if defined(WIN3264) && !defined(FEAT_GUI)
3908 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3909#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (s == NULL)
3913 return;
3914
3915 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003916 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003918 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 s[0] = term_7to8bit(string);
3920 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003921
3922#if defined(WIN3264) && !defined(FEAT_GUI)
3923 if (s[0] == K_NUL)
3924 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003925 STRMOVE(s + 1, s);
3926 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003927 }
3928#endif
3929
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003930 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 need_gather = TRUE; /* need to fill termleader[] */
3933
3934 /*
3935 * need to make space for more entries
3936 */
3937 if (tc_len == tc_max_len)
3938 {
3939 tc_max_len += 20;
3940 new_tc = (struct termcode *)alloc(
3941 (unsigned)(tc_max_len * sizeof(struct termcode)));
3942 if (new_tc == NULL)
3943 {
3944 tc_max_len -= 20;
3945 return;
3946 }
3947 for (i = 0; i < tc_len; ++i)
3948 new_tc[i] = termcodes[i];
3949 vim_free(termcodes);
3950 termcodes = new_tc;
3951 }
3952
3953 /*
3954 * Look for existing entry with the same name, it is replaced.
3955 * Look for an existing entry that is alphabetical higher, the new entry
3956 * is inserted in front of it.
3957 */
3958 for (i = 0; i < tc_len; ++i)
3959 {
3960 if (termcodes[i].name[0] < name[0])
3961 continue;
3962 if (termcodes[i].name[0] == name[0])
3963 {
3964 if (termcodes[i].name[1] < name[1])
3965 continue;
3966 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
3969 if (termcodes[i].name[1] == name[1])
3970 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971 if (flags == ATC_FROM_TERM && (j = termcode_star(
3972 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003973 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 /* Don't replace ESC[123;*X or ESC O*X with another when
3975 * invoked from got_code_from_term(). */
3976 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003977 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003978 && s[len - 1]
3979 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003980 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003982 vim_free(s);
3983 return;
3984 }
3985 }
3986 else
3987 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003989 vim_free(termcodes[i].code);
3990 --tc_len;
3991 break;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 }
3995 /*
3996 * Found alphabetical larger entry, move rest to insert new entry
3997 */
3998 for (j = tc_len; j > i; --j)
3999 termcodes[j] = termcodes[j - 1];
4000 break;
4001 }
4002
4003 termcodes[i].name[0] = name[0];
4004 termcodes[i].name[1] = name[1];
4005 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004006 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007
4008 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4009 * accept modifiers. */
4010 termcodes[i].modlen = 0;
4011 j = termcode_star(s, len);
4012 if (j > 0)
4013 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 ++tc_len;
4015}
4016
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004018 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004019 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004020 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004021 */
4022 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004023termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004024{
4025 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4026 if (len >= 3 && code[len - 2] == '*')
4027 {
4028 if (len >= 5 && code[len - 3] == ';')
4029 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004030 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004031 return 1;
4032 }
4033 return 0;
4034}
4035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004037find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 int i;
4040
4041 for (i = 0; i < tc_len; ++i)
4042 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4043 return termcodes[i].code;
4044 return NULL;
4045}
4046
4047#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4048 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004049get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
4051 if (i >= tc_len)
4052 return NULL;
4053 return &termcodes[i].name[0];
4054}
4055#endif
4056
4057 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004058del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 int i;
4061
4062 if (termcodes == NULL) /* nothing there yet */
4063 return;
4064
4065 need_gather = TRUE; /* need to fill termleader[] */
4066
4067 for (i = 0; i < tc_len; ++i)
4068 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4069 {
4070 del_termcode_idx(i);
4071 return;
4072 }
4073 /* not found. Give error message? */
4074}
4075
4076 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004077del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 int i;
4080
4081 vim_free(termcodes[idx].code);
4082 --tc_len;
4083 for (i = idx; i < tc_len; ++i)
4084 termcodes[i] = termcodes[i + 1];
4085}
4086
4087#ifdef FEAT_TERMRESPONSE
4088/*
4089 * Called when detected that the terminal sends 8-bit codes.
4090 * Convert all 7-bit codes to their 8-bit equivalent.
4091 */
4092 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004093switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 int i;
4096 int c;
4097
4098 /* Only need to do something when not already using 8-bit codes. */
4099 if (!term_is_8bit(T_NAME))
4100 {
4101 for (i = 0; i < tc_len; ++i)
4102 {
4103 c = term_7to8bit(termcodes[i].code);
4104 if (c != 0)
4105 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004106 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 termcodes[i].code[0] = c;
4108 }
4109 }
4110 need_gather = TRUE; /* need to fill termleader[] */
4111 }
4112 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004113 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115#endif
4116
4117#ifdef CHECK_DOUBLE_CLICK
4118static linenr_T orig_topline = 0;
4119# ifdef FEAT_DIFF
4120static int orig_topfill = 0;
4121# endif
4122#endif
4123#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4124/*
4125 * Checking for double clicks ourselves.
4126 * "orig_topline" is used to avoid detecting a double-click when the window
4127 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4128 */
4129/*
4130 * Set orig_topline. Used when jumping to another window, so that a double
4131 * click still works.
4132 */
4133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004134set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 orig_topline = wp->w_topline;
4137# ifdef FEAT_DIFF
4138 orig_topfill = wp->w_topfill;
4139# endif
4140}
4141#endif
4142
4143/*
4144 * Check if typebuf.tb_buf[] contains a terminal key code.
4145 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4146 * + max_offset].
4147 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004148 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * With a match, the match is removed, the replacement code is inserted in
4150 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4151 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004152 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4153 * "buflen" is then the length of the string in buf[] and is updated for
4154 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
4156 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004157check_termcode(
4158 int max_offset,
4159 char_u *buf,
4160 int bufsize,
4161 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 char_u *tp;
4164 char_u *p;
4165 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004168 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int offset;
4170 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004171 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004172 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 int new_slen;
4175 int extra;
4176 char_u string[MAX_KEY_CODE_LEN + 1];
4177 int i, j;
4178 int idx = 0;
4179#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004180# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4181 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 char_u bytes[6];
4183 int num_bytes;
4184# endif
4185 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 int is_click, is_drag;
4187 int wheel_code = 0;
4188 int current_button;
4189 static int held_button = MOUSE_RELEASE;
4190 static int orig_num_clicks = 1;
4191 static int orig_mouse_code = 0x0;
4192# ifdef CHECK_DOUBLE_CLICK
4193 static int orig_mouse_col = 0;
4194 static int orig_mouse_row = 0;
4195 static struct timeval orig_mouse_time = {0, 0};
4196 /* time of previous mouse click */
4197 struct timeval mouse_time; /* time of current mouse click */
4198 long timediff; /* elapsed time in msec */
4199# endif
4200#endif
4201 int cpo_koffset;
4202#ifdef FEAT_MOUSE_GPM
4203 extern int gpm_flag; /* gpm library variable */
4204#endif
4205
4206 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4207
4208 /*
4209 * Speed up the checks for terminal codes by gathering all first bytes
4210 * used in termleader[]. Often this is just a single <Esc>.
4211 */
4212 if (need_gather)
4213 gather_termleader();
4214
4215 /*
4216 * Check at several positions in typebuf.tb_buf[], to catch something like
4217 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4218 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004219 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * This is used often, KEEP IT FAST!
4221 */
4222 for (offset = 0; offset < max_offset; ++offset)
4223 {
4224 if (buf == NULL)
4225 {
4226 if (offset >= typebuf.tb_len)
4227 break;
4228 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4229 len = typebuf.tb_len - offset; /* length of the input */
4230 }
4231 else
4232 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004233 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 break;
4235 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004236 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238
4239 /*
4240 * Don't check characters after K_SPECIAL, those are already
4241 * translated terminal chars (avoid translating ~@^Hx).
4242 */
4243 if (*tp == K_SPECIAL)
4244 {
4245 offset += 2; /* there are always 2 extra characters */
4246 continue;
4247 }
4248
4249 /*
4250 * Skip this position if the character does not appear as the first
4251 * character in term_strings. This speeds up a lot, since most
4252 * termcodes start with the same character (ESC or CSI).
4253 */
4254 i = *tp;
4255 for (p = termleader; *p && *p != i; ++p)
4256 ;
4257 if (*p == NUL)
4258 continue;
4259
4260 /*
4261 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4262 * are in Insert mode.
4263 */
4264 if (*tp == ESC && !p_ek && (State & INSERT))
4265 continue;
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004268 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004269 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271#ifdef FEAT_GUI
4272 if (gui.in_use)
4273 {
4274 /*
4275 * GUI special key codes are all of the form [CSI xx].
4276 */
4277 if (*tp == CSI) /* Special key from GUI */
4278 {
4279 if (len < 3)
4280 return -1; /* Shouldn't happen */
4281 slen = 3;
4282 key_name[0] = tp[1];
4283 key_name[1] = tp[2];
4284 }
4285 }
4286 else
4287#endif /* FEAT_GUI */
4288 {
4289 for (idx = 0; idx < tc_len; ++idx)
4290 {
4291 /*
4292 * Ignore the entry if we are not at the start of
4293 * typebuf.tb_buf[]
4294 * and there are not enough characters to make a match.
4295 * But only when the 'K' flag is in 'cpoptions'.
4296 */
4297 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004298 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if (cpo_koffset && offset && len < slen)
4300 continue;
4301 if (STRNCMP(termcodes[idx].code, tp,
4302 (size_t)(slen > len ? len : slen)) == 0)
4303 {
4304 if (len < slen) /* got a partial sequence */
4305 return -1; /* need to get more chars */
4306
4307 /*
4308 * When found a keypad key, check if there is another key
4309 * that matches and use that one. This makes <Home> to be
4310 * found instead of <kHome> when they produce the same
4311 * key code.
4312 */
4313 if (termcodes[idx].name[0] == 'K'
4314 && VIM_ISDIGIT(termcodes[idx].name[1]))
4315 {
4316 for (j = idx + 1; j < tc_len; ++j)
4317 if (termcodes[j].len == slen &&
4318 STRNCMP(termcodes[idx].code,
4319 termcodes[j].code, slen) == 0)
4320 {
4321 idx = j;
4322 break;
4323 }
4324 }
4325
4326 key_name[0] = termcodes[idx].name[0];
4327 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 break;
4329 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004330
4331 /*
4332 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004333 * <Esc>[123;*X (modslen == slen - 3)
4334 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4335 * When there is a modifier the * matches a number.
4336 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004337 */
4338 if (termcodes[idx].modlen > 0)
4339 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004340 modslen = termcodes[idx].modlen;
4341 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342 continue;
4343 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004345 {
4346 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004347
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 return -1; /* need to get more chars */
4350
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004351 if (tp[modslen] == termcodes[idx].code[slen - 1])
4352 slen = modslen + 1; /* no modifiers */
4353 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354 continue; /* no match */
4355 else
4356 {
4357 /* Skip over the digits, the final char must
4358 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004359 for (j = slen - 2; j < len && (isdigit(tp[j])
4360 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004361 ;
4362 ++j;
4363 if (len < j) /* got a partial sequence */
4364 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004365 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4366 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004367
Bram Moolenaara529ce02017-06-22 22:37:57 +02004368 modifiers_start = tp + slen - 2;
4369
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004370 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004371 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004372 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004373 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004374 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004375 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004376 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004377 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004378 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004379 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004380
4381 slen = j;
4382 }
4383 key_name[0] = termcodes[idx].name[0];
4384 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004385 break;
4386 }
4387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 }
4390
4391#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004392 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004393 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004394 * detecting the start of these mouse codes they might as well be
4395 * another key code or terminal response. */
4396# ifdef FEAT_MOUSE_DEC
4397 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004398# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004399# ifdef FEAT_MOUSE_PTERM
4400 || key_name[0] == KS_PTERM_MOUSE
4401# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004402 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004404 /* Check for some responses from the terminal starting with
4405 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004406 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004407 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004408 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004409 * Also eat other possible responses to t_RV, rxvt returns
4410 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4411 * mrxvt has been reported to have "+" in the version. Assume
4412 * the escape sequence ends with a letter or one of "{|}~".
4413 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004414 * - Cursor position report: <Esc>[{row};{col}R
4415 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004416 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004417 *
4418 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004419 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004420 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004421
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004422 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004423 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004424 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004425 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004427 int col = 0;
4428 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004429#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004430 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004431#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004434 for (i = 2 + (tp[0] != CSI); i < len
4435 && !(tp[i] >= '{' && tp[i] <= '~')
4436 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004437 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004438 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004439 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004440#ifdef FEAT_MBYTE
4441 row_char = tp[i - 1];
4442#endif
4443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004445 {
4446 LOG_TR("Not enough characters for CRV");
4447 return -1;
4448 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004449 if (extra > 0)
4450 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004451
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004452#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004453 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4454 * u7_status is not "sent", it may be from a previous Vim that
4455 * just exited. But not for <S-F3>, it sends something
4456 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004457 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004458 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004459 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004460 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004461 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004462
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004463 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004464 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004465# ifdef FEAT_AUTOCMD
4466 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004467# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004468 if (col == 2)
4469 aw = "single";
4470 else if (col == 3)
4471 aw = "double";
4472 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4473 {
4474 /* Setting the option causes a screen redraw. Do
4475 * that right away if possible, keeping any
4476 * messages. */
4477 set_option_value((char_u *)"ambw", 0L,
4478 (char_u *)aw, 0);
4479# ifdef DEBUG_TERMRESPONSE
4480 {
4481 char buf[100];
4482 int r = redraw_asap(CLEAR);
4483
4484 sprintf(buf,
4485 "set 'ambiwidth', redraw_asap(): %d",
4486 r);
4487 log_tr(buf);
4488 }
4489# else
4490 redraw_asap(CLEAR);
4491# endif
4492 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004493 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004494 key_name[0] = (int)KS_EXTRA;
4495 key_name[1] = (int)KE_IGNORE;
4496 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004497# ifdef FEAT_EVAL
4498 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4499# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004500 }
4501 else
4502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004504 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004506 int version = col;
4507
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004508 LOG_TR("Received CRV response");
4509 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004510# ifdef FEAT_AUTOCMD
4511 did_cursorhold = TRUE;
4512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 /* If this code starts with CSI, you can bet that the
4515 * terminal uses 8-bit codes. */
4516 if (tp[0] == CSI)
4517 switch_to_8bit();
4518
4519 /* rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004520 * Screen sends 40500.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 * Ignore it for when the user has set 'term' to xterm,
4522 * even though it's an rxvt. */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004523 if (version > 20000)
4524 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004526 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004528 int need_flush = FALSE;
4529
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004530 /* Only set 'ttymouse' automatically if it was not set
4531 * by the user already. */
4532 if (!option_was_set((char_u *)"ttym"))
4533 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004534# ifdef TTYM_SGR
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004535 if (version >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004536 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004537 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004538 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004539# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004540 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004541 if (version >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004542 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004544 }
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004547 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004549 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 check_for_codes = TRUE;
4551 need_gather = TRUE;
4552 req_codes_from_term();
4553 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004554
4555 /* libvterm sends 0;100;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004556 if (version == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004557 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004558 {
4559 /* If run from Vim $COLORS is set to the number of
4560 * colors the terminal supports. Otherwise assume
4561 * 256, libvterm supports even more. */
4562 if (mch_getenv((char_u *)"COLORS") == NULL)
4563 may_adjust_color_count(256);
4564 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004565
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004566 /* Detect terminals that set $TERM to something like
4567 * "xterm-256colors" but are not fully xterm
4568 * compatible. */
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004569# ifdef MACOS
4570 /* Mac Terminal.app sends 1;95;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004571 if (version == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004572 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004573 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004574# endif
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004575 /* Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004576 * xfce4-terminal sends 1;2802;0.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004577 * screen sends 83;40500;0
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004578 * Assuming any version number over 2500 is not an
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004579 * xterm (without the limit for rxvt and screen). */
Bram Moolenaar3d8d2c72017-09-05 21:57:27 +02004580 if (col >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004581 is_not_xterm = TRUE;
4582
4583 /* PuTTY sends 0;136;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004584 if (version == 136
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004585 && STRNCMP(tp + extra - 2, "0;136;0c", 8) == 0)
4586 is_not_xterm = TRUE;
4587
4588 /* Konsole sends 0;115;0 */
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004589 if (version == 115
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004590 && STRNCMP(tp + extra - 2, "0;115;0c", 8) == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004591 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004592
4593 /* Only request the cursor style if t_SH and t_RS are
4594 * set. Not for Terminal.app, it can't handle t_RS, it
4595 * echoes the characters to the screen. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004596 if (rcs_status == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004597 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004598 && *T_CSH != NUL
4599 && *T_CRS != NUL)
4600 {
4601 LOG_TR("Sending cursor style request");
4602 out_str(T_CRS);
Bram Moolenaar4db25542017-08-28 22:43:05 +02004603 rcs_status = STATUS_SENT;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004604 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004605 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004606
4607 /* Only request the cursor blink mode if t_RC set. Not
4608 * for Gnome terminal, it can't handle t_RC, it
4609 * echoes the characters to the screen. */
4610 if (rbm_status == STATUS_GET
4611 && !is_not_xterm
4612 && *T_CRC != NUL)
4613 {
4614 LOG_TR("Sending cursor blink mode request");
4615 out_str(T_CRC);
4616 rbm_status = STATUS_SENT;
4617 need_flush = TRUE;
4618 }
4619
4620 if (need_flush)
4621 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004623 slen = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004625 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626# endif
4627# ifdef FEAT_AUTOCMD
4628 apply_autocmds(EVENT_TERMRESPONSE,
4629 NULL, NULL, FALSE, curbuf);
4630# endif
4631 key_name[0] = (int)KS_EXTRA;
4632 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004634
Bram Moolenaar4db25542017-08-28 22:43:05 +02004635 /* Check blinking cursor from xterm:
4636 * {lead}?12;1$y set
4637 * {lead}?12;2$y not set
4638 *
4639 * {lead} can be <Esc>[ or CSI
4640 */
4641 else if (rbm_status == STATUS_SENT
4642 && tp[(j = 1 + (tp[0] == ESC))] == '?'
4643 && i == j + 6
4644 && tp[j + 1] == '1'
4645 && tp[j + 2] == '2'
4646 && tp[j + 3] == ';'
4647 && tp[i - 1] == '$'
4648 && tp[i] == 'y')
4649 {
4650 initial_cursor_blink = (tp[j + 4] == '1');
4651 rbm_status = STATUS_GOT;
4652 LOG_TR("Received cursor blinking mode response");
4653 key_name[0] = (int)KS_EXTRA;
4654 key_name[1] = (int)KE_IGNORE;
4655 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004656# ifdef FEAT_EVAL
4657 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4658# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004659 }
4660
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004661 /*
4662 * Check for a window position response from the terminal:
4663 * {lead}3;{x}:{y}t
4664 */
4665 else if (waiting_for_winpos
4666 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4667 || (len >= 3 && tp[0] == CSI))
4668 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4669 && tp[j + 1] == ';')
4670 {
4671 j += 2;
4672 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4673 ;
4674 if (i < len && tp[i] == ';')
4675 {
4676 winpos_x = atoi((char *)tp + j);
4677 j = i + 1;
4678 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4679 ;
4680 if (i < len && tp[i] == 't')
4681 {
4682 winpos_y = atoi((char *)tp + j);
4683 /* got finished code: consume it */
4684 key_name[0] = (int)KS_EXTRA;
4685 key_name[1] = (int)KE_IGNORE;
4686 slen = i + 1;
4687 }
4688 }
4689 if (i == len)
4690 {
4691 LOG_TR("not enough characters for winpos");
4692 return -1;
4693 }
4694 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004695 }
4696
4697 /* Check for background color response from the terminal:
4698 *
4699 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4700 *
4701 * {lead} can be <Esc>] or OSC
4702 * {tail} can be '\007', <Esc>\ or STERM.
4703 *
4704 * Consume any code that starts with "{lead}11;", it's also
4705 * possible that "rgba" is following.
4706 */
4707 else if (*T_RBG != NUL
4708 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4709 || tp[0] == OSC))
4710 {
4711 j = 1 + (tp[0] == ESC);
4712 if (len >= j + 3 && (argp[0] != '1'
4713 || argp[1] != '1' || argp[2] != ';'))
4714 i = 0; /* no match */
4715 else
4716 for (i = j; i < len; ++i)
4717 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4718 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004719 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004720 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4721 && tp[j + 11] == '/' && tp[j + 16] == '/'
4722 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004723 {
4724 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4725 + tp[j+17]) ? "light" : "dark";
4726
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004727 LOG_TR("Received RBG response");
4728 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004729 if (STRCMP(p_bg, newval) != 0)
4730 {
4731 /* value differs, apply it */
4732 set_option_value((char_u *)"bg", 0L,
4733 (char_u *)newval, 0);
4734 reset_option_was_set((char_u *)"bg");
4735 redraw_asap(CLEAR);
4736 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004737 }
4738
4739 /* got finished code: consume it */
4740 key_name[0] = (int)KS_EXTRA;
4741 key_name[1] = (int)KE_IGNORE;
4742 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf6d9f962017-08-24 20:21:16 +02004743 if (tp[i] == 0x07 && i + 1 < len && tp[i + 1] == 0x18)
4744 /* Sometimes the 0x07 is followed by 0x18, unclear
4745 * when this happens. */
4746 ++slen;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004747# ifdef FEAT_EVAL
4748 set_vim_var_string(VV_TERMRGBRESP, tp, slen);
4749# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004750 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004751 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004752 if (i == len)
4753 {
4754 LOG_TR("not enough characters for RB");
4755 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004759 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004760 * {lead}{flag}+r<hex bytes><{tail}
4761 *
4762 * {lead} can be <Esc>P or DCS
4763 * {flag} can be '0' or '1'
4764 * {tail} can be Esc>\ or STERM
4765 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004766 * Check for cursor shape response from xterm:
4767 * {lead}1$r<number> q{tail}
4768 *
4769 * {lead} can be <Esc>P or DCS
4770 * {tail} can be Esc>\ or STERM
4771 *
4772 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004773 */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004774 else if ((check_for_codes || rcs_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004775 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 || tp[0] == DCS))
4777 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004778 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004779 if (len < j + 3)
4780 i = len; /* need more chars */
4781 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004782 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004783 else if (argp[1] == '+')
4784 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004785 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004786 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004787 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 || tp[i] == STERM)
4789 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004790 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 got_code_from_term(tp + j, i);
4792 key_name[0] = (int)KS_EXTRA;
4793 key_name[1] = (int)KE_IGNORE;
4794 slen = i + 1 + (tp[i] == ESC);
4795 break;
4796 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004797 }
4798 else if ((len >= j + 6 && isdigit(argp[3]))
4799 && argp[4] == ' '
4800 && argp[5] == 'q')
4801 {
4802 /* cursor shape response */
4803 i = j + 6;
4804 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4805 || tp[i] == STERM)
4806 {
4807 int number = argp[3] - '0';
4808
4809 /* 0, 1 = block blink, 2 = block
4810 * 3 = underline blink, 4 = underline
4811 * 5 = vertical bar blink, 6 = vertical bar */
4812 number = number == 0 ? 1 : number;
4813 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004814 /* The blink flag is actually inverted, compared to
4815 * the value set with T_SH. */
Bram Moolenaar4db25542017-08-28 22:43:05 +02004816 initial_cursor_shape_blink =
4817 (number & 1) ? FALSE : TRUE;
4818 rcs_status = STATUS_GOT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004819 LOG_TR("Received cursor shape response");
4820
4821 key_name[0] = (int)KS_EXTRA;
4822 key_name[1] = (int)KE_IGNORE;
4823 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004824# ifdef FEAT_EVAL
4825 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
4826# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004827 }
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004831 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004832 /* These codes arrive many together, each code can be
4833 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004834 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004835 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 }
4839#endif
4840
4841 if (key_name[0] == NUL)
4842 continue; /* No match at this position, try next one */
4843
4844 /* We only get here when we have a complete termcode match */
4845
4846#ifdef FEAT_MOUSE
4847# ifdef FEAT_GUI
4848 /*
4849 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4850 * so that we know which window to scroll later.
4851 */
4852 if (gui.in_use
4853 && key_name[0] == (int)KS_EXTRA
4854 && (key_name[1] == (int)KE_X1MOUSE
4855 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004856 || key_name[1] == (int)KE_MOUSELEFT
4857 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 || key_name[1] == (int)KE_MOUSEDOWN
4859 || key_name[1] == (int)KE_MOUSEUP))
4860 {
4861 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4862 if (num_bytes == -1) /* not enough coordinates */
4863 return -1;
4864 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4865 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4866 slen += num_bytes;
4867 }
4868 else
4869# endif
4870 /*
4871 * If it is a mouse click, get the coordinates.
4872 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004873 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004875 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876# endif
4877# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004878 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879# endif
4880# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004881 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882# endif
4883# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004884 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004886# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004887 || key_name[0] == KS_URXVT_MOUSE
4888# endif
4889# ifdef FEAT_MOUSE_SGR
4890 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004891 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 )
4894 {
4895 is_click = is_drag = FALSE;
4896
Bram Moolenaar864207d2008-06-24 22:14:38 +00004897# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4898 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (key_name[0] == (int)KS_MOUSE)
4900 {
4901 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004902 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * s == encoded button state:
4904 * 0x20 = left button down
4905 * 0x21 = middle button down
4906 * 0x22 = right button down
4907 * 0x23 = any button release
4908 * 0x60 = button 4 down (scroll wheel down)
4909 * 0x61 = button 5 down (scroll wheel up)
4910 * add 0x04 for SHIFT
4911 * add 0x08 for ALT
4912 * add 0x10 for CTRL
4913 * add 0x20 for mouse drag (0x40 is drag with left button)
4914 * c == column + ' ' + 1 == column + 33
4915 * r == row + ' ' + 1 == row + 33
4916 *
4917 * The coordinates are passed on through global variables.
4918 * Ugly, but this avoids trouble with mouse clicks at an
4919 * unexpected moment and allows for mapping them.
4920 */
4921 for (;;)
4922 {
4923#ifdef FEAT_GUI
4924 if (gui.in_use)
4925 {
4926 /* GUI uses more bits for columns > 223 */
4927 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4928 if (num_bytes == -1) /* not enough coordinates */
4929 return -1;
4930 mouse_code = bytes[0];
4931 mouse_col = 128 * (bytes[1] - ' ' - 1)
4932 + bytes[2] - ' ' - 1;
4933 mouse_row = 128 * (bytes[3] - ' ' - 1)
4934 + bytes[4] - ' ' - 1;
4935 }
4936 else
4937#endif
4938 {
4939 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4940 if (num_bytes == -1) /* not enough coordinates */
4941 return -1;
4942 mouse_code = bytes[0];
4943 mouse_col = bytes[1] - ' ' - 1;
4944 mouse_row = bytes[2] - ' ' - 1;
4945 }
4946 slen += num_bytes;
4947
4948 /* If the following bytes is also a mouse code and it has
4949 * the same code, dump this one and get the next. This
4950 * makes dragging a whole lot faster. */
4951#ifdef FEAT_GUI
4952 if (gui.in_use)
4953 j = 3;
4954 else
4955#endif
4956 j = termcodes[idx].len;
4957 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4958 && tp[slen + j] == mouse_code
4959 && tp[slen + j + 1] != NUL
4960 && tp[slen + j + 2] != NUL
4961#ifdef FEAT_GUI
4962 && (!gui.in_use
4963 || (tp[slen + j + 3] != NUL
4964 && tp[slen + j + 4] != NUL))
4965#endif
4966 )
4967 slen += j;
4968 else
4969 break;
4970 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004973# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4974 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004975 || key_name[0] == KS_SGR_MOUSE
4976 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004977 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004978 /* URXVT 1015 mouse reporting mode:
4979 * Almost identical to xterm mouse mode, except the values
4980 * are decimal instead of bytes.
4981 *
4982 * \033[%d;%d;%dM
4983 * ^-- row
4984 * ^----- column
4985 * ^-------- code
4986 *
4987 * SGR 1006 mouse reporting mode:
4988 * Almost identical to xterm mouse mode, except the values
4989 * are decimal instead of bytes.
4990 *
4991 * \033[<%d;%d;%dM
4992 * ^-- row
4993 * ^----- column
4994 * ^-------- code
4995 *
4996 * \033[<%d;%d;%dm : mouse release event
4997 * ^-- row
4998 * ^----- column
4999 * ^-------- code
5000 */
5001 p = modifiers_start;
5002 if (p == NULL)
5003 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005004
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005005 mouse_code = getdigits(&p);
5006 if (*p++ != ';')
5007 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005008
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005009 /* when mouse reporting is SGR, add 32 to mouse code */
5010 if (key_name[0] == KS_SGR_MOUSE
5011 || key_name[0] == KS_SGR_MOUSE_RELEASE)
5012 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005013
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005014 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
5015 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005016
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005017 mouse_col = getdigits(&p) - 1;
5018 if (*p++ != ';')
5019 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005020
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005021 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005022
Bram Moolenaar5fe69122017-06-23 23:00:08 +02005023 /* The modifiers were the mouse coordinates, not the
5024 * modifier keys (alt/shift/ctrl/meta) state. */
5025 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005026 }
5027# endif
5028
5029 if (key_name[0] == (int)KS_MOUSE
5030#ifdef FEAT_MOUSE_URXVT
5031 || key_name[0] == (int)KS_URXVT_MOUSE
5032#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005033#ifdef FEAT_MOUSE_SGR
5034 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02005035 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005036#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005037 )
5038 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005039# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /*
5041 * Handle mouse events.
5042 * Recognize the xterm mouse wheel, but not in the GUI, the
5043 * Linux console with GPM and the MS-DOS or Win32 console
5044 * (multi-clicks use >= 0x60).
5045 */
5046 if (mouse_code >= MOUSEWHEEL_LOW
5047# ifdef FEAT_GUI
5048 && !gui.in_use
5049# endif
5050# ifdef FEAT_MOUSE_GPM
5051 && gpm_flag == 0
5052# endif
5053 )
5054 {
5055 /* Keep the mouse_code before it's changed, so that we
5056 * remember that it was a mouse wheel click. */
5057 wheel_code = mouse_code;
5058 }
5059# ifdef FEAT_MOUSE_XTERM
5060 else if (held_button == MOUSE_RELEASE
5061# ifdef FEAT_GUI
5062 && !gui.in_use
5063# endif
5064 && (mouse_code == 0x23 || mouse_code == 0x24))
5065 {
5066 /* Apparently used by rxvt scroll wheel. */
5067 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
5068 }
5069# endif
5070
5071# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
5072 else if (use_xterm_mouse() > 1)
5073 {
5074 if (mouse_code & MOUSE_DRAG_XTERM)
5075 mouse_code |= MOUSE_DRAG;
5076 }
5077# endif
5078# ifdef FEAT_XCLIPBOARD
5079 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
5080 {
5081 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
5082 stop_xterm_trace();
5083 else
5084 start_xterm_trace(mouse_code);
5085 }
5086# endif
5087# endif
5088 }
5089# endif /* !UNIX || FEAT_MOUSE_XTERM */
5090# ifdef FEAT_MOUSE_NET
5091 if (key_name[0] == (int)KS_NETTERM_MOUSE)
5092 {
5093 int mc, mr;
5094
5095 /* expect a rather limited sequence like: balancing {
5096 * \033}6,45\r
5097 * '6' is the row, 45 is the column
5098 */
5099 p = tp + slen;
5100 mr = getdigits(&p);
5101 if (*p++ != ',')
5102 return -1;
5103 mc = getdigits(&p);
5104 if (*p++ != '\r')
5105 return -1;
5106
5107 mouse_col = mc - 1;
5108 mouse_row = mr - 1;
5109 mouse_code = MOUSE_LEFT;
5110 slen += (int)(p - (tp + slen));
5111 }
5112# endif /* FEAT_MOUSE_NET */
5113# ifdef FEAT_MOUSE_JSB
5114 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5115 {
5116 int mult, val, iter, button, status;
5117
5118 /* JSBTERM Input Model
5119 * \033[0~zw uniq escape sequence
5120 * (L-x) Left button pressed - not pressed x not reporting
5121 * (M-x) Middle button pressed - not pressed x not reporting
5122 * (R-x) Right button pressed - not pressed x not reporting
5123 * (SDmdu) Single , Double click, m mouse move d button down
5124 * u button up
5125 * ### X cursor position padded to 3 digits
5126 * ### Y cursor position padded to 3 digits
5127 * (s-x) SHIFT key pressed - not pressed x not reporting
5128 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005129 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
5131
5132 p = tp + slen;
5133 button = mouse_code = 0;
5134 switch (*p++)
5135 {
5136 case 'L': button = 1; break;
5137 case '-': break;
5138 case 'x': break; /* ignore sequence */
5139 default: return -1; /* Unknown Result */
5140 }
5141 switch (*p++)
5142 {
5143 case 'M': button |= 2; break;
5144 case '-': break;
5145 case 'x': break; /* ignore sequence */
5146 default: return -1; /* Unknown Result */
5147 }
5148 switch (*p++)
5149 {
5150 case 'R': button |= 4; break;
5151 case '-': break;
5152 case 'x': break; /* ignore sequence */
5153 default: return -1; /* Unknown Result */
5154 }
5155 status = *p++;
5156 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5157 mult /= 10, p++)
5158 if (*p >= '0' && *p <= '9')
5159 val += (*p - '0') * mult;
5160 else
5161 return -1;
5162 mouse_col = val;
5163 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5164 mult /= 10, p++)
5165 if (*p >= '0' && *p <= '9')
5166 val += (*p - '0') * mult;
5167 else
5168 return -1;
5169 mouse_row = val;
5170 switch (*p++)
5171 {
5172 case 's': button |= 8; break; /* SHIFT key Pressed */
5173 case '-': break; /* Not Pressed */
5174 case 'x': break; /* Not Reporting */
5175 default: return -1; /* Unknown Result */
5176 }
5177 switch (*p++)
5178 {
5179 case 'c': button |= 16; break; /* CTRL key Pressed */
5180 case '-': break; /* Not Pressed */
5181 case 'x': break; /* Not Reporting */
5182 default: return -1; /* Unknown Result */
5183 }
5184 if (*p++ != '\033')
5185 return -1;
5186 if (*p++ != '\\')
5187 return -1;
5188 switch (status)
5189 {
5190 case 'D': /* Double Click */
5191 case 'S': /* Single Click */
5192 if (button & 1) mouse_code |= MOUSE_LEFT;
5193 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5194 if (button & 4) mouse_code |= MOUSE_RIGHT;
5195 if (button & 8) mouse_code |= MOUSE_SHIFT;
5196 if (button & 16) mouse_code |= MOUSE_CTRL;
5197 break;
5198 case 'm': /* Mouse move */
5199 if (button & 1) mouse_code |= MOUSE_LEFT;
5200 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5201 if (button & 4) mouse_code |= MOUSE_RIGHT;
5202 if (button & 8) mouse_code |= MOUSE_SHIFT;
5203 if (button & 16) mouse_code |= MOUSE_CTRL;
5204 if ((button & 7) != 0)
5205 {
5206 held_button = mouse_code;
5207 mouse_code |= MOUSE_DRAG;
5208 }
5209 is_drag = TRUE;
5210 showmode();
5211 break;
5212 case 'd': /* Button Down */
5213 if (button & 1) mouse_code |= MOUSE_LEFT;
5214 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5215 if (button & 4) mouse_code |= MOUSE_RIGHT;
5216 if (button & 8) mouse_code |= MOUSE_SHIFT;
5217 if (button & 16) mouse_code |= MOUSE_CTRL;
5218 break;
5219 case 'u': /* Button Up */
5220 if (button & 1)
5221 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5222 if (button & 2)
5223 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5224 if (button & 4)
5225 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5226 if (button & 8)
5227 mouse_code |= MOUSE_SHIFT;
5228 if (button & 16)
5229 mouse_code |= MOUSE_CTRL;
5230 break;
5231 default: return -1; /* Unknown Result */
5232 }
5233
5234 slen += (p - (tp + slen));
5235 }
5236# endif /* FEAT_MOUSE_JSB */
5237# ifdef FEAT_MOUSE_DEC
5238 if (key_name[0] == (int)KS_DEC_MOUSE)
5239 {
5240 /* The DEC Locator Input Model
5241 * Netterm delivers the code sequence:
5242 * \033[2;4;24;80&w (left button down)
5243 * \033[3;0;24;80&w (left button up)
5244 * \033[6;1;24;80&w (right button down)
5245 * \033[7;0;24;80&w (right button up)
5246 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5247 * Pe is the event code
5248 * Pb is the button code
5249 * Pr is the row coordinate
5250 * Pc is the column coordinate
5251 * Pp is the third coordinate (page number)
5252 * Pe, the event code indicates what event caused this report
5253 * The following event codes are defined:
5254 * 0 - request, the terminal received an explicit request
5255 * for a locator report, but the locator is unavailable
5256 * 1 - request, the terminal received an explicit request
5257 * for a locator report
5258 * 2 - left button down
5259 * 3 - left button up
5260 * 4 - middle button down
5261 * 5 - middle button up
5262 * 6 - right button down
5263 * 7 - right button up
5264 * 8 - fourth button down
5265 * 9 - fourth button up
5266 * 10 - locator outside filter rectangle
5267 * Pb, the button code, ASCII decimal 0-15 indicating which
5268 * buttons are down if any. The state of the four buttons
5269 * on the locator correspond to the low four bits of the
5270 * decimal value,
5271 * "1" means button depressed
5272 * 0 - no buttons down,
5273 * 1 - right,
5274 * 2 - middle,
5275 * 4 - left,
5276 * 8 - fourth
5277 * Pr is the row coordinate of the locator position in the page,
5278 * encoded as an ASCII decimal value.
5279 * If Pr is omitted, the locator position is undefined
5280 * (outside the terminal window for example).
5281 * Pc is the column coordinate of the locator position in the
5282 * page, encoded as an ASCII decimal value.
5283 * If Pc is omitted, the locator position is undefined
5284 * (outside the terminal window for example).
5285 * Pp is the page coordinate of the locator position
5286 * encoded as an ASCII decimal value.
5287 * The page coordinate may be omitted if the locator is on
5288 * page one (the default). We ignore it anyway.
5289 */
5290 int Pe, Pb, Pr, Pc;
5291
5292 p = tp + slen;
5293
5294 /* get event status */
5295 Pe = getdigits(&p);
5296 if (*p++ != ';')
5297 return -1;
5298
5299 /* get button status */
5300 Pb = getdigits(&p);
5301 if (*p++ != ';')
5302 return -1;
5303
5304 /* get row status */
5305 Pr = getdigits(&p);
5306 if (*p++ != ';')
5307 return -1;
5308
5309 /* get column status */
5310 Pc = getdigits(&p);
5311
5312 /* the page parameter is optional */
5313 if (*p == ';')
5314 {
5315 p++;
5316 (void)getdigits(&p);
5317 }
5318 if (*p++ != '&')
5319 return -1;
5320 if (*p++ != 'w')
5321 return -1;
5322
5323 mouse_code = 0;
5324 switch (Pe)
5325 {
5326 case 0: return -1; /* position request while unavailable */
5327 case 1: /* a response to a locator position request includes
5328 the status of all buttons */
5329 Pb &= 7; /* mask off and ignore fourth button */
5330 if (Pb & 4)
5331 mouse_code = MOUSE_LEFT;
5332 if (Pb & 2)
5333 mouse_code = MOUSE_MIDDLE;
5334 if (Pb & 1)
5335 mouse_code = MOUSE_RIGHT;
5336 if (Pb)
5337 {
5338 held_button = mouse_code;
5339 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005340 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 is_drag = TRUE;
5343 showmode();
5344 break;
5345 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005346 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 break;
5348 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5349 break;
5350 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005351 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 break;
5353 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5354 break;
5355 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005356 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5359 break;
5360 case 8: return -1; /* fourth button down */
5361 case 9: return -1; /* fourth button up */
5362 case 10: return -1; /* mouse outside of filter rectangle */
5363 default: return -1; /* should never occur */
5364 }
5365
5366 mouse_col = Pc - 1;
5367 mouse_row = Pr - 1;
5368
5369 slen += (int)(p - (tp + slen));
5370 }
5371# endif /* FEAT_MOUSE_DEC */
5372# ifdef FEAT_MOUSE_PTERM
5373 if (key_name[0] == (int)KS_PTERM_MOUSE)
5374 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005375 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 p = tp + slen;
5378
5379 action = getdigits(&p);
5380 if (*p++ != ';')
5381 return -1;
5382
5383 mouse_row = getdigits(&p);
5384 if (*p++ != ';')
5385 return -1;
5386 mouse_col = getdigits(&p);
5387 if (*p++ != ';')
5388 return -1;
5389
5390 button = getdigits(&p);
5391 mouse_code = 0;
5392
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005393 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
5395 case 4: mouse_code = MOUSE_LEFT; break;
5396 case 1: mouse_code = MOUSE_RIGHT; break;
5397 case 2: mouse_code = MOUSE_MIDDLE; break;
5398 default: return -1;
5399 }
5400
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005401 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 case 31: /* Initial press */
5404 if (*p++ != ';')
5405 return -1;
5406
5407 num_clicks = getdigits(&p); /* Not used */
5408 break;
5409
5410 case 32: /* Release */
5411 mouse_code |= MOUSE_RELEASE;
5412 break;
5413
5414 case 33: /* Drag */
5415 held_button = mouse_code;
5416 mouse_code |= MOUSE_DRAG;
5417 break;
5418
5419 default:
5420 return -1;
5421 }
5422
5423 if (*p++ != 't')
5424 return -1;
5425
5426 slen += (p - (tp + slen));
5427 }
5428# endif /* FEAT_MOUSE_PTERM */
5429
5430 /* Interpret the mouse code */
5431 current_button = (mouse_code & MOUSE_CLICK_MASK);
5432 if (current_button == MOUSE_RELEASE
5433# ifdef FEAT_MOUSE_XTERM
5434 && wheel_code == 0
5435# endif
5436 )
5437 {
5438 /*
5439 * If we get a mouse drag or release event when
5440 * there is no mouse button held down (held_button ==
5441 * MOUSE_RELEASE), produce a K_IGNORE below.
5442 * (can happen when you hold down two buttons
5443 * and then let them go, or click in the menu bar, but not
5444 * on a menu, and drag into the text).
5445 */
5446 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5447 is_drag = TRUE;
5448 current_button = held_button;
5449 }
5450 else if (wheel_code == 0)
5451 {
5452# ifdef CHECK_DOUBLE_CLICK
5453# ifdef FEAT_MOUSE_GPM
5454# ifdef FEAT_GUI
5455 /*
5456 * Only for Unix, when GUI or gpm is not active, we handle
5457 * multi-clicks here.
5458 */
5459 if (gpm_flag == 0 && !gui.in_use)
5460# else
5461 if (gpm_flag == 0)
5462# endif
5463# else
5464# ifdef FEAT_GUI
5465 if (!gui.in_use)
5466# endif
5467# endif
5468 {
5469 /*
5470 * Compute the time elapsed since the previous mouse click.
5471 */
5472 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005473 if (orig_mouse_time.tv_sec == 0)
5474 {
5475 /*
5476 * Avoid computing the difference between mouse_time
5477 * and orig_mouse_time for the first click, as the
5478 * difference would be huge and would cause multiplication
5479 * overflow.
5480 */
5481 timediff = p_mouset;
5482 }
5483 else
5484 {
5485 timediff = (mouse_time.tv_usec
5486 - orig_mouse_time.tv_usec) / 1000;
5487 if (timediff < 0)
5488 --orig_mouse_time.tv_sec;
5489 timediff += (mouse_time.tv_sec
5490 - orig_mouse_time.tv_sec) * 1000;
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 orig_mouse_time = mouse_time;
5493 if (mouse_code == orig_mouse_code
5494 && timediff < p_mouset
5495 && orig_num_clicks != 4
5496 && orig_mouse_col == mouse_col
5497 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005498 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005500 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005502 )
5503#ifdef FEAT_WINDOWS
5504 /* Double click in tab pages line also works
5505 * when window contents changes. */
5506 || (mouse_row == 0 && firstwin->w_winrow > 0)
5507#endif
5508 )
5509 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 ++orig_num_clicks;
5511 else
5512 orig_num_clicks = 1;
5513 orig_mouse_col = mouse_col;
5514 orig_mouse_row = mouse_row;
5515 orig_topline = curwin->w_topline;
5516#ifdef FEAT_DIFF
5517 orig_topfill = curwin->w_topfill;
5518#endif
5519 }
5520# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5521 else
5522 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5523# endif
5524# else
5525 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5526# endif
5527 is_click = TRUE;
5528 orig_mouse_code = mouse_code;
5529 }
5530 if (!is_drag)
5531 held_button = mouse_code & MOUSE_CLICK_MASK;
5532
5533 /*
5534 * Translate the actual mouse event into a pseudo mouse event.
5535 * First work out what modifiers are to be used.
5536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (orig_mouse_code & MOUSE_SHIFT)
5538 modifiers |= MOD_MASK_SHIFT;
5539 if (orig_mouse_code & MOUSE_CTRL)
5540 modifiers |= MOD_MASK_CTRL;
5541 if (orig_mouse_code & MOUSE_ALT)
5542 modifiers |= MOD_MASK_ALT;
5543 if (orig_num_clicks == 2)
5544 modifiers |= MOD_MASK_2CLICK;
5545 else if (orig_num_clicks == 3)
5546 modifiers |= MOD_MASK_3CLICK;
5547 else if (orig_num_clicks == 4)
5548 modifiers |= MOD_MASK_4CLICK;
5549
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005550 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5551 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005553 if (wheel_code != 0
5554 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005555 {
5556 if (wheel_code & MOUSE_CTRL)
5557 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005558 if (wheel_code & MOUSE_ALT)
5559 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 key_name[1] = (wheel_code & 1)
5561 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 else
5564 key_name[1] = get_pseudo_mouse_code(current_button,
5565 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005566
5567 /* Make sure the mouse position is valid. Some terminals may
5568 * return weird values. */
5569 if (mouse_col >= Columns)
5570 mouse_col = Columns - 1;
5571 if (mouse_row >= Rows)
5572 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574#endif /* FEAT_MOUSE */
5575
5576#ifdef FEAT_GUI
5577 /*
5578 * If using the GUI, then we get menu and scrollbar events.
5579 *
5580 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5581 * four bytes which are to be taken as a pointer to the vimmenu_T
5582 * structure.
5583 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005584 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005585 * is one byte with the tab index.
5586 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5588 * by one byte representing the scrollbar number, and then four bytes
5589 * representing a long_u which is the new value of the scrollbar.
5590 *
5591 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5592 * KE_FILLER followed by four bytes representing a long_u which is the
5593 * new value of the scrollbar.
5594 */
5595# ifdef FEAT_MENU
5596 else if (key_name[0] == (int)KS_MENU)
5597 {
5598 long_u val;
5599
5600 num_bytes = get_long_from_buf(tp + slen, &val);
5601 if (num_bytes == -1)
5602 return -1;
5603 current_menu = (vimmenu_T *)val;
5604 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005605
5606 /* The menu may have been deleted right after it was used, check
5607 * for that. */
5608 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5609 {
5610 key_name[0] = KS_EXTRA;
5611 key_name[1] = (int)KE_IGNORE;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005615# ifdef FEAT_GUI_TABLINE
5616 else if (key_name[0] == (int)KS_TABLINE)
5617 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005618 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005619 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5620 if (num_bytes == -1)
5621 return -1;
5622 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005623 if (current_tab == 255) /* -1 in a byte gives 255 */
5624 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005625 slen += num_bytes;
5626 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005627 else if (key_name[0] == (int)KS_TABMENU)
5628 {
5629 /* Selecting tabline tab or using its menu. */
5630 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5631 if (num_bytes == -1)
5632 return -1;
5633 current_tab = (int)bytes[0];
5634 current_tabmenu = (int)bytes[1];
5635 slen += num_bytes;
5636 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638# ifndef USE_ON_FLY_SCROLL
5639 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5640 {
5641 long_u val;
5642
5643 /* Get the last scrollbar event in the queue of the same type */
5644 j = 0;
5645 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5646 && tp[j + 2] != NUL; ++i)
5647 {
5648 j += 3;
5649 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5650 if (num_bytes == -1)
5651 break;
5652 if (i == 0)
5653 current_scrollbar = (int)bytes[0];
5654 else if (current_scrollbar != (int)bytes[0])
5655 break;
5656 j += num_bytes;
5657 num_bytes = get_long_from_buf(tp + j, &val);
5658 if (num_bytes == -1)
5659 break;
5660 scrollbar_value = val;
5661 j += num_bytes;
5662 slen = j;
5663 }
5664 if (i == 0) /* not enough characters to make one */
5665 return -1;
5666 }
5667 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5668 {
5669 long_u val;
5670
5671 /* Get the last horiz. scrollbar event in the queue */
5672 j = 0;
5673 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5674 && tp[j + 2] != NUL; ++i)
5675 {
5676 j += 3;
5677 num_bytes = get_long_from_buf(tp + j, &val);
5678 if (num_bytes == -1)
5679 break;
5680 scrollbar_value = val;
5681 j += num_bytes;
5682 slen = j;
5683 }
5684 if (i == 0) /* not enough characters to make one */
5685 return -1;
5686 }
5687# endif /* !USE_ON_FLY_SCROLL */
5688#endif /* FEAT_GUI */
5689
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005690 /*
5691 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5692 */
5693 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5694
5695 /*
5696 * Add any modifier codes to our string.
5697 */
5698 new_slen = 0; /* Length of what will replace the termcode */
5699 if (modifiers != 0)
5700 {
5701 /* Some keys have the modifier included. Need to handle that here
5702 * to make mappings work. */
5703 key = simplify_key(key, &modifiers);
5704 if (modifiers != 0)
5705 {
5706 string[new_slen++] = K_SPECIAL;
5707 string[new_slen++] = (int)KS_MODIFIER;
5708 string[new_slen++] = modifiers;
5709 }
5710 }
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005713 key_name[0] = KEY2TERMCAP0(key);
5714 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005716 {
5717 /* from ":set <M-b>=xx" */
5718#ifdef FEAT_MBYTE
5719 if (has_mbyte)
5720 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5721 else
5722#endif
5723 string[new_slen++] = key_name[1];
5724 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005725 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5726 && key_name[1] == KE_IGNORE)
5727 {
5728 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5729 * to indicate what happened. */
5730 retval = KEYLEN_REMOVED;
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 else
5733 {
5734 string[new_slen++] = K_SPECIAL;
5735 string[new_slen++] = key_name[0];
5736 string[new_slen++] = key_name[1];
5737 }
5738 string[new_slen] = NUL;
5739 extra = new_slen - slen;
5740 if (buf == NULL)
5741 {
5742 if (extra < 0)
5743 /* remove matched chars, taking care of noremap */
5744 del_typebuf(-extra, offset);
5745 else if (extra > 0)
5746 /* insert the extra space we need */
5747 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5748
5749 /*
5750 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5751 * typebuf.tb_buf[]!
5752 */
5753 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5754 (size_t)new_slen);
5755 }
5756 else
5757 {
5758 if (extra < 0)
5759 /* remove matched characters */
5760 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005761 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005763 {
5764 /* Insert the extra space we need. If there is insufficient
5765 * space return -1. */
5766 if (*buflen + extra + new_slen >= bufsize)
5767 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005769 (size_t)(*buflen - offset));
5770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005772 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005774 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776
Bram Moolenaar2951b772013-07-03 12:45:31 +02005777#ifdef FEAT_TERMRESPONSE
5778 LOG_TR("normal character");
5779#endif
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 return 0; /* no match found */
5782}
5783
5784/*
5785 * Replace any terminal code strings in from[] with the equivalent internal
5786 * vim representation. This is used for the "from" and "to" part of a
5787 * mapping, and the "to" part of a menu command.
5788 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5789 * '<'.
5790 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5791 *
5792 * The replacement is done in result[] and finally copied into allocated
5793 * memory. If this all works well *bufp is set to the allocated memory and a
5794 * pointer to it is returned. If something fails *bufp is set to NULL and from
5795 * is returned.
5796 *
5797 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5798 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5799 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5800 * instead of a CTRL-V.
5801 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005802 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005803replace_termcodes(
5804 char_u *from,
5805 char_u **bufp,
5806 int from_part,
5807 int do_lt, /* also translate <lt> */
5808 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 int i;
5811 int slen;
5812 int key;
5813 int dlen = 0;
5814 char_u *src;
5815 int do_backslash; /* backslash is a special character */
5816 int do_special; /* recognize <> key codes */
5817 int do_key_code; /* recognize raw key codes */
5818 char_u *result; /* buffer for resulting string */
5819
5820 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005821 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5823
5824 /*
5825 * Allocate space for the translation. Worst case a single character is
5826 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5827 */
5828 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5829 if (result == NULL) /* out of memory */
5830 {
5831 *bufp = NULL;
5832 return from;
5833 }
5834
5835 src = from;
5836
5837 /*
5838 * Check for #n at start only: function key n
5839 */
5840 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5841 {
5842 result[dlen++] = K_SPECIAL;
5843 result[dlen++] = 'k';
5844 if (src[1] == '0')
5845 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5846 else
5847 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5848 src += 2;
5849 }
5850
5851 /*
5852 * Copy each byte from *from to result[dlen]
5853 */
5854 while (*src != NUL)
5855 {
5856 /*
5857 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005858 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 */
5860 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5861 {
5862#ifdef FEAT_EVAL
5863 /*
5864 * Replace <SID> by K_SNR <script-nr> _.
5865 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5866 */
5867 if (STRNICMP(src, "<SID>", 5) == 0)
5868 {
5869 if (current_SID <= 0)
5870 EMSG(_(e_usingsid));
5871 else
5872 {
5873 src += 5;
5874 result[dlen++] = K_SPECIAL;
5875 result[dlen++] = (int)KS_EXTRA;
5876 result[dlen++] = (int)KE_SNR;
5877 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5878 dlen += (int)STRLEN(result + dlen);
5879 result[dlen++] = '_';
5880 continue;
5881 }
5882 }
5883#endif
5884
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005885 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (slen)
5887 {
5888 dlen += slen;
5889 continue;
5890 }
5891 }
5892
5893 /*
5894 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5895 * Note that this is also checked after replacing the <> form.
5896 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5897 * it could be a character in the file.
5898 */
5899 if (do_key_code)
5900 {
5901 i = find_term_bykeys(src);
5902 if (i >= 0)
5903 {
5904 result[dlen++] = K_SPECIAL;
5905 result[dlen++] = termcodes[i].name[0];
5906 result[dlen++] = termcodes[i].name[1];
5907 src += termcodes[i].len;
5908 /* If terminal code matched, continue after it. */
5909 continue;
5910 }
5911 }
5912
5913#ifdef FEAT_EVAL
5914 if (do_special)
5915 {
5916 char_u *p, *s, len;
5917
5918 /*
5919 * Replace <Leader> by the value of "mapleader".
5920 * Replace <LocalLeader> by the value of "maplocalleader".
5921 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5922 */
5923 if (STRNICMP(src, "<Leader>", 8) == 0)
5924 {
5925 len = 8;
5926 p = get_var_value((char_u *)"g:mapleader");
5927 }
5928 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5929 {
5930 len = 13;
5931 p = get_var_value((char_u *)"g:maplocalleader");
5932 }
5933 else
5934 {
5935 len = 0;
5936 p = NULL;
5937 }
5938 if (len != 0)
5939 {
5940 /* Allow up to 8 * 6 characters for "mapleader". */
5941 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5942 s = (char_u *)"\\";
5943 else
5944 s = p;
5945 while (*s != NUL)
5946 result[dlen++] = *s++;
5947 src += len;
5948 continue;
5949 }
5950 }
5951#endif
5952
5953 /*
5954 * Remove CTRL-V and ignore the next character.
5955 * For "from" side the CTRL-V at the end is included, for the "to"
5956 * part it is removed.
5957 * If 'cpoptions' does not contain 'B', also accept a backslash.
5958 */
5959 key = *src;
5960 if (key == Ctrl_V || (do_backslash && key == '\\'))
5961 {
5962 ++src; /* skip CTRL-V or backslash */
5963 if (*src == NUL)
5964 {
5965 if (from_part)
5966 result[dlen++] = key;
5967 break;
5968 }
5969 }
5970
5971#ifdef FEAT_MBYTE
5972 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005973 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
5975 {
5976 /*
5977 * If the character is K_SPECIAL, replace it with K_SPECIAL
5978 * KS_SPECIAL KE_FILLER.
5979 * If compiled with the GUI replace CSI with K_CSI.
5980 */
5981 if (*src == K_SPECIAL)
5982 {
5983 result[dlen++] = K_SPECIAL;
5984 result[dlen++] = KS_SPECIAL;
5985 result[dlen++] = KE_FILLER;
5986 }
5987# ifdef FEAT_GUI
5988 else if (*src == CSI)
5989 {
5990 result[dlen++] = K_SPECIAL;
5991 result[dlen++] = KS_EXTRA;
5992 result[dlen++] = (int)KE_CSI;
5993 }
5994# endif
5995 else
5996 result[dlen++] = *src;
5997 ++src;
5998 }
5999 }
6000 result[dlen] = NUL;
6001
6002 /*
6003 * Copy the new string to allocated memory.
6004 * If this fails, just return from.
6005 */
6006 if ((*bufp = vim_strsave(result)) != NULL)
6007 from = *bufp;
6008 vim_free(result);
6009 return from;
6010}
6011
6012/*
6013 * Find a termcode with keys 'src' (must be NUL terminated).
6014 * Return the index in termcodes[], or -1 if not found.
6015 */
6016 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006017find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006020 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
6022 for (i = 0; i < tc_len; ++i)
6023 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006024 if (slen == termcodes[i].len
6025 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 return i;
6027 }
6028 return -1;
6029}
6030
6031/*
6032 * Gather the first characters in the terminal key codes into a string.
6033 * Used to speed up check_termcode().
6034 */
6035 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006036gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 int i;
6039 int len = 0;
6040
6041#ifdef FEAT_GUI
6042 if (gui.in_use)
6043 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
6044#endif
6045#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006046 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 termleader[len++] = DCS; /* the termcode response starts with DCS
6048 in 8-bit mode */
6049#endif
6050 termleader[len] = NUL;
6051
6052 for (i = 0; i < tc_len; ++i)
6053 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6054 {
6055 termleader[len++] = termcodes[i].code[0];
6056 termleader[len] = NUL;
6057 }
6058
6059 need_gather = FALSE;
6060}
6061
6062/*
6063 * Show all termcodes (for ":set termcap")
6064 * This code looks a lot like showoptions(), but is different.
6065 */
6066 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006067show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 int col;
6070 int *items;
6071 int item_count;
6072 int run;
6073 int row, rows;
6074 int cols;
6075 int i;
6076 int len;
6077
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006078#define INC3 27 /* try to make three columns */
6079#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#define GAP 2 /* spaces between columns */
6081
6082 if (tc_len == 0) /* no terminal codes (must be GUI) */
6083 return;
6084 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
6085 if (items == NULL)
6086 return;
6087
6088 /* Highlight title */
6089 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
6090
6091 /*
6092 * do the loop two times:
6093 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006094 * 2. display the medium items (medium length strings)
6095 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006097 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 /*
6100 * collect the items in items[]
6101 */
6102 item_count = 0;
6103 for (i = 0; i < tc_len; i++)
6104 {
6105 len = show_one_termcode(termcodes[i].name,
6106 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006107 if (len <= INC3 - GAP ? run == 1
6108 : len <= INC2 - GAP ? run == 2
6109 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 items[item_count++] = i;
6111 }
6112
6113 /*
6114 * display the items
6115 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006116 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006118 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (cols == 0)
6120 cols = 1;
6121 rows = (item_count + cols - 1) / cols;
6122 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006123 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 rows = item_count;
6125 for (row = 0; row < rows && !got_int; ++row)
6126 {
6127 msg_putchar('\n'); /* go to next line */
6128 if (got_int) /* 'q' typed in more */
6129 break;
6130 col = 0;
6131 for (i = row; i < item_count; i += rows)
6132 {
6133 msg_col = col; /* make columns */
6134 show_one_termcode(termcodes[items[i]].name,
6135 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006136 if (run == 2)
6137 col += INC2;
6138 else
6139 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141 out_flush();
6142 ui_breakcheck();
6143 }
6144 }
6145 vim_free(items);
6146}
6147
6148/*
6149 * Show one termcode entry.
6150 * Output goes into IObuff[]
6151 */
6152 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006153show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 char_u *p;
6156 int len;
6157
6158 if (name[0] > '~')
6159 {
6160 IObuff[0] = ' ';
6161 IObuff[1] = ' ';
6162 IObuff[2] = ' ';
6163 IObuff[3] = ' ';
6164 }
6165 else
6166 {
6167 IObuff[0] = 't';
6168 IObuff[1] = '_';
6169 IObuff[2] = name[0];
6170 IObuff[3] = name[1];
6171 }
6172 IObuff[4] = ' ';
6173
6174 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6175 if (p[1] != 't')
6176 STRCPY(IObuff + 5, p);
6177 else
6178 IObuff[5] = NUL;
6179 len = (int)STRLEN(IObuff);
6180 do
6181 IObuff[len++] = ' ';
6182 while (len < 17);
6183 IObuff[len] = NUL;
6184 if (code == NULL)
6185 len += 4;
6186 else
6187 len += vim_strsize(code);
6188
6189 if (printit)
6190 {
6191 msg_puts(IObuff);
6192 if (code == NULL)
6193 msg_puts((char_u *)"NULL");
6194 else
6195 msg_outtrans(code);
6196 }
6197 return len;
6198}
6199
6200#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6201/*
6202 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6203 * termcap codes from the terminal itself.
6204 * We get them one by one to avoid a very long response string.
6205 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006206static int xt_index_in = 0;
6207static int xt_index_out = 0;
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006210req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 xt_index_out = 0;
6213 xt_index_in = 0;
6214 req_more_codes_from_term();
6215}
6216
6217 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006218req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 char buf[11];
6221 int old_idx = xt_index_out;
6222
6223 /* Don't do anything when going to exit. */
6224 if (exiting)
6225 return;
6226
6227 /* Send up to 10 more requests out than we received. Avoid sending too
6228 * many, there can be a buffer overflow somewhere. */
6229 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6230 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006231# ifdef DEBUG_TERMRESPONSE
6232 char dbuf[100];
6233
6234 sprintf(dbuf, "Requesting XT %d: %s",
6235 xt_index_out, key_names[xt_index_out]);
6236 log_tr(dbuf);
6237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 sprintf(buf, "\033P+q%02x%02x\033\\",
6239 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6240 out_str_nf((char_u *)buf);
6241 ++xt_index_out;
6242 }
6243
6244 /* Send the codes out right away. */
6245 if (xt_index_out != old_idx)
6246 out_flush();
6247}
6248
6249/*
6250 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6251 * A "0" instead of the "1" indicates a code that isn't supported.
6252 * Both <name> and <string> are encoded in hex.
6253 * "code" points to the "0" or "1".
6254 */
6255 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006256got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258#define XT_LEN 100
6259 char_u name[3];
6260 char_u str[XT_LEN];
6261 int i;
6262 int j = 0;
6263 int c;
6264
6265 /* A '1' means the code is supported, a '0' means it isn't.
6266 * When half the length is > XT_LEN we can't use it.
6267 * Our names are currently all 2 characters. */
6268 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6269 {
6270 /* Get the name from the response and find it in the table. */
6271 name[0] = hexhex2nr(code + 3);
6272 name[1] = hexhex2nr(code + 5);
6273 name[2] = NUL;
6274 for (i = 0; key_names[i] != NULL; ++i)
6275 {
6276 if (STRCMP(key_names[i], name) == 0)
6277 {
6278 xt_index_in = i;
6279 break;
6280 }
6281 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006282# ifdef DEBUG_TERMRESPONSE
6283 {
6284 char buf[100];
6285
6286 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6287 log_tr(buf);
6288 }
6289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (key_names[i] != NULL)
6291 {
6292 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6293 str[j++] = c;
6294 str[j] = NUL;
6295 if (name[0] == 'C' && name[1] == 'o')
6296 {
6297 /* Color count is not a key code. */
6298 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006299 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
6301 else
6302 {
6303 /* First delete any existing entry with the same code. */
6304 i = find_term_bykeys(str);
6305 if (i >= 0)
6306 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006307 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
6309 }
6310 }
6311
6312 /* May request more codes now that we received one. */
6313 ++xt_index_in;
6314 req_more_codes_from_term();
6315}
6316
6317/*
6318 * Check if there are any unanswered requests and deal with them.
6319 * This is called before starting an external program or getting direct
6320 * keyboard input. We don't want responses to be send to that program or
6321 * handled as typed text.
6322 */
6323 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006324check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 int c;
6327
6328 /* If no codes requested or all are answered, no need to wait. */
6329 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6330 return;
6331
6332 /* Vgetc() will check for and handle any response.
6333 * Keep calling vpeekc() until we don't get any responses. */
6334 ++no_mapping;
6335 ++allow_keys;
6336 for (;;)
6337 {
6338 c = vpeekc();
6339 if (c == NUL) /* nothing available */
6340 break;
6341
6342 /* If a response is recognized it's replaced with K_IGNORE, must read
6343 * it from the input stream. If there is no K_IGNORE we can't do
6344 * anything, break here (there might be some responses further on, but
6345 * we don't want to throw away any typed chars). */
6346 if (c != K_SPECIAL && c != K_IGNORE)
6347 break;
6348 c = vgetc();
6349 if (c != K_IGNORE)
6350 {
6351 vungetc(c);
6352 break;
6353 }
6354 }
6355 --no_mapping;
6356 --allow_keys;
6357}
6358#endif
6359
6360#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6361/*
6362 * Translate an internal mapping/abbreviation representation into the
6363 * corresponding external one recognized by :map/:abbrev commands;
6364 * respects the current B/k/< settings of 'cpoption'.
6365 *
6366 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006367 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 *
6369 * It uses a growarray to build the translation string since the
6370 * latter can be wider than the original description. The caller has to
6371 * free the string afterwards.
6372 *
6373 * Returns NULL when there is a problem.
6374 */
6375 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006376translate_mapping(
6377 char_u *str,
6378 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 garray_T ga;
6381 int c;
6382 int modifiers;
6383 int cpo_bslash;
6384 int cpo_special;
6385 int cpo_keycode;
6386
6387 ga_init(&ga);
6388 ga.ga_itemsize = 1;
6389 ga.ga_growsize = 40;
6390
6391 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6392 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6393 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6394
6395 for (; *str; ++str)
6396 {
6397 c = *str;
6398 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6399 {
6400 modifiers = 0;
6401 if (str[1] == KS_MODIFIER)
6402 {
6403 str++;
6404 modifiers = *++str;
6405 c = *++str;
6406 }
6407 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6408 {
6409 int i;
6410
6411 /* try to find special key in termcodes */
6412 for (i = 0; i < tc_len; ++i)
6413 if (termcodes[i].name[0] == str[1]
6414 && termcodes[i].name[1] == str[2])
6415 break;
6416 if (i < tc_len)
6417 {
6418 ga_concat(&ga, termcodes[i].code);
6419 str += 2;
6420 continue; /* for (str) */
6421 }
6422 }
6423 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6424 {
6425 if (expmap && cpo_special)
6426 {
6427 ga_clear(&ga);
6428 return NULL;
6429 }
6430 c = TO_SPECIAL(str[1], str[2]);
6431 if (c == K_ZERO) /* display <Nul> as ^@ */
6432 c = NUL;
6433 str += 2;
6434 }
6435 if (IS_SPECIAL(c) || modifiers) /* special key */
6436 {
6437 if (expmap && cpo_special)
6438 {
6439 ga_clear(&ga);
6440 return NULL;
6441 }
6442 ga_concat(&ga, get_special_key_name(c, modifiers));
6443 continue; /* for (str) */
6444 }
6445 }
6446 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6447 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6448 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6449 if (c)
6450 ga_append(&ga, c);
6451 }
6452 ga_append(&ga, NUL);
6453 return (char_u *)(ga.ga_data);
6454}
6455#endif
6456
6457#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6458static char ksme_str[20];
6459static char ksmr_str[20];
6460static char ksmd_str[20];
6461
6462/*
6463 * For Win32 console: update termcap codes for existing console attributes.
6464 */
6465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006466update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 struct builtin_term *p;
6469
6470 p = find_builtin_term(DEFAULT_TERM);
6471 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6472 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6473 attr | 0x08); /* FOREGROUND_INTENSITY */
6474 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6475 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6476
6477 while (p->bt_string != NULL)
6478 {
6479 if (p->bt_entry == (int)KS_ME)
6480 p->bt_string = &ksme_str[0];
6481 else if (p->bt_entry == (int)KS_MR)
6482 p->bt_string = &ksmr_str[0];
6483 else if (p->bt_entry == (int)KS_MD)
6484 p->bt_string = &ksmd_str[0];
6485 ++p;
6486 }
6487}
6488#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006489
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006490#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006491 static int
6492hex_digit(int c)
6493{
6494 if (isdigit(c))
6495 return c - '0';
6496 c = TOLOWER_ASC(c);
6497 if (c >= 'a' && c <= 'f')
6498 return c - 'a' + 10;
6499 return 0x1ffffff;
6500}
6501
6502 guicolor_T
6503gui_get_color_cmn(char_u *name)
6504{
Bram Moolenaar28726652017-01-06 18:16:19 +01006505 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6506 * values as used by the MS-Windows GDI api. It should be used only for
6507 * MS-Windows GDI builds. */
6508# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6509# undef RGB
6510# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006511# ifndef RGB
6512# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6513# endif
6514# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006515 FILE *fd;
6516 char line[LINE_LEN];
6517 char_u *fname;
6518 int r, g, b, i;
6519 guicolor_T color;
6520
6521 struct rgbcolor_table_S {
6522 char_u *color_name;
6523 guicolor_T color;
6524 };
6525
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006526 /* Only non X11 colors (not present in rgb.txt) and colors in
6527 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006528 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006529 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6530 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6531 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6532 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6533 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6534 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6535 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6536 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6537 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6538 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6539 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6540 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6541 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006542 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6543 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006544 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006545 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006546 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6547 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6548 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6549 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6550 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6551 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6552 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6553 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6554 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006555 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006556 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006557 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6558 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006559 };
6560
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006561 static struct rgbcolor_table_S *colornames_table;
6562 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006563
6564 if (name[0] == '#' && STRLEN(name) == 7)
6565 {
6566 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006567 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006568 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6569 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6570 if (color > 0xffffff)
6571 return INVALCOLOR;
6572 return color;
6573 }
6574
6575 /* Check if the name is one of the colors we know */
6576 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6577 if (STRICMP(name, rgb_table[i].color_name) == 0)
6578 return rgb_table[i].color;
6579
6580 /*
6581 * Last attempt. Look in the file "$VIM/rgb.txt".
6582 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006583 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006584 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006585 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006586
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006587 /* colornames_table not yet initialized */
6588 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6589 if (fname == NULL)
6590 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006591
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006592 fd = fopen((char *)fname, "rt");
6593 vim_free(fname);
6594 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006595 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006596 if (p_verbose > 1)
6597 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6598 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006599 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006600
6601 for (counting = 1; counting >= 0; --counting)
6602 {
6603 if (!counting)
6604 {
6605 colornames_table = (struct rgbcolor_table_S *)alloc(
6606 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6607 if (colornames_table == NULL)
6608 {
6609 fclose(fd);
6610 return INVALCOLOR;
6611 }
6612 rewind(fd);
6613 }
6614 size = 0;
6615
6616 while (!feof(fd))
6617 {
6618 size_t len;
6619 int pos;
6620
6621 ignoredp = fgets(line, LINE_LEN, fd);
6622 len = strlen(line);
6623
6624 if (len <= 1 || line[len - 1] != '\n')
6625 continue;
6626
6627 line[len - 1] = '\0';
6628
6629 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6630 if (i != 3)
6631 continue;
6632
6633 if (!counting)
6634 {
6635 char_u *s = vim_strsave((char_u *)line + pos);
6636
6637 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006638 {
6639 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006640 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006641 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006642 colornames_table[size].color_name = s;
6643 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6644 }
6645 size++;
6646 }
6647 }
6648 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006649 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006650
6651 for (i = 0; i < size; i++)
6652 if (STRICMP(name, colornames_table[i].color_name) == 0)
6653 return colornames_table[i].color;
6654
Bram Moolenaarab302212016-04-26 20:59:29 +02006655 return INVALCOLOR;
6656}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006657
6658 guicolor_T
6659gui_get_rgb_color_cmn(int r, int g, int b)
6660{
6661 guicolor_T color = RGB(r, g, b);
6662
6663 if (color > 0xffffff)
6664 return INVALCOLOR;
6665 return color;
6666}
Bram Moolenaarab302212016-04-26 20:59:29 +02006667#endif